php简单的xpath问题 [英] php simple xpath question
问题描述
我有这个 div:
Product1
我也有这个 div:
Product2
如何更改此 xpath 查询以获取包含产品名称的任何 div?而不是获得完全匹配.
/html/body//div[@class='product-name']
我用谷歌搜索了它,但我能找到的只是在搜索节点内的值而不是属性时如何使用 contains
.
编辑
您可以使用 contains()
:
/html/body//div[contains(@class, 'product-name')]
更新:
正如 @Alejandro 在他的评论中指出的那样,这也将匹配包含 产品名称
.查看他对 XPath 1.0 解决方案的回答.
如果您使用 XPath 2.0,您也可以这样做:
/html/body//div[exists(index-of(tokenize(@class, "\s+"), "product-name"))]
I have this div:
<div class="product-name">Product1</div>
I also have this div:
<div class="product-name gold">Product2</div>
How can I alter this xpath query to get whatever divs which contains product-name? Instead of getting an exact match.
/html/body//div[@class='product-name']
I googled it, but all I could find is how to use contains
when searching for a value within a node, and not an attribute.
EDIT
You can use contains()
:
/html/body//div[contains(@class, 'product-name')]
Update:
As @Alejandro points out in his comment, this would also match any class that contains product-name
. See his answer for a XPath 1.0 solution.
If you use XPath 2.0, you could also do this:
/html/body//div[exists(index-of(tokenize(@class, "\s+"), "product-name"))]
这篇关于php简单的xpath问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!