尊重节点位置和属性值的 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>
现在我可以构建一个查找以下内容的单个查询:
查找 Type “Book” 的所有 Item 节点,其中第二个 Param 节点的值为“John”。 所以我想找到作者名字是“John”的所有书籍。
请注意,我使用的是 .NET XPathDocument。
Is there a way to build a XPath query that finds a node at a certain position AND with a certain attribute value?
Consider the following sample 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>
Now can I build a single query that finds the following:
Find all Item nodes of Type "Book" where the 2nd Param node has a Value of "John".
So I would like to find all books where the authors frist name is "John".
Note that I am using .NET XPathDocument.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
只拥有书籍项目的要求怎么样?
尝试这个:
What about the requirement to have only the Item that are Books?
Try this:
因此仅限于 XPath V1。
您可以在谓词中包含(相对和绝对)路径。 所以类似:(
我会尝试避免“
//
”,因为它需要搜索整个 DOM,但如果没有更多上下文,就无法提供更好的轴。)So limited to XPath V1.
You can include (relative and absolute) paths in a predicate. So something like:
(I would try and avoid "
//
", as it requires a search of the whole DOM, but can't provide a better axis without more context.)表达式为:
The expression would be: