2009-06-03 87 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']]