0

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 create a directory, but i'm not sure how to have ANT parse the XML.

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.