Xpath to select elements that has specific child [duplicate]

孤街醉人 提交于 2021-02-11 12:44:49

问题


<item id=1>
  <name>item1</name>
  <price>30</price>
</item>
<item id=2>
  <name>item2</name>      
</item>

I need an xpath to select only items that DO NOT have price for .net.


回答1:


For your original question:

item[price]

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

For the inverse, to select only item elements that do not have a price, you can use

item[not(price)]


来源:https://stackoverflow.com/questions/13764383/xpath-to-select-elements-that-has-specific-child

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!