XMLUnit-与顺序不相同的XML相比存在问题 [英] XMLUnit - Issue comparing to XMLs that are not in same order
问题描述
我正在使用XMLUnit2来比较2个没有相同顺序元素的XML.我看到以下错误-
I am using XMLUnit2 to compare 2 XMLs which don't have elements in the same order. I am seeing the below error -
差异=预期的子代'billingCode',但为'null'-比较 在 /translateServiceRequestResponse [1]/translateServiceRequestReturn [1]/legacyCode [2]/billingCode [1]转换为NULL
Differences = Expected child 'billingCode' but was 'null' - comparing at /translateServiceRequestResponse[1]/translateServiceRequestReturn[1]/legacyCode[2]/billingCode[1] to NULL
代码
Diff myDiff = DiffBuilder.compare(controlResponse).ignoreWhitespace().ignoreComments().withTest(testResponse).withNodeMatcher(new DefaultNodeMatcher(ElementSelectors.byNameAndText)).checkForSimilar().build();
System.out.println("Differences = " + myDiff.toString());
控制文件
<translateServiceRequestResponse>
<translateServiceRequestReturn>
<legacyCode>
<amount>0</amount>
<billingCode>VS128</billingCode>
<description>HD Rec</description>
<priceName>EquipChoice X1 HD TP</priceName>
<pricingElementName>X1 HD Receiver</pricingElementName>
<codeAmount>0</codeAmount>
<lobSubType>0</lobSubType>
<addressable>1</addressable>
<nonStandard>false</nonStandard>
</legacyCode>
<legacyCode>
<amount>0</amount>
<billingCode>VF123</billingCode>
<description>HD Rec</description>
<packageCode>VE286</packageCode>
<priceName>EquipChoice X1 HD TP</priceName>
<pricingElementName>X1 HD Receiver</pricingElementName>
<codeAmount>0</codeAmount>
<lobSubType>0</lobSubType>
<addressable>1</addressable>
<nonStandard>false</nonStandard>
</legacyCode>
<legacyCode>
<amount>0</amount>
<billingCode>VF170</billingCode>
<description>HD Rec</description>
<packageCode>VE286</packageCode>
<priceName>EquipChoice X1 HD TP</priceName>
<pricingElementName>X1 HD Receiver</pricingElementName>
<codeAmount>2.5</codeAmount>
<lobSubType>0</lobSubType>
<addressable>1</addressable>
<nonStandard>false</nonStandard>
</legacyCode>
</translateServiceRequestReturn>
测试文件
<translateServiceRequestResponse>
<translateServiceRequestReturn>
<legacyCode>
<amount>0</amount>
<billingCode>VS128</billingCode>
<description>HD Rec</description>
<priceName>EquipChoice X1 HD TP</priceName>
<pricingElementName>X1 HD Receiver</pricingElementName>
<codeAmount>0</codeAmount>
<lobSubType>0</lobSubType>
<addressable>1</addressable>
<nonStandard>false</nonStandard>
</legacyCode>
<legacyCode>
<amount>0</amount>
<billingCode>VF170</billingCode>
<description>HD Rec</description>
<packageCode>VE286</packageCode>
<priceName>EquipChoice X1 HD TP</priceName>
<pricingElementName>X1 HD Receiver</pricingElementName>
<codeAmount>2.5</codeAmount>
<lobSubType>0</lobSubType>
<addressable>1</addressable>
<nonStandard>false</nonStandard>
</legacyCode>
<legacyCode>
<amount>0</amount>
<billingCode>VF123</billingCode>
<description>HD Rec</description>
<packageCode>VE286</packageCode>
<priceName>EquipChoice X1 HD TP</priceName>
<pricingElementName>X1 HD Receiver</pricingElementName>
<codeAmount>0</codeAmount>
<lobSubType>0</lobSubType>
<addressable>1</addressable>
<nonStandard>false</nonStandard>
</legacyCode>
</translateServiceRequestReturn>
推荐答案
XMLUnit使用ElementSelector
来确定对照和测试文档中两组同级中的哪些元素彼此匹配.它将从文档的根开始匹配,并且您必须确保它在每个级别都选择正确的分支.一旦做出决定,XMLUnit就不会回溯.
XMLUnit uses the ElementSelector
in order to determine which elements of two sets of siblings in the control and test documents to match with each other. It will start matching from the root of the documents and you must ensure it picks the correct branches at each level. Once a decision has been made, XMLUnit won't back-track.
选择ElementSelector
时,必须始终确保它有助于XMLUnit采取正确的分支,使其与文档的逻辑结构所要求的最接近文档的根.您说的元素与legacyCode
元素的顺序不同,因此必须帮助XMLUnit在其中进行正确的选择. byNameAndText
在这里无济于事. byNameAndText
中根本没有嵌套的文本,因此它们都是相同的,并且XMLUnit按文档顺序匹配元素.
When picking an ElementSelector
you must always ensure it helps XMLUnit to take the correct branch as close to the root of the document as the logical structure of your document requires. The elements you say are not in the same order are the legacyCode
elements, so you must help XMLUnit to make the correct selection among them. byNameAndText
won't help here. There is no nested text in byNameAndText
at all, so they are all the same and XMLUnit matches the elements in document order.
这与 https:中关于tr
的问题完全相同.//github.com/xmlunit/user-guide/wiki/SelectingNodes
This is exactly the same problem as the one about tr
s in https://github.com/xmlunit/user-guide/wiki/SelectingNodes
在我看来,您的legacyCode
似乎是由嵌套在billingCode
元素中的文本标识的.如果是这样,您可以使用ElementSelector
之类的
To me looks as if your legacyCode
would be identified by the text nested into the billingCode
element. If so you could use an ElementSelector
like
ElementSelectors.conditionalBuilder()
.whenElementIsNamed("legacyCode").thenUse(ElementSelectors.byXPath("./billingCode", ElementSelectors.byNameAndText))
.elseUse(ElementSelectors.byName)
.build();
您可能需要对其进行调整,以使其适用于文档中未显示的部分,或者确实需要byNameAndText
而不是树的其他某些部分而不是byName
.
You may need to adapt this so it works for parts of the of the document you haven't shown or if byNameAndText
really is required rather than byName
for some other parts of the tree.
这篇关于XMLUnit-与顺序不相同的XML相比存在问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!