How to run saxon xslt transformation in java

Robert Petty picture Robert Petty · Oct 21, 2016 · Viewed 12.8k times · Source

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?

Answer

Michael Kay picture Michael Kay · Oct 22, 2016

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.