Finding unique nodes with xslt

uniquexpathxslt

I have an xml document that contains some "Item" elements with ids. I want to make a list of the unique Item ids. The Item elements are not in a list though – they can be at any depth within the xml document – for example:


<Node>
  <Node>
    <Item id="1"/>
    <Item id="2"/>
  </Node>
  <Node>
    <Item id="1"/>
    <Node>
      <Item id="3"/>
    </Node>
  </Node>
  <Item id="2"/>
</Node>

I would like the output 1,2,3 (or a similar representation). If this can be done with a single xpath then even better!

I have seen examples of this for lists of sibling elements, but not for a general xml tree structure. I'm also restricted to using xslt 1.0 methods. Thanks!

Best Answer

Selecting all unique items with a single XPath expression (without indexing, beware of performance issues):

//Item[not(@id = preceding::Item/@id)]
Related Topic