如何使用 XQuery 检索父节点? [英] How to retrieve parent node using XQuery?
问题描述
我正在使用 XML 和 XQuery.我通常使用相对于父节点的 XPath 表达式来检索其子节点.但是,如果我有一个子节点,我不确定如何做相反的事情,我如何检索它的父节点.
I am using XML and XQuerie. I usually use an XPath expression relative to a parent node to retrieve its child node. But, I am not sure how to do the opposite meaning if I have a child node, how do I retrieve its parent node.
<node id="50>
<childnode1 childid="51" />
<childnode2 childid="52" />
</node>
如果我有节点 <childnode1 childid="51"/>
,我如何检索它的父节点:<node id="50>
If I have the node <childnode1 childid="51" />
, how do I retrieve its parent: <node id="50>
推荐答案
简短回答:
..
这将选择当前(上下文)节点的父节点.
This selects the parent of the current (context) node.
更长、更笼统的答案:
//node()[childnode1/@childid="51"]
这将选择文档中具有名为 childnode1
的子元素的任何节点,该子元素具有属性 childid
,其值为51".
This selects any node in the document that has a child element named childnode1
, that has an attibute childid
, whose value is '51'.
应该尽量避免使用包含 //
缩写的表达式,因为这可能非常低效.仅当事先不知道 XML 文档的结构时才使用//".
One should try to avoid an expression that contains the //
abbreviation, because this may be very inefficient. Use '//' only when the structure of the XML document isn't known in advance.
最佳答案:
ExpressionSelectingTheChildNode/..
这篇关于如何使用 XQuery 检索父节点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!