需要查找并删除for循环中相同节点的重复或更多次出现(node的精确副本)

时间:2018-09-04 10:54:58

标签: marklogic marklogic-8

Input:

    <relations>
      <relation>
        <isIdenticalInternationalStandardOf id="AssetID" metadataStatus="Publishable">1234</isIdenticalInternationalStandardOf>
        <isIdenticalInternationalStandardOf id="PrimaryDesignator">abc</isIdenticalInternationalStandardOf>
      </relation>
      <relation>
         <isIdenticalRegionalStandardOf id="AssetID" metadataStatus="Publishable">1234</isIdenticalRegionalStandardOf>
         <isIdenticalRegionalStandardOf id="PrimaryDesignator">abc</isIdenticalRegionalStandardOf>
      </relation>
      <relation>
        <supersededBy id="AssetID" metadataStatus="Publishable">5647</supersededBy>
        <supersededBy id="PrimaryDesignator">pqr</supersededBy>
      </relation>
      <relation>
        <replacedBy id="AssetID" metadataStatus="Publishable">1234</replacedBy>
        <replacedBy id="PrimaryDesignator">abc</replacedBy>
      </relation>
      <relation>
        <supersededBy id="AssetID" metadataStatus="Publishable">1234</supersededBy>
        <supersededBy id="PrimaryDesignator">xyz</supersededBy>
      </relation>
      <relation>
        <isIdenticalInternationalStandardOf id="AssetID" metadataStatus="Publishable">1234</isIdenticalInternationalStandardOf>
        <isIdenticalInternationalStandardOf id="PrimaryDesignator">abc</isIdenticalInternationalStandardOf>
      </relation>
       <relation>
        <isIdenticalInternationalStandardOf id="AssetID" metadataStatus="Publishable">1234</isIdenticalInternationalStandardOf>
        <isIdenticalInternationalStandardOf id="PrimaryDesignator">abc</isIdenticalInternationalStandardOf>
      </relation>
    </relations>

我可以从Input中找到重复项,这是代码:

let $data := $each-search-copy/relations
let $map := map:map()
let $uniqueList :=
      for $outer at $i in $data/relation
          for $inner at $j in $data/relation
               where $i ne $j
                  return 
                     if(fn:deep-equal($outer, $inner)) then
                     if(fn:exists(map:get($map, xs:string($j)))) then () else map:put($map, xs:string($i), xs:string($j)) 
                     else ()

let $duplicate :=
      for $each at $i in $data/relation
          return 
            if(fn:exists(map:get($map, xs:string($i)))) 
              then (
                     fn:string-join((xdmp:quote($each), "&#10;"), "|")
                    ) else () 

查找重复节点已完成,现在我要编写更新事务查询,该查询将仅保留唯一的节点列表。 如果存在多次,则应将其删除。

借助MAP构造,如果存在重复但没有多次出现,我可以删除。

2 个答案:

答案 0 :(得分:2)

您可以通过为要删除的每个节点调用xdmp:node-delete()函数来删除节点。

您可以在一个for循环中更简单地标识那些重复的元素,还可以使用一个where子句来测试relationship元素是否与{{1} {1}}兄弟姐妹,通过确保deep-equal()的值(保证相同的节点相同)来排除for循环中正在处理的当前relationship元素:

relationship

答案 1 :(得分:0)

除了删除重复项外,您还可以重建重复数据删除列表并进行一次替换。例如遍历列表并输出以前从未见过的所有关系。

let $uniqueList :=
  for $outer at $i in $data/relation
  where every $inner in ($data/relation)[1 to $i - 1]
    satisfies fn:not(fn:deep-equal($inner, $outer))
  return $outer

return xdmp:node-replace($data, <relations>{ $uniqueList }</relations>)