2010-10-06 48 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