Abbey Workshop | ||||||||||
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.
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.
Download source for: build.xml
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.
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.
Click here to get the student_directory.xml and the student_html.xsl files.
Copyright © Abbey Workshop 2006