简体   繁体   English

如何仅使用Ant和XSLT创建目标目录中的文件列表的XML文件?

[英]How can I create an XML file that is a list of files in a target directory using only Ant and XSLT?

Using only Ant and XSLT, I'd like to create an XML file that is a list of XML files in a specific directory. 仅使用Ant和XSLT,我想创建一个XML文件,它是特定目录中的XML文件列表。

Ant's concat task doesn't do the job as I end up with a list that's not XML -- ie. Ant的concat任务不起作用,因为我最终得到的是一个不是XML的列表 - 即。 it doesn't have a single root element. 它没有单个根元素。

I have an XSLT file that I apply, using the XSLT Ant task, that uses the collection() function. 我有一个XSLT文件,我使用XSLT Ant任务,使用collection()函数。 This produces exactly the result I want, but it tries to do so for each file in the target directory -- I want just one list. 这产生了我想要的结果,但它尝试对目标目录中的每个文件执行此操作 - 我只想要一个列表。 My XSLT is operating on every file in the target directory (collection) -- how can I limit the number of tims the XSLT is applied? 我的XSLT正在目标目录(集合)中的每个文件上运行 - 如何限制应用XSLT的时间数?

Here's what I have so far: 这是我到目前为止所拥有的:

XML files are in the target directory c:\\tmp XML文件位于目标目录c:\\tmp

This is the XSL file that I apply to the files in the target directory (using the Ant XSLT task); 这是我应用于目标目录中的文件的XSL文件(使用Ant XSLT任务);

 <xsl:template match="/">
    <xsl:call-template name="generatelist" />
</xsl:template>

<xsl:template name="generatelist">
    <xsl:result-document href="list.xml">
        <xsl:element name="list">
            <xsl:element name="dir">
                <xsl:for-each
                    select="collection('file:///C:/tmp?select=*.xml')">
                    <xsl:element name="file">
                        <xsl:attribute name="name">
                            <xsl:value-of select="tokenize(document-uri(.), '/')[last()]" />
                        </xsl:attribute>
                    </xsl:element>
                </xsl:for-each>
            </xsl:element>
        </xsl:element>
    </xsl:result-document>
</xsl:template>

And this is the resulting XML list: 这是生成的XML列表:

<list>
    <dir>
        <file name="filename_1.xml"/>
        <file name="filename_2.xml"/>
         . . .
        <file name="filename_n.xml"/>
    </dir>
</list>

Thanks. 谢谢。

Drew 德鲁

Adding the Ant XSLT task that I'm using: 添加我正在使用的Ant XSLT任务:

<xslt basedir="${staging_2}"
      destdir="${staging_3}" extension=".xml" includes="**/*.xml"
      style="create_list.xsl">     
</xslt>

XSLT really isn't the appropriate tool for your needs. XSLT确实不适合您的需求。 XSLT is best for transforming XML into new XML. XSLT最适合将XML转换为新的XML。 In this case, however, the source isn't XML; 但是,在这种情况下,源不是XML; it's a filesystem directory. 它是一个文件系统目录。

Given this, it's fine to just generate the XML directly. 鉴于此,只需直接生成XML即可。 The following Ant script uses the third-party Ant-Contrib library's <for> task: 以下Ant脚本使用第三方Ant-Contrib库的<for>任务:

<project name="ant-echo-xml" default="run" basedir=".">
    <taskdef resource="net/sf/antcontrib/antlib.xml" />

    <target name="run">
        <property name="dest-xml.file" value="list.xml"/>

        <echo file="${dest-xml.file}"
><![CDATA[<list>
    <dir>
]]></echo>

        <for param="src-xml.absolute-path">
            <fileset dir="my-dir" includes="*.xml"/>
            <sequential>
                <local name="src-xml.basename"/>
                <basename property="src-xml.basename" file="@{src-xml.absolute-path}"/>

                <echo file="${dest-xml.file}" append="yes"
>        <![CDATA[<file name="${src-xml.basename}"/>
]]></echo>
            </sequential>
        </for>

        <echo file="${dest-xml.file}" append="yes"
><![CDATA[    </dir>
</list>
]]></echo>
    </target>
</project>

Outputs: 输出:

<list>
    <dir>
        <file name="filename_1.xml"/>
        <file name="filename_2.xml"/>
    </dir>
</list>

Since the XSLT itself takes care of enumerating the file names you just need to run it once, ie give it just one file to use as input and one file to use as output. 由于XSLT本身负责枚举文件名,您只需要运行一次,即只给它一个文件用作输入,一个文件用作输出。 The stylesheet doesn't use anything from the input document so any input file will do as long as it's XML, you could use the stylesheet itself as its own input. 样式表不使用输入文档中的任何内容,因此任何输入文件都可以使用XML,您可以使用样式表本身作为自己的输入。

<xslt style="create_list.xsl" in="create_list.xsl" out="list.xsl" />

and remove the <xsl:result-document> from the stylesheet so it just outputs to the default result document (the one specified by out="..." in build.xml). 并从样式表中删除<xsl:result-document> ,以便它只输出到默认结果文档(在build.xml中由out="..."指定的文档)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM