Abbey Workshop

Ant: Using Saxon and XSLT 2.0 with Ant

Well I have been wondering for some time how to make Saxon your Ant XSLT processor instead of Xalan. Recently, troubleshooting an issue allowed me some deeper exploration. It turns out that making Saxon your XSLT processor is pretty easy. Basically all you need is a test XSLT 2.0 file to verify that your are using Saxon and a minor tweak to the xslt task in Ant.

First, take a look at the sample xslt 2.0 code.

Listing for: xslt2-test.xsl

   1 <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
   2 <xsl:output method="xml"/>
   3 
   4 <xsl:template match="/">
   5     <test>
   6     <time><xsl:value-of select="current-date()"/></time>
   7   </test>
   8 </xsl:template>
   9 
  10 </xsl:stylesheet>

This small snippet of XSLT 2.0 code calls the XPath 2.0 currentdate() function. If an XSLT 2.0 processor is called, you get an output XML document with the current date in it. If an XSLT 1.0 processor like Xalan is called, the processor chokes and you get an error message like this.

Buildfile: build.xml

xslt2:
     [xslt] Processing /home/howto/xslt/ant-saxon/test.xml to
	 /home/howto/xslt/ant-saxon/xslt2-test.xml
     [xslt] Loading stylesheet
	 /home/howto/xslt/ant-saxon/xslt2-test.xsl
     [xslt] : Error! Error checking type of the expression
	 'funcall(current-date, [])'.
     [xslt] : Fatal Error! Could not compile stylesheet
     [xslt] Failed to process /home/howto/xslt/ant-saxon/test.xml

BUILD FAILED
/home/howto/xslt/ant-saxon/build.xml:5: Fatal error during
transformation

Total time: 1 second
		

Since there is no currentdate() XPath function in an XSLT 1.0 processor, it is a pretty easy way to check.

Now for the Ant code. Here is the modified build file to make Ant use Saxon.

Listing for: build.xml

   1 <project name="TranformXml" default="TransformAll">
   2   <target name="xslt2">
   3     <!-- Transform one file into an HTML file -->
   4     <xslt in="test.xml" out="xslt2-test.xml"
   5       style="xslt2-test.xsl" force="true">
   6       <classpath location="/home/ap/saxon/saxon8.jar" />
   7     </xslt>
   8   </target>
   9   
  10   <target name="TransformAll" depends="xslt2" />
  11 </project>

You mean you only have to put the path to the saxon8.jar file in the classpath? That's it?

That's it boys and girls, that is all it takes. That small change should allow to easily plug-in Saxon into your Ant XSLT build scripts. Have fun.

Content Last Updated 2007