将Saxon与Xalan一起使用

问题描述 投票:0回答:1

我有一个应用程序,我使用Xalan作为XSLT处理器。我现在想用Saxon。我想确保所有现有的转换仍然有效。因此,我想将Xalan用于所有现有的XML文件。对于新的XML文件,我想使用Saxon。总结一下,我想同时使用两个处理器。因此我像这样实例化处理器:

TransformerFactory.newInstance("com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl", null);
or
TransformerFactory.newInstance("net.sf.saxon.TransformerFactoryImpl", null);

在我现有的单元测试中,我使用的是Xalan。但是当我将Saxon添加到我的类路径时,其中一些失败了。失败的测试都使用Apache FOP来创建PDF文件。不同之处在于,现在生成的PDF中插入了一些制表符(缩进键)(不在可见内容中,我只是在比较字节码时看到它们)。我认为这种行为很奇怪,因为我仍然使用Xalan并期望与我在类路径中没有Saxon时相同的结果。那么,当Saxon进入类路径时,还有什么改变呢?

当我添加

System.setProperty("javax.xml.transform.TransformerFactory",
            "com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl");

在我的测试中,它再次与Saxon一起使用在类路径中。但这不是我的生产环境的解决方案,因为我想在两个处理器之间动态切换。

那么当我将Saxon添加到我的类路径时,有谁知道还有什么变化?非常感谢你!

更新:我已经设置了jaxp.debug标志并得到以下输出(在类路径中使用Xalan和Saxon)

    JAXP: created new instance of class org.apache.xalan.xsltc.trax.TransformerFactoryImpl using ClassLoader: null
JAXP: created new instance of class org.apache.xalan.xsltc.trax.TransformerFactoryImpl using ClassLoader: null
JAXP: created new instance of class org.apache.xalan.xsltc.trax.TransformerFactoryImpl using ClassLoader: null
Calling com.saxonica.SchemaFactoryImpl.isSchemaLanguageSupported("http://www.w3.org/2001/XMLSchema"); returning true
JAXP: created new instance of class org.apache.xalan.xsltc.trax.TransformerFactoryImpl using ClassLoader: null
JAXP: find factoryId =javax.xml.transform.TransformerFactory
JAXP: find factoryId =javax.xml.transform.TransformerFactory
JAXP: find factoryId =javax.xml.transform.TransformerFactory
JAXP: find factoryId =javax.xml.transform.TransformerFactory
JAXP: find factoryId =javax.xml.transform.TransformerFactory
JAXP: created new instance of class org.apache.xalan.xsltc.trax.TransformerFactoryImpl using ClassLoader: null

当我从类路径中删除saxon时,我得到以下输出:

JAXP: created new instance of class org.apache.xalan.xsltc.trax.TransformerFactoryImpl using ClassLoader: null
JAXP: created new instance of class org.apache.xalan.xsltc.trax.TransformerFactoryImpl using ClassLoader: null
JAXP: created new instance of class org.apache.xalan.xsltc.trax.TransformerFactoryImpl using ClassLoader: null
JAXP: created new instance of class org.apache.xalan.xsltc.trax.TransformerFactoryImpl using ClassLoader: null
JAXP: find factoryId =javax.xml.transform.TransformerFactory
JAXP: find factoryId =javax.xml.transform.TransformerFactory
JAXP: find factoryId =javax.xml.transform.TransformerFactory
JAXP: find factoryId =javax.xml.transform.TransformerFactory
JAXP: find factoryId =javax.xml.transform.TransformerFactory
JAXP: created new instance of class org.apache.xalan.xsltc.trax.TransformerFactoryImpl using ClassLoader: null

所以我真的使用Xalan处理器。输出的差异是在类路径中没有撒克逊人我看不到这条线

Calling com.saxonica.SchemaFactoryImpl.isSchemaLanguageSupported("http://www.w3.org/2001/XMLSchema"); returning true

在阅读了这个question后,我添加了建议的线路以获取有关所有工厂的信息。当我使用xalan处理器(在类路径中使用saxon)时,我得到了

DocumentBuilderFactory implementation: com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl loaded from: Java Runtime
    XPathFactory implementation: org.apache.xpath.jaxp.XPathFactoryImpl loaded from: file:/D:/repository/xalan/xalan/2.7.1/xalan-2.7.1.jar
    TransformerFactory implementation: com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl loaded from: Java Runtime
    SAXParserFactory implementation: com.sun.org.apache.xerces.internal.jaxp.SAXParserFactoryImpl loaded from: Java Runtime

当我使用Saxon时,我得到了

    DocumentBuilderFactory implementation: com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl loaded from: Java Runtime
XPathFactory implementation: org.apache.xpath.jaxp.XPathFactoryImpl loaded from: file:/D:/repository/xalan/xalan/2.7.1/xalan-2.7.1.jar
TransformerFactory implementation: com.saxonica.config.EnterpriseTransformerFactory loaded from: file:/D:/repository/de/soptim/contrib/net/sf/saxon/Saxon-EE/9.8.0.14/Saxon-EE-9.8.0.14.jar
SAXParserFactory implementation: com.sun.org.apache.xerces.internal.jaxp.SAXParserFactoryImpl loaded from: Java Runtime
java xslt xsl-fo saxon xalan
1个回答
1
投票

我认为你必须有一些使用TransformerFactory.newInstance()但没有指定处理器的转换。尝试设置jaxp.debug系统属性以获取加载过程的诊断信息。

我建议设置系统属性

System.setProperty("javax.xml.transform.TransformerFactory",
            "com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl");

以便默认调用Xalan并使用

TransformerFactory saxon = new net.sf.saxon.TransformerFactoryImpl();

在你想要调用Saxon的情况下。如果要动态做出决策,请使用您自己的代码中的某些条件逻辑来控制它。

© www.soinside.com 2019 - 2024. All rights reserved.