我正在使用 xmlunit 包类比较两个 XML 文件。我试图在节点选择时忽略节点的顺序。但仍然在差异结果中,我观察到它正在比较两个 xml 中相同深度的节点并显示它不同。我在这里感兴趣的节点是“记录”,节点选择的元素是 timeDimensionStartDate。请让我知道我哪里出错了。
我在 pom.xml 中使用以下依赖项,以便使用 xmlunit2 包。
<dependency>
<groupId>org.xmlunit</groupId>
<artifactId>xmlunit-core</artifactId>
<version>2.8.2</version>
</dependency>
<dependency>
<groupId>org.xmlunit</groupId>
<artifactId>xmlunit-matchers</artifactId>
<version>2.8.2</version>
</dependency>
控制节点:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<dataRecord xmlns="https://someinfo">
<dh>
<!---some fields -->
</dh>
<records>
<record>
<rt>123</rt>
<srvcs>
<srvc>ABC</srvc>
</srvcs>
<timeDimensionStartDate>20211003</timeDimensionStartDate>
<timeDimensionEndDate>20211003</timeDimensionEndDate>
<!---Other elements--->
</record>
<!--Multiple record nodes --->
</records>
测试节点
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<dataRecord xmlns="https://someinfo">
<dh>
<!---some fields -->
</dh>
<records>
<record>
<rt>123</rt>
<srvcs>
<srvc>ABC</srvc>
</srvcs>
<timeDimensionStartDate>20211005</timeDimensionStartDate>
<timeDimensionEndDate>20211005</timeDimensionEndDate>
<!---Other elements--->
</record>
<!--Multiple record nodes --->
<record>
<rt>123</rt>
<srvcs>
<srvc>ABC</srvc>
</srvcs>
<timeDimensionStartDate>20211003</timeDimensionStartDate>
<timeDimensionEndDate>20211003</timeDimensionEndDate>
<!---Other elements--->
</record>
<!--Multiple record nodes --->
</records>
收到的差异:信息:预期文本值“20211003”但为“20211005” - 比较 /dataRecord[1]/records[1]/record[1]/timeDimensionStartDate[1]/text() 处的 <timeDimensionStartDate ...>20211003 [1] 到 <timeDimensionStartDate ...>20211005 在 /dataRecord[1]/records[1]/record[1]/timeDimensionStartDate[1]/text()[1] (不同)
用于comaprison的XMLunit代码片段:
Diff nodeDiff = DiffBuilder.compare(source)
.withTest(target).ignoreComments().ignoreWhitespace().checkForSimilar()
.withNodeMatcher(new DefaultNodeMatcher(ElementSelectors.conditionalBuilder()
.whenElementIsNamed("record").thenUse(ElementSelectors.byXPath("./timeDimensionStartDate", ElementSelectors.byNameAndText)).
elseUse(ElementSelectors.byName).build()))
.withDifferenceEvaluator(((comparison, outcome) -> {
if (outcome == ComparisonResult.DIFFERENT &&
comparison.getType() == ComparisonType.CHILD_NODELIST_SEQUENCE) {
return ComparisonResult.EQUAL;
}
return outcome;
})).build();