How can I select only distinct elements for the XML document using XPATH?I've tried to use the 'distinct-values' function but it didn't work for some reason..
The XML is similar to the following:
<catalog>
<product>
<size>12</size>
<price>1000</price>
<rating>1</rating>
</product>
<product>
<size>10</size>
<price>1000</price>
<rating>1</rating>
<year>2010</year>
</product>
</catalog>
So what I want to get is the list of distinct children of all the product elements.In the given example it would be - size,price,rating,year My xpath was something like : distinct-values(catalog/product/*)
You need the distinct values of the element names - something like:
In XPath 2.0:
In XPath 1.0 this cannot be produced with a single XPath expression.
Using XSLT 1.0:
When this transformation is applied on the provided XML document, the wanted result is produced:
A more efficient XSLT 1.0 transformation, using keys:
distinct-values()
is available in XPath 2.0. Are you using that?If
distinct-values()
is not available, the standard way of getting distinct values is to usenot(@result = preceding:: @result)
to get unique @result. It will give you the first occurrence only.