XSLT从子目录转换多个文件

时间:2011-05-25 18:43:52

标签: xml xslt transform

我创建了一个可以转换单个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>

2 个答案:

答案 0 :(得分:7)

这可能是最简单的示例,如何处理文件系统子树中的所有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文档(未使用,忽略)时,此转换将标识规则应用于{{1}中任意*.xml个文件中包含的每个XML文档}文件系统的子树。

要进行更多有意义的处理,需要覆盖身份模板 - 在C:/Temp模式下。

在您的具体情况下,我相信您可以直接替换

inFile

<强>与

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

这将在当前(文档)节点之外选择的节点上应用所需的模板。

答案 1 :(得分:0)

我只是想添加一个轻量级的Dimitre的优秀答案。如果您在<foo>目录中有<foo>个文档(根节点),并且<xsl:template match="/"> <xsl:apply-templates select="collection($my_url)/foo"/> </xsl:template> 有一个可用的XSLT程序,那么只需创建这样的顶级模板:

<xsl:param name="my_url"/>

假设您希望将URL作为参数{{1}},在命令行中指定。

相关问题