Selecting Distinct Values using XPath Expression

Hi,

XPath is one of the flexible standard for querying an xml document.

Let us consider the following CD xml as example:-

Empire Burlesque Bob Dylan 10.90 Hide your heart Bonnie Tyler 9.90 Greatest Hits Dolly Parton 9.90 Hide your heart Bonnie Tyler 9.90

In the above xml, if we would like to get distinct country names, then we can make use of the preceding-sibling property to check for any
existence of the country name so as to filter and show only distinct country names.

/catalog/cd[not(@country=preceding-sibling::cd/@country)]/@country

The above expression would select only distinct country names i.e. UK and USA only once each.

Cheers.

This article is part of the GWB Archives. Original Author: Harish Ranganathan

New on Geeks with Blogs