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

XPath one of multiple attribute values with condition

给定以下表达式,如何使用=更简洁明了?

(//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 是这个问题的动力。

在 XPath 2.0 中,

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

会 select 文档中最后一个 h2 元素,其 id 属性值为 foobarbaz .

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

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