When comparing the following two documents with XMLUnit (latest version- 2.3.0) In this document, the order of elements is irrelevant, however XMLUnit is taking this into account, and since the new element is appended to the beginning of the parent tag, it is detecting multiple changes when only one has occured.
<ACVS>
<ACV>
<N>735</N>
<C>S135</C>
</ACV>
<ACV>
<N>736</N>
<C>C0S135</C>
</ACV>
<ACV>
<N>7Q4</N>
<C>C0S135</C>
</ACV>
</ACVS>
<ACVS>
<ACV>
<N>701</N>
<C>C10S120</C>
</ACV>
<ACV>
<N>735</N>
<C>S135</C>
</ACV>
<ACV>
<N>736</N>
<C>C0S135</C>
</ACV>
<ACV>
<N>7Q4</N>
<C>C0S135</C>
</ACV>
</ACVS>
Diff fileChanges = DiffBuilder.compare(Input.fromFile(previous))
.withTest(Input.fromFile(current))
.ignoreWhitespace()
.checkForSimilar()
.build();
fileChanges.getDifferences()
I end up with a number of changes as the elements are matched incorrectly:
When I am hoping for a single change:
Is there any way I can achieve this result using XMLUnit?
You want to help XMLUnit understand which ACV
s belong together by specifying an ElementSelector
that identifies the pairs. But even then you'll end up with two Difference
s, apart from "new tag added" there will be a "number of children of ACVS
is different.
From looking at your examples you seem to think about your ACV
s as identified by the textual content of the N
child. This is what you need to tell XMLUnit. The situation is pretty similar to the tr
example in https://github.com/xmlunit/user-guide/wiki/SelectingNodes (replace tr
with ACV
and th
with N
). You should be able to get the expected result via
ElementSelectors.conditionalBuilder()
.whenElementIsNamed("ACV").thenUse(ElementSelectors.byXPath("./N", ElementSelectors.byNameAndText))
.elseUse(ElementSelectors.byName)
.build();
or a variation of it.