Abbey Workshop

Ant: Performing XSLT Tranformations

Apache Ant Java build tool is normally used to build Java programs. However, it also includes a task for performing XSLT Transformations. The transformation task can be applied to a single file, a directory, or an entire directory tree. Being able to transform many files at the same time is a very useful and powerful feature.

Transforming a Single File

To setup a tranformation of a single file, you must specify an input XML file, desired output file, and the stylesheet to be used to perform the transformation. This information is specified in an Ant target using the Xslt Ant task. The following build file will be use in this example and all the examples that follow.

Listing for: build.xml

   1 <project name="TranformXml" default="TransformFile">
   2   <target name="TransformFile">
   3     <!-- Transform one file into an HTML file -->
   4     <xslt in="student_directory.xml" out="student.html"
   5       style="student_html.xsl"
   6     />
   7   </target>
   8   <target name="TransformAll">
   9     <!-- Transform all the files in the current directory -->
  10     <xslt basedir="." destdir="."
  11       includes="**/*.ext.xml"
  12       style="student_html.xsl"
  13     />
  14   </target>
  15 </project>

The XML code for the TransformFile target is pretty straightforward. As stated before, specify the input, output, and stylesheet. Running the Ant script transforms the XML file into an HTML document.

Transforming Multiple Files

Ant can also be used to transform a number of XML files with a single stylesheet. Lines 8-14 shows a target that will transform all the files in the current directory tree. Notice that in the includes attribute *.ext.xml is used to specify the files for transformation instead of *.xml. This is a trick to avoid tranforming control files like build.xml. Since a directory tree may contain any number of XML course or control files, you want to be specific about which files are transformed. Therefore, adding an extension before the .xml extension makes the transformation specific to certain class of XML files.

Of course, if the files are in a separate directory you can use the basedir and destdir attributes to keep the XML files and transformation output in different directories. In this example, everything is done in the same directory as the build.xml file. Using the Ant task in this way will produce files with a .ext.html extention. The HTML files have the same prepended file name as the name before .ext.xml files.

Listing for: student_directory.xml

   1 <student_list>
   2   <student>
   3     <name>George Washington</name>
   4     <major>Politics</major>
   5     <phone>312-123-4567</phone>
   6     <email></email>
   7   </student>
   8   <student>
   9     <name>Janet Jones</name>
  10     <major>Undeclared</major>
  11     <phone>311-122-2233</phone>
  12     <email></email>
  13   </student>
  14   <student>
  15     <name>Joe Taylor</name>
  16     <major>Engineering</major>
  17     <phone>211-111-2333</phone>
  18     <email></email>
  19   </student>
  20 </student_list>

Listing for: student_html.xsl

   1 <xsl:stylesheet xmlns:xsl="" version="1.0">
   2 <xsl:output method="html"/>
   4 <xsl:template match="/">
   5 <html>
   6   <head>
   7     <title>Student Directory</title>
   8   </head>
   9   <body>
  10   <xsl:apply-templates />
  11   </body>
  12 </html>
  13 </xsl:template>
  15 <xsl:template match="student_list">
  16   <h3>Student Directory for</h3>
  17   <xsl:apply-templates />
  18 </xsl:template>
  20 <xsl:template match="name">
  21   <p>Name: <xsl:apply-templates /></p>
  22 </xsl:template>
  24 <xsl:template match="major">
  25   <p>Major: <xsl:apply-templates /></p>
  26 </xsl:template>
  28 <xsl:template match="phone">
  29   <p>Phone: <xsl:apply-templates /></p>
  30 </xsl:template>
  32 <xsl:template match="email">
  33   <p>Email: <xsl:apply-templates /></p>
  34 </xsl:template>
  36 </xsl:stylesheet>