distinct in Xpath?

Antoine picture Antoine · May 11, 2010 · Viewed 50.2k times · Source

I have this XML file, from which I'd like to count the number of users referenced in it. But they can appear in more than one category, and I'd like these duplicates not to be taken into account.
In the example below, the query should return 3 and not 4. Is there a way in XPath to do so? Users are not sorted at all.

<list>
  <group name='QA'>
    <user name='name1'>name1@email</user>
    <user name='name2'>name2@email</user>
  </group>
  <group name='DEV'>
    <user name='name3'>name3@email</user>
    <user name='name2'>name2@email</user>
  </group>
</list>

Answer

Dimitre Novatchev picture Dimitre Novatchev · May 11, 2010

A pure XPath 1.0 -- one-liner:

Use:

count(/*/group/user[not(. = ../following-sibling::group/user)])