Reputation: 2849
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.
Upvotes: 0
Views: 1442
Reputation: 5271
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.
Upvotes: 0
Reputation: 338198
/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()
.
Upvotes: 5