How to run saxon xslt transformation in java

十年热恋 提交于 2021-02-07 06:34:27

问题


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?


回答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.




回答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.




回答3:


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

Refer javadoc here.




回答4:


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



来源:https://stackoverflow.com/questions/40181386/how-to-run-saxon-xslt-transformation-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!