I have XML in the following format:
<Root>
<Elem1>
<SubElem1>A</SubElem1>
...
</Elem1>
<Elem1>
<SubElem1>B</SubElem1>
...
</Elem1>
<Elem2>
...
</Elem2>
...
</Root>
I want to instruct XmlUnit's diff engine to ignore the order of ElemN elements, but only within their "group".
E.g.:
- If the second and the first Elem1 in the previous example would change order => equal
- If the Elem2 would become before Elem1 or in the middle of Elem1 => difference
Is there a way to achieve this result?
Yes, this is the job of
ElementSelector. By default XMLUnit (2.x) compares elements in the order they appear in but you can influence which of the siblings are picked by using anElementSelector.I your case you seem to be happy with choosing elements in order unless they are named
Elem1. ForElem1you'd like to choose pairs based on the text nested into theSubElement1child. One way to write such anElementSelectorcould beSee the XMLUnit user guide for more details and examples.