开发者

php simple xpath question

开发者 https://www.devze.com 2023-02-16 16:04 出处:网络
I have this div: <div class=\"product-name\">Product1</div> I also have this div: <div class=\"product-name gold\">Prod开发者_如何学运维uct2</div>

I have this div:

<div class="product-name">Product1</div>

I also have this div:

<div class="product-name gold">Prod开发者_如何学运维uct2</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

php simple xpath question


The classic XPath 1.0 answer for this existencial test on sequence is:

/html/body//div[
   contains(
      concat(' ',normalize-space(@class),' '),
      ' product-name '
   )
]


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"))]


/html/body//div[contains(@class,'product-name')]

Attributes are also nodes, they are called attribute nodes.

0

精彩评论

暂无评论...
验证码 换一张
取 消