尊重节点位置和属性值的 XPath 查询

发布于 2024-07-23 06:29:52 字数 659 浏览 3 评论 0原文

有没有办法构建一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

ら栖息 2024-07-30 06:29:53

只拥有书籍项目的要求怎么样?

尝试这个:

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

What about the requirement to have only the Item that are Books?

Try this:

/Item[@Type='Book'][Param[2][@Value='John']]
梦归所梦 2024-07-30 06:29:53

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

因此仅限于 XPath V1。

您可以在谓词中包含(相对和绝对)路径。 所以类似:(

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

我会尝试避免“//”,因为它需要搜索整个 DOM,但如果没有更多上下文,就无法提供更好的轴。)

Note that I am using .NET XPathDocument.

So limited to XPath V1.

You can include (relative and absolute) paths in a predicate. So something like:

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

(I would try and avoid "//", as it requires a search of the whole DOM, but can't provide a better axis without more context.)

落花随流水 2024-07-30 06:29:53

表达式为:

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

The expression would be:

//Item/Param[2][@Value='John']
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文