Wednesday, July 16, 2014

Listing down unique tags in entire XML

I have a requirement here, I would try to keep it in very simple terms,

I have a raw information in the form of XML:

<MyFruits>
<Apple>23</Apple>
<Mango>12</Mango>
<Orange>10</Orange>
<Apple>19</Apple>
</MyFruits>

I want to get only unique fruits among them. (Apple,Mango and Orange)

Can anyone write an XPath to retrieve this?
Status : Archived

You can give your answers as comments

Hint : 
Had the XML be like below

<MyFruits>
<Apple>23</Apple>
<Apple>19</Apple>
<Mango>12</Mango>
<Orange>10</Orange>
</MyFruits>

This XPath would have worked 

//MyFruits/*[not(name(.)=name(following-sibling::*))]


1 comment:

  1. This is not possible in XPath 1.0 and is quite simple in XPath 2.0

    distinct-values(/MyFruits/*/name()) Would fetch the unique values from entire XML in XPath2.0
    You can test XPath 2.0 here http://videlibri.sourceforge.net/cgi-bin/xidelcgi

    Details about the above question you can find it here.

    http://stackoverflow.com/a/24780345/3073401

    ReplyDelete