0

I can't seem to work out how to set namespace when comparing XML's using xmlunit-2

Tried like:

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

    }

but always get

org.xmlunit.XMLUnitException: The prefix "text" for element "text:b" is not bound.

stripping away the namespace prefix from elements make it work, but I would like to learn how register properly the namespace with DiffBuilder.

The same problem/ignorence I experience with xmlunit-1.x so hints using that library I would appreciate as well.

EDIT, based on the answer

By adding the namespace attribute to the root node I managed to bind the namespace

<a xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0">

thanks Stefan

4

1 回答 1

2

NamespaceContext仅用于与比较的“目标”关联的 XPath。它不打算用于为您比较的 XML 文档提供映射。

无法将 XML 名称空间绑定到 XMLUnit 中文档本身之外的前缀。这意味着您要么必须使用xmlns属性,要么根本不使用前缀。

于 2016-01-13T17:09:25.033 回答