【问题标题】:Xquery finding duplciate IDsXquery 查找重复的 ID
【发布时间】:2010-10-06 18:08:53
【问题描述】:

我有一个 XML 数据库,其中包含具有 id 的元素。这些都是独一无二的。它们还有一个辅助标识符,将它们链接到另一个数据库中的类似对象。这些并非都是独一无二的。

是否有可以让我识别所有非唯一 ID 的 XQuery?我可以计算有多少使用 distinct-values(),但这无助于识别有重复的 ID!

示例 XML:(每个对象都包含在 eXist 数据库中的单独文件中)

<object id="uniqueID123">
  <secondary identifier="nonUnique888"/>
</object>

<object id="uniqueID456">
  <secondary identifier="nonUnique888"/>
</object>

<object id="uniqueID789">
  <secondary identifier="Unique999"/>
</object>

我想识别重复的字符串“nonUnique888”。

【问题讨论】:

  • @user320425:从语义上讲,这是 this 的副本,因为 XQuery 是 XPath 的超集。
  • 哇,$vSeq[index-of($vSeq,.)[2]] 确实是一个非常优雅的解决方案!我没有意识到 index-of() 是这样工作的,太习惯了 Java 的 find-the-first 风格。
  • @user320425:好问题(+1)。阅读我希望包含最短解决方案的答案。

标签: xml duplicates xquery xpath-2.0


【解决方案1】:

以下查询返回所有非唯一标识符:

let $sec := doc('source')/root/object/secondary
for $id in distinct-values($sec/@identifier)
where count($sec[@identifier eq $id]) gt 1
return $id

【讨论】:

    【解决方案2】:

    使用

    let $vSeq := /object/secondary/@identifier
      return
        $vSeq[index-of($vSeq,.)[2]] 
    

    阅读说明here

    【讨论】:

      【解决方案3】:

      将此代码存储在 xml 文件中

      let $path:="/db/test/all.xml"
      let $a := xmldb:store( $col,'adub.xml',<root></root>)  
      
      let $sec := doc($path)//profile
      for $id in distinct-values($sec/mail)
      where count($sec[mail eq $id]) gt 1
      return 
       update insert  
                  <profile>
                      {$id}
                      </profile>
         into  doc($a)/root 
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-08-19
        • 1970-01-01
        • 2010-10-01
        • 2013-07-11
        • 1970-01-01
        • 2014-12-25
        • 1970-01-01
        • 2023-03-09
        相关资源
        最近更新 更多