java 如何在java中运行撒克逊xslt转换
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40181386/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to run saxon xslt transformation in java
提问by Robert Petty
I can easily run the following in command line to transform an xml file:
我可以轻松地在命令行中运行以下命令来转换 xml 文件:
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?
我想从一个 java 文件中得到完全相同的结果,这样我就可以在我正在制作的程序的一部分中使用它。我已将 saxon9he.jar 放在构建路径中,但如何在命令行之外调用相同的命令?
采纳答案by Michael Kay
The documentation is here: http://www.saxonica.com/documentation/index.html#!using-xsl/embedding
文档在这里: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.
Saxon 提供了两种用于从 Java 应用程序运行 XSLT 转换的 API:JAXP API 和 s9api API。JAXP 是几乎所有 Java XSLT 处理器都提供的标准接口,因此,如果您希望应用程序具有可移植性,就可以使用这个接口;它的缺点是 (a) 它非常面向 XSLT 1.0,这使得很难利用 XSLT 2.0 和 XSLT 3.0 中的新功能,以及 (b) 它没有与 API 集成得特别好,用于相关任务,例如模式处理和 XPath 评估。
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.
s9api API 与 Saxon 在各种任务(包括 XSLT、XQuery、XPath 和 XSD 验证)中的功能更加匹配,但不可移植。
It's your choice.
这是你的选择。
回答by Morten Jorgensen
You're best off working with the standard Java APIs for XML and XSLT processing: java.xml.transform
您最好使用用于 XML 和 XSLT 处理的标准 Java API: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.
您需要访问的第一个类是 javax.xml.transform.TransformerFactory,您可以使用它来创建一个 Transformer 对象,然后您可以使用它来运行 XSLT 转换。或者,您可以使用 TransformerFactory 创建一个 Templates 对象(这将导致 Saxon 预处理/编译/等您的 XSLT 样式表),然后重复使用 Templates 对象为您的 XSLT 转换创建 Transformer 对象。
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:
为了确保 javax.xml.transform.TransformerFactory 类映射到 Saxon 实现,您可以使用命令行选项修改引导类路径,或者使用代码来执行相同的操作:
System.setProperty("javax.xml.transform.TransformerFactory", "net.sf.saxon.TransformerFactoryImpl");
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.
完成此操作后,对 TransformerFactory.newInstance() 的任何调用都将神奇地创建 Saxon TransformerFactory 实现。使用此方法会好得多,因为您将获得标准 Java API 的好处,并且以后可以自由地转移到其他 XSLT 处理器。您可能想考虑使用 XSLTC,它是 Apache Xalan 的一部分,因为它对于某些类型的 XSLT 样式表更快。
回答by Sundararaj Govindasamy
回答by Robert Petty
Using what @Martin Honnen said I got:
使用@Martin Honnen 所说的,我得到了:
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
似乎工作得很好。谢谢您的帮助