gpt4 book ai didi

xquery - 执行 xdmp :node-replace() for a sequence of elements and replace that with one element

转载 作者:行者123 更新时间:2023-12-02 09:59:24 27 4
gpt4 key购买 nike

所以我有一个有趣的问题,假设我在 MarkLogic 数据库中有这个文档(example.xml):

<Enrolls>
<Enroll>
<Status> Active </Status>
<boom> boom2 </boom>
</Enroll>
<Enroll>
<Status> Active </Status>
<boom> boom </boom>
</Enroll>
<Enroll>
<Status> Inactive </Status>
<boom> boom </boom>
</Enroll>
</Enrolls>

我想用一个节点替换所有“事件”注册元素,因此基本上我的最终结果应该是:

<Enrolls>
<boom> boom for the actives </boom>
<Enroll>
<Status> Inactive </Status>
<boom> boom </boom>
</Enroll>
</Enrolls>

为了完成此任务,这是我编写的代码:

xdmp:node-replace((doc("example.xml")/Enrolls/Enroll[Status eq " Active "]), <boom> boom for the actives </boom>)

但这就是我得到的结果:

<Enrolls>
<boom> boom for the actives </boom>
<boom> boom for the actives </boom>
<Enroll>
<Status> Inactive </Status>
<boom> boom </boom>
</Enroll>
</Enrolls>

该代码将每个事件注册替换为我指定要替换的同一节点。我希望它同时用一个节点替换两个节点。我怎样才能做到这一点并得到我想要的结果?

最佳答案

考虑对事件节点执行 xdmp:node-delete 操作,并在父节点上执行单独的 xdmp:node-insert-child 操作。

for $active in doc("example.xml")/Enrolls/Enroll[Status eq " Active "]
return
if ($active/following-sibling::Enroll[Status eq " Active "])
then xdmp:node-delete($active)
else xdmp:node-replace($active, <boom> boom for the actives </boom>)

或者对第一个执行 xdmp:node-replace,对其他执行 xdmp:node-delete。您应该能够在一个请求中完成所有这些操作,因此只需一次提交。

let $enrolls := doc("example.xml")/Enrolls
return (
$enrolls/Enroll[Status eq " Active "]/xdmp:node-delete(.),
xdmp:node-insert-child($enrolls, <boom> boom for the actives </boom>)
)

您还可以重建父节点,并将其全部替换。这可能更容易推理,并且性能可能相似。

let $enrolls := doc("example.xml")/Enrolls
return
xdmp:node-replace($enrolls,
<Enrolls>
<boom> boom for the actives </boom>
{$enrolls/* except $enrolls/Enroll[Status eq " Active "]}
</Enrolls>)

关于xquery - 执行 xdmp :node-replace() for a sequence of elements and replace that with one element,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60549137/

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