简体   繁体   English

无法使用XMLUnit注册名称空间

[英]Can't register namespace with XMLUnit

I can't seem to work out how to set namespace when comparing XML's using xmlunit-2 在使用xmlunit-2比较XML时,我似乎无法弄清楚如何设置名称空间

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. org.xmlunit.XMLUnitException:元素“ text:b”的前缀“ text”未绑定。

stripping away the namespace prefix from elements make it work, but I would like to learn how register properly the namespace with DiffBuilder . 从元素中DiffBuilder名称空间前缀可以使其正常工作,但是我想学习如何使用DiffBuilder正确注册名称空间。

The same problem/ignorence I experience with xmlunit-1.x so hints using that library I would appreciate as well. 我在xmlunit-1.x遇到的同样的问题/无知,所以暗示使用该库,我也将不胜感激。

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 谢谢斯蒂芬

NamespaceContext is only used for the XPath associated with the "targets" of comparisions. NamespaceContext仅用于与比较的“目标”关联的XPath。 It is not intended to be used to provide mappings for the XML documents you compare. 它不打算用于为您比较的XML文档提供映射。

There is no way of binding XML namespaces to prefixes outside of the documents themselves in XMLUnit. 在XMLUnit中,无法将XML名称空间绑定到文档本身之外的前缀。 This means you either must use xmlns attributes or not use prefixes at all. 这意味着您要么必须使用xmlns属性,要么根本不使用前缀。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM