AND和OR的xpath逻辑运算符优先级,不带括号

Luk*_*hen 5 xpath operator-precedence

我正在写一个xpath表达式来实现这个目的:

//parent[(childA[contains(.,"foo")] or childB[contains(.,"foo")]) AND (childA[contains(.,"bar")] or childB[contains(.,"bar")])]
Run Code Online (Sandbox Code Playgroud)

这是选择其子项(childA或childB或childA和childB一起)包含字符串"foo"和"bar"的父项.

我不确定是否正确使用AND之前和之后的括号.xpath可以这样括号吗?

提前致谢!!

kes*_*lam 8

http://www.w3.org/TR/xpath/#section-Expressions.特别是:

Parentheses may be used for grouping.
Run Code Online (Sandbox Code Playgroud)

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. 
Run Code Online (Sandbox Code Playgroud)

  • Re 3 &gt; 2 &gt; 1,这仅适用于 XPath 1.0。在 XPath 2.0 中不再允许此表达式。(问题不是专门针对 XPath 1.0)。 (2认同)