gpt4 book ai didi

java - 与 Xalan 一起使用 Saxon

转载 作者:行者123 更新时间:2023-12-02 10:50:55 29 4
gpt4 key购买 nike

我有一个应用程序,我使用 Xalan 作为 XSLT 处理器。我现在想使用撒克逊语。我想确保所有现有的转换仍然有效。因此我想对所有现有的 XML 文件使用 Xalan。对于新的 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 处理器。输出的差异在于,类路径中没有 saxon 我看不到该行

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

最佳答案

我认为您必须有一些使用 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 的情况下。如果您想动态做出决定,请在您自己的代码中使用一些条件逻辑来控制它。

关于java - 与 Xalan 一起使用 Saxon,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52198567/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com