xquery - 对一系列元素执行 xdmp:node-replace() 并将其替换为一个元素
问题描述
所以我有一个有趣的问题,假设我在 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
在第一个上做 a ,然后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>)
推荐阅读
- javascript - Expo react-native 使用 css-loader
- javafx-8 - 如何使用 ICU 排序规则对 JavaFX TableView 中的数据进行排序?
- html - 嵌入在 HTML 文本中的图像未在 Dropbox 上加载
- python - 如何在最后一次技术检查 last_checkup datefield() 后 11 个月后发送电子邮件?
- iis - PDFjs ERR_TOO_MANY_REDIRECTS Request.Cookies
- sqlalchemy - SQLAlchemy:使用它的唯一键获取子表的主键以在主表中使用
- c# - 如何将图像添加到 WPF 中的 ListView
- javascript - 使用 Onclick 在 React 中修改状态
- javascript - 如何操作 JSON 元素
- python - 切换到 Python/Selenium 后无法与 iframe 交互