XPath带有条件的多个属性值之一

时间:2017-09-30 13:29:52

标签: html xpath attributes equality

考虑到以下表达式,我如何使用=更简洁明了?

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

这是我尝试过的,但是我的翻译说它无效:

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

我不能使用contains(),因为我需要防止子串匹配。我使用XPath 1.0和an answer to a related question是这个问题的推动力。

1 个答案:

答案 0 :(得分:4)

在XPath 2.0中,

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

会选择文档中的最后一个h2元素,其id属性值为foobarbaz

在XPath 1.0中,您将使用显式布尔or运算符:

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