考虑节点位置和属性值的XPath查询
有没有一种方法可以构建一个XPath查询,在某个位置找到一个具有特定属性值的节点 考虑以下示例xml:考虑节点位置和属性值的XPath查询,xpath,xpathdocument,Xpath,Xpathdocument,有没有一种方法可以构建一个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
<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>
现在,我可以构建一个查询来查找以下内容:
查找类型为“Book”的所有项目节点,其中第二个参数节点的值为“John”。
所以我想找到所有作者第一个名字是“约翰”的书
请注意,我使用的是.NET XPathDocument
请注意,我使用的是.NET XPathDocument
因此仅限于XPathV1
您可以在谓词中包含(相对和绝对)路径。比如:
//Item[@Type='Book'][./Param[2][@Value = 'John']]
(我会尽量避免使用“
/
”,因为它需要搜索整个DOM,但如果没有更多上下文,就无法提供更好的轴。)表达式是:
//Item/Param[2][@Value='John']
那么要求只包含书籍的项目呢 试试这个:
/Item[@Type='Book'][Param[2][@Value='John']]
你的表达不仅限于书本。