XSLT 从子目录转换多个文件 [英] XSLT Transform multiple files from subdirectory

查看:44
本文介绍了XSLT 从子目录转换多个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个可以转换单个 XML 文件的 XSLT 文件.但是,我有数百个包含多个 xml 文件的目录.XSLT 中有没有办法转换所有这些文件.我正在使用集合函数来获取所有文件的列表.但是,现在不确定如何应用转换.

I have created a XSLT file that can transform a single XML file. However, I have several hundred directories with multiple xml files. Is there a way in XSLT to transform all these files. I am using the collection function to get a list of all files. But, not sure how to apply the transform now.

这是我的示例 XSLT 文件.基本上,我想遍历所有 xml 文件并将模板表应用于单个文件.所有这些转换的输出都需要在一个单一的纯文本文件中.

Here is my example XSLT file. Basically, I want to loop through all the xml files and apply the template table on the individual file. The output of all these transforms needs to be in one single flat text file.

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

推荐答案

这可能是如何处理文件系统子树中所有 xml 文件的最简单示例(使用 collection() 函数作为在撒克逊语中实施):

Here is probably the simplest example how to process all the xml files in a file system subtree (using the collection() function as implemented in Saxon):

<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 文档文件系统的 C:/Temp 子树.

When applied on any XML document (not used, ignored), this transformation applies the identity rule to every XML document contained in any of the *.xml files in the C:/Temp subtree of the file system.

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

To do more neaningful processing, one needs to override the identity template -- in the inFile mode.

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

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

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

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

and this applies the desired templates on the nodes selected off the current (document) node.

这篇关于XSLT 从子目录转换多个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆