如何使用 XPath 选择具有特定属性的第一个元素 [英] How to select the first element with a specific attribute using XPath

查看:41
本文介绍了如何使用 XPath 选择具有特定属性的第一个元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

XPath bookstore/book[1] 选择 bookstore 下的第一个 book 节点.

The XPath bookstore/book[1] selects the first book node under bookstore.

如何选择匹配更复杂条件的第一个节点,例如匹配 /bookstore/book[@location='US']

How can I select the first node that matches a more complicated condition, e.g. the first node that matches /bookstore/book[@location='US']

推荐答案

使用:

(/bookstore/book[@location='US'])[1]

这将首先获得 location 属性等于 'US' 的 book 元素.然后它将从该集合中选择第一个节点.请注意括号的使用,这是某些实现所必需的.

This will first get the book elements with the location attribute equal to 'US'. Then it will select the first node from that set. Note the use of parentheses, which are required by some implementations.

请注意,这与 /bookstore/book[1][@location='US'] 不同,除非第一个元素也恰好具有该位置属性.

Note, this is not the same as /bookstore/book[1][@location='US'] unless the first element also happens to have that location attribute.

这篇关于如何使用 XPath 选择具有特定属性的第一个元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆