Dimitre 的答案在您的情况下看起来是最快的解决方案。但既然你问了,这里有一个 XProc 替代方案:
<p:declare-step version="1.0" xmlns:p="http://www.w3.org/ns/xproc" xmlns:c="http://www.w3.org/ns/xproc-step" exclude-inline-prefixes="#all" name="main">
<!-- create context for p:variable with base-uri pointing to the location of this file -->
<p:input port="source"><p:inline><x/></p:inline></p:input>
<!-- any params passed in from outside get passed through to p:xslt automatically! -->
<p:input port="parameters" kind="parameter"/>
<!-- configuration options for steering input and output -->
<p:option name="input-dir" select="'./'"/>
<p:option name="input-filter" select="'^report_.*\.xml$'"/>
<p:option name="output-dir" select="'./'"/>
<!-- resolve any path to base uri of this file, to make sure they are absolute -->
<p:variable name="abs-input-dir" select="resolve-uri($input-dir, base-uri(/))"/>
<p:variable name="abs-output-dir" select="resolve-uri($output-dir, base-uri(/))"/>
<!-- first step: get list of all files in input-dir -->
<p:directory-list>
<p:with-option name="path" select="$abs-input-dir"/>
</p:directory-list>
<!-- iterate over each file to load it -->
<p:for-each>
<p:iteration-source select="//c:file[matches(@name, $input-filter)]"/>
<p:load>
<p:with-option name="href" select="resolve-uri(/c:file/@name, $abs-input-dir)"/>
</p:load>
</p:for-each>
<!-- wrap all files in a reports element to be able to hand it in to the xslt as a single input document -->
<p:wrap-sequence wrapper="reports"/>
<!-- apply the xslt (stylesheet is loaded below) -->
<p:xslt>
<p:input port="stylesheet">
<p:pipe step="style" port="result"/>
</p:input>
</p:xslt>
<!-- store the result in the output dir -->
<p:store>
<p:with-option name="href" select="resolve-uri('merged-reports.xml', $abs-output-dir)"/>
</p:store>
<!-- loading of the stylesheet.. -->
<p:load href="process-reports.xsl" name="style"/>
</p:declare-step>
例如,将上述内容存储为 process-reports.xpl。您可以使用 XMLCalabash (http://xmlcalabash.com/download/) 运行它。你可以像这样运行它:
java -jar calabash.jar process-reports.xpl input-dir=./ output-dir=./
上面的代码假设有一个 process-reports.xsl,它接受一个包含所有报告的文档,并对其进行一些处理。您也可以在纯 XProc 中进行处理,但您可能更喜欢这种方式。
您还可以将 p:xslt 步骤向上移动到 p:for-each 内(在 p:load 下方),这将导致 xslt 单独应用于每个报告。
祝你好运!