While selecting data from XML files using XPath there would be some scenarios where you want to select only list of unique nodes

Let’s take a simple example from the following XML

<?xml
version=1.0
encoding=utf-8?>

<Categories>

<CategoryID>1</CategoryID>

<CategoryID>1</CategoryID>

<CategoryID>2</CategoryID>

<CategoryID>3</CategoryID>

<CategoryID>3</CategoryID>

<CategoryID>4</CategoryID>

<CategoryID>4</CategoryID>

</Categories>

 

From above XML you want to select only unique category ids and return the following result

Below expressions of XPath 1.0 will allow you to achieve the above result, Later one is fully qualified XPath slightly bigger but work universally.

//CategoryID[not(preceding::CategoryID= .)]

or

//*[local-name()=’CategoryID’ and not(preceding::*[local-name()=’CategoryID’ ] = .)]

Advertisements