XML 使用 XML 单元进行比较在重新排序元素时不起作用
XML comparison using XMLunit not working when elements are reordered
我有两个 XML
<books><book language="English"><authors><author>Niklaus Wirth</author></authors></book><book language="English"><authors><author>Mark Twain</author></authors></book><book language="English"><authors><author>O.-J. Dahl</author></authors></book></books>
<books><book language="English"><authors><author>Niklaus Wirth</author></authors></book><book language="English"><authors><author>O.-J. Dahl</author></authors></book><book language="English"><authors><author>Mark Twain</author></authors></book></books>
最后两个<author>
标签的顺序不同,应该是相似的。
这是代码
public static void compare() throws FileNotFoundException{
String sourceXML=convertXMLtoString(source);
String targetXML=convertXMLtoString(target);
System.out.println(sourceXML);
System.out.println(targetXML);
Diff mydiff=DiffBuilder.compare(sourceXML)
.withTest(targetXML)
.withNodeMatcher(new DefaultNodeMatcher(ElementSelectors.byNameAndText))
.checkForSimilar()
//.withNodeMatcher()
.build();
Iterable<Difference> differences = mydiff.getDifferences();
for(Difference d:differences){
System.out.println(d.toString());
}
}
这是输出。
Expected child 'author' but was 'null' - comparing <author...> at /books[1]/book[2]/authors[1]/author[1] to <NULL> (DIFFERENT)
Expected child 'null' but was 'author' - comparing <NULL> to <author...> at /books[1]/book[2]/authors[1]/author[1] (DIFFERENT)
Expected child 'author' but was 'null' - comparing <author...> at /books[1]/book[3]/authors[1]/author[1] to <NULL> (DIFFERENT)
Expected child 'null' but was 'author' - comparing <NULL> to <author...> at /books[1]/book[3]/authors[1]/author[1] (DIFFERENT)
任何人都可以告诉我如何忽略这个吗?我认为将 NameandText 与元素选择器一起使用应该忽略该顺序。
谢谢
使用 byNameAndText
确保选择正确的 author
标签,但这已经太晚了,因为 XMLUnit 需要 select 正确的 book
来进行比较成功。一旦确定要比较哪个 book
元素,它就永远不会在不同的子树中搜索元素。
我不认为 author
最终区分了这些书,但在没有任何其他提示的情况下,类似于
ElementSelectors.conditionalBuilder()
.whenElementIsNamed("book").thenUse(ElementSelectors.byXPath("./authors/author", ElementSelectors.byNameAndText))
.elseUse(ElementSelectors.byNameAndText)
.build();
应该可以。
我有两个 XML
<books><book language="English"><authors><author>Niklaus Wirth</author></authors></book><book language="English"><authors><author>Mark Twain</author></authors></book><book language="English"><authors><author>O.-J. Dahl</author></authors></book></books>
<books><book language="English"><authors><author>Niklaus Wirth</author></authors></book><book language="English"><authors><author>O.-J. Dahl</author></authors></book><book language="English"><authors><author>Mark Twain</author></authors></book></books>
最后两个<author>
标签的顺序不同,应该是相似的。
这是代码
public static void compare() throws FileNotFoundException{
String sourceXML=convertXMLtoString(source);
String targetXML=convertXMLtoString(target);
System.out.println(sourceXML);
System.out.println(targetXML);
Diff mydiff=DiffBuilder.compare(sourceXML)
.withTest(targetXML)
.withNodeMatcher(new DefaultNodeMatcher(ElementSelectors.byNameAndText))
.checkForSimilar()
//.withNodeMatcher()
.build();
Iterable<Difference> differences = mydiff.getDifferences();
for(Difference d:differences){
System.out.println(d.toString());
}
}
这是输出。
Expected child 'author' but was 'null' - comparing <author...> at /books[1]/book[2]/authors[1]/author[1] to <NULL> (DIFFERENT)
Expected child 'null' but was 'author' - comparing <NULL> to <author...> at /books[1]/book[2]/authors[1]/author[1] (DIFFERENT)
Expected child 'author' but was 'null' - comparing <author...> at /books[1]/book[3]/authors[1]/author[1] to <NULL> (DIFFERENT)
Expected child 'null' but was 'author' - comparing <NULL> to <author...> at /books[1]/book[3]/authors[1]/author[1] (DIFFERENT)
任何人都可以告诉我如何忽略这个吗?我认为将 NameandText 与元素选择器一起使用应该忽略该顺序。 谢谢
使用 byNameAndText
确保选择正确的 author
标签,但这已经太晚了,因为 XMLUnit 需要 select 正确的 book
来进行比较成功。一旦确定要比较哪个 book
元素,它就永远不会在不同的子树中搜索元素。
我不认为 author
最终区分了这些书,但在没有任何其他提示的情况下,类似于
ElementSelectors.conditionalBuilder()
.whenElementIsNamed("book").thenUse(ElementSelectors.byXPath("./authors/author", ElementSelectors.byNameAndText))
.elseUse(ElementSelectors.byNameAndText)
.build();
应该可以。