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

一曲冷凌霜 提交于 2021-01-29 07:11:48

问题


I've a XML as below

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE ResourceObject PUBLIC "my_corp.dtd" "my_corp.dtd">
<ResourceObject displayName="TESTNGAD\AggUserFSP test" identity="CN=AggUserFSP test,OU=FSPAggeFrame,OU=unittests,DC=TestNGAD,DC=local" objectType="account" uuid="{97182a65-61f2-443c-b0fa-477d0821d8c4}">
   <Attributes>
     <Map>
       <entry key="accountFlags">
         <value>
           <List>
             <String>Normal User Account</String>
             <String>Password Cannot Expire</String>
           </List>
         </value>
       </entry>
       <entry key="homePhone" value="6555"/>
       <entry key="l" value="Pune"/>
       <entry key="memberOf">
         <value>
           <List>
             <String>CN=FSPGRP2,OU=ADAggF,OU=unittests2,DC=AUTODOMAIN,DC=LOCAL</String>
             <String>CN=FSPGRP1,OU=ADAggF,OU=unittests2,DC=AUTODOMAIN,DC=LOCAL</String>
             <String>CN=LocalAggFrame,OU=FSPAggeFrame,OU=unittests,DC=TestNGAD,DC=local</String>
           </List>
         </value>
       </entry>
       <entry key="objectClass">
         <value>
           <List>
             <String>top</String>
             <String>person</String>
             <String>organizationalPerson</String>
             <String>user</String>
           </List>
         </value>
       </entry>
       <entry key="sn" value="test"/>
       <entry key="st" value="MH"/>
       <entry key="streetAddress" value="SB ROAD"/>
       <entry key="title" value="QA"/>
       <entry key="userPrincipalName" value="AggUserFSP test@TestNGAD.local"/>
     </Map>
   </Attributes>
 </ResourceObject>

And, following XPATH I tried to ignore the order of the elements but still it is not working, can someone help me out here? I referred the discussion here https://github.com/xmlunit/xmlunit/issues/123


diff = DiffBuilder
                    .compare(control)
                    .withTest(test)
                    .checkForSimilar().checkForIdentical() //ignore the order of 'elements` but check they are identical
                    .normalizeWhitespace()
                    .ignoreComments()
                    .ignoreWhitespace()
                    //.ignoreElementContentWhitespace()
                    .withNodeMatcher(new DefaultNodeMatcher(ElementSelectors.conditionalBuilder()
                            .whenElementIsNamed("Attributes").thenUse(ElementSelectors.byXPath("./Map/entry[@key]", ElementSelectors.byNameAndText))
                            .elseUse(ElementSelectors.byName)
                            .build()))
                    .build();

回答1:


I assume you wish to match entry elements inside of Map element, and you wish to match them them by their key attribute, thus ignoring the order?

If so, I suggest you to write your NodeMatcher like this:

    .withNodeMatcher(new DefaultNodeMatcher(ElementSelectors.conditionalBuilder()
    .whenElementIsNamed("entry").thenUse(
                        (e1, e2) -> StringUtils.equals(e1.getAttribute("key"), e2.getAttribute("key")))
                        .elseUse(byName)
                        .build()



来源:https://stackoverflow.com/questions/61184818/xmlunit-2-0-xpath-doesnt-ignoring-the-xml-node-order

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!