问题描述
在以下XPath表达式://div[@id=myID]|p
中,//
运算符是否同时应用于联合运算符的两端?还是该表达式会简单地返回文档中所有具有id
属性值myID
的div
元素以及所有作为上下文节点子元素的p
元素?
In this XPath expression: //div[@id="myID"]|p
, does the //
operator get applied to both sides of the union operator? Or would this expression simply return all div
elements in the document that have an id
attribute value of myID
and all p
elements that are children of the context node?
是否存在有关XPath运算符绑定和关联性的参考?
Is there a reference for XPath operator binding and associativity?
推荐答案
XPath运算符顺序优先级
XPath EBNF语法暗示了运算符之间的优先级(从最低到最高):
XPath Operator Order Precedence
The XPath EBNF grammar implies the following precedence among operators (lowest to highest):
(另请参见: XML路径语言(XPath)3.0 )
由于//
和[]
的优先级高于|
(联合),因此您的XPath表达式
Since //
and []
are of higher precedence than |
(union), your XPath expression
//div[@id="myID"]|p
说
- 使用
@id
属性值选择文档中的所有div
元素等于myID
- 并选择所有上下文元素的子元素
p
- 并采用这两组元素的联合
- select all
div
elements in the document with@id
attribute valueequal tomyID
, - and select all
p
elements that are children of the context element, - and take the union of those two sets of elements
产生最终结果(如您在第二次解释中所预期的那样).
to produce the final result (as you anticipated in your second interpretation).
这篇关于在XPath中,运算符之间的优先级是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!