Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如果存在xsi:type和不同的名称空间前缀,则xmlunit.Diff返回similor=false_Java_Xml - Fatal编程技术网

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

Java 如果存在xsi:type和不同的名称空间前缀,则xmlunit.Diff返回similor=false,java,xml,Java,Xml,此代码: 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\

此代码:

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());
import org.custommonkey.xmlunit.Diff;
字符串结果=”;
字符串正确=”;
Diff Diff=新的Diff(结果,正确);
System.out.println(“diff:+diff”);
System.out.println(“diff.simular():”+diff.simular());
结果:

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:org.custommonkey.xmlunit.diff
[不完全相同]应为命名空间前缀“ns1”,但为“ns2”-将at/Square[1]与at/Square[1]进行比较
[不同]预期属性值为“ns1:Shape”,但为“ns2:Shape”-将at/Square[1]/@type与at/Square[1]/@type进行比较
差异相似():false

我希望diff.simular()是真的。 还是说这是错误的原因?还是一只虫子

如果删除xsi:type信息,则返回true


知道如何修复它吗?

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

实现自定义差异Listener执行此操作

    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) {

        }
    });

对于XMLUnit2.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;
    }
})

干得好!我向项目组发布了一个指向您修复的链接。希望他们能在下一个版本中包含它(2009年的最后一个版本…)。此案例和其他案例在其上次修订版中正常工作。他们将很快发布一个版本:XMLUnit1.4已经发布,它也解决了这个问题。