简体   繁体   English

xsltproc在多个文件之前和之后添加文本

[英]xsltproc add text before and after multiple files

I'm using the xsltproc utility to transform multiple xml test results into pretty printed console output using a command like the following. 我正在使用xsltproc实用程序,使用如下xsltproc将多个xml测试结果转换成漂亮的打印控制台输出。

xsltproc stylesheet.xslt testresults/*

Where stylesheet.xslt looks something like this: 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>

This gives me an output similar to this: 这给了我类似于以下的输出:

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

What I want is the following: 我想要的是以下内容:

begin
TestSuite: 1
TestSuite: 2
TestSuite: 3
end

Googling is turning up empty. 谷歌搜索变成空的。 I suspect I might be able to merge the xml files somehow before I give them to xsltproc , but I was hoping for a simpler solution. 我怀疑我可以将xml文件合并到xsltproc ,但是我希望有一个更简单的解决方案。

xsltproc transforms each specified XML document separately, as indeed is the only sensible thing for it to do because XSLT operates on a single source tree, and xsltproc doesn't have enough information to compose multiple documents into a single tree. xsltproc转换每个指定的XML文档,这确实是唯一明智的做法,因为XSLT在单个源树上运行,并且xsltproc没有足够的信息将多个文档组合到单个树中。 Since your template emits text nodes with the "begin" and "end" text, those nodes are emitted for each input document. 由于您的模板会发出带有“开始”和“结束”文本的文本节点,因此将为每个输入文档发出这些节点。

There are several ways you could arrange to have just one "begin" and one "end". 有几种方法可以安排只有一个“开始”和一个“结束”。 All of the reasonable ones start with lifting the text nodes out your template for <testsuite> elements. 所有合理的选择都始于将文本节点提升出<testsuite>元素的模板。 If each "TestSuite:" line in the output should correspond to one <testsuite> element then you'll need to do that even if you physically merge the input documents. 如果输出中的每个“ TestSuite:”行应对应一个<testsuite>元素,那么即使您实际合并了输入文档,也需要这样做。

One solution would be to remove the responsibility for the "begin" and "end" lines from XSLT altogether. 一种解决方案是完全取消XSLT对“开始”和“结束”行的责任。 For example, remove the xsl:text elements from the stylesheet and write a simple script such as this: 例如,从样式表中删除xsl:text元素,并编写一个简单的脚本,例如:

echo begin
xsltproc stylesheet.xslt testresults/*
echo end

Alternatively, if the individual XML files do not start with XML declarations, then you might merge them dynamically, by running xsltproc with a command such as this: 或者,如果各个XML文件不是以XML声明开头,则可以通过使用以下命令运行xsltproc来动态合并它们:

{ echo "<suites>"; cat testresults/*; echo "</suites>"; } \
    | xsltproc stylesheet.xslt -

The corresponding stylesheet might then take a form along these lines: 然后,相应的样式表可能采用以下形式的形式:

<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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM