【问题标题】:how to compare prefixed with no prefix xml documents in xmlunit to get similar result如何在xmlunit中比较前缀与无前缀xml文档以获得相似的结果
【发布时间】:2021-07-16 21:32:39
【问题描述】:

xmlnit 无法识别以下两个“相同”的 xml(除了一个已定义命名空间)文档相似:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns3:message xmlns:ns3="https://www.bookmarks.dev/xml/bookmarks">
    <ns3:bookmarks>
        <ns3:bookmark>
            <ns3:name>Bookmarks and Snippets Manager</ns3:name>
            <ns3:url>https://www.bookmarks.dev</ns3:url>
        </ns3:bookmark>
    </ns3:bookmarks>
</ns3:message>
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<message>
    <bookmarks>
        <bookmark>
            <name>Bookmarks and Snippets Manager</name>
            <url>https://www.bookmarks.dev</url>
        </bookmark>
    </bookmarks>
</message>

比较两者的失败单元测试:

  @Test
  void givenSameMessageOneWithoutNamespace_shouldBeSimilar() {
    ClassLoader classLoader = getClass().getClassLoader();
    final var withNamespaceInput =
        Input.from(
            new File(classLoader.getResource("with-namespace.xml").getFile()));
    final var noNamespaceInput =
        Input.from(
            new File(
                classLoader
                    .getResource("no-namespace.xml")
                    .getFile()));

    final Diff documentDiff =
            DiffBuilder.compare(withNamespaceInput)
                    .withTest(noNamespaceInput)
                    .checkForSimilar()
                    .build();

    assertThat(documentDiff.hasDifferences()).isFalse();
  }

不同之处在于Expected namespace uri 'null' but was 'https://www.bookmarks.dev/xml/bookmarks' - comparing &lt;message...&gt; at /message[1] to &lt;ns3:message...&gt; at /message[1] (DIFFERENT)...

任何想法如何配置比较器以忽略第二个文档中缺少的前缀?

【问题讨论】:

    标签: java xml xmlunit xmlunit-2


    【解决方案1】:

    我的问题是,在生成无命名空间文档时,我没有在根元素中定义默认命名空间。添加它可以解决问题,并且 xmlunit 将它们识别为相似:

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <message xmlns="https://www.bookmarks.dev/xml/bookmarks">
        <bookmarks>
            <bookmark>
                <name>Bookmarks and Snippets Manager</name>
                <url>https://www.bookmarks.dev</url>
            </bookmark>
        </bookmarks>
    </message>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-12-16
      • 1970-01-01
      • 2012-11-01
      • 1970-01-01
      • 2013-04-09
      • 2020-07-20
      • 1970-01-01
      相关资源
      最近更新 更多