我正在尝试使XPATH返回计数大于5的产品的名称。
这是XML:
<?xml version ="1.0" encoding ="UTF -8"?>
<list>
<product>
<name>A</name>
<parts>
<part count="3">X3</part>
<part count="1">Y2</part>
<part count="2">Z6</part >
</parts>
</product>
<product>
<name>B</name>
<parts>
<part count="1">G3</part>
<part count="2">Y6</part>
</parts>
</product>
<product>
<name>C</name>
<parts>
<part count="6">K9</part >
<part count="3">Y6</part>
</parts>
</product>
</list>
我的尝试是:
//name/following-sibling::parts/part[@count>5]
More readable version
/list/product/parts/part[@count>5]
我制作的XPATH返回的
<part count="6">K9</part >
部分正确,但是没有返回产品名称。在这一点上,我什至不确定是否应该返回实际名称(在这种情况下为C),甚至可以返回真实名称,或者我的方法是否正确。任何输入都将受到欢迎。
最佳答案
怎么样
//product[parts/part/@count > 5]/name
要么
//part[@count > 5]/ancestor::product/name
要么
//name[../parts/part/@count > 5]
正如他们所说,有很多方法可以给猫皮。
关于xml - 如何从XPATH检索名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27886977/