Home > other >  Xpath to find following-sibling where two attributes match
Xpath to find following-sibling where two attributes match

Time:10-02

Want to build an XPath Query to find all following siblings of xml nodes where two attributes match.

<testsuites>
<testcase classname="john" name="login">
<testcase classname=john" name="login">
<testcase classname="peter" name="logout">
<testcase classname="peter" name="login">
</testsuites>

Here I want to only select following sibling of the node where name as well as classname both matches.

Following works but it doesn't matches the @classname attribute with the selected sibling.

(xmlstarlet ed -d '/testsuites/testcase[@name=following-sibling::testcase/@name and @classname=following-sibling::testcase/@classname]' < report.junit) > final.xml

My purpose is to find duplicate nodes where @name and @classname attribute matches, and then delete all except the one that is last in the order.

CodePudding user response:

Try using this xpath expression:

xml  ed -d  "//testcase[@classname=.//following-sibling::testcase/@classname][@name=.//following-sibling::testcase/@name]"  

CodePudding user response:

It works better if xpath selects the first following sibling

//testcase[@classname=./following-sibling::*[1]/@classname and @name=./following-sibling::*[1]/@name]

Finds:

<testcase classname="john" name="login"/>

Making @name not match

//testcase[@classname=./following-sibling::*[1]/@classname and @name!=./following-sibling::*[1]/@name]

Finds:

<testcase classname="peter" name="logout"/>
  • Related