【发布时间】:2019-05-17 23:01:17
【问题描述】:
如何使用 XSLT 1.0 仅输出最后一个重复节点? 我使用 xsltproc 处理器。
输入.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>
我当前的 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>
注意:我无法更改嵌套匹配的完成方式(对于每个类,然后是对于每个测试方法,因为出于其他原因我需要这样做)
当前输出.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(每个重复的测试方法只输出最后一个节点):
<?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>
【问题讨论】: