[英]Using xsl to parse xml output, use of variables?
我正在尝试解析googletest的xml文件输出,以便以漂亮的方式在html页面中显示结果。 我对此有一个明确的了解,并找到了可行的解决方案。 只剩下一件事要做。 当前,每个类都有一个标题,每个功能和测试都显示在下面,我正在寻找一种从xml属性提取函数名称的方法,以使其具有自己的子标题。 下面的例子
班级名称
functionName_testName
functionName_test2Name
function2Name_testName
班级名称
functionName
测试名
test2Name
function2Name
测试名
要解析的XML
<testsuite name="testPacketProcessor" tests="27" failures="0" disabled="0" errors="0" time="36.875">
<testcase name="testInitialise_IpNotSet" status="run" time="0" classname="testPacketProcessor" />
<testcase name="testInitialise_GoodIp" status="run" time="2.046" classname="testPacketProcessor" />
<testcase name="testInitialise_BadIp" status="run" time="0.032" classname="testPacketProcessor" />
</testsuite>
当前实施的XSL
<xsl:for-each select="testsuites/testsuite">
<div class="testHeading">
<span><xsl:value-of select="substring-after(@name,'test')"/></span>
</div>
<xsl:for-each select="testcase">
<div class="testReport">
<xsl:if test="not(starts-with(@name,'DISABLED'))"><xsl:value-of select="substring-after(@name,'test')"/></xsl:if>
<xsl:if test="starts-with(@name,'DISABLED')"><xsl:value-of select="substring-after(@name,'DISABLED_test')"/></xsl:if>
<xsl:text> - </xsl:text>
<xsl:if test="starts-with(@status,'run')">
<xsl:if test="failure"><xsl:text>Fail</xsl:text></xsl:if>
<xsl:if test= "not(failure)"><xsl:text>Pass</xsl:text></xsl:if>
</xsl:if>
<xsl:if test="starts-with(@status,'not')"><xsl:text>Disabled</xsl:text></xsl:if>
</div>
</xsl:for-each>
</xsl:for-each>
以上面的xml为例,我希望在分组处理器标题下有一个“ Initialise”子标题,在该子标题下加倍了每个测试
最好的解决方法的任何建议将不胜感激。 我看过xsl变量,但是无法理解如何处理无法更改值的情况。 还阅读了一些有关xsl模板和递归的知识,但我不确定在这种情况下如何工作。
提前致谢
Dougie
此转换为每个测试用例生成子标题和名称 :
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="testcase">
Subheading: <xsl:value-of select=
"substring-before(substring-after(@name, 'test'),
'_'
)
"/>
Name: <xsl:value-of select="substring-after(@name, '_')"/>
<xsl:text>
</xsl:text>
</xsl:template>
</xsl:stylesheet>
当应用于提供的XML文档时:
<testsuites>
<testsuite name="testPacketProcessor" tests="27" failures="0" disabled="0" errors="0" time="36.875">
<testcase name="testInitialise_IpNotSet" status="run" time="0" classname="testPacketProcessor" />
<testcase name="testInitialise_GoodIp" status="run" time="2.046" classname="testPacketProcessor" />
<testcase name="testInitialise_BadIp" status="run" time="0.032" classname="testPacketProcessor" />
</testsuite>
</testsuites>
产生想要的正确结果 :
Subheading: Initialise
Name: IpNotSet
Subheading: Initialise
Name: GoodIp
Subheading: Initialise
Name: BadIp
说明 :使用标准XPath函数substring-before()
和substring-after()
编辑 :OP在一条评论中澄清说,他想按功能分组(烦恼的坏问题?):
此转换执行所需的分组:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:key name="kTestsByFunction" match="testcase"
use="substring-before(substring-after(@name, 'test'),
'_'
)"
/>
<xsl:template match=
"testcase
[generate-id()
=
generate-id(key('kTestsByFunction',
substring-before(substring-after(@name, 'test'),
'_'
)
)[1]
)
]
">
Subheading:
<xsl:value-of select=
"substring-before(substring-after(@name, 'test'),
'_'
)
"/>
<xsl:for-each select=
"key('kTestsByFunction',
substring-before(substring-after(@name, 'test'),
'_'
)
)
">
Name:
<xsl:value-of select="substring-after(@name, '_')"/>
<xsl:text>
</xsl:text>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
并产生想要的结果 :
Subheading:
Initialise
Name:
IpNotSet
Name:
GoodIp
Name:
BadIp
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.