Yuliya Sokhrannaya
Yuliya Sokhrannaya

Reputation: 83

Xpath get distinct nodes using preceding-sibling

I need to get distinct values //name() withount distinct-values(//*/name())

I tried do like this, but its dosent work.

//*/name()[.!=//preceding-sibling::*]

How can i repair it?

Upvotes: 1

Views: 6796

Answers (1)

Siva Charan
Siva Charan

Reputation: 18064

Using XPath 1.0, to get the distinct values

For name attribute,

/*/*[not(@name = preceding::*/@name)]

For node name,

/*/*[not(name() = preceding::*/name())]

My Sample XML:

<?xml version="1.0" encoding="UTF-8"?>
<root>
    <friend1 name="abc"/>
    <friend2 name="def"/>
    <friend3 name="abc"/>
    <friend1 name="abcd"/>
    <friend5 name="abcd"/>
    <friend6 name="xyz"/>
    <friend8 name="789"/>
    <friend0 name="pqr"/>
    <friend9 name="lmn"/>
    <friend2 name="lmn"/>
    <friend5 name="123"/>
    <friend7 name="456"/>
    <friend12 name="789"/>
</root>

Upvotes: 4

Related Questions