1

The following simple example demonstrates the problem:

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());
    }
}

Running the above example the following exception is generated:

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

My project is running in Java 8 environment. If I comment out the

.ignoreComments()

line the code is working as expected.

Would you help how can I ignore comments in XML comparison (of course with running Saxon as XSLT provider)? Thanks in advance.

Miklos Krivan
  • 1,732
  • 20
  • 14
  • Of course I can remove comments with a simple regex like xmlString.replaceAll("(?s)", "") but I would like to use ignoreComments() in the chain. – Miklos Krivan Jun 25 '17 at 07:10

1 Answers1

2

There's obviously a bit of a history of recent bugs here.

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

which refers to

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

And looking at github, I see that the relevant code in xmlunit is changing daily.

So it's going to be important to work out exactly what version of everything you are using.

I don't know if you are associated with the xmlunit project in any way. Generally if an application using Saxon is failing, it's best to raise the bug report in the first instance against the application, if only because they may well already know about it; they're also better placed to provide a repro that demonstrates the bug in Saxon if that's where it is. But if that doesn't get you anywhere, raise it at saxonica.plan.io.

StackOverflow isn't really an ideal vehicle for tracking bugs, especially ones that involve the integration of multiple products, so I'd advice following it up through support channels.

Two further observations:

(a) the warning messages

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

are probably significant. Saxon 9.8 has introduced a change, which I'm afraid is going to be rather inconvenient to open source projects like xmlunit: the HE product no longer supports backwards compatibility mode with 1.0. This may well mean that xmlunit will not work with Saxon-HE 9.8 unless it makes changes.

(b) the DOM HIERARCHY_REQUEST_ERR probably arises because the stylesheet is trying to write a text node as a child of the document node, which is allowed in the XDM model but not in DOM. However, I suspect this is a secondary error caused by the fact that the expected template rules have not fired, as a consequence of the error that arose during pattern matching. When an error occurs in pattern matching, the pattern is treated as not matching, and Saxon continues with a warning.

Michael Kay
  • 156,231
  • 11
  • 92
  • 164
  • Thanks Michael for your answer. I have read your suggested documents before I have created my question here (they did not help to solve my problem just helped to bring closer to understand the situation) and I have written feedback there as well. I am using xmlunit-core 2.3.0 and Saxon-HE 9.8.0-2 from maven central. Xmlunit was my choice only for collecting differences in two XML documents. I have only problem with ignoreComments. Do you think I can use different solution for this? Should I wait for new releases? – Miklos Krivan Jun 26 '17 at 05:05
  • I would't use Saxon 9.8 with xmlunit until someone from xmlunit confirms that it's supported. – Michael Kay Jun 26 '17 at 08:24
  • Maybe I was not clear that with Saxon 9.7 the ignoreWhitespace() also not working with xmlUnit. With Saxon 9.8 this is working now. Only the ignoreComments() is not working (it was not working in 9.7 version as well) so the situation is much better for me. First I have used xmlUnit for XML comparison, later I have moved toward Saxon because of XSLT 2.0 support which became important for me. Than I have encountered some interference between the two libraries. My unit tests are working (only ignoreComments fails). – Miklos Krivan Jun 27 '17 at 05:41
  • Let's try and use the support channel to get this one resolved. – Michael Kay Jun 27 '17 at 07:09
  • Thx Michael, I will join to the ticket at your mentioned saxonica link :-) – Miklos Krivan Jun 30 '17 at 14:26
  • I have upgraded just now to xmlunit-2.4.0 and saxon-HE 9.7.0-19 and the ignoreComments() method works again perfect. I have downgraded to saxon 9.7 from 9.8 (because of strict XSLT 2.0 rule in saxon 9.8 as you have suggested). – Miklos Krivan Jul 26 '17 at 06:56
  • I just add a comment about the ignoreComments() issue (https://github.com/xmlunit/xmlunit/issues/92) has it could be fix in Xmlunit2. – Stephane Toussaint Aug 24 '17 at 13:14
  • For information the ignoreComments() issue with Saxon 9.8 has been fixed as mentioned in https://github.com/xmlunit/xmlunit/issues/99. Waiting for 2.5.0 release – Stephane Toussaint Sep 01 '17 at 08:17