Html 基于子节点的存在选择节点的Xpath表达式?
我正在尝试使用Xpath查找具有给定CSS class.product的所有Html 基于子节点的存在选择节点的Xpath表达式?,html,xml,xpath,Html,Xml,Xpath,我正在尝试使用Xpath查找具有给定CSS class.product的所有 <div class="product"> <a href="#product/1966740"><img class="cover_image" src="https://foobar.JPG" alt="foobar" title="foobar"> <h3>foobar</h3></a> <div class="
<div class="product">
<a href="#product/1966740"><img class="cover_image" src="https://foobar.JPG" alt="foobar" title="foobar">
<h3>foobar</h3></a>
<div class="price">$66.00</div>
<div class="rrp">$219.11</div>
</div>
然而,我只想找到那些没有卖完的产品。已售完的产品如下所示:
<div class="product">
<img class="availability" src="https://sold_out_tag.png" alt="sold out">
<a href="#product/1963553"><img class="cover_image" src="foobar.jpg" alt="foobar" title="foobar">
<h3>FooBar<h3></a>
<div class="price">$40.00</div>
<div class="rrp">$129.33</div>
</div>
你有什么想法可以让它工作吗
干杯,
Victor使用以下XPath:
//div[@class="product" and not(img[@alt="sold out"])]
你就快到了。img子路径周围不需要[]s:
//div[@class="product" and not(img[@alt = "sold out"])]
另一种编写基本相同内容的方法是:
//div[@class="product" and not(img/@alt = "sold out")]
最后,由于类属性可以包含多个类,因此最好使用contains检查类:
//div[contains(concat(' ', @class, ' '), " product ") and not(img/@alt = "sold out")]
你犯了一个应该犯的小错误
//div[@class=product]/img[@alt!=售罄]酷,这一款有效!=问题-您说过我不需要[]-XPath如何知道我要求它检查子节点的存在?其次,@class与空格的连接是什么?最后—您推荐给XPath?1的任何好指南都不会在执行其计算之前将其内容转换为布尔值。如果这些内容是节点集表达式,则如果节点集为空,布尔值将为false;如果节点集为非空,布尔值将为true。2空格可防止由于类名的部分匹配而导致误报。例如,包含“非产品”,“产品”将根据需要计算为true,而包含“非产品”,“产品”将根据需要计算为false。我能给你的最好的建议就是读这本书,然后练习,练习,练习。
//div[@class="product" and not(img/@alt = "sold out")]
//div[contains(concat(' ', @class, ' '), " product ") and not(img/@alt = "sold out")]