Select a list of unique element from a node list using 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 开发者_StackOverflow社区select such a list with Xpath?
Try the following xpath:
/Deserts/Desert[not(@Code=preceding-sibling::Desert/@Code)]
It will return distinct deserts by checking the Desert Code attribute.
XPath 2.0 solution:
(/*/*/@Code)[index-of(/*/*/@Code,.)[1]]
精彩评论