5

How can I configure XmlUnit.Net to ignore the XML declaration when comparing two documents?

Assume I have the following control document:

<?xml version="1.0" encoding="utf-8"?>
<a><amount>1</amount></a>

Which I want to compare with:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<a><amount>1</amount></a>

The comparison should result in no differences.

My expectation would be that using a NodeFilter like so should work, but it doesn't:

var diff = DiffBuilder.Compare(control)
    .WithTest(test)
    .WithNodeFilter(n => n.NodeType != XmlNodeType.XmlDeclaration)
    .Build();

diff.Differences.Count().Should().Be(0);

The assertion fails with two differences - one for the encoding (different casing) and another for the standalone attribute. I'm not interested in any.

Whether I say n.NodeType != XmlNodeType.XmlDeclaration or n.NodeType == XmlNodeType.XmlDeclaration makes no difference.

I am using XMLUnit.Core v2.5.1.

RMo
  • 132
  • 7

1 Answers1

6

NodeFilter only applies to nodes that are children of other nodes (returned by XmlNode.ChildNodes). Unfortunately this is not the case for the document type declaration, which probably is a bug.

In your case you want to tweak the DifferenceEvaluator and downgrade the differences you are not interested in. Something like

DifferenceEvaluators.Chain(DifferenceEvaluators.Default,
    DifferenceEvaluators.DowngradeDifferencesToEqual(ComparisonType.XML_STANDALONE, ComparisonType.XML_ENCODING))

would swallow the differences.

Maybe you don't want to just count the differences but also look at their severity. The difference in encoding would be a "similar" difference, while the different values of standalone are critical.

Stefan Bodewig
  • 3,260
  • 15
  • 22
  • Using the evaluator you describe solves the problem for me. Will note the severity differences. Thank you for your dedication to XmlUnit, appreciated! – RMo Apr 11 '18 at 14:17
  • you're welcome. I've opened an issue for `NodeFilter` and document types, it is going to be fixed in the next release. – Stefan Bodewig Apr 11 '18 at 15:14
  • I see that you added a commit for this: https://github.com/xmlunit/xmlunit/commit/4f5ce2c86f45f82c2bbee96c715fd271a32faab2 How to use it though? – theprogrammer Aug 08 '23 at 18:50