如何将Saxon设置为Java中的Xslt处理器? [英] How to set Saxon as the Xslt processor in Java?
问题描述
这是一个简单的问题,但我无法找到答案。我有一个XSLT 2.0样式表,我正在尝试用Java处理。它依赖于Saxon的XSL元素。
我当前的类在使用简单的XSLT 1.0时运行良好,但是我使用使用Saxon构建的2.0 XSLT得到了无法识别的元素的错误。
我无法弄清楚如何告诉Java使用Saxon作为处理器。我在班上使用javax.xml.transform。这是我可以设置的属性吗?我该怎么做呢?
谢谢!
已编辑
我想出如何设置属性以使用Saxon,但现在我是收到此错误。
找不到提供商net.sf.saxon.TransformerFactoryImpl
如何在我的申请中加入Saxon?
有多种方法(按查找优先顺序):
直接实例化
明确实例化 Saxon工厂(向迈克尔上面的评论致敬):
TransformerFactory fact = new net.sf.saxon.TransformerFactoryImpl ()
这种方法意味着您的代码在编译时被锁定为使用Saxon。这可以被看作是一个优势(没有运行错误的处理器的风险)或缺点(没有机会在执行时配置不同的处理器 - 甚至萨克森企业版)。
对于Saxon-PE,替换 com.saxonica.config.ProfessionalTransformerFactory
。对于Saxon-EE,替换 com.saxonica.config.EnterpriseTransformerFactory
。
指定类名
构造时指定工厂类:
TransformerFactory fact = TransformerFactory.newInstance(
net.sf.saxon.TransformerFactoryImpl,null);
注意:从Java 6开始提供。 Java 5版本没有这种方法。
这种方法允许您在执行时选择处理器,同时仍然避免了类路径搜索的成本和风险。例如,您的应用程序可以提供一些配置机制,允许它通过在各种Saxon工厂类之间进行选择来运行不同的Saxon版本。
使用系统属性
在创建实例之前设置 javax.xml.transform.TransformerFactory
系统属性:
System.setProperty(javax.xml.transform.TransformerFactory,
net.sf.saxon.TransformerFactoryImpl);
或者在命令行上(为便于阅读而破损):
java -Djavax.xml.transform.TransformerFactory =
cnet.sf.saxon.TransformerFactoryImpl YourApp
此方法的缺点是系统属性会影响整个Java VM。设置此属性以选择Saxon可能意味着应用程序中的某些其他模块(您可能甚至不知道)开始使用Saxon而不是Xalan,如果它使用特定于Xalan的XSLT构造,则该模块可能会失败。使用属性文件 创建以下文件: 具有以下内容: 此方法与使用系统属性具有类似的结果。 在CLASSPATH上的任何JAR中创建以下文件: 具有以下内容: 这种方法的缺点是对类路径的一个小改动可能会导致应用程序使用不同的XSLT引擎运行,也许是应用程序从未测试过的引擎with。 如果以上都不做,那么平台默认将加载 请注意,'platform'在这里表示Java VM,而不是它运行的硬件或操作系统。对于所有当前已知的Java VM,平台缺省值是Xalan的版本(仅支持XSLT 1.0)。无法保证将来每个Java VM都会如此。 我认为这个答案是反对Java方式的一个论点做事。 This is a simple question, but one I cannot find the answer to. I have an XSLT 2.0 stylesheet that I'm trying to process in Java. It relies on XSL elements from Saxon. My current class works fine with simple XSLT 1.0, but I'm getting errors about unrecognized elements with my 2.0 XSLT built with Saxon. I cannot figure out how to tell Java to use Saxon as the processor. I'm using javax.xml.transform in my class. Is this a property I can set? What do I set it to?
Thanks! Edited
I figured out how to set the property to use Saxon, but now I'm getting this error. How do I include Saxon in my application? There are multiple ways to do this (in order of lookup precedence): Explicitly instantiate the Saxon factory (with a nod to Michael's comment above): This approach means that your code is locked into using Saxon at compile time. This can be seen as an advantage (no risk of it running with the wrong processor) or a disadvantage (no opportunity to configure a different processor at execution time - not even Saxon Enterprise Edition). For Saxon-PE, substitute Specify the factory class when constructing it: Note: available as of Java 6. The Java 5 version does not have this method. This approach allows you to choose the processor at execution time, while still avoiding the costs and risks of a classpath search. For example, your application could provide some configuration mechanism to allow it to run with different Saxon editions by choosing between the various Saxon factory classes. Set the Or on the command line (line broken for readability): This approach has the disadvantage that system properties affect the whole Java VM. Setting this property to select Saxon could mean that some other module in the application, which you might not even know about, starts using Saxon instead of Xalan, and that module could fail as a result if it uses Xalan-specific XSLT constructs. Create the following file: With the following contents: This approach has similar consequences to using the system property. Create the following file in any JAR on the CLASSPATH: With the following contents: This approach has the disadvantage that a small change to the classpath could cause the application to run with a different XSLT engine, perhaps one that the application has never been tested with. If none of the above are done, then the platform default Note that 'platform' here means the Java VM, not the hardware or operating system that it is running on. For all current known Java VMs, the platform default is a version of Xalan (which only supports XSLT 1.0). There is no guarantee that this will always be true of every Java VM in the future. I'd consider this answer an argument against the Java way of doing things. 这篇关于如何将Saxon设置为Java中的Xslt处理器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
JRE / lib / jaxp.properties
javax.xml.transform.TransformerFactory = net.sf.saxon.TransformerFactoryImpl
服务加载器
META-INF / services / javax.xml.transform.TransformerFactory
net.sf.saxon.TransformerFactoryImpl
平台默认
TransformerFactory
实例。可以在找到对此可插拔层的友好描述。这里。
Provider net.sf.saxon.TransformerFactoryImpl not found
Direct Instantiation
TransformerFactory fact = new net.sf.saxon.TransformerFactoryImpl()
com.saxonica.config.ProfessionalTransformerFactory
. For Saxon-EE, substitute com.saxonica.config.EnterpriseTransformerFactory
.Specify Class Name
TransformerFactory fact = TransformerFactory.newInstance(
"net.sf.saxon.TransformerFactoryImpl", null);
Use System Property
javax.xml.transform.TransformerFactory
system property before creating an instance:System.setProperty("javax.xml.transform.TransformerFactory",
"net.sf.saxon.TransformerFactoryImpl");
java -Djavax.xml.transform.TransformerFactory=
cnet.sf.saxon.TransformerFactoryImpl YourApp
Use Properties File
JRE/lib/jaxp.properties
javax.xml.transform.TransformerFactory=net.sf.saxon.TransformerFactoryImpl
Service Loader
META-INF/services/javax.xml.transform.TransformerFactory
net.sf.saxon.TransformerFactoryImpl
Platform Default
TransformerFactory
instance will be loaded. A friendly description of this plugability layer can be found here.