Sign In/My Account | View Cart  
advertisement


Listen Print Discuss

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>
O'Reilly Network Safari Bookshelf
Safari is an electronic reference library that lets you search hundreds of best-selling technical books from O'Reilly and the best of the rest.
Search this book on Safari:
XSLT
 

Code Fragments only
Try Safari FREE for 14 days with no obligation.
  • 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

Comment on this articleGot a question or a comment on this article? Share it in our forum.
(* You must be a
member of XML.com to use this feature.)
Comment on this Article


Titles Only Titles Only Newest First
  • xslt in ant
    2004-01-31 13:03:53 shreya Ramaswamy [Reply]

    Hi,


    How can I use xslt in a target build file, which is compiled and run by Ant? I am trying to parse an xml file (input.xml) using an xsl file(dbconv.xsl) to produce an output xml file (output.xml). I used <xslt> in the following way:


    <property name="saxon6" value="com.icl.saxon.TransformerFactoryImpl" />
    <property name="saxon6.classpath" value="C:\DOWNLOAD\Saxon\saxon.jar" />


    <xslt out="${build.dir}/output.xml" in="${build.bin}/input.xml" style="dbconv.xsl" processor="${saxon6}" >


    However, I am getting the following error:


    BUILD FAILED
    file:C:/Project/project/build.xml:121: java.lang.ClassNotFoundException: com.icl.saxon.TransformerFactoryImpl


    I tried all possible combinations of processors and classpaths. Even <factory> does not work. Any help would be greatly appreciated.Thanks!


    Shreya

    • xslt in ant
      2004-02-07 12:30:55 shreya Ramaswamy [Reply]

      Hello,


      Thank you for the answer. I tried using 'mtxslt' and followed the instructions in the article. The error is still the same:


      BUILD FAILED


      file:C:/Project/project/build.xml:142: java.lang.ClassNotFoundException: org.xmLP.ant.taskdefs.optional.Saxon6Liaison


      I have included the saxon.jar in my classpath. What could be wrong?


      Shreya


      • xslt in ant
        2004-02-08 10:08:10 Anthony Coates [Reply]

        Now you need to add the 'mtxslt' JAR file to your Ant classpath. Cheers, Tony.

        • xslt in ant
          2004-02-16 12:07:56 shreya Ramaswamy [Reply]

          Anthony Coates,


          Thanks.I did this and it works. However, I have a strange problem.When I remove mtxslt-1.5.jar from my classpath and specify the path explicitly in my code, it does not work for some reason. I am getting the same error:


          BUILD FAILED


          file:C:/Project/project/build.xml:142: java.lang.ClassNotFoundException: org.xmLP.ant.taskdefs.optional.Saxon6Liaison



          I have my jar files(saxon,mtxslt,xalan) in C:\test\files\ext directory; input.xml and the xsl file in C:\test\files directory.


          Here's my code:


          <property name="files.dir" value="files"/>
          <property name="files.ext" value="${files.dir}/ext"/>
          <taskdef name="mtxslt"
          classname="org.xmLP.ant.taskdefs.xslt.XSLTProcess">
          <classpath>


          <fileset dir="${files.dir}/ext">
          <include name="mtxslt-1.5.jar"/>
          </fileset>


          </classpath>
          </taskdef>


          <property name="saxon6"
          value="org.xmLP.ant.taskdefs.optional.Saxon6Liaison"/>
          <property name="saxon6.classpath" value="${files.ext}/saxon.jar"/>


          <target name="saxon6">
          <mtxslt processor="${saxon6}" in="${files.dir}/input.xml"
          style="${files.dir}/dbconv.xsl" out="output.xml"
          classpath="${saxon6.classpath}">
          <param name="target" expression="saxon"/>
          </mtxslt>

          </target>


          Any help would be appreciated.Thanks for your time.


          Shreya


    • xslt in ant
      2004-02-01 07:54:15 Anthony Coates [Reply]

      Two things need to change. One, you need to use the 'mtxslt' task, not the standard Ant 'xslt' task. Also, your 'mtxslt' take would need to contain


      classpath="${saxon6.classpath}"


      Just follow the instructions in the article.
      Cheers, Tony.

      • xslt in ant
        2004-02-01 08:02:22 Anthony Coates [Reply]

        Oops, I meant your mtxslt 'task', not 'take'.

  • Re: Article.xml (DocBook 4.2), and XSLT...?
    2002-12-13 11:04:57 Anthony Coates [Reply]

    I will post the article source & my (minimal) DocBook to XHTML transform script at http://mtxslt.sf.net/ over the Christmas 2002/New Year 2003 break. Remind me if I don't.


    What I won't be posting is the pre-DocBook version and transformation scripts, as I used an early development version of xmLP 1.1, my XML Literate Programming tool (written in XSLT, http://www.xmLP.org/), and I don't want to release that as yet.


    Cheers, Tony.

  • Article.xml (DocBook 4.2), and XSLT...?
    2002-12-12 17:45:26 William Reilly [Reply]

    Thank you for the article and accompanying ZIP, including your "article.html".
    Might you consider making the "article.xml" and the XSLT (and Ant task) available in the ZIP as well? Appreciated, if you can arrange it.
    Best regards,
    William Reilly
    wreilly@digitas.com
    Boston, Mass.