XPath one of multiple attribute values with condition

◇◆丶佛笑我妖孽 提交于 2019-12-11 00:09:24

问题


Given the following expression, how can I use = to be more concise and explicit?

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

Here's what I tried, but my interpreter says it's not valid:

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

I cannot use contains() because I need to guard against substring matches. I'm using XPath 1.0 and an answer to a related question is the impetus for this question.


回答1:


In XPath 2.0,

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

would select the last h2 element in the document with an id attribute value of foo, bar, or baz.

In XPath 1.0, you'd use explicit boolean or operators:

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


来源:https://stackoverflow.com/questions/46503195/xpath-one-of-multiple-attribute-values-with-condition

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!