XSLT 1.0 (xsltproc) - 只输出最后一个重复节点 [英] XSLT 1.0 (xsltproc) - Output only the last duplicate node
问题描述
如何使用 XSLT 1.0 仅输出最后一个重复节点?我使用 xsltproc 处理器.
How do I output just the last duplicate node using XSLT 1.0? I use xsltproc processor.
输入.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>
注意:我无法改变嵌套匹配的完成方式(for-each class,然后for-each test-method,因为其他原因我需要这样)
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)
当前输出.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(每个重复的测试方法只输出最后一个节点):
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>
推荐答案
如果您使用的是 xsltproc
(即 libxslt
处理器),您可以利用EXSLT set:distinct()
扩展函数,以便使用 慕尼黑分组:
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>
<小时>
或者,如果您愿意,您可以在纯 XSLT 1.0 中这样做:
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>
<小时>
注意使用 [last()]
谓词代替通常的 [1]
.
这篇关于XSLT 1.0 (xsltproc) - 只输出最后一个重复节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!