【发布时间】:2011-09-02 00:34:04
【问题描述】:
我创建了一个可以转换单个 XML 文件的 XSLT 文件。但是,我有数百个包含多个 xml 文件的目录。 XSLT 中有没有办法转换所有这些文件。我正在使用收集功能来获取所有文件的列表。但是,现在不确定如何应用转换。
这是我的示例 XSLT 文件。基本上,我想遍历所有 xml 文件并将模板表应用于单个文件。所有这些转换的输出都需要放在一个平面文本文件中。
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:foo="http://whatever">
<xsl:output method="text" encoding="ISO-8859-1"/>
<xsl:template name="process">
<xsl:variable name="files" select="collection('file:///C:/files/testResults?select=*.xml;recurse=yes')"/>
<xsl:for-each select="$files">
<xsl:if test="(not(contains(document-uri(.), 'SuiteSetUp'))) and (not(contains(document-uri(.), 'SuiteTearDown')))">
<xsl:value-of select="tokenize(document-uri(.), '/')[last()]"></xsl:value-of>
<xsl:apply-templates select="/testResults/result/tables/table[14]">
<xsl:with-param name="title" select="/testResults/rootPath"></xsl:with-param>
</xsl:apply-templates>
<xsl:apply-templates select="/testResults/result/tables/table[15]"/>
</xsl:if>
</xsl:for-each>
</xsl:template>
<xsl:template match="table">
<xsl:param name="testName"></xsl:param>
<xsl:for-each select="row">
<xsl:if test="position() > 2">
<xsl:variable name="choices" select="col[2]"></xsl:variable>
<xsl:if test="contains($choices, 'fail')">
<xsl:value-of select="$testName"></xsl:value-of>
<xsl:text>|</xsl:text>
<xsl:value-of select="col[1]"></xsl:value-of>
<xsl:text>|</xsl:text>
<xsl:value-of select="foo:getCorrectChoices(col[2])"></xsl:value-of>
<xsl:text>|</xsl:text>
<xsl:value-of select="foo:getExpectedChoices(col[2])"></xsl:value-of>
<xsl:text>|</xsl:text>
<xsl:call-template name="NewLine"/>
</xsl:if>
</xsl:if>
</xsl:for-each>
</xsl:template>
<xsl:template name="NewLine">
<xsl:text>
</xsl:text>
</xsl:template>
<xsl:function name="foo:getCorrectChoices">
<xsl:param name="content"></xsl:param>
<xsl:analyze-string select="$content" regex="\[(\[.+?\])\] fail">
<xsl:matching-substring>
<xsl:value-of select="regex-group(1)"></xsl:value-of>
</xsl:matching-substring>
</xsl:analyze-string>
</xsl:function>
<xsl:function name="foo:getExpectedChoices">
<xsl:param name="content"></xsl:param>
<xsl:analyze-string select="$content" regex="fail\(expected \[(\[.+?\])\]">
<xsl:matching-substring>
<xsl:value-of select="regex-group(1)"></xsl:value-of>
</xsl:matching-substring>
</xsl:analyze-string>
</xsl:function>
</xsl:stylesheet>
【问题讨论】:
-
您使用的是哪种 XSLT 处理器?如果您使用的是 Saxon,则可以直接从命令行执行此操作。 Saxon 能够转换您希望对其应用相同转换的一批文件。
-
我正在使用一个名为 EditX 的 XSLT 编辑器来构建我的 XSLT,我想也许我可以从那里运行它。但看起来我需要从其他地方运行它。我看到了 XSLT 的 ant 任务。这行得通吗?
-
你在什么操作系统上运行?