Reputation: 616
How do I output just the last duplicate node WITHIN the upper/parent element using XSLT 1.0? I use xsltproc processor.
Input.xml
<testng-results>
<suite>
<test>
<class name="system.apps.CreateTerritory">
<test-method status="PASS" name="initTest" is-config="true"> </test-method>
<test-method status="FAIL" name="ABC"> </test-method>
</class>
<class name="system.apps.CreateAccount">
<test-method status="PASS" name="initTest" is-config="true"> </test-method>
<test-method status="SKIP" name="DEF"> </test-method>
<test-method status="PASS" name="initTest" is-config="true"> </test-method>
<test-method status="FAIL" name="DEF"> </test-method>
</class>
<class name="system.apps.CreateProposal">
<test-method status="PASS" name="initTest" is-config="true"> </test-method>
<test-method status="SKIP" name="DEF"> </test-method>
<test-method status="PASS" name="initTest" is-config="true"> </test-method>
<test-method status="FAIL" name="DEF"> </test-method>
</class>
</test>
</suite>
</testng-results>
My Current XSL:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:str="http://exslt.org/strings" extension-element-prefixes="str" version="1.0">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" />
<xsl:strip-space elements="*" />
<xsl:key name="test-by-name" match="test-method[not(@is-config)]" use="@name" />
<xsl:template match="/">
<Suite>
<xsl:for-each select="testng-results/suite/test/class">
<xsl:for-each select="test-method[not(@is-config)][count(. | key('test-by-name', @name)[last()]) = 1]">
<Test>
<Class_Name>
<xsl:value-of select="../@name"/>
</Class_Name>
<Method_Name>
<xsl:value-of select="@name"/>
</Method_Name>
<Status>
<xsl:value-of select="@status"/>
</Status>
</Test>
</xsl:for-each>
</xsl:for-each>
</Suite>
</xsl:template>
</xsl:stylesheet>
Note: I can't change the way the nested match is done (for-each class and then for-each test-method as I need to be this way for other reasons)
Current Output.xml (the problem is that the duplicity is being checked ACROSS classes instead of WITHIN that class):
<?xml version="1.0" encoding="UTF-8"?>
<Suite>
<Test>
<Class_Name>system.apps.CreateTerritory</Class_Name>
<Method_Name>ABC</Method_Name>
<Status>FAIL</Status>
</Test>
<Test>
<Class_Name>system.apps.CreateProposal</Class_Name>
<Method_Name>DEF</Method_Name>
<Status>FAIL</Status>
</Test>
</Suite>
Expected Output.xml (only the last node is outputted for every duplicate test-method WITHIN THAT CLASS):
<?xml version="1.0" encoding="UTF-8"?>
<Suite>
<Test>
<Class_Name>system.apps.CreateTerritory</Class_Name>
<Method_Name>ABC</Method_Name>
<Status>FAIL</Status>
</Test>
<Test>
<Class_Name>system.apps.CreateAccount</Class_Name>
<Method_Name>DEF</Method_Name>
<Status>FAIL</Status>
</Test>
<Test>
<Class_Name>system.apps.CreateProposal</Class_Name>
<Method_Name>DEF</Method_Name>
<Status>FAIL</Status>
</Test>
</Suite>
Upvotes: 0
Views: 63
Reputation: 116992
As I mentioned in a comment on your previous question, you need to add a class identifier to the key. Assuming that each class has a unique name
, this could be:
<xsl:key name="test-by-name" match="test-method[not(@is-config)]" use="concat(@name, '|', ../@name)" />
Then:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:key name="test-by-name" match="test-method[not(@is-config)]" use="concat(@name, '|', ../@name)" />
<xsl:template match="/testng-results">
<Suite>
<xsl:for-each select="suite/test/class/test-method[not(@is-config)][count(. | key('test-by-name', concat(@name, '|', ../@name))[last()]) = 1]">
<Test>
<Class_Name>
<xsl:value-of select="../@name"/>
</Class_Name>
<Method_Name>
<xsl:value-of select="@name"/>
</Method_Name>
<Status>
<xsl:value-of select="@status"/>
</Status>
</Test>
</xsl:for-each>
</Suite>
</xsl:template>
</xsl:stylesheet>
will return:
Result
<?xml version="1.0" encoding="UTF-8"?>
<Suite>
<Test>
<Class_Name>system.apps.CreateTerritory</Class_Name>
<Method_Name>ABC</Method_Name>
<Status>FAIL</Status>
</Test>
<Test>
<Class_Name>system.apps.CreateAccount</Class_Name>
<Method_Name>DEF</Method_Name>
<Status>FAIL</Status>
</Test>
<Test>
<Class_Name>system.apps.CreateProposal</Class_Name>
<Method_Name>DEF</Method_Name>
<Status>FAIL</Status>
</Test>
</Suite>
Upvotes: 1