使用ant和xslt动态连接xml文件

Cao*_*lte 5 xml ant xslt saxon

我们有大量的xml配置文件,我们希望在构建时将它们合并到一个主版本中.较小的配置文件更容易维护,一个大文件加载更快,所以我想这是一个流行的构建转换过程,我会在网上找到很多很好的例子.

我能够在StackOverflow找到问题的一部分的一些好的解决方案,但它们都依赖于知道需要合并的xml文件的名称.这对我来说似乎是一种不必要的开销.应该可以编写一个构建脚本,它可以动态计算需要哪些输入xml文件.

不幸的是,我能找到的唯一方法是实现这一目标.它的工作原理如下,

  1. 使用随机的ant任务我偷了互联网,将目录列表写入xml文件.
  2. 将xml文件提供给xslt转换,然后可以加载引用xml文件的其他目录,并将它们连接起来.
  3. 删除包含目录列表的临时xml文件.

这是蚂蚁脚本

<taskdef name="xml-dir-list"
  classname="net.matthaynes.xml.dirlist.AntFileListing"
  classpath="antlib/xml-dir-listing.0.1.jar;
    antlib/jakarta-regexp-1.5.jar;antlib/log4j-1.2.14.jar"/>

<macrodef name="build-plugin-xml" description="todo">
    <attribute name="pluginName"/>

    <xml-dir-list depth="0" verbose="false" 
      srcDir="${src.dir}/@{pluginName}/forms/" includesRegEx="\.xml$" 
      destFile="${src.dir}/@{pluginName}/forms/fileList.xml"/>

    <xslt in="${src.dir}/forms/fileList.xml"
      out="${src.dir}/@{pluginName}/@{pluginName}_extn.yuix
      style="${src.dir}/@{pluginName}/forms/extn.yuix.xsl" />

    <delete file="${src.dir}/@{pluginName}/forms/fileList.xml"/>
</macrodef>
Run Code Online (Sandbox Code Playgroud)

这是样式表,

<xsl:stylesheet version="1.0" 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

    <xsl:template match="/">
        <Forms applicationId="YFSSYS00011">
            <GlobalExtensions>
                <Tasks/>
            </GlobalExtensions> 
            <xsl:apply-templates select="directory/file"/>
        </Forms>
    </xsl:template>

    <xsl:template match="file">
        <xsl:copy-of select="document(@name)/Forms/Form"/>
    </xsl:template>
</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)

有没有人找到一种更简单的方法来实现在XSLT中合并哪些文件的动态发现?XSLT无法直接读取目录并不奇怪,但我希望找到一种更简单的方法来传递文件名列表,而不是通过另一个xml文件.

实施解决方案

一旦我对蚂蚁脚本做了一些额外的调整,Dimitre的解决方案效果很好,

<taskdef name="saxon-xslt" classname="net.sf.saxon.ant.AntTransform"
  classpath="antlib/saxon9.jar;antlib/saxon9-ant.jar"/>
Run Code Online (Sandbox Code Playgroud)

[...]

<macrodef name="build-plugin-xml" description="todo">
    <attribute name="pluginName"/>

    <saxon-xslt 
      in="build.xml"
      out="${compca.src.dir}/temp/@{pluginName}/@{pluginName}_extn.yuix"
      style="antscripts/extn.yuix.xsl">
    <param name="formsDir" 
      expression="${compca.src.dir}/@{pluginName}/forms/"/>
    </saxon-xslt>
</macrodef>
Run Code Online (Sandbox Code Playgroud)

和xsl样式表(我移动了)

   <xsl:stylesheet version="1.0" 
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
      <xsl:param name="formsDir" />
        <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

        <xsl:template match="/">
            <Forms applicationId="YFSSYS00011">
                <GlobalExtensions>
                    <Tasks/>
                </GlobalExtensions>
                <xsl:apply-templates select=
                  "collection(
                  concat('file:///',
                  $formsDir,
                  '?select=*.yuix;recurse=yes;on-error=ignore'
                  )
                  )/*
                "/>
            </Forms>

        </xsl:template>

        <xsl:template match="file">
            <xsl:copy-of select="/Forms/Form"/>
        </xsl:template>
    </xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)

这些调整只是让Saxon9加载并使用参数设置目录.

Dim*_*hev 6

有没有人找到一种更简单的方法来实现在XSLT中合并哪些文件的动态发现?XSLT无法直接读取目录并不奇怪,但我希望找到一种更简单的方法来传递文件名列表,而不是通过另一个xml文件.

可以使用XPath 2/0/XSLT 2.0完成XML文件的动态发现和处理.更具体地说,必须使用XPath 2.0 collection()功能.

有关详细信息,请参阅我对此问题的回答.

因此,如果ANT可以使用合适的XSLT 2.0处理器(我建议使用Saxon),则问题有一个完整的解决方案,使用标准collection()功能.