我有以下的XPath匹配类范围的属性:XPath的通配符属性值
//span[@class='amount']
我想匹配的是有“量”的类属性的所有元素,但也可能有其他类作为好。我以为我可以做到这一点:
//span[@class='*amount*']
但这不起作用......我该怎么做?
我有以下的XPath匹配类范围的属性:XPath的通配符属性值
//span[@class='amount']
我想匹配的是有“量”的类属性的所有元素,但也可能有其他类作为好。我以为我可以做到这一点:
//span[@class='*amount*']
但这不起作用......我该怎么做?
使用下面的表达式:
//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
元素,但不是最后一个。
您需要使用contains方法。见How to use XPath contains() here?和http://www.exampledepot.com/egs/org.w3c.dom/xpath_GetElemByAttr.html
//span[contains(@class,'amount')]
不幸的是,这也会匹配'class =“lookamountain”'。 @lwburk有正确的答案。 – 2011-12-23 23:48:11
这正是我要找的,非常感谢。 – 2011-12-23 19:42:15
如果假设,我想检索span标签内的文本内容,那么代码是什么。我将结果存储在nodelist中,并试图使用nodelist.item(0).getFirstChild()。getNodeValue()来打印内容。但是这不会返回任何东西。请帮助我。 – TheGaME 2014-11-04 17:15:23
它的工作...!谢谢 – 2016-04-07 04:31:58