views:

42

answers:

2

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".

A: 

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
Shcheklein
Worked perfectly, thanks :)
+1  A: 

Use:

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

Read the explanation here.

Dimitre Novatchev