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

标签 xquery marklogic-9

所以我有一个有趣的问题,假设我在 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/

相关文章:

javascript - 在 marklogic 中找不到来自不同数据库的模块

MarkLogic - 自定义休息 GET REST 服务的性能变化

gradle - 通过ml-gradle为同一模块创建多个MarkLogic调度任务

rest - 从 MarkLogic 8 中的 rest 端点调用具有依赖关系的 xquery 库

xquery - MarkLogic 8 - Corb 传递 $URI 以外的参数

javascript - 我可以使用哪些搜索选项来限制 marklogic 搜索 API 关键字搜索,使其不搜索提到的 json 属性值?

marklogic - 无法在查询控制台中创建新选项卡(在 MarkLogic 中)

xml - 如何在 XQuery 1.0 中提取列表的第一个元素?

xpath - XQuery忽略父元素

group-by - 如何在 Marklogic 中使用 Group By?