【问题标题】:XPath wildcard in attribute value属性值中的 XPath 通配符
【发布时间】:2012-01-27 00:55:40
【问题描述】:

我有以下 XPath 来匹配类跨度的属性:

//span[@class='amount']

我想匹配所有具有“数量”类属性但也可能具有其他类的元素。我想我可以这样做:

//span[@class='*amount*'] 

但这不起作用...我该怎么做?

【问题讨论】:

    标签: c# xpath html-agility-pack


    【解决方案1】:

    使用以下表达式:

    //span[contains(concat(' ', @class, ' '), ' amount ')]
    

    您可以单独使用contains,但这也可以匹配someamount 之类的类。在以下输入上测试上述表达式:

    <root>
      <span class="test amount blah"/>
      <span class="amount test"/>
      <span class="test amount"/>
      <span class="amount"/>
      <span class="someamount"/>
    </root>
    

    它将选择前四个span 元素,但不会选择最后一个。

    【讨论】:

    • 如果假设,我想检索 span 标签内的文本内容,那么代码是什么。我已将结果存储在 nodelist 中并尝试使用 nodelist.item(0).getFirstChild().getNodeValue() 打印内容。但这不会返回任何东西。请帮帮我。
    • 有趣的是,总共需要四个 (2 x 2) 空白。
    【解决方案2】:

    你需要使用 contains 方法。见How to use XPath contains() here?

    //span[contains(@class,'amount')]

    【讨论】:

    • 不幸的是,这也将匹配class="lookamountain"。 @lwburk 有正确的答案。
    猜你喜欢
    • 1970-01-01
    • 2013-04-10
    • 1970-01-01
    • 2015-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-21
    相关资源
    最近更新 更多