6

这段代码:

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

结果是:

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

我希望 diff.similar() 是真的。还是有理由说它是假的?或者它是一个错误?

如果我删除 xsi:type 信息,它将返回 true。

知道如何解决吗?

4

2 回答 2

5

XMLUnit 不理解 xsi 类型。它对属性值进行简单的字符串比较。

实现一个自定义的 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) {

        }
    });
于 2013-02-01T14:46:23.130 回答
3

对于 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;
    }
})
于 2016-07-23T01:20:13.307 回答