1

xml无论xml标签顺序如何,我都试图将两个文件与属性和子元素进行比较。我正在使用XMLUnit相同的。我创建了一个diff使用提到的代码,并期望它会说它是相似的。但实际上它给出了差异。我想知道如何xml在忽略标签顺序的同时比较提到的文件,因为我的代码没有正确地做到这一点。

Diff xmlDiff = DiffBuilder
        .compare(expected)
        .withTest(actual)
        .ignoreWhitespace()
        .normalizeWhitespace()
        .checkForSimilar()
        .withNodeMatcher(new DefaultNodeMatcher(
                        ElementSelectors.and(
                                ElementSelectors.byXPath("/root/tag", ElementSelectors.byNameAndAllAttributes),
                                ElementSelectors.byXPath("/root/tag/fixedValue", ElementSelectors.byNameAndText)
                        )
                ))
                .build();

源 XML

<root>
    <tag attr1="attr1" attr2="attr12">
        <fixedValue>v1</fixedValue>
    </tag>
    <tag attr1="attr2" attr2="attr22">
        <fixedValue>v2</fixedValue>
    </tag>
</root>

目标 XML

<root>
    <tag attr1="attr2" attr2="attr22">
        <fixedValue>v2</fixedValue>
    </tag>
    <tag attr1="attr1" attr2="attr12">
        <fixedValue>v1</fixedValue>
    </tag>
</root>

如果我打印差异,它会产生以下输出(应该说'SIMILAR'而不是'DIFFERENT')

Expected attribute value 'attr1' but was 'attr2' - comparing <tag attr1="attr1"...> at /root[1]/tag[1]/@attr1 to <tag attr1="attr2"...> at /root[1]/tag[1]/@attr1 (DIFFERENT)
Expected attribute value 'attr12' but was 'attr22' - comparing <tag attr2="attr12"...> at /root[1]/tag[1]/@attr2 to <tag attr2="attr22"...> at /root[1]/tag[1]/@attr2 (DIFFERENT)
Expected text value 'v1' but was 'v2' - comparing <fixedValue ...>v1</fixedValue> at /root[1]/tag[1]/fixedValue[1]/text()[1] to <fixedValue ...>v2</fixedValue> at /root[1]/tag[1]/fixedValue[1]/text()[1] (DIFFERENT)
Expected attribute value 'attr2' but was 'attr1' - comparing <tag attr1="attr2"...> at /root[1]/tag[2]/@attr1 to <tag attr1="attr1"...> at /root[1]/tag[2]/@attr1 (DIFFERENT)
Expected attribute value 'attr22' but was 'attr12' - comparing <tag attr2="attr22"...> at /root[1]/tag[2]/@attr2 to <tag attr2="attr12"...> at /root[1]/tag[2]/@attr2 (DIFFERENT)
Expected text value 'v2' but was 'v1' - comparing <fixedValue ...>v2</fixedValue> at /root[1]/tag[2]/fixedValue[1]/text()[1] to <fixedValue ...>v1</fixedValue> at /root[1]/tag[2]/fixedValue[1]/text()[1] (DIFFERENT)
4

0 回答 0