将 Xalan 与 Saxon 一起使用 [英] Using Xalan alongside Saxon
问题描述
我在我的应用程序中使用 Xalan,但需要使用 Saxon 和参考实现来生成测试输出以进行比较.我想在单元测试期间同时使用它们.但是,一旦我在项目 .pom 中添加了对 Saxon 的依赖项,该应用程序似乎在测试期间将 Saxon 用于所有 xslt 和 XPath 操作:
I use Xalan in my application, but need to use Saxon with a reference implementation to generate test output to compare with. I want to use them both during unit tests. However, as soon as I add an dependency on Saxon in the project .pom, the application seems to use Saxon for all xslt and XPath operations during tests:
<dependency>
<groupId>net.sf.saxon</groupId>
<artifactId>Saxon-HE</artifactId>
<version>9.4</version>
<scope>test</scope>
</dependency>
这会使主应用程序在生成输出时由于不同的 XPath 行为而失败.在测试范围之外运行主应用程序时,它可以工作.
This makes the main application fail when generating output due to a different XPath behaviour. When running the main application outside of test scope it works.
如何在测试期间使用 Xalan 运行主应用程序,但使用 Saxon 运行测试?
How can I run the main application using Xalan, but the tests using Saxon, during tests?
在运行 Xalan 和 Saxon 部分之前,我尝试设置以下属性:
I have tried setting the following property before running the Xalan and Saxon parts:
System.setProperty("javax.xml.transform.TransformerFactory", "org.apache.xalan.processor.TransformerFactoryImpl ");
System.setProperty("javax.xml.transform.TransformerFactory", "net.sf.saxon.TransformerFactoryImpl");
我还尝试将 Xalan 和 Saxon 部分放在不同的项目中,我也尝试在第三个项目中使用它们,结果相同.
I have also tried to put the Xalan and Saxon parts in different projects, and I have also tried to use them both from a third project, with the same result.
推荐答案
避免依赖 JAXP 工厂机制来选择您的转换引擎.而是明确加载您想要的引擎:它更可靠,速度也更快.对于撒克逊人,替换调用
Avoid relying on the JAXP factory mechanism for selecting your transformation engine. Instead load the engine you want explicitly: it's much more reliable and much faster. For Saxon, replace the call on
TransformerFactory.newInstance()
与
new net.sf.saxon.TransformerFactoryImpl()
和 Xalan 使用
new org.apache.xalan.processor.TransformerFactoryImpl()
这篇关于将 Xalan 与 Saxon 一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!