2017-09-30 65 views
1

給出以下表達式,如何使用=可以更簡潔明確?XPath多個屬性值中的一個,條件爲

(//h2/@id[contains(.,"foo") or contains(.,"bar") or contains(.,"baz"))[last()] 

這是我嘗試過,但我解釋說,這是無效的:

(//h2/@id[text() = ("foo", "bar", "baz")])[last()] 

我不能使用,因爲我需要防範的子字符串匹配。我正在使用XPath 1.0和an answer to a related question是這個問題的動力。

回答

1

在XPath 2.0,

//h2[@id = ("foo", "bar", "baz")][last()] 

將選擇文檔中最後h2元件用的foobar,或baz一個id屬性值。

在XPath 1.0,你會使用顯式布爾or運營商:

//h2[@id="foo" or @id="bar" or @id="baz"][last()] 
+1

和高個,用於提供的XPath 2.0當量。太感謝了!! –