简体   繁体   English

如果存在xsi:type和不同的名称空间前缀,则xmlunit.Diff返回similar = false

[英]xmlunit.Diff returns similar=false if there is a xsi:type and a different namespace prefix

This code: 这段代码:

import org.custommonkey.xmlunit.Diff;

String result = "<ns1:Square xsi:type=\"ns1:Shape\" xmlns:ns1=\"http://example.com/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"/>";
String correct = "<ns2:Square xsi:type=\"ns2:Shape\" xmlns:ns2=\"http://example.com/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"/>";

Diff diff = new Diff(result, correct);
System.out.println("diff:" + diff);
System.out.println("diff.similar(): " + diff.similar());

results in: 结果是:

diff: org.custommonkey.xmlunit.Diff
[not identical] Expected namespace prefix 'ns1' but was 'ns2' - comparing <ns1:Square...> at /Square[1] to <ns2:Square...> at /Square[1]

[different] Expected attribute value 'ns1:Shape' but was 'ns2:Shape' - comparing <ns1:Square xsi:type="ns1:Shape"...> at /Square[1]/@type to <ns2:Square xsi:type="ns2:Shape"...> at /Square[1]/@type

diff.similar(): false

I would expect diff.similar() to be true. 我希望diff.similar()是真的。 or is there a reason why it is false? 还是有理由说它是假的? or it is a bug? 或者它是一个错误?

it returns true if I remove the xsi:type info. 如果我删除xsi:type info,它返回true。

any idea how to fix it? 任何想法如何解决它?

XMLUnit doesn't understand xsi type . XMLUnit不了解xsi类型。 It does a simple string comparison for attribute value. 它对属性值进行简单的字符串比较。

Implements a custom DifferenceListener do the trick 实现自定义DifferenceListener可以做到这一点

    final Diff d = new Diff(result, correct);
    d.overrideDifferenceListener(new DifferenceListener() {

        public int differenceFound(Difference difference) {

            final Node controlNode = difference.getControlNodeDetail().getNode();
            final Node testNode = difference.getTestNodeDetail().getNode();
            if (difference.getId() == DifferenceConstants.ATTR_VALUE_ID
                && isXSIType(controlNode) && isXSIType(testNode)) {
                if (getNameSpaceFromPrefix(controlNode).compareTo(getNameSpaceFromPrefix(testNode)) != 0) {
                    return RETURN_ACCEPT_DIFFERENCE;
                }
                String withoutPrefixControl = getNameWithoutPrefix(controlNode);
                String withoutPrefixTest = getNameWithoutPrefix(testNode);

                if (withoutPrefixControl.compareTo(withoutPrefixTest) == 0) {
                    return RETURN_IGNORE_DIFFERENCE_NODES_IDENTICAL;
                }
            }
            return RETURN_ACCEPT_DIFFERENCE;
        }

        boolean isXSIType(org.w3c.dom.Node node) {
            return node.getNodeType() == Node.ATTRIBUTE_NODE &&
                node.getLocalName().compareTo("type") == 0 &&
                node.getNamespaceURI() == "http://www.w3.org/2001/XMLSchema-instance";
        }

        private String getNameSpaceFromPrefix(Node node) {
            final int beginIndex = node.getNodeValue().indexOf(":");
            if (beginIndex == -1) {
                return "";
            }
            return node.lookupNamespaceURI(node.getNodeValue().substring(0, beginIndex));
        }

        private String getNameWithoutPrefix(Node controlNode) {
            final int beginIndex = controlNode.getNodeValue().indexOf(":");
            if (beginIndex == -1) {
                return controlNode.getNodeValue();
            }
            return controlNode.getNodeValue().substring(beginIndex);
        }

        public void skippedComparison(org.w3c.dom.Node node, org.w3c.dom.Node node1) {

        }
    });

For XMLUnit 2.x add this to diff builder 对于XMLUnit 2.x,将其添加到diff builder

.withDifferenceEvaluator(new DifferenceEvaluator() {
    public ComparisonResult evaluate(Comparison comparison, ComparisonResult comparisonResult) {

        // skip namespace prefix comparison
        if (comparison.getType().equals(ComparisonType.NAMESPACE_PREFIX))
            return ComparisonResult.SIMILAR;
        return comparisonResult;
    }
})

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

相关问题 尚未声明前缀“ xsi”的命名空间 - Namespace for prefix 'xsi' has not been declared 如何在xmlunit中比较前缀与无前缀xml文档以获得相似的结果 - how to compare prefixed with no prefix xml documents in xmlunit to get similar result 与元素类型“beans”相关联的属性“xsi:schemaLocation”的前缀“xsi”未绑定 - The prefix “xsi” for attribute “xsi:schemaLocation” associated with an element type “beans” is not bound 错误:“尚未声明前缀“xsi”的命名空间。 - ERROR: 'Namespace for prefix 'xsi' has not been declared.' XMLUnit与HTML文件的差异 - XMLUnit diff with HTML files maven 发布:准备命名空间前缀“xsi”与元素声明的附加命名空间冲突 - maven release:prepare namespace prefix “xsi” collides with an additional namespace declared by the element 与元素类型“ web-app”相关联的属性“ xsi:schemaLocation”的前缀“ xsi”未绑定 - The prefix “xsi” for attribute “xsi:schemaLocation” associated with an element type “web-app” is not bound 无法使用XMLUnit注册名称空间 - Can't register namespace with XMLUnit 删除元素中的xsi命名空间 - Remove xsi namespace in element 带有 XMLUnit 2 和默认命名空间的 Java XPath - Java XPath with XMLUnit 2 and Default Namespace
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM