Search code examples
javaxml-parsingxmlunit

XMLUnit - How to compare a self-closing tag to one which isn't?


I have two pieces of XML, a part of which is :

<userActionRequiredCode>0</userActionRequiredCode>

<userActionRequiredCode xsi:nil="1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>

These are in different files which I am comparing using XMLUnit.

These are different as you can see, not same, not similar. What options can I set for XMLUnit or the Diff class or anywhere so that I can take this difference into consideration.

Currently this doesn't show up in the list of differences.

Edit : Updated sample.

UPDATE : The original problem wasn't with XMLUnit recognizing the difference. It was a mistake on my part. XMLUnit correctly recognizes the difference. This particular problem, hence is bogus, irreplicable.


Solution

  • A simple MCVE shows them to be different, so if you don't get a difference, then you setup XMLUnit to not do so.

    final String control = "<userActionRequiredCode>0</userActionRequiredCode>";
    final String test = "<userActionRequiredCode xsi:nil=\"1\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"/>";
    
    Diff myDiff = DiffBuilder.compare(Input.fromString(control))
                             .withTest(Input.fromString(test))
                             .build();
    System.out.println(myDiff.toString());
    

    OUTPUT

    Expected child nodelist length '1' but was '0' - comparing <userActionRequiredCode...> at /userActionRequiredCode[1] to <userActionRequiredCode...> at /userActionRequiredCode[1]