【问题标题】:XSLT Transform multiple files from subdirectoryXSLT 从子目录转换多个文件
【发布时间】: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>&#xA;</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 任务。这行得通吗?
  • 你在什么操作系统上运行?

标签: xml xslt transform


【解决方案1】:

这可能是如何处理文件系统子树中的所有 xml 文件的最简单示例(使用 Saxon 中实现的 collection() 函数):

<xsl:stylesheet version="2.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:template match="node()|@*" mode="inFile">
     <xsl:copy>
       <xsl:apply-templates mode="inFile" select="node()|@*"/>
     </xsl:copy>
 </xsl:template>

 <xsl:template match="/">
   <xsl:apply-templates mode="inFile" select=
   "collection('file:///C:/temp?select=*.xml;recurse=yes')"/>
 </xsl:template>
</xsl:stylesheet>

当应用于任何 XML 文档(未使用,忽略)时,此转换将身份规则应用于包含在任何 *.xml 文件中的每个 XML 文档文件系统。

要进行更精细的处理,需要覆盖身份模板 -- 在inFile 模式下。

在您的具体情况下,我相信您可以简单地替换:

            <xsl:apply-templates select="/testResults/result/tables/table[14]">     

与

            <xsl:apply-templates select="./testResults/result/tables/table[14]">    

这会将所需的模板应用到从当前(文档)节点中选择的节点上。

【讨论】:

  • 当我在 C# 中转换它时出现错误“集合是未知的 xslt 函数”。请帮帮我
  • @shansfk,是的,.NET 附带的 XSLT 引擎不支持 XSLT 2.0。问题是如何在 XSLT 2.0 中做到这一点——正如 OP 代码所提供的那样。您可以安装和使用 Saxon.NET,然后这种转换将产生预期的结果。
【解决方案2】:

我只是想添加 Dimitre 出色答案的轻量级版本。如果您有一个目录中的&lt;foo&gt; 文档(根节点),并且有一个用于&lt;foo&gt; 的工作XSLT 程序,只需像这样制作您的顶级模板:

<xsl:template match="/">
    <xsl:apply-templates select="collection($my_url)/foo"/>
</xsl:template>

假设您希望在命令行中指定 URL 作为参数 &lt;xsl:param name="my_url"/&gt;。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-01-07
    • 1970-01-01
    • 1970-01-01
    • 2022-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-04
    相关资源
    最近更新 更多