2013-07-18 184 views
1

我的XML结构是这样,获取Xpath的节点值

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

我需要的产品类型值,如果用户提供了品牌标识。对于例如,如果用户输入品牌编号3,然后我需要返回的产品类型1型

我用XPath表达式

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

但它不工作尝试。什么是正确的xpath表达式。

回答

3

你有一个简单的嵌套问题,因为brand兄弟product不是anscestor喜欢你的XPath查询暗示。

简单地更改为:

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

结果:

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

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