当比较使用xmlunit-2的XML时,我似乎想不出如何设置名称空间
试着像:
@Test
public void testDiff_withIgnoreWhitespaces_shouldSucceed() {
// prepare testData
String controlXml = "<a><text:b>Test Value</text:b></a>";
String testXml = "<a>\n <text:b>\n Test Value\n </text:b>\n</a>";
Map<String, String> namespaces = new HashMap<String, String>();
namespaces.put("text","urn:oasis:names:tc:opendocument:xmlns:text:1.0");
// run test
Diff myDiff = DiffBuilder.compare(Input.fromString(controlXml).build())
.withTest(Input.fromString(testXml).build())
.withNamespaceContext(namespaces)
.ignoreWhitespace()
.build();
// validate result
Assert.assertFalse("XML similar " + myDiff.toString(), myDiff.hasDifferences());
}但总能得到
org.xmlunit.XMLUnitException:元素"text:b“的前缀"text”不绑定。
从元素中去掉名称空间前缀使其工作,但我想了解如何使用DiffBuilder正确地注册名称空间。
我在xmlunit-1.x中所遇到的同样的问题/无知,因此暗示我也会使用这个库。
编辑,根据答案
通过将命名空间属性添加到根节点,我设法绑定了命名空间。
<a xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0">
谢谢斯特凡
发布于 2016-01-13 17:09:25
NamespaceContext只用于与比较的“目标”相关联的XPath。它不打算用于为您比较的XML文档提供映射。
在XMLUnit中,无法将XML名称空间绑定到文档本身之外的前缀。这意味着您要么必须使用xmlns属性,要么根本不使用前缀。
https://stackoverflow.com/questions/34770388
复制相似问题