tags:

views:

167

answers:

2

I have an XML document like this:

<wii>
  <game>
    <type genre="arcade" />
    <type genre="sport" />
  </game>
  <game>
    <type genre="platform" />
    <type genre="arcade" />
  </game>
</wii>

How to list all genres without repetition using only XPath?

Thanks.

A: 

Have you tried the distinct-values() function?

distinct-values(//type/@genre)

It's obviously of no use if that function isn't available to you.

Here's a link to an approach that uses only XPath.

Martin Owen
Thanks, but it's not available..
Carmine Paolino
+5  A: 
/wii/game/type/@genre[not(. = preceding::type/@genre)]

In plain English this selects any @genre attribute node for which there is no equally valued @genre node in the preceding part of the document.

The equality = operator, when given a plain value and a node-set, it compares the plain value to every node in the node-set, returning true only if all nodes match (note that != does not do that, it compares to the first node of the set only!). The result must be negated with not().

Tomalak