XML单元 - 在不同的xml元素上使用自定义元素选择器

时间:2017-05-04 10:27:05

标签: java xml diff xmlunit xmlunit-2

我在使用XMLUnit(2.2.1)比较xml文档中的各种元素时遇到了问题。在我的文档中有几个xml元素,我想 知道,他们是否彼此不同。但是,我不想以相同的方式比较所有xml元素。有时我只是想用它们的名字来比较它们。在其他情况下,我想通过名称和属性或名称和文字来比较它们。

这是一个例子(见评论)

控制

<?xml version="1.0" encoding="UTF-8"?>
<ROOT>
   <LANGUAGES>
      <!-- Compare LANGUAGE by Name and Attribute -->
      <LANGUAGE VALUE="DE" />
      <LANGUAGE VALUE="EN" />
      <LANGUAGE VALUE="IT" />
      <LANGUAGE VALUE="FR" />
   </LANGUAGES>
   <CODES>
      <!-- Compare CODE by Name and Text -->
      <CODE>10000-1</CODE>
      <CODE>20000-2</CODE>
      <CODE>30000-3</CODE>
      <CODE>40000-4</CODE>
   </CODES>
   <CONTACT> <!-- Compare CONTACT and Children just by Name -->
      <FIRSTNAME>Max</FIRSTNAME>
      <SURNAME>Mustermann</SURNAME>
   </CONTACT>
</ROOT>

测试:

<?xml version="1.0" encoding="UTF-8"?>
<ROOT>
   <LANGUAGES>
      <!-- Compare LANGUAGE by Name and Attribute -->
      <LANGUAGE VALUE="DE" />
      <LANGUAGE VALUE="FR" />
   </LANGUAGES>
   <CODES>
      <!-- Compare CODE by Name and Text -->
      <CODE>20000-2</CODE>
      <CODE>40000-4</CODE>
   </CODES>
   <CONTACT> <!-- Compare CONTACT and Children by Name -->
      <FIRSTNAME>Tim</FIRSTNAME>
      <SURNAME>Mustermann</SURNAME>
   </CONTACT>
</ROOT>

我将ElementSelectors与ElementSelectors.conditionalBuilder(https://github.com/xmlunit/user-guide/wiki/SelectingNodes#conditional-elementselectors)结合使用,仅对特定元素应用ElementSelector(whenElementIsNamed)。也许这不是我想达到的正确方法。

这是我用于测试的代码:

public void xmlDiff() {
    String control = getControlDocument(); //
    String test = getTestDocument(); //
    Diff myDiff = DiffBuilder.compare(control)//
            .withTest(test) //
            .ignoreWhitespace() //
            .ignoreComments() //
            .checkForSimilar() //
            .withNodeMatcher(new DefaultNodeMatcher(partialElementSelector("LANGUAGE", ElementSelectors.byNameAndAllAttributes), partialElementSelector("CODE",ElementSelectors.byNameAndText), ElementSelectors.byName)) //
            .build();
    assertThat(myDiff.hasDifferences()).isTrue(); // 
}

    private ElementSelector partialElementSelector(final String expectedName, final ElementSelector elementSelector) {
        return ElementSelectors.conditionalBuilder().whenElementIsNamed(expectedName).thenUse(elementSelector).build();
    }

我真正需要的是两个LANGUAGE(EN,IT)和两个CODE(10000-1,30000-3)已被删除(未替换)和FIRSTNAME更改的信息。

如果有或没有XML Unit(DiffBuilder),我怎样才能获得这些信息?

感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

ElementSelector的构造函数中使用三个DefaultNodeMatcher和在多个条件下使用单个ElementSelector之间存在细微差别。

您希望使用三个选择器实现的目标也可以写为

ElementSelectors.conditionalBuilder()
    .whenElementIsNamed("LANGUAGE")
    .thenUse(ElementSelectors.byNameAndAllAttributes)
    .whenElementIsNamed("CODE")
    .thenUse(ElementSelectors.byNameAndText)
    .elseUse(ElementSelectors.byName)
    .build();
乍一看,这似乎也是如此,但事实并非如此。事实上它会起作用。这是我从你的例子中得到的:

Expected child nodelist length '4' but was '2' - comparing <LANGUAGES...> at /ROOT[1]/LANGUAGES[1] to <LANGUAGES...> at /ROOT[1]/LANGUAGES[1] (DIFFERENT)
Expected child 'LANGUAGE' but was 'null' - comparing <LANGUAGE...> at /ROOT[1]/LANGUAGES[1]/LANGUAGE[2] to <NULL> (DIFFERENT)
Expected child 'LANGUAGE' but was 'null' - comparing <LANGUAGE...> at /ROOT[1]/LANGUAGES[1]/LANGUAGE[3] to <NULL> (DIFFERENT)
Expected child nodelist length '4' but was '2' - comparing <CODES...> at /ROOT[1]/CODES[1] to <CODES...> at /ROOT[1]/CODES[1] (DIFFERENT)
Expected child 'CODE' but was 'null' - comparing <CODE...> at /ROOT[1]/CODES[1]/CODE[1] to <NULL> (DIFFERENT)
Expected child 'CODE' but was 'null' - comparing <CODE...> at /ROOT[1]/CODES[1]/CODE[3] to <NULL> (DIFFERENT)
Expected text value 'Max' but was 'Tim' - comparing <FIRSTNAME ...>Max</FIRSTNAME> at /ROOT[1]/CONTACT[1]/FIRSTNAME[1]/text()[1] to <FIRSTNAME ...>Tim</FIRSTNAME> at /ROOT[1]/CONTACT[1]/FIRSTNAME[1]/text()[1] (DIFFERENT)

所以会发生以下情况:当XMLUnit使用LANGUAGE VALUE查看en时,您构建的第一个条件ElementSelector将返回{{1} }所有候选false元素。

使用多参数LANGUAGE构造函数时,系统会询问下一个DefaultNodeMatcher - 在您的情况下也会返回ElementSelector,因为它只对false元素感兴趣。然后咨询第三个CODE,它将很乐意接受任何名为ElementSelector的测试元素。

使用上面设置的单LANGUAGE时,只要ElementSelector返回Predicate,组合选择器将永远不会参考任何替代方案。这意味着选择器返回true后,LANGUAGE元素将不再有任何想法。

我会尝试使用您的示例更新https://github.com/xmlunit/user-guide/wiki/SelectingNodes#nodematcher,并希望能够帮助您改进文档。