以嵌套XML获取Xpath节点值

时间:2013-07-18 09:58:37

标签: xml xpath

我的xml结构就像,

<items>
<item>
    <brand>
        <id> 3 </id>
     </brand>
    <product>
        <productType>Type 1</productType>
    </product>
</item>
<item>
    <brand>
        <id> 4 </id>
     </brand>
    <product>
        <productType>Type 2</productType>
    </product>
</item>
</items>

如果用户提供品牌ID,我需要获取产品类型值。例如,如果用户输入品牌ID 3,那么我需要返回产品类型类型1

我尝试使用Xpath表达式

 /items/item/brand[id = 3]/product/productType

但它不起作用。什么是正确的xpath表达式。

1 个答案:

答案 0 :(得分:3)

你有一个简单的嵌套问题,因为brand兄弟product不是anscestor 就像你的XPath查询所暗示的那样。

只需更改为:

/items/item[brand/id = 3]/product/productType

结果:

Element='<productType>Type 1</productType>'

http://www.freeformatter.com/xpath-tester.html处尝试。

相关问题