2014-03-19 129 views
5

我正在写一个XPath表达式来实现这一目标: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是否有这样的括号?

在此先感谢!

回答

7

请参阅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. 
+0

以Re 3> 2> 1,这是仅适用于的XPath 1.0真。在XPath 2.0中,这个表达式不再被允许。 (这个问题不是专门关于XPath 1.0的)。 –