gpt4 book ai didi

xml - 选择节点但忽略返回子节点的 XQuery

转载 作者:行者123 更新时间:2023-12-03 16:57:22 25 4
gpt4 key购买 nike

我正在尝试创建一个 xquery 表达式,它将返回选定的节点但不会返回它们的子节点。这可能最好用一个例子来说明。我有以下 myNode 节点:

<myNode>
<myElements id="1">
<myElement key="one">aaa</myElement>
<myElement key="two" >bbb</myElement>
<myElement key="three">ccc</myElement>
</myElements>
<myElements id="2">
<myElement key="one">ddd</myElement>
<myElement key="two" >eee</myElement>
<myElement key="three">fff</myElement>
</myElements>
</myNode>

我有兴趣返回 节点,但仅此而已。我期望的返回如下所示:

<myNode>
<myElements id="1" />
<myElements id="2" />
</myNode>

<myNode>
<myElements id="1"></myElements>
<myElements id="2"></myElements>
</myNode>

我目前有一个类似于以下内容的 xpath 表达式(为此图进行了简化),正如预期的那样,它将返回 myElement 子元素:


$results/myNode/MyElements

我是不是找错了树?这在 XPath/XQuery 中甚至可能吗?

最佳答案

试试这个递归算法..

    xquery version "1.0";

declare function local:passthru($x as node()) as node()* { for $z in $x/node() return local:recurseReplace($z) };

declare function local:recurseReplace($x as node()) {
typeswitch ($x)
(: Changes based on a condition :)
case element(myElements) return <myElements id="{$x/@id}" />
(: IGNORE ANY CHANGES :)
case text() return $x
case comment() return comment {"an altered comment"}
case element() return element {fn:node-name($x)} {for $a in $x/attribute()
return $a, local:passthru($x)}
default return ()
};

let $doc :=
<myNode>
<myElements id="1">
<myElement key="one">aaa</myElement>
<myElement key="two" >bbb</myElement>
<myElement key="three">ccc</myElement>
</myElements>
<myElements id="2">
<myElement key="one">ddd</myElement>
<myElement key="two" >eee</myElement>
<myElement key="three">fff</myElement>
</myElements>
</myNode>
return local:recurseReplace($doc)

关于xml - 选择节点但忽略返回子节点的 XQuery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5159100/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com