xml - XSLT 从子目录转换多个文件

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

最佳答案

这可能是最简单的示例,说明如何处理文件系统子树中的所有 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 文档文件系统的 C:/Temp 子树。

要进行更有意义的处理,需要在 inFile 模式下覆盖标识模板。

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

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

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

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

关于xml - XSLT 从子目录转换多个文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6129262/

相关文章:

python - 在 Python 中从 XML 数据创建字典(使用 xml.etree.ElementTree)

java - Android Studio : Using radioButtons for a quiz

html - 谷歌浏览器和其他 : saving xml+xsl as html

c++ transform with pair get Segmentation fault

javascript - HTML - 影响固定元素的主体旋转

ruby-on-rails - 哈希数组的 transform_keys

jquery - 使用 jQuery 解析带有冒号的 XML 节点值

c# - 为什么我的新 XmlTextReader(stream) 以数兆字节读入内存而不是正确地流式传输?

html - 将div格式化成表格

xml - 替换各种 XML 文件中的字符串