I have below XMLs with list of Customers and each Customer has list of transactions. I have managed to write XMLUnit code to ignore the order of Customers but not the transactions. The transactions have bit of unique structure(which cannot be altered). Below are the sample XMLs and code. Please help.
XML 1:
<Customers>
<Customer>
<Id>1</Id>
<Transactions>
<Transaction>
<CashTransaction>
<amount>100</amount>
</CashTransaction>
</Transaction>
<Transaction>
<CardTransaction>
<amount>200</amount>
</CardTransaction>
</Transaction>
</Transactions>
</Customer>
<Customer>
<Id>2</Id>
<Transactions>
<Transaction>
<CardTransaction>
<amount>500</amount>
</CardTransaction>
</Transaction>
<Transaction>
<CashTransaction>
<amount>400</amount>
</CashTransaction>
</Transaction>
</Transactions>
</Customer>
</Customers>
XML 2:
<Customers>
<Customer>
<Id>2</Id>
<Transactions>
<Transaction>
<CashTransaction>
<amount>400</amount>
</CashTransaction>
</Transaction>
<Transaction>
<CardTransaction>
<amount>500</amount>
</CardTransaction>
</Transaction>
</Transactions>
</Customer>
<Customer>
<Id>1</Id>
<Transactions>
<Transaction>
<CardTransaction>
<amount>200</amount>
</CardTransaction>
</Transaction>
<Transaction>
<CashTransaction>
<amount>100</amount>
</CashTransaction>
</Transaction>
</Transactions>
</Customer>
</Customers>
Code:
Diff diffs = DiffBuilder.compare(Input.from(resourceAsStream1))
.withTest(Input.from(resourceAsStream2))
.ignoreComments()
.ignoreWhitespace()
.normalizeWhitespace()
.checkForSimilar()
.withDifferenceEvaluator(evaluator)
.withNodeMatcher(new DefaultNodeMatcher(ElementSelectors.conditionalBuilder()
.whenElementIsNamed("Customer")
.thenUse(ElementSelectors.byXPath("./Id", ElementSelectors.byNameAndText))
.elseUse(ElementSelectors.byName)
.build()
)).build();
Differences shown:
Expected child 'CashTransaction' but was 'null' - comparing <CashTransaction...> at /Customers[1]/Customer[1]/Transactions[1]/Transaction[1]/CashTransaction[1] to <NULL>
Expected child 'null' but was 'CardTransaction' - comparing <NULL> to <CardTransaction...> at /Customers[1]/Customer[2]/Transactions[1]/Transaction[1]/CardTransaction[1]
Expected child 'CardTransaction' but was 'null' - comparing <CardTransaction...> at /Customers[1]/Customer[1]/Transactions[1]/Transaction[2]/CardTransaction[1] to <NULL>
Expected child 'null' but was 'CashTransaction' - comparing <NULL> to <CashTransaction...> at /Customers[1]/Customer[2]/Transactions[1]/Transaction[2]/CashTransaction[1]
..
..