How to ignore XML declaration differences with XmlUnit?
Asked Answered
T

1

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.

Totalizator answered 11/4, 2018 at 8:44 Comment(0)
Q
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.

Quesnay answered 11/4, 2018 at 13:43 Comment(3)
Using the evaluator you describe solves the problem for me. Will note the severity differences. Thank you for your dedication to XmlUnit, appreciated!Totalizator
you're welcome. I've opened an issue for NodeFilter and document types, it is going to be fixed in the next release.Quesnay
I see that you added a commit for this: github.com/xmlunit/xmlunit/commit/… How to use it though?Valval

© 2022 - 2024 — McMap. All rights reserved.