You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在MarkLogic中用XQuery提取XML重复relation节点及文件名

提取MarkLogic中重复的节点并返回文件名

刚好能帮你搞定这个需求,下面是针对单个relation.xml文档的XQuery代码,直接在MarkLogic的Query Console里就能运行:

xquery version "1.0-ml";

// 加载目标XML文档,确保URI和你库中的文档路径一致
let $target-doc := doc("relation.xml")
// 提取所有<relation>节点
let $all-relations := $target-doc//relation
// 找出<normativelyReferencedBy>文本重复的值
let $duplicate-ref-texts := 
  for $rel in $all-relations
  let $ref-text := $rel/normativelyReferencedBy/text()
  group by $ref-text
  // 只保留出现次数大于1的引用文本
  having count($rel) > 1
  return $ref-text
// 遍历重复的引用文本,返回文件名+对应节点内容
return
  for $ref in $duplicate-ref-texts
  let $matching-relations := $all-relations[normativelyReferencedBy/text() = $ref]
  return
    concat("relation.xml ", xdmp:quote($matching-relations))

代码说明:

  • doc("relation.xml"):加载指定URI的XML文档,如果你的文档在特定集合或目录下,记得调整这个URI(比如doc("/path/to/relation.xml"))。
  • group by $ref-text:按<normativelyReferencedBy>的文本内容分组,having count($rel) > 1筛选出重复的分组。
  • xdmp:quote($matching-relations):把XML节点转换成格式化的字符串,方便和文件名拼接返回。

如果之后需要处理多个XML文档中的重复节点,这里有个扩展版本,会自动识别每个节点所属的文件名:

xquery version "1.0-ml";

// 提取指定集合中所有文档的<relation>节点(也可以用doc()指定单个文档)
let $all-relations := collection()//relation
// 按引用文本分组,同时保留每个节点的文档URI
let $duplicate-groups := 
  for $rel in $all-relations
  let $ref-text := $rel/normativelyReferencedBy/text()
  let $doc-uri := base-uri($rel)
  group by $ref-text
  having count($rel) > 1
  return ($ref-text, $rel)
// 返回每个重复节点的文件名+节点内容
return
  for $group in $duplicate-groups
  let $rels := $group[position() > 1]
  return
    for $rel in $rels
    return concat(base-uri($rel), " ", xdmp:quote($rel))

内容的提问来源于stack exchange,提问作者Akbar aLI

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 08:40:39