问题描述
我有2个xml文件:
问题出在属性前缀中.
<element xmlns:prefix1="namespace" prefix1:attribute="some value">Some text</element>
<element xmlns:prefix2="namespace" prefix2:attribute="some value">Some text</element>
这两个xml是相同的,具有相同的名称空间,但具有不同的前缀.如果我与xmlunit比较->断言失败.我该如何处理?
these two xml are the same, with the same namespace, but with different prefixes. If I compare with xmlunit -> assertion fails. How can I handle it?
在相似()或相同()比较的情况下,我有错误:
in case of similar() or identical() comparison I have error:
预期属性名称为'message:MessageNameString',但为'null'
期望的属性名称为'message:MessageVersion',但为'null'
预期的属性名称为"null",但为"mes:MessageNameString"
预期的属性名称为'null',但为'mes:MessageVersion'
Expected attribute name 'message:MessageNameString' but was 'null'
Expected attribute name 'message:MessageVersion' but was 'null'
Expected attribute name 'null' but was 'mes:MessageNameString'
Expected attribute name 'null' but was 'mes:MessageVersion'
推荐答案
以下测试通过了相似"检查,但未通过相同"检查:
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.
这篇关于如何使用Java和xmlunit比较具有相同名称空间但前缀不同的两个xml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!