XSLT 1.0 (xsltproc) - Output only the last duplica

2019-08-27 20:10发布

How do I output just the last duplicate node 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>
        </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:template match="/">
        <Suite>
            <xsl:for-each select="testng-results/suite/test/class">
                 <xsl:for-each select="test-method">
                     <xsl:if test="not(@is-config)">
                             <Test>
                                 <Method_Name>
                                     <xsl:value-of select="@name"/>
                                 </Method_Name>
                                 <Status>
                                     <xsl:value-of select="@status"/>
                                 </Status>
                             </Test>
                     </xsl:if>
                 </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:

<?xml version="1.0" encoding="UTF-8"?>
<Suite>
  <Test>
    <Method_Name>ABC</Method_Name>
    <Status>FAIL</Status>
  </Test>
  <Test>
    <Method_Name>DEF</Method_Name>
    <Status>SKIP</Status>
  </Test>
  <Test>
    <Method_Name>DEF</Method_Name>
    <Status>FAIL</Status>
  </Test>
</Suite>

Expected Output.xml (only the last node is outputted for every duplicate test-method):

<?xml version="1.0" encoding="UTF-8"?>
<Suite>
  <Test>
    <Method_Name>ABC</Method_Name>
    <Status>FAIL</Status>
  </Test>
  <Test>
    <Method_Name>DEF</Method_Name>
    <Status>FAIL</Status>
  </Test>
</Suite>

标签: xslt xslt-1.0
1条回答
等我变得足够好
2楼-- · 2019-08-27 20:52

If you're using xsltproc (that is the libxslt processor), you can take advantage of the EXSLT set:distinct() extension function in order to utilize a shortened version of Muenchian grouping:

XSLT 1.0 (+EXSLT)

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:set="http://exslt.org/sets"
extension-element-prefixes="set">
<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="/testng-results">
    <xsl:variable name="tests" select="suite/test/class/test-method[not(@is-config)]" />
    <Suite>
        <xsl:for-each select="set:distinct($tests/@name)">
            <Test>
                <Method_Name>
                    <xsl:value-of select="."/>
                </Method_Name>
                <Status>
                    <xsl:value-of select="key('test-by-name', .)[last()]/@status"/>
                </Status>
            </Test>
        </xsl:for-each>
    </Suite>
</xsl:template>

</xsl:stylesheet>

Or, if you prefer, you can do it in pure XSLT 1.0 as:

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="@name" />

<xsl:template match="/testng-results">
    <Suite>
        <xsl:for-each select="suite/test/class/test-method[not(@is-config)][count(. | key('test-by-name', @name)[last()]) = 1]">
            <Test>
                <Method_Name>
                    <xsl:value-of select="."/>
                </Method_Name>
                <Status>
                    <xsl:value-of select="@status"/>
                </Status>
            </Test>
        </xsl:for-each>
    </Suite>
</xsl:template>

</xsl:stylesheet>

Note the use of the [last()] predicate instead of the usual [1].

查看更多
登录 后发表回答