我有这个来源xml
<TestRun>
<Results>
<UnitTestResult testId="1f32f6b9-6623-1321-32b5-b2e84ef44f61" testName="TestMethod1" outcome="Passed">
<Output>
<TextMessages>
<Message>__OWNER__='Team-2' some other stuff1</Message>
</TextMessages>
</Output>
</UnitTestResult>
<UnitTestResult testId="1f32f6b9-6623-1321-32b5-b2e84ef44f62" testName="TestMethod2" outcome="Failed">
<Output>
<TextMessages>
<Message>__OWNER__='Team-1' some other stuff2</Message>
</TextMessages>
</Output>
</UnitTestResult>
<UnitTestResult testId="1f32f6b9-6623-1321-32b5-b2e84ef44f63" testName="TestMethod3" outcome="Failed">
<Output>
<TextMessages>
<Message>__OWNER__='Team-1' some other stuff 3</Message>
</TextMessages>
</Output>
</UnitTestResult>
<UnitTestResult testId="1f32f6b9-6623-1321-32b5-b2e84ef44f64" testName="TestMethod4" outcome="Failed">
<Output>
<TextMessages>
<Message>__OWNER__='Team-2' some other stuff 4</Message>
</TextMessages>
</Output>
</UnitTestResult>
</Results>
</TestRun>
我需要按所有者对失败的测试进行分组,这是Message节点的子字符串。
所以结果应该是
<TestResults>
<Owner name="Team-1">
<TestMethod name="TestMethod2"/>
<TestMethod name="TestMethod3"/>
</Owner>
<Owner name="Team-2">
<TestMethod name="TestMethod4"/>
</Owner>
任何想法如何实现这一点(最好使用XSLT 1.0)?
答案 0 :(得分:1)
对于XSLT 1.0中的分组,请参阅: http://www.jenitennison.com/xslt/grouping/muenchian.html
-
根据您的修改进行了编辑:
要按子字符串分组,您可以将密钥定义为:
<xsl:key name="result" match="UnitTestResult" use="substring-before(substring-after(Output/TextMessages/Message, $apos), $apos)" />
其中$ apos定义为:
<xsl:variable name="apos">'</xsl:variable>
答案 1 :(得分:0)
草稿版本I用双引号引用的团队名称(不是单引号) 输出不是xml而是纯文本。但是脚本会进行分组。
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="text" indent="yes" omit-xml-declaration="yes" />
<xsl:strip-space elements="*" />
<xsl:variable name="apos">'</xsl:variable>
<xsl:key name="owner-by-name" match="UnitTestResult[@outcome='Failed']"
use="substring-before(substring-after(Output/TextMessages/Message,'__OWNER__="'),'"')" />
<xsl:template match="*/text()" />
<xsl:template match="TestRun/Results">
<xsl:for-each
select="UnitTestResult[generate-id(.)=generate-id(key('owner-by-name',substring-before(substring-after(Output/TextMessages/Message,'__OWNER__="'),'"'))[1])]">
<xsl:text>
</xsl:text>
<xsl:text>
</xsl:text>
<xsl:value-of
select="substring-before(substring-after(Output/TextMessages/Message,'__OWNER__="'),'"')" />
<xsl:text>
------------------------------------</xsl:text>
<xsl:for-each
select="key('owner-by-name', substring-before(substring-after(Output/TextMessages/Message,'__OWNER__="'),'"'))">
<xsl:text>
</xsl:text>
<xsl:value-of select="@testName" />
</xsl:for-each>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>