Apache Ant/XSLT

Apache Ant has a task called (or its synonym ) that performs an XML transform on a file or group of files.

Here is an example XML transformation target:

In the ant target there are three files you must specify:
 * in The name of the source XML input file
 * out The name of the XML output file
 * style The name of the XSLT file

To test this you can create a "dummy" input file:

Hello World XSLT Transform
To get started, here is a small "hello world" transform file. The transform looks for the root data element of the input file but does not actually process any of the input file data elements:

You can now execute this from a command line. The following is an example run from a Microsoft Windows command shell:

C:\XMLClass\XSLT\Lab1>ant Buildfile: build.xml MyXSLT: [xslt] Processing C:\XMLClass\XSLT\Lab1\MyInput.xml to C:\XMLClass\XSLT\Lab 1\MyOutput.xml [xslt] Loading stylesheet C:\XMLClass\XSLT\Lab1\MyTransform.xslt BUILD SUCCESSFUL Total time: 1 second

The output will appear in a file called MyOutput.xml

Transforming Files containing external References
Sometimes you may need to transform XML files containing external references, like URLs in DTDs or Schema definitions.

Quite often, parsing or validating against such external files can not be totally disabled. Saxon for example will want to read DTDs even if parsing them is disabled (parameter "-dtd:off" or equivalent).

In such cases it may also occur, that the development workstation is connected to a company intranet that is protected by a firewall from the internet, and needs some sort of proxy or socks configuration.

In these cases, the only solution to successfully execute the transformation is by adding this connection configuration to the ant script.

Example (taken from a bigger build.xml file):

Passing Parameters from Ant into an XSLT script
You can also pass parameters from an ant build file into an XSLT. This is handy if you need to run the same transform with small variations. You can do this by simply adding the  tag the target:

The ant task now looks like the following:

Here is a sample transform that takes a single input parameter:

This will create the following output:

Note that there are three different lines. One came from the transform file, one came from the input XML file and one was passed directly in from the ant file.

Checking dependencies
By default, the XSLT task will check the file time stamps to see if the output file is newer than the input file. If the outputs are newer the task should not have to re-run the transform. But sometimes a transform will import other transforms files and Ant does not check the timestamps of imported files. (Perhaps they will add that as an option in the future.) But all is not lost. We can achieve the same results by using the tag. Here is an example:

In the above example the source transform (Content2HTML.xsl) imported the other four page fragment transforms located in the XSLTDir (HTMLHeader.xsl, PageHeader.xsl, LeftNav.xsl and PageFooter.xsl). It created the files in the BuildDir directory. If any of the inputs files change, the outputs will be regenerated.

This is a handy way to build a little ant-based web content management system. You just put the HTML content in a directory and the transform can wrap the HTML headers, navigation bars and footers around your content. The HTML for each page can just be a  section that is copied into the output using the  command.