Select a list of a unique item from the node list using XPATH

Is it possible in XPATH to select a list of a unique element from 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 result list to have only 3 nodes (Popsicle / Ice Cream / Cheese Cake).

How can I select such a list using Xpath?

+6
source share
2 answers

Try the following xpath:

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

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

+4
source

XPath 2.0 Solution :

 (/*/*/@Code)[index-of(/*/*/@Code,.)[1]] 
+2
source

Source: https://habr.com/ru/post/888628/


All Articles