2010-10-06 47 views
2

我有一個XML數據庫,其中包含具有ID的元素。這些都是獨一無二的。他們還有一個輔助標識符,將它們鏈接到另一個數據庫中的類似對象。這些並不都是獨一無二的。Xquery查找duplciate 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」。

+0

@ user320425這個代碼存儲:Semanticly這是[此]重複(http://stackoverflow.com/questions/133092/how-do-you-identify-duplicate -elements-in-an-xpath-20-sequence#287360),因爲XQuery是XPath的超集。 – 2010-10-06 20:41:57

+0

哇,$ vSeq [index-of($ vSeq,。)[2]]的確是一個非常優雅的解決方案!我沒有意識到index-of()像那樣工作,也習慣Java的find-the-first風格。 – Nick 2010-10-07 09:23:04

+0

@ user320425:好問題(+1)。閱讀我希望包含最短解決方案的答案。 – 2010-10-07 18:51:35

回答

3

下面的查詢返回的所有非唯一標識符:

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

工作完美,謝謝:) – Nick 2010-10-07 11:01:58

2

使用

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

閱讀說明here

0

使用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