如何使用XMLUnit DiffBuilder来检测属性和元素列表中的差异

问题描述

我需要比较两个XML字符串,其中标记中的任意属性以及任意插入到标记序列中或从标记序列中删除标记都有可能发生变化。

我发现,如果我使用 ElementSelectors.byNameAndText ,我会得到适当的差异以进行属性更改,除非存在序列插入或缺失

我发现,如果我使用 ElementSelectors.byNameAndAllAttributes ,我会得到插入标签序列或从标签序列中删除标签的相应差异,但没有合适的属性更改详细信息。

如何结合这两种差异生成机制?

控制XML

String myControlXML =
    "<?xml version='1.0' encoding='UTF-8'?>" +
    "<ns2:policy xmlns:ns2='com.xyz' version='2' id='ABCD'>" +
    "<ns2:widgets>" +
    "<ns2:widget name='foo' id='17'>" +
    "<ns2:desc text='Does foo widget stuff' version='2'/>" +
    "</ns2:widget>" +
    "<ns2:widget name='bar' id='19'>" +
    "<ns2:desc text='Does bar widget stuff' version='4'/>" +
    "</ns2:widget>" +
    "</ns2:widgets>" +
    "</ns2:policy>";

测试XML

String myTestXML1 =
    "<?xml version='1.0' encoding='UTF-8'?>" +
    "<ns2:policy xmlns:ns2='com.xyz' version='13' id='ABCD'>" +
    "<ns2:widgets>" +
    "<ns2:widget name='foo' id='17'>" +
    "<ns2:desc text='Does foo widget stuff' version='2'/>" +
    "</ns2:widget>" +
    "<ns2:widget name='bzz' id='15'>" +
    "<ns2:desc text='Does bzz widget stuff' version='6'/>" +
    "</ns2:widget>" +
    "<ns2:widget name='bar' id='19'>" +
    "<ns2:desc text='Does bar widget stuff' version='4'/>" +
    "</ns2:widget>" +
    "</ns2:widgets>" +
    "</ns2:policy>";

区分代码

Diff myDiff7 = DiffBuilder.compare(myControlXML)
    .withTest(myTestXML1)
    .withNodeMatcher(new
        DefaultNodeMatcher(ElementSelectors.byNameAndAllAttributes))
    .build();

差异结果1

2 diffs
  - type = child
  - message = ns2:policy:Expected child '{com.xyz}policy' but was 'null' - comparing <ns2:policy...> at /policy[1] to <NULL>

  - type = child
  - message = ns2:policy:Expected child 'null' but was '{com.xyz}policy' - comparing <NULL> to <ns2:policy...> at /policy[1]

这似乎是在说“某些属性不同”

与ns2:policy行匹配,

差异结果2

2 diffs
  - type = child nodelist sequence
  - message = widget:Expected child nodelist sequence '1' but was '2' - comparing <ns2:widget...> at /policy[1]/widgets[1]/widget[2] to <ns2:widget...> at /policy[1]/widgets[1]/widget[3]
  - type = child
  - message = widget:Expected child 'null' but was '{com.xyz}widget' - comparing <NULL> to <ns2:widget...> at /policy[1]/widgets[1]/widget[2]

这似乎是正确的说法,将小部件[2]移至小部件[3]并添加了新的小部件[2]。

具有初始XML值,但使用ElementSelectors.byNameAndText而不是ElementSelectors.byNameAndAllAttributes

差异结果3

6 diffs
  - type = attribute value
  - message = Expected attribute value '2' but was '13' - comparing <ns2:policy version="2"...> at /policy[1]/@version to <ns2:policy version="13"...> at /policy[1]/@version
  - type = attribute value
  - message = Expected attribute value '19' but was '15' - comparing <ns2:widget id="19"...> at /policy[1]/widgets[1]/widget[2]/@id to <ns2:widget id="15"...> at /policy[1]/widgets[1]/widget[2]/@id
  - type = attribute value
  - message = Expected attribute value 'bar' but was 'bzz' - comparing <ns2:widget name="bar"...> at /policy[1]/widgets[1]/widget[2]/@name to <ns2:widget name="bzz"...> at /policy[1]/widgets[1]/widget[2]/@name
  - type = attribute value
  - message = Expected attribute value 'Does bar widget stuff' but was 'Does bzz widget stuff' - comparing <ns2:desc text="Does bar widget stuff"...> at /policy[1]/widgets[1]/widget[2]/desc[1]/@text to <ns2:desc text="Does bzz widget stuff"...> at /policy[1]/widgets[1]/widget[2]/desc[1]/@text
  - type = attribute value
  - message = Expected attribute value '4' but was '6' - comparing <ns2:desc version="4"...> at /policy[1]/widgets[1]/widget[2]/desc[1]/@version to <ns2:desc version="6"...> at /policy[1]/widgets[1]/widget[2]/desc[1]/@version
  - type = child
  - message = Expected child 'null' but was '{com.xyz}widget' - comparing <NULL> to <ns2:widget...> at /policy[1]/widgets[1]/widget[3]

这似乎在正确地比较ns2:policy行,但在将旧窗口小部件2与新窗口小部件2比较不正确,并指示添加了新窗口小部件[3]。

在此先感谢您能给我的帮助。

编辑:

在这里XMLUnit-2.0 xpath doesn't ignoring the XML node order

找到了部分答案
Diff myDiff7a = DiffBuilder.compare(myControlXML)
    .withTest(myTestXML1)
    .withNodeMatcher(new DefaultNodeMatcher(ElementSelectors.conditionalBuilder()
        .whenElementIsNamed("widget").thenUse(
            (e1,e2) -> StringUtils.equals(e1.getAttribute("name"),e2.getAttribute("name")))
        .elseUse(ElementSelectors.byName)
        .build()))
    .build();

将“栏”小部件的“ id”属性修改为21

这将产生结果:

3 diffs
  - type = child nodelist sequence
  - message = Expected child nodelist sequence '1' but was '2' - comparing <ns2:widget...> at /policy[1]/widgets[1]/widget[2] to <ns2:widget...> at /policy[1]/widgets[1]/widget[3]
  - type = attribute value
  - message = Expected attribute value '19' but was '21' - comparing <ns2:widget id="19"...> at /policy[1]/widgets[1]/widget[2]/@id to <ns2:widget id="21"...> at /policy[1]/widgets[1]/widget[3]/@id
  - type = child
  - message = Expected child 'null' but was '{com.xyz}widget' - comparing <NULL> to <ns2:widget...> at /policy[1]/widgets[1]/widget[2]

这似乎是正确的说法:“小部件2已移至小部件3,小部件2的ID从19更改为小部件3的ID值为21,并添加了新的小部件2。”

唯一缺少的是如何对任意标签属性执行此操作。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)