简体   繁体   中英

How to run saxon xslt transformation in java

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?

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.

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.

Runtime rt = Runtime.getRuntime();
Process pr = rt.exec("java -jar saxon9he.jar -o:outputfile.xml data.xml transform.xslt");

Refer javadochere .

Using what @Martin Honnen said I got:

import net.sf.saxon.Transform;

class XSLTransform{
    public static void main(String args[]) throws Exception {

        String[] arglist = {"-o:outputfile.xml","data.xml", "transform.xslt"};

        Transform.main(arglist);

    }
}

Seems to be working great. Thanks for the help

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM