如何获取 Xpath 表达式的子树?
如果说我有以下 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
听起来 XPath 表达式在运行时之前是未知的。
并且您想要获取 XPath 表达式选择的每个节点的父节点...是吗?
元素是每个comment/@id
属性的父元素。获取
元素相当于获取其子树(后代),因为您可以使用进一步的 XPath 表达式或 DOM 函数提取其后代,也可以将 复制到元素中。
元素及其后代。回答您的问题的一种方法是简单地将“/..”附加到 XPath 表达式的末尾。例如
,
它相当于 @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 eachcomment/@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.
would become
which is equivalent to @Dan's answer, but can be easily derived from whatever the XPath expression may be.
将检索具有 id 属性的所有评论元素。
Would retrieve all comment elements with an id attribute.
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?