XmlUnit - how to ignore namespace prefix and compare 2 xml files?

1.2k views Asked by At

I have two xml files. First contains namespace prefix and the second one doesn't. So when I assert them I want to ignore these prefixes and check for equality.

 @Test
    public void test1(){
        String control = "<prefix1:element xmlns:prefix1=\"namespace\" >Some text</prefix1:element>";
        String test = "<element xlmns=\"namespace\">Some text</element>";

        XmlAssert.assertThat(controlElement).and(testElement)
                .ignoreChildNodesOrder()
                .ignoreComments()
                .ignoreWhitespace()
                .withNodeMatcher(new DefaultNodeMatcher((ElementSelector) (control,test)->
                {
                return control!=null && test !=null && Objects.equals(control.getLocalName(),test.getLocalName());
                }
                ))
                .withDifferenceEvaluator((comparison,outcome)->{
                    System.out.println(comparison);
                    System.out.println(outcome);
                    if (outcome == ComparisonResult.EQUAL){
                        return outcome;
                    }
                    if (comparison.getType() == ComparisonType.NAMESPACE_URI
                    || comparison.getType()==ComparisonType.NAMESPACE_PREFIX){
                        System.out.println("aaaa");
                        return ComparisonResult.SIMILAR;
                    }
                    return outcome;
                })
                .areSimilar();
    }

So, this test is not passing and I get the following message:

java.lang.AssertionError: 
Expecting:
 <<prefix1:element xmlns:prefix1="namespace">Some text</prefix1:element>>
to be equal to:
 <<element xlmns="namespace">Some text</element>>
but was not.

And what I need is that - this test case should be passing because all of the nodes and values are equal. Basically, a way to ignore the prefix

0

There are 0 answers