2014-03-19 131 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的)。 –