以下简单示例演示了该问题:
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 提供程序运行)?提前致谢。