2009-06-03 60 views
0

有沒有辦法建立一個XPath查詢,找到一個節點在某個位置,並與某個屬性值?XPath查詢,尊重節點位置和屬性值

考慮下面的示例XML:

<Item Type="Book"> 
<!--1st Param node in a Book item is always the autors last name--> 
<Param Value="Updike" /> 
<!--2nd Param node in a Book item is always the autors first name--> 
<Param Value="John" /> 
<!--3rd Param node in a Book item is always the book title--> 
<Param Value="Toward the End of Time" /></Item> 

現在我可以構建查找以下一個查詢:

找到類型「周易」的所有項目節點,其中第二Param節點具有值「」約翰「。 所以我想找到作者姓名爲「John」的所有書籍。

請注意,我正在使用.NET XPathDocument。

回答

2

請注意,我正在使用.NET XPathDocument。

僅限於XPath V1。

您可以在謂詞中包含(相對和絕對)路徑。因此,像:

//Item[@Type='Book'][./Param[2][@Value = 'John']] 

(我會盡量避免「//」,因爲它需要搜索整個DOM,但沒有更多的情況下不能提供更好的軸)

+0

你的表達並不侷限於書籍。 – 2009-06-03 16:40:38

0

表達將是:

//Item/Param[2][@Value='John'] 
5

如果要求只有物品是書籍,那該怎麼辦?

試試這個:

/Item[@Type='Book'][Param[2][@Value='John']]