2017-07-25 94 views
0

我使用xsltproc实用程序将多个xml测试结果转换为使用类似以下命令的漂亮的打印控制台输出。xsltproc在多个文件之前和之后添加文本

xsltproc stylesheet.xslt testresults/* 

stylesheet.xslt看起来是这样的:

<!-- One testsuite per xml test report file --> 
<xsl:template match="/testsuite"> 
    <xsl:text>begin</xsl:text> 
    ... 
    <xsl:text>end</xsl:text> 
</xsl:template> 

这给我类似这样的输出:

begin 
TestSuite: 1 
end 
begin 
TestSuite: 2 
end 
begin 
TestSuite: 3 
end 

我想是这样的:

begin 
TestSuite: 1 
TestSuite: 2 
TestSuite: 3 
end 

谷歌搜索是T.掏空。我怀疑我可能能够以某种方式合并xml文件,然后我将它们提供给xsltproc,但我希望获得更简单的解决方案。

回答

1

xsltproc分别转换每个指定的XML文档,因为XSLT在单一源树上运行,并且xsltproc没有足够的信息将多个文档组合到一棵树中,因此确实是它唯一明智的做法。由于您的模板使用“开始”和“结束”文本发出文本节点,因此会为每个输入文档发出这些节点。

有几种方法可以安排只有一个“开始”和一个“结束”。 所有的合理的开始与解除您的模板文字节点为<testsuite>元素。如果输出中的每个“TestSuite:”行都对应一个<testsuite>元素,则即使您物理合并输入文档,也需要这样做。

一个解决方案是从XSLT中全部删除“开始”和“结束”行的责任。例如,删除从样式表中的xsl:text元素,写一个简单的脚本像这样:

echo begin 
xsltproc stylesheet.xslt testresults/* 
echo end 

另外,如果单独的XML文件不与XML声明开始,那么你可能会动态地将它们合并,通过运行xsltproc用如此的命令:

相应样式表可能再取一个形式沿着这些路线:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:output method="text"/> 

    <xsl:template match="/suites"> 
    <!-- the transform of the root element produces the "begin" and "end" --> 
    <xsl:text>begin&#x0A;</xsl:text> 
    <xsl:apply-templates select="testsuite"/> 
    <xsl:text>&#x0A;end</xsl:text> 
    </xsl:template> 

    <xsl:template match="testsuite"> 
    ... 
    </xsl:template> 
</xsl:stylesheet> 
相关问题