【问题标题】:XMLUnit-2.0 xpath doesn't ignoring the XML node orderXMLUnit-2.0 xpath 不会忽略 XML 节点顺序
【发布时间】:2020-07-25 19:56:20
【问题描述】:

我有一个如下的 XML

<?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>

并且,在 XPATH 之后,我试图忽略元素的顺序,但仍然无法正常工作,有人可以帮我吗?我在这里提到了讨论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();

【问题讨论】:

    标签: java xml xmlunit xmlunit-2


    【解决方案1】:

    我假设您希望匹配 Map 元素内的 entry 元素,并且希望通过它们的 key 匹配 它们 属性,从而忽略顺序?

    如果是这样,我建议你这样编写 NodeMatcher:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-08-16
      • 1970-01-01
      • 1970-01-01
      • 2020-01-19
      • 1970-01-01
      • 2014-03-09
      • 1970-01-01
      相关资源
      最近更新 更多