【问题标题】:XPath one of multiple attribute values with conditionXPath 具有条件的多个属性值之一
【发布时间】:2017-09-30 13:29:51
【问题描述】:

鉴于以下表达式,如何使用= 更简洁明确?

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

【问题讨论】:

    标签: html xpath attributes equality


    【解决方案1】:

    在 XPath 2.0 中,

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

    将选择文档中最后一个id 属性值为foobarbazh2 元素。

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

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

    【讨论】:

    • 对于提供 XPath 2.0 等效项的高五。谢谢一百万!
    猜你喜欢
    • 2012-05-02
    • 1970-01-01
    • 2016-10-02
    • 2015-06-27
    • 2015-04-10
    • 2013-08-01
    • 2022-01-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多