How to run saxon xslt transformation in java

15,336

Solution 1

The documentation is here: http://www.saxonica.com/documentation/index.html#!using-xsl/embedding

Saxon offers two APIs for running XSLT transformations from a Java application: the JAXP API, and the s9api API. JAXP is a standard interface offered by almost all Java XSLT processors, so you want to use this one if you want your application to be portable; its disadvantage is that (a) it's very oriented to XSLT 1.0 and that makes it hard to take advantage of new capabilities in XSLT 2.0 and XSLT 3.0, and (b) it doesn't integrate especially well with APIs for related tasks such as schema processing and XPath evaluation.

The s9api API is much more closely matched to Saxon's capabilities across a variety of tasks including XSLT, XQuery, XPath, and XSD validation, but isn't portable.

It's your choice.

Solution 2

You're best off working with the standard Java APIs for XML and XSLT processing: java.xml.transform

The first class you need to access is javax.xml.transform.TransformerFactory, which you use to create a Transformer object, which you then use to run your XSLT transform. Alternatively, you can use the TransformerFactory to create a Templates object (which will cause Saxon to pre-process/compile/etc your XSLT stylesheet), and then use the Templates object repeatedly to create Transformer objects for your XSLT transforms.

In order to make sure that the javax.xml.transform.TransformerFactory class maps to the Saxon implementation, you can either fiddle around with the boot classpath using command-line options, or use code to do the same:

System.setProperty("javax.xml.transform.TransformerFactory", "net.sf.saxon.TransformerFactoryImpl");

Once this is done, any calls to TransformerFactory.newInstance() will magically create Saxon TransformerFactory implementations. You're much better off using this method as you'll get the benefits of the standard Java APIs, and you have the freedom to move to other XSLT processors later. You might want to look into using XSLTC, which is part of Apache Xalan, as it is faster for certain types of XSLT stylesheets.

Share:
15,336
Robert Petty
Author by

Robert Petty

Updated on June 04, 2022

Comments

  • Robert Petty
    Robert Petty about 2 years

    I can easily run the following in command line to transform an xml file:

    java -jar saxon9he.jar -o:outputfile.xml data.xml transform.xslt
    

    I would like to do the exact same results from within a java file so I can use it in part of a program I'm making. I have put the saxon9he.jar in the build path but how can I call that same command outside the commandline?

  • Robert Petty
    Robert Petty over 7 years
    That doesn't seem to be working. When I run it nothing happens. no error or transformation
  • Sundararaj Govindasamy
    Sundararaj Govindasamy over 7 years
  • Michael Kay
    Michael Kay over 7 years
    This really isn't a good way to go. Sooner or later you'll want to move beyond doing a single-shot transformation where the stylesheet and source document are in filestore and the result document ends up in filestore. For example, you might want to run the same stylesheet against multiple source documents, without recompiling it for each application.; or you might want to do a second transformation on the result of the first. Saxon's Java APIs are much more flexible than the command line options available.