如何获取 Xpath 表达式的子树?

发布于 2024-12-01 04:39:07 字数 1385 浏览 1 评论 0原文

如果说我有以下 XML 文件:

<title text=“title1">
    <comment id=“comment1">
        <data> this is part of comment one</data>
        <data> this is some more of comment one</data>
    </comment>
    <comment id=“comment2”>
        <data> this is part of comment two</data>
        <data> this is some more of comment two</data>
        <data> this is even some more of comment two</data>
    </comment>
</title>

并且我得到以下 XPath 查询:

/title/comment/@id

获取表达式所属子树的最佳方法是什么。

例如,上面的表达式将返回:

  • id="comment1"
  • id="comment2"

其中,comment1 属于子树:

    <comment id="comment1">
        <data> this is part of comment one</data>
        <data> this is some more of comment one</data>
    </comment>

并且, comment2 属于子树:

    <comment id=“comment2">
        <data> this is part of comment two</data>
        <data> this is some more of comment two</data>
        <data> this is even some more of comment two</data>
    </comment>

我想要这个的原因是这样我可以递归地调用 Xpath 表达式解析到的子树。

我正在使用 DOM 在 Java 中进行编码。

If say I’ve got the following XML file:

<title text=“title1">
    <comment id=“comment1">
        <data> this is part of comment one</data>
        <data> this is some more of comment one</data>
    </comment>
    <comment id=“comment2”>
        <data> this is part of comment two</data>
        <data> this is some more of comment two</data>
        <data> this is even some more of comment two</data>
    </comment>
</title>

And I’m given the following XPath query:

/title/comment/@id

What would be the best way to get the subtree to which the expression belongs.

So for instance, the above expression would return:

  • id="comment1"
  • id="comment2"

To which, comment1 belongs to subtree:

    <comment id="comment1">
        <data> this is part of comment one</data>
        <data> this is some more of comment one</data>
    </comment>

And, comment2 belongs to subtree:

    <comment id=“comment2">
        <data> this is part of comment two</data>
        <data> this is some more of comment two</data>
        <data> this is even some more of comment two</data>
    </comment>

The reason I want this is so that I can recursively make a call on the sub-tree where the Xpath expression is resolved to.

I am coding this in Java using DOM.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

隱形的亼 2024-12-08 04:39:07

听起来 XPath 表达式在运行时之前是未知的。
并且您想要获取 XPath 表达式选择的每个节点的父节点...是吗? 元素是每个 comment/@id 属性的父元素。获取 元素相当于获取其子树(后代),因为您可以使用进一步的 XPath 表达式或 DOM 函数提取其后代,也可以将 复制到元素中。 元素及其后代。

回答您的问题的一种方法是简单地将“/..”附加到 XPath 表达式的末尾。例如

"/title/comment/@id"

"/title/comment/@id/.."

它相当于 @Dan 的答案,但可以轻松地从 XPath 表达式中导出。

It sounds like the XPath expression is unknown until run time.
And you want to obtain the parent of each node selected by the XPath expression... is that right? The <comment> element is the parent of each comment/@id attribute. Obtaining the <comment> element is equivalent to obtaining its subtree (descendants), because you can extract its descendants using further XPath expressions or DOM functions, or you can copy the <comment> element with its descendants.

One way to answer your question then is to simply append "/.." to the end of the XPath expression. E.g.

"/title/comment/@id"

would become

"/title/comment/@id/.."

which is equivalent to @Dan's answer, but can be easily derived from whatever the XPath expression may be.

话少心凉 2024-12-08 04:39:07
/title/comment[@id]

将检索具有 id 属性的所有评论元素。

/title/comment[@id]

Would retrieve all comment elements with an id attribute.

彼岸花ソ最美的依靠 2024-12-08 04:39:07

XPath 表达式返回节点,在您的例子中是属性节点。调用 Java 代码可以对这些节点执行任何操作,例如导航到它们的父节点或其父节点的后代。

但是,为什么不在 XSLT 或 XQuery 中完成全部工作,而不是在 XPath 中完成一半工作,在 Java 中完成一半工作呢?

The XPath expression returns nodes, in your case attribute nodes. The calling Java code can do whatever it likes with these nodes, for example navigate to their parent or to the descendants of their parents.

But instead of doing half the work in XPath and half in Java, why not do it all in XSLT or XQuery?

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