Apache Ant

1. Ant and XSLT

1.

Ant and XSLT

Robert Koberg

I use Apache's Ant: Apache and the Xslt task: docs

For example:

<xslt basedir="doc" destdir="build/doc"
      extension=".html" style="my.xsl">
  <factory name="org.apache.xalan.processor.TransformerFactoryImpl">
    <attribute name="http://xml.apache.org/xalan/features/optimize"
value="true"/>
  </factory>
  <param name="someParam" expression="some value"/>
</xslt>

This allows you to choose a directory (or directories) where your source lives and apply a transformation to output them to some other directory.

DaveP adds, for Saxon

This is a basic transformation, works with ant 1.62 Change antHome, and the locations of the saxon jars to suite your installation.

Note. Some java installations have an endorsed directory. See sun for details on j2se 1.4.2. sun defines 1.5 as behaving the same. The impact of this is to pre-empt the jars in your classpath. Unless you use this facility, you can delete any jars in that directory and be explicit with the classpath for a particular use. That way you are in charge of the process, without Sun overriding your choices. If you don't, this process may not work, picking up jars from the endorsed directory. Find this as follows:


<java-home>\lib\endorsed          [Microsoft Windows]
<java-home>/lib/endorsed          [Solaris or Linux]

Here <java-home> refers to the directory where the runtime software
is installed (which is the top-level directory of the Java 2 Runtime
Environment or the jre directory in the Java 2 SDK).


To continue: call with


 $>ant    

to transform using xslt 1.0, and


$>ant transform2

to use saxon 8 (XSLT 2)

file build.xml

<?xml version="1.0" encoding="utf-8" ?>
 <project name="Saxon Transform" default="transform1" basedir="." >
 
 <property name="antHome" value="/apps/ant"/>
 
 <property name="saxon6engine" 
    value="com.icl.saxon.TransformerFactoryImpl"/>
 <property name="saxon8engine" 
   value="org.apache.xerces.parsers.SAXParser"/>
 <property name="xslfoEngine" value="com.renderx.xep.XSLDriver"/>
 <property name="saxon6Jar" value="/myjava/saxon653.jar"/>
 <property name="saxon8Jar" value="/myjava/saxon8.jar"/>
 <property name="xepJar" value="xep372_client.jar"/>
 
 <property name="processor" value="trax"/> <!--  -->
 
 
 <!-- File location properties -->
 <property name="srcDir" value="${basedir}"/>
 <property name="dstDir" value="/temp"/> <!-- output directory -->
 <property name="ss" value="/sgml/xsl"/><!-- Stylesheet directory -->
 
 
 
 <!-- Transform, XSLT 1.0 -->
 
 <!-- extension - of output file
Not needed if you specify both in and out fully?
 -->
 <!-- in    - file to process -->
 <!-- style - stylesheet -->
 <!-- out   - output file required-->
 
 <target name="transform1"
 	description="Transform using XSLT 1.0">
 <xslt basedir="${srcdir}" 
       destdir="${dstDir}"
       extension="xml"
       style="${ss}/identity.xsl"
       classpath="${saxon6jar};${antHome}/lib/ant-trax.jar"
       processor="${processor}"
       in="inputXML.xml"
       out="outputXML.xml"/>
 </target>
 
 <target name="transform2"
 	description="Transform using XSLT 2.0">
 <xslt basedir="${srcdir}" 
       destdir="${dstDir}"
       extension="xml"
       style="${ss}/identity2.xsl"
       classpath="${saxon8jar};${antHome}/lib/ant-trax.jar"
       processor="${processor}"
       in="inputXML.xml"
       out="outputXML.xml"/>
 </target>
 
 
 
 </project>

Wendell adds: Also relevant to this is an excellent article by Tony Coates: http://www.xml.com/pub/a/2002/12/11/ant-xml.html "Running Multiple XSLT Engines with Ant" "With mtxslt, it is possible to ignore the value of the Java javax.xml.transform.TransformerFactory property and simply load a particular XSLT engine directly."

It predates the very latest Saxon, but since it is designed to allow folding new processors in, it ought to work. (It does require you to install and run an 'mtxslt' Ant task, which is on Sourceforge.)