2013-07-18 46 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試試吧。