如何使用XQuery检索父节点?

发布于 2024-09-19 04:59:52 字数 314 浏览 3 评论 0原文

我正在使用 XML 和 XQuerie。我通常使用相对于父节点的 XPath 表达式来检索其子节点。但是,我不确定如果我有一个子节点如何执行相反的操作,如何检索其父节点。

<node id="50>
  <childnode1 childid="51" />
  <childnode2 childid="52" />
</node>

如果我有节点 ,如何检索其父节点:

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>

If I have the node <childnode1 childid="51" />, how do I retrieve its parent: <node id="50>

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

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

发布评论

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

评论(4

ヤ经典坏疍 2024-09-26 04:59:52

简短回答

..

这选择当前(上下文)节点的父节点。

更长、更一般的答案

//node()[childnode1/@childid="51"]

这会选择文档中具有名为 childnode1 的子元素的任何节点,该节点具有属性 childid,其值是“51”。

应尽量避免使用包含 // 缩写的表达式,因为这可能非常低效。仅当事先未知 XML 文档的结构时才使用“//”。

最佳答案

ExpressionSelectingTheChildNode/..

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'.

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.

Best answer:

ExpressionSelectingTheChildNode/..
停顿的约定 2024-09-26 04:59:52

您使用 .. 来获取父级,如下所示:

../childnode1

因此,如果您有一些如下所示的 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>

那么 XQuery

//../d[@id = "4"]

将返回带有 id< 的 c 节点3 的 /code>。

you use .. to get the parent, like this:

../childnode1

so if you have some XML document like this:

<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

//../d[@id = "4"]

would return the c node with id of 3.

愿与i 2024-09-26 04:59:52

这是获取父节点 (..) 的更复杂的示例。

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>

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

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>
幽梦紫曦~ 2024-09-26 04:59:52

或者,您也可以使用 fn:root() Xquery 函数。

Alternatively, you can also make use of fn:root() Xquery function.

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