开发者

How can I turn the structure of an XML file into a folder structure using ANT

开发者 https://www.devze.com 2022-12-28 06:54 出处:网络
I would like to be able to pass an XML file to an ANT build script and have it create a folder structure mimicking the nodal structure of the XML, using the build files parent directory as the root.

I would like to be able to pass an XML file to an ANT build script and have it create a folder structure mimicking the nodal structure of the XML, using the build files parent directory as the root.

For Example using:

<root>
    <folder1>
         <folder1-1/>
    </folder1>
    <folder2/>
    <folder3>
         <folder3-1/>
    </folder3>
</root>

ant would create:

folder1
   -folder1-1
folder2
folder3
   -folder3-1 

I know how to crea开发者_运维百科te a directory, but i'm not sure how to have ANT parse the XML.


One option would be to use the xslt task to do the heavy lifting. For example, generate a second ant script and invoke it.

build.xml:

<project default="mkdirs">
  <target name="mkdirs">
    <xslt style="mkdir.xslt" in="dirs.xml" out="mkdir.build.xml"/>
    <ant antfile="mkdir.build.xml"/>
  </target>
</project>

Place mkdir.xslt in the same directory as build.xml:

<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:template match="text()"/>

  <xsl:template match="root">
    <project><xsl:text>&#10;</xsl:text>
      <xsl:apply-templates/>
    </project>
  </xsl:template>

  <xsl:template match="*">
    <mkdir>
      <xsl:attribute name="dir">
        <xsl:for-each select="ancestor::*">
          <xsl:if test="position() != 1">
            <xsl:value-of select="name()"/>
            <xsl:text>/</xsl:text>
          </xsl:if>
        </xsl:for-each>
        <xsl:value-of select="name()"/>
      </xsl:attribute>
    </mkdir><xsl:text>&#10;</xsl:text>

    <xsl:apply-templates/>
  </xsl:template>
</xsl:transform>

Example mkdir.build.xml output from the xslt task:

<?xml version="1.0" encoding="UTF-8"?><project>
<mkdir dir="folder1"/>
<mkdir dir="folder1/folder1-1"/>
<mkdir dir="folder2"/>
<mkdir dir="folder3"/>
<mkdir dir="folder3/folder3-1"/>
</project>

I'm not fluent in XSLT, so it might be possible to improve on the for-each loop.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号