Jitendra Kumar
Jitendra Kumar

Reputation: 181

XPATH 1.0 find duplicate based on two elements/keys

Here is my XML

<?xml version = '1.0' encoding = 'UTF-8'?>
<cmps>
      <cmp>
         <name>abc</name>
         <id>302</id>
      </cmp>
      <cmp>
         <name>abc</name>
         <id>370</id>
      </cmp>
      <cmp>
         <name>abc</name>
         <id>073</id>
      </cmp>
      <cmp>
         <name>abc</name>
         <id>302</id>
      </cmp>
      <cmp>
         <name>ab</name>
         <id>370</id>
      </cmp>

</cmps>  

=================== In the above xml, want to find out all the duplicates based on name and id element. so this xml has one duplicate element which has same name and id. but below xpath expression shows two duplicate element , can somebody correct the expression...any help is appreciated

cmps//cmp[id= (following-sibling::cmp/id) and name= (following-sibling::cmp/name) ]

Output of above xpath expression:

<cmp>
         <name>abc</name>
         <id>302</id>
</cmp>
<cmp>
         <name>abc</name>
         <id>370</id>
</cmp>

Expected output :

<cmp>
             <name>abc</name>
             <id>302</id>
</cmp>

Upvotes: 0

Views: 566

Answers (2)

Jitendra Kumar
Jitendra Kumar

Reputation: 181

It seems, we can not accomplish this task in xpath 1.0

Upvotes: 0

matthias_h
matthias_h

Reputation: 11416

You can use not():

//cmps//cmp[not(id= (following-sibling::cmp/id) 
                and name = (following-sibling::cmp/name))]

Result:

<cmp>
  <name>abc</name>
  <id>302</id>
</cmp>
<cmp>
  <name>abc</name>
  <id>073</id>
</cmp>
<cmp>
  <name>ab</name>
  <id>370</id>
</cmp>

Upvotes: 1

Related Questions