XmlUnit - 如何忽略命名空间前缀并比较 2 个 xml 文件?

问题描述

我有两个 xml 文件。第一个包含命名空间前缀,第二个不包含。因此,当我断言它们时,我想忽略这些前缀并检查是否相等。

 @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();
    }

因此,此测试未通过,我收到以下消息:

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.

我需要的是 - 这个测试用例应该通过,因为所有的节点和值都是相等的。基本上,一种忽略前缀的方法

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)