Shuyi
Shuyi

Reputation: 916

compare value in different node in Xpath

So, here is an example of the XML

<a>
  <b id=0> 
    <c k=1/>
  </b>
  <b id=1>
   <c k=2/>
  </b>
</a>

I need to find all pairs of c where the K value is the same, and report the id of b ... I tried loops, following::b , all not working ... so, any suggestions?

Upvotes: 1

Views: 677

Answers (1)

M. Page
M. Page

Reputation: 2814

Try:

//b[c/@k = following-sibling::b/c/@k]

With:

<a>
  <b id="0"> 
    <c k="1"/>
  </b>
  <b id="1">
   <c k="2"/>
  </b>
  <b id="2">
   <c k="2"/>
  </b>
  <b id="3"> 
    <c k="1"/>
  </b>
</a>

It will give:

<b id="0">
   <c k="1"/>
</b>
-----------------------
<b id="1">
   <c k="2"/>
</b>

Upvotes: 2

Related Questions