Reputation: 617
I have an XML database which contains elements which have an id. These are all unique. They also have a secondary identifier which links them to a similar object in another database. These are not all unique.
Is there an XQuery which would let me identify all the non-unique IDs? I can count how many there are using distinct-values(), but that doesn't help identify the IDs which have duplicates!
Example XML: (each object is contained in a separate file in the eXist database)
<object id="uniqueID123">
<secondary identifier="nonUnique888"/>
</object>
<object id="uniqueID456">
<secondary identifier="nonUnique888"/>
</object>
<object id="uniqueID789">
<secondary identifier="Unique999"/>
</object>
I would want to identify the duplicated string "nonUnique888".
Upvotes: 2
Views: 3056
Reputation: 866
use this code store in xml file
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
Upvotes: 0
Reputation: 243529
Use:
let $vSeq := /object/secondary/@identifier
return
$vSeq[index-of($vSeq,.)[2]]
Read the explanation here.
Upvotes: 2
Reputation: 6334
The following query returns all non unique identifiers:
let $sec := doc('source')/root/object/secondary
for $id in distinct-values($sec/@identifier)
where count($sec[@identifier eq $id]) gt 1
return $id
Upvotes: 3