Apache Ant/Running Saxon

From Wikibooks, open books for an open world
Jump to navigation Jump to search

Motivation[edit | edit source]

You want to have an Apache Ant task that runs the Saxon XSLT transform.

Method[edit | edit source]

Download the Saxon jar file. Put the saxon.jar file in a lib folder. Run the following test.

Source Code[edit | edit source]

Build File[edit | edit source]

The following is how Saxon is invoked from Apache Ant.

<target name="test-saxon">
   <xslt classpath="lib\saxon8.jar"
      in="in.xml" 
      out="out.html" 
      style="check-version.xsl">
      <factory name="net.sf.saxon.TransformerFactoryImpl"/>
   </xslt>
</target>

Note that if you are running in Eclipse you will have to go to the "Preferences" menu and add the saxon9.jar file to Ant/Runtime/Ant Home Entries. Just click the "Add JARs" and add the saxon9jar file the end of this list.

XSLT Version Check[edit | edit source]

check-version.xsl:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output omit-xml-declaration="yes" indent="yes"/>
    <xsl:template match="/">
        <results>
            <Version><xsl:value-of select="system-property('xsl:version')" /></Version>
            <Vendor><xsl:value-of select="system-property('xsl:vendor')" /></Vendor>
            <Vendor-URL><xsl:value-of select="system-property('xsl:vendor-url')" /></Vendor-URL>
        </results>
    </xsl:template>
</xsl:stylesheet>

Or if you are generating a web page:

<xsl:stylesheet version="1.0"
   xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
   <xsl:template match="/">
      <html>
        <head>
         <title>XSL Version</title>
        </head>
        <body>
           <p>Version:
           <xsl:value-of select="system-property('xsl:version')" />
           <br />
           Vendor:
           <xsl:value-of select="system-property('xsl:vendor')" />
           <br />
           Vendor URL:
           <xsl:value-of select="system-property('xsl:vendor-url')" />
           </p>
        </body>
     </html>
  </xsl:template>
</xsl:stylesheet>

Results for XALAN[edit | edit source]

Results for Apache XALAN

  1.0
  Vendor: Apache Software Foundation (Xalan XSLT)
  Vendor URL: http://xml.apache.org/xalan-j

Results for Saxon[edit | edit source]

Version: 2.0 Vendor: SAXON 9.1.0.7 from Saxonica Vendor URL: http://www.saxonica.com/