Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select a list of unique element from a node list using XPATH

Tags:

dom

xml

xpath

Is it possible in XPATH to select a list of unique element from a node where there are many of the same?

<Deserts>
 <Desert Code="C1">Popsicle<Desert>
 <Desert Code="H2">Ice Cream<Desert>
 <Desert Code="C1">Popsicle<Desert>
 <Desert Code="T1">Cheese Cake<Desert>
</Deserts>

In this example I want the resulting list to have only 3 nodes (Popsicle / Ice Cream / Cheese Cake).

How can I select such a list with Xpath?

like image 890
CaBieberach Avatar asked May 20 '11 10:05

CaBieberach


1 Answers

Try the following xpath:

/Deserts/Desert[not(@Code=preceding-sibling::Desert/@Code)]

It will return distinct deserts by checking the Desert Code attribute.

like image 133
dogbane Avatar answered Nov 04 '22 10:11

dogbane