javaxmlxmlunitxmlunit-2

Ignore all elements not present in the test XML with XMLUnit


In XMLUnit 2.x, can I ignore all elements in the actual ("control") XML that are not present in my test ("expected") XML?

Example:

Actual:

<a>123</a>
<b>456</b>
<c>789</c>

Test:

<b>456</b>
<c>xxx</c>

I would want to detect the difference in <c> but ignore the <a> tag completely because it's not present in the test XML.

The idea is that I have a big XML that I want to compare, but only a subset of elements is interesting to me, and I don't want to give XMLUnit a still large list of element names to ignore or to not ignore.


Solution

  • You can use your own DifferenceEvaluator and return ComparisonResult.SIMILAR (or even EQUAL) on all CHILD_NODELIST_LENGTH and CHILD_LOOKUP comparisons.

    If you only want to ignore elements added in your test document (rather than ignore all elements not present on the other side independent of direction) then you should only downgrade CHILD_NODELIST_LENGTH if the control length is larger than the test length and CHILD_LOOKUPs where the node is missing on the test side.