Xpath 选择具有特定子元素的元素 [英] Xpath to select elements that has specific child
本文介绍了Xpath 选择具有特定子元素的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
<item id=1>
<name>item1</name>
<price>30</price>
</item>
<item id=2>
<name>item2</name>
</item>
我需要一个 xpath 来仅选择没有 .net 价格的项目.
I need an xpath to select only items that DO NOT have price for .net.
推荐答案
对于您的原始问题:
item[price]
将为您提供所有具有 price
元素子元素的 item
元素.这包括一个空的
所以如果你想避免匹配
will give you all item
elements that have a price
element child. This includes an empty <price/>
so if you want to avoid matching
<item>
<name>item3</name>
<price></price>
</item>
那么您需要以下其中一项
then you need one of the following
item[price/text()]
item[normalize-space(price)]
相反,要仅选择没有 price
的 item
元素,您可以使用
For the inverse, to select only item
elements that do not have a price
, you can use
item[not(price)]
这篇关于Xpath 选择具有特定子元素的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文