XML.com: XML From the Inside Out
oreilly.comSafari Bookshelf.Conferences.

advertisement

Running Multiple XSLT Engines with Ant
by Anthony Coates | Pages: 1, 2

A Multiple XSLT Engine Example

This example uses a few new Ant elements. A taskdef is required to associate the task name mtxslt with the Java class which implements it. Actually, you can call mtxslt anything you want just by changing the name in the taskdef.

The property definitions are used to define values that can be retrieved by name throughout the build file, which is similar to defining a string variable in a programming language. Property definitions are used to define short names for qualified Java class names and for file paths, since both of these tend to be long and reduce the readability and maintainability of the build file if repeated.

In this example, different XSLT engines are used to apply the same stylesheet transform.xsl to the same input input.xml. The resulting HTML files can then be compared.

<project
  name="test"default="all">
  <taskdef
    name="mtxslt"classname="org.xmLP.ant.taskdefs.xslt.XSLTProcess"/>
  <property
    name="trax"value="org.xmLP.ant.taskdefs.optional.TraXLiaison"/>
  <property
    name="xalan2"value="org.xmLP.ant.taskdefs.optional.Xalan2Liaison"/>
  <property
    name="xalan2.classpath"value="D:\home\tony\XSLT\xalan-j_2_4_0\bin\xalan.jar"/>
  <property
    name="saxon6"value="org.xmLP.ant.taskdefs.optional.Saxon6Liaison"/>
  <property
    name="saxon6.classpath"value="D:\home\tony\XSLT\Saxon-6.5.2\saxon.jar"/>
  <property
    name="saxon7"value="org.xmLP.ant.taskdefs.optional.Saxon7Liaison"/>
  <property
    name="saxon7.classpath"value="D:\home\tony\XSLT\Saxon-7.1\saxon7.jar"/>
  <property
    name="oracle9"value="org.xmLP.ant.taskdefs.optional.Oracle9Liaison"/>
  <property
    name="oracle9.classpath"
	value="D:\home\tony\XSLT\xdk_java_9_2_0_3_0\lib\xmlparserv2.jar"/>
  <target
    name="all"depends="trax1,trax2,trax3,trax4,xalan2,saxon6,saxon7,oracle9"/>
  <target name="trax1">
    <xslt
      processor="trax"in="input.xml"style="transform.xsl"out="trax1.html">
      <param
        name="target"expression="trax1"/>
    </xslt>
  </target>
  <target name="trax2">
    <mtxslt
      processor="trax"in="input.xml"style="transform.xsl"out="trax2.html">
      <param
        name="target"expression="trax2"/>
    </mtxslt>
  </target>
  <target name="trax3">
    <xslt
      processor="${trax}"in="input.xml"style="transform.xsl"out="trax3.html">
      <param
        name="target"expression="trax3"/>
    </xslt>
  </target>
  <target name="trax4">
    <mtxslt
      processor="${trax}"in="input.xml"style="transform.xsl"out="trax4.html">
      <param
        name="target"expression="trax4"/>
    </mtxslt>
  </target>
  <target name="xalan2">
    <mtxslt
      processor="${xalan2}"in="input.xml"style="transform.xsl"out="xalan2.html"
	  classpath="${xalan2.classpath}">
      <param
        name="target"expression="xalan2"/>
    </mtxslt>
  </target>
  <target name="saxon6">
    <mtxslt
      processor="${saxon6}"in="input.xml"style="transform.xsl"out="saxon6.html"
	  classpath="${saxon6.classpath}">
      <param
        name="target"expression="saxon6"/>
    </mtxslt>
  </target>
  <target name="saxon7">
    <mtxslt
      processor="${saxon7}"in="input.xml"style="transform.xsl"out="saxon7.html"
	  classpath="${saxon7.classpath}">
      <param
        name="target"expression="saxon7"/>
    </mtxslt>
  </target>
  <target name="oracle9">
    <mtxslt
      processor="${oracle9}"in="input.xml"style="transform.xsl"out="oracle9.html"
	  classpath="${oracle9.classpath}">
      <param
        name="target"expression="oracle9"/>
    </mtxslt>
  </target>
  <target name="clean">
    <delete>
      <fileset
        dir="."includes="*.html"/>
    </delete>
  </target>
</project>
  • The target trax1 simply uses the standard xslt task to transform the input file, as in the earlier examples.

  • The target trax2 is identical to trax1, except that it uses mtxslt instead of xslt. This demonstrates that mtxslt implements the standard behavior of the xslt task.

  • The target trax3 is similar to trax1, except that the value of the processor attribute is the value of the property trax (i.e., org.xmLP.ant.taskdefs.optional.TraXLiaison). This is a feature of the xslt task that only becomes apparent when you look at the Ant source code. The processor can optionally be a qualified class name for an Ant XSLT liaison class. This is the mechanism that mtxslt exploits to support multiple XSLT engines.

    This particular XSLT liaison class connects with the default JAXP/TraX XSLT engine, so the result is identical to that produced by the target trax1.

  • The target trax4 is identical to trax3, except that it uses mtxslt instead of xslt.

  • The targets xalan2, saxon6, saxon7, and oracle9 use mtxslt to call Xalan 2, Saxon 6, Saxon 7, and Oracle XDK 9 respectively. Once the appropriate properties have been defined, mtxslt attributes look nearly identical to standard xslt attributes. Note, however, the addition of a classpath attribute, which is required so that Ant loads the correct JAR archive for each XSLT engine.

The target parameter that is passed to the stylesheet allows the Ant target name to be embedded in each HTML product file to make identification of the files easier. It serves no other purpose.

That's all there is to it. You now not only know how to use Ant to control XSLT, you also know how to use mtxslt to control which XSLT engines are used within an Ant build. (All of the example files from this article can be downloaded as a ZIP archive.)

Conclusion

Ant is a powerful cross-platform tool for controlling build processes and is ideal for controlling multifile builds involving XSLT stylesheets. Using mtxslt, you can go further and invoke multiple Java XSLT engines during a single build, which is ideal for portability testing.

It may be worth mentioning that this article was written using an extended version of DocBook 4.2 and then converted to XHTML using an XSLT stylesheet -- a process controlled by an Ant build file. As well as building the article, Ant controlled the extraction of the Ant build file code out of the DocBook source and into the example build files, as well as the regression testing of the examples. It really works.

Resources

Example files
Articles
Ant
XSLT Engines
JAXP/TraX


1 to 3 of 3
  1. xslt in ant
    2004-01-31 13:03:53 shreya Ramaswamy
  2. Re: Article.xml (DocBook 4.2), and XSLT...?
    2002-12-13 11:04:57 Anthony Coates
  3. Article.xml (DocBook 4.2), and XSLT...?
    2002-12-12 17:45:26 William Reilly
1 to 3 of 3