Using Ant With XSLT 2.0 and Saxon

You will first have to download the Saxon libraries. You can find them at the SourceForge Saxon Web Page I put the files in my C:/Apps/saxon8 folder

I then add the following to a properties file:

– content of my.properties file –

Saxon8HomeDir=C:/Apps/saxon8
saxon8jar=${Saxon8HomeDir}/saxon8.jar
# used to make sure Saxon gets the right XSLT 2.0 processor
processor=trax

– end of my.properties file –

Here is an excerpt from my build file

– contents of build.xml –

<!-- load the local properties file -->
<property file="my.properties"/>

<!-- a sample task that demonstrates the use of Saxon 8 -->
<target name="XSL using Saxon"
	description="Demonstration of XSL using Saxon">
   <xslt
      in="MyInput.xml"
      out="MyOutput.htm"
      style="MyTransform.xsl"
      classpath="${saxon8jar};${antHome}/lib/ant-trax.jar"
      processor="${processor}" >
   </xslt>
</target>

The following is an handy transform for printing the XSLT 2.0 and system related variables
MyTransform.xsl –

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
   <xsl:output method="html"/>
      <xsl:template match="/">
         <html>
            <head>
               <title>Print XSLT Related Environment Variables</title>
            </head>
            <body>
                <ul>
                   <li><b>Version: </b><xsl:value-of select="system-property('xsl:version')"/>
                   </li>
                   <li><b>Vendor: </b><xsl:value-of select="system-property('xsl:vendor')"/>
                   </li>
                   <li><b>Vendor URL: </b><xsl:value-of select="system-property('xsl:vendor-url')"/>
                   </li>
                   <li><b>Java Version: </b><xsl:value-of select="system-property('java.version')"/>
                   </li>
                   <li><b>OS Name: </b><xsl:value-of select="system-property('os.name')"/>
                   </li>
                   <li><b>File Seperator: </b><xsl:value-of select="system-property('file.separator')"/>
                   </li>
                </ul>
            </body>
        </html>
    </xsl:template>
</xsl:stylesheet>

This should display something like the following:

– output of MyOutput.htm –

  • Version: 2.0
  • Vendor: SAXON 8.4 from Saxonica
  • Vendor URL: http://www.saxonica.com/
  • Java Version: 1.5.0_02
  • OS Name: Windows XP
  • File Seperator: \

To use this just create an empty XML input file with a valid root:
MyInput.xml –

<?xml version="1.0" encoding="UTF-8"?>
<root>
   <empty></empty>
</root>

Or once you have downloaded saxon8.jar, simply call Ant with -lib path/to/saxon8.jar on the command line, and use <xslt> as usual. Since Saxon implements the TraX API, and is now first on Ant's classpath, <xslt> will automatically pick it up. Wrap the appropriate -lib option in a custom startup script, or use an ANT_OPTS EnvironmentVariable to avoid having to type it all the time. --DD

  • No labels