考虑到以下表达式,我如何使用=
更简洁明了?
(//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是这个问题的推动力。
答案 0 :(得分:4)
在XPath 2.0中,
//h2[@id = ("foo", "bar", "baz")][last()]
会选择文档中的最后一个h2
元素,其id
属性值为foo
,bar
或baz
。
在XPath 1.0中,您将使用显式布尔or
运算符:
//h2[@id="foo" or @id="bar" or @id="baz"][last()]