如何在 XPath 中识别多个具有相同名称的元素? [英] How can you identify multiple elements with the same name in XPath?

查看:78
本文介绍了如何在 XPath 中识别多个具有相同名称的元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一个包含多个同名元素的文档怎么办——例如,我将如何检索第二个元素?

What if I had a document that had multiple elements with the same name -- how would I retrieve, for instance, the second element?

<doc>
...
 <element name="same">foo</element>
...
 <element name="same">bar</element>
...
 <element name="same">baz</element>
...
</doc>

我希望像//elem[@name='same'][2] 之类的东西能够工作.

I'd expect something like //elem[@name='same'][2] to work.

此外,如何在 xpath 中找到具有可变数量的同名元素的倒数第二个元素

Additionally, how would I find the second from last element in xpath with a variable number of elements with the same name

推荐答案

[ ] 的优先级高于//(而且//"实际上只是一个缩写,不是运算符).之所以如此,是因为根据 XPath 1.0规格,

[ ] has higher priority than // (and "//" is actually only an abbreviation, not an operator). This is so, because according to the XPath 1.0 Spec,

"//是/descendant-or-self::node()/" 的缩写

"// is short for /descendant-or-self::node()/"

及以后:

"注意:位置路径//para[1]与位置路径/descendant::para[1]<的意思不同/code>.后者选择第一个后代 para 元素;前者选择作为其父元素的第一个 para 孩子的所有后代 para 元素."

"NOTE: The location path //para[1] does not mean the same as the location path /descendant::para[1]. The latter selects the first descendant para element; the former selects all descendant para elements that are the first para children of their parents."

因此,XPath 表达式:

Therefore, the XPath expression:

   //element[@name='same'][2]

     //element[@name='same'][2]

意思是:

选择文档中名为element"的任何元素,具有值为same"的属性name",并且该元素是其父元素的第二个这样的子元素.

Select any element in the document, that is named "element", has an attribute "name" with value "same", and this element is the second such child of its parent.

你想要的是:

   (//element[@name='same'])[2]

     (//element[@name='same'])[2]

注意括号,它覆盖了 [] 的更高优先级.

Note the brackets, which override the higher precedence of [].

类似地,最后一个这样的节点由以下 XPath 表达式选择:

Similarly, the last but one such node is selected by the following XPath expression:

   (//element[@name='same'])[last()-1]

     (//element[@name='same'])[last()-1]

最后,一个必要的警告://"缩写的使用非常昂贵,因为它会导致遍历整个(子)树.每当知道文档的结构时,建议使用更具体的结构(位置路径).

Finally, a necessary warning: The use of the "//" abbreviation is very expensive as it causes the whole (sub)tree to be traversed. Whenever the structure of the document is known, it is recommended to use more specific constructs (location paths).

这篇关于如何在 XPath 中识别多个具有相同名称的元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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