xml 如何使用 XQuery 检索父节点?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3712452/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to retrieve parent node using XQuery?
提问by sony
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.
我正在使用 XML 和 XQuery。我通常使用相对于父节点的 XPath 表达式来检索其子节点。但是,如果我有一个子节点,我不确定如何做相反的事情,我如何检索它的父节点。
<node id="50>
<childnode1 childid="51" />
<childnode2 childid="52" />
</node>
If I have the node <childnode1 childid="51" />, how do I retrieve its parent: <node id="50>
如果我有 node <childnode1 childid="51" />,我如何检索它的父级:<node id="50>
采纳答案by Dimitre Novatchev
Short answer:
简短的回答:
..
This selects the parent of the current (context) node.
这将选择当前(上下文)节点的父节点。
Longer and more general answers:
更长更一般的答案:
//node()[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'.
这将选择文档中具有名为 的子元素的任何节点,该子元素childnode1具有属性childid,其值为“51”。
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.
应该尽量避免使用包含//缩写的表达式,因为这可能非常低效。仅当 XML 文档的结构事先未知时才使用“//”。
Best answer:
最佳答案:
ExpressionSelectingTheChildNode/..
回答by Nathan Hughes
you use ..to get the parent, like this:
你..用来获取父母,像这样:
../childnode1
so if you have some XML document like this:
所以如果你有一些这样的 XML 文档:
<a id="1">
<b id="2">
<c id="3">
<d id="4"/>
</c>
<c id="5"/>
<c id="6">
<d id="7"/>
</c>
</b>
</a>
then the XQuery
然后是 XQuery
//../d[@id = "4"]
would return the cnode with idof 3.
将返回c带有idof的节点3。
回答by Xavier John
Here is a more complex example of getting the parent node (..).
这是获取父节点 (..) 的更复杂示例。
Q) Find all situations where one country's most popular language is another country's least popular, and both countries list more than one language. https://prod-c2g.s3.amazonaws.com/db/Winter2013/files/countries.xml
Q) 找出一个国家最流行的语言是另一个国家最不流行的所有情况,并且这两个国家都列出了不止一种语言。 https://prod-c2g.s3.amazonaws.com/db/Winter2013/files/countries.xml
A)
一种)
for $b in doc("countries.xml")/countries/country/language
for $c in doc("countries.xml")/countries/country/language
where $b/../@name != $c/../@name
and data($b) = data($c)
and count($b/../language) > 1
and count($c/../language) > 1
and $b/@percentage = max($b/../language/@percentage)
and $c/@percentage = min($c/../language/@percentage)
return
<LangPair language="{data($b)}">
<MostPopular>{data($b/../@name)}</MostPopular>
<LeastPopular>{data($c/../@name)}</LeastPopular>
</LangPair>
回答by bosari
Alternatively, you can also make use of fn:root()Xquery function.
或者,您也可以使用fn:root()Xquery 功能。

