且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在 XPath 中识别多个具有相同名称的元素?

更新时间:2023-09-23 18:56:34

[ ] 的优先级高于//(而且//"实际上只是一个缩写,不是运算符).之所以如此,是因为根据 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].后者选择第一个后代 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).