1

以下简单示例演示了该问题:

import org.xmlunit.builder.DiffBuilder;
import org.xmlunit.builder.Input;
import org.xmlunit.diff.ComparisonControllers;
import org.xmlunit.diff.DefaultNodeMatcher;
import org.xmlunit.diff.Diff;
import org.xmlunit.diff.ElementSelectors;

public class AnyDiff {

    public static void main(String[] args) throws Exception {
        String controlstr = "<root>hello</root>";
        String teststr = "<root>hello</root>";
        Diff diff = DiffBuilder.compare(Input.fromString(controlstr))
                .withTest(Input.fromString(teststr))
                .withNodeMatcher(new DefaultNodeMatcher(ElementSelectors.byNameAndText))
                .checkForSimilar()
                .ignoreWhitespace() // this is working with newest Saxon 9.8.0-2 (not worked with 9.7.0-15
                .ignoreComments()  // this is not working even with newest Saxon 9.8.0-2
                .withComparisonController(ComparisonControllers.Default)
                .build();
        System.out.println(diff.hasDifferences());
    }
}

运行上面的示例会生成以下异常:

run:
Warning at char 6 in template/@match on line 1 column 122 
  XTDE0160: An error occurred matching pattern {( element() | text() | comment() |
  processing-instruction() )[not(error("XPath 1.0 Compatibility Mode is not available in
  this configuration"))]}: XPath 1.0 Compatibility Mode is not available in this configuration
Warning at char 6 in template/@match on line 1 column 122 
  XTDE0160: An error occurred matching pattern {( element() | text() | comment() |
  processing-instruction() )[not(error("XPath 1.0 Compatibility Mode is not available in
  this configuration"))]}: XPath 1.0 Compatibility Mode is not available in this configuration
Error 
  org.w3c.dom.DOMException: HIERARCHY_REQUEST_ERR: An attempt was made to insert a node
  where it is not permitted. 
Exception in thread "main" org.xmlunit.XMLUnitException: org.w3c.dom.DOMException: HIERARCHY_REQUEST_ERR: An attempt was made to insert a node where it is not permitted. 
        at org.xmlunit.transform.Transformation.transformTo(Transformation.java:190)
        at org.xmlunit.transform.Transformation.transformToDocument(Transformation.java:220)
        at org.xmlunit.input.CommentLessSource.<init>(CommentLessSource.java:45)
        at org.xmlunit.builder.DiffBuilder.wrap(DiffBuilder.java:389)
        at org.xmlunit.builder.DiffBuilder.build(DiffBuilder.java:368)
        at hu.telekom.reflex.util.AnyDiff.main(AnyDiff.java:27)
Caused by: net.sf.saxon.trans.XPathException: org.w3c.dom.DOMException: HIERARCHY_REQUEST_ERR: An attempt was made to insert a node where it is not permitted. 
        at net.sf.saxon.dom.DOMWriter.characters(DOMWriter.java:222)
        at net.sf.saxon.event.ProxyReceiver.characters(ProxyReceiver.java:190)
        at net.sf.saxon.event.ComplexContentOutputter.characters(ComplexContentOutputter.java:277)
        at net.sf.saxon.trans.TextOnlyCopyRuleSet.process(TextOnlyCopyRuleSet.java:75)
        at net.sf.saxon.trans.Mode.applyTemplates(Mode.java:477)
        at net.sf.saxon.trans.TextOnlyCopyRuleSet.process(TextOnlyCopyRuleSet.java:66)
        at net.sf.saxon.trans.Mode.applyTemplates(Mode.java:477)
        at net.sf.saxon.trans.TextOnlyCopyRuleSet.process(TextOnlyCopyRuleSet.java:66)
        at net.sf.saxon.trans.Mode.applyTemplates(Mode.java:477)
        at net.sf.saxon.Controller.transformDocument(Controller.java:2389)
        at net.sf.saxon.Controller.transform(Controller.java:1953)
        at net.sf.saxon.s9api.XsltTransformer.transform(XsltTransformer.java:589)
        at net.sf.saxon.jaxp.TransformerImpl.transform(TransformerImpl.java:73)
        at org.xmlunit.transform.Transformation.transformTo(Transformation.java:186)
        ... 5 more
Caused by: org.w3c.dom.DOMException: HIERARCHY_REQUEST_ERR: An attempt was made to insert a node where it is not permitted. 
        at com.sun.org.apache.xerces.internal.dom.ParentNode.internalInsertBefore(ParentNode.java:361)
        at com.sun.org.apache.xerces.internal.dom.ParentNode.insertBefore(ParentNode.java:288)
        at com.sun.org.apache.xerces.internal.dom.CoreDocumentImpl.insertBefore(CoreDocumentImpl.java:447)
        at com.sun.org.apache.xerces.internal.dom.NodeImpl.appendChild(NodeImpl.java:237)
        at net.sf.saxon.dom.DOMWriter.characters(DOMWriter.java:219)
        ... 18 more

我的项目在 Java 8 环境中运行。如果我注释掉

.ignoreComments()

行代码按预期工作。

您能否帮助我如何忽略 XML 比较中的注释(当然,将 Saxon 作为 XSLT 提供程序运行)?提前致谢。

4

1 回答 1

2

显然这里有一些最近的错误的历史。

https://github.com/xmlunit/xmlunit/issues/92

指的是

https://saxonica.plan.io/issues/3241

并且查看 github,我看到 xmlunit 中的相关代码每天都在变化。

因此,准确确定您正在使用的所有内容的版本非常重要。

我不知道您是否以任何方式与 xmlunit 项目相关联。通常,如果使用 Saxon 的应用程序失败,最好在第一时间针对该应用程序提出错误报告,如果只是因为他们可能已经知道它;他们也可以更好地提供一个重现撒克逊人的错误,如果那是它所在的地方。但是,如果这对您没有任何帮助,请在 saxonica.plan.io 上提出。

StackOverflow 并不是跟踪错误的理想工具,尤其是涉及集成多个产品的错误,所以我建议通过支持渠道跟进它。

两个进一步的观察:

(a) 警告信息

XTDE0160:匹配模式发生错误 {( element() | text() | comment() | processing-instruction() )[not(error("XPath 1.0 Compatibility Mode is not available in this configuration"))]}: XPath 1.0 兼容模式在此配置中不可用

可能很重要。Saxon 9.8 引入了一个变化,恐怕对 xmlunit 这样的开源项目会很不方便:HE 产品不再支持与 1.0 的向后兼容模式。这很可能意味着 xmlunit 将无法与 Saxon-HE 9.8 一起使用,除非它进行更改。

(b) DOM HIERARCHY_REQUEST_ERR 的出现可能是因为样式表试图将文本节点写入文档节点的子节点,这在 XDM 模型中是允许的,但在 DOM 中是不允许的。但是,我怀疑这是由于模式匹配期间出现错误而未触发预期模板规则这一事实引起的次要错误。当模式匹配发生错误时,该模式被视为不匹配,并且 Saxon 继续发出警告。

于 2017-06-25T18:17:27.727 回答