[英]xsltproc add text before and after multiple files
我正在使用xsltproc
实用程序,使用如下xsltproc
将多个xml测试结果转换成漂亮的打印控制台输出。
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
谷歌搜索变成空的。 我怀疑我可以将xml文件合并到xsltproc
,但是我希望有一个更简单的解决方案。
xsltproc
转换每个指定的XML文档,这确实是唯一明智的做法,因为XSLT在单个源树上运行,并且xsltproc
没有足够的信息将多个文档组合到单个树中。 由于您的模板会发出带有“开始”和“结束”文本的文本节点,因此将为每个输入文档发出这些节点。
有几种方法可以安排只有一个“开始”和一个“结束”。 所有合理的选择都始于将文本节点提升出<testsuite>
元素的模板。 如果输出中的每个“ TestSuite:”行应对应一个<testsuite>
元素,那么即使您实际合并了输入文档,也需要这样做。
一种解决方案是完全取消XSLT对“开始”和“结束”行的责任。 例如,从样式表中删除xsl:text
元素,并编写一个简单的脚本,例如:
echo begin
xsltproc stylesheet.xslt testresults/*
echo end
或者,如果各个XML文件不是以XML声明开头,则可以通过使用以下命令运行xsltproc
来动态合并它们:
{ echo "<suites>"; cat testresults/*; echo "</suites>"; } \
| xsltproc stylesheet.xslt -
然后,相应的样式表可能采用以下形式的形式:
<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
</xsl:text>
<xsl:apply-templates select="testsuite"/>
<xsl:text>
end</xsl:text>
</xsl:template>
<xsl:template match="testsuite">
...
</xsl:template>
</xsl:stylesheet>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.