且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用Java和xmlunit比较具有相同名称空间但前缀不同的两个xml

更新时间:2022-11-17 15:41:23

以下测试通过了相似"检查,但未通过相同"检查:

The following test passes the "similar" check but fails the "identical" check:

    String control = "<prefix1:element xmlns:prefix1=\"namespace\" prefix1:attribute=\"x\">Some text</prefix1:element>";
    String test = "<prefix2:element xmlns:prefix2=\"namespace\" prefix2:attribute=\"x\">Some text</prefix2:element>";
    try
    {
        Diff diff = XMLUnit.compareXML( control, test );
        assertTrue( "Similar", diff.similar() );
        assertTrue( "Identical", diff.identical() );
    }
    catch ( Exception e )
    {
        fail( e.getMessage() );
    }

从xmlunit API文档中:

From the xmlunit API docs:

  • 相同:文档中节点的内容和顺序完全相同
  • 相似:文档中节点的内容相同,但存在细微差异,例如兄弟元素的排序,名称空间前缀的值,隐式属性值的使用

因此,使用相似"检查应该可以为您提供所需的信息.

So using the "similar" check should give you what you want.

添加了前缀属性,结果相同.

added prefixed attributes, same result.