XPath AND和OR的逻辑运算符优先级,不使用括号

5
我正在编写一个XPath表达式来实现这个功能:
//parent[(childA[contains(.,"foo")] or childB[contains(.,"foo")]) AND (childA[contains(.,"bar")] or childB[contains(.,"bar")])]

需要选择一个父节点,其子节点(childA或childB或childA和childB都存在)都包含字符串"foo"和"bar"。

我不确定在AND前后使用括号是否正确。XPath能像这样使用括号吗?

提前感谢你的帮助!!

1个回答

8
请参见http://www.w3.org/TR/xpath/#section-Expressions,特别是:
Parentheses may be used for grouping.

并且

NOTE: The effect of the above grammar is that the order of precedence is 
(lowest precedence first):

    or 
    and
    =, !=
    <=, <, >=, >

and the operators are all left associative. For example, 3 > 2 > 1 is 
equivalent to (3 > 2) > 1, which evaluates to false. 

2
在XPath 1.0中,3>2>1是正确的。但在XPath 2.0中,这个表达式不再被允许。(问题并没有特别涉及XPath 1.0)。 - Michael Kay

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接