0

I have below xml data:

Control:

 <Data>

        <propertyValues>
            <propertyName>Name1</propertyName>
            <value>
                <text>
                    <value>Value1</value>
                </text>
            </value>
        </propertyValues>
        <propertyValues>
            <propertyName>Name2</propertyName>
            <value>
                <text>
                    <value>Value2</value>
                </text>
            </value>
        </propertyValues>
 </Data>

Test:

 <Data>
            <propertyValues>
                <propertyName>Name2</propertyName>
                <value>
                    <text>
                        <value>Value2</value>
                    </text>
                </value>
            </propertyValues>
            <propertyValues>
                <propertyName>Name1</propertyName>
                <value>
                    <text>
                        <value>Value1</value>
                    </text>
                </value>
            </propertyValues>
 </Data>

And I would expect these 2 documents are "same".

How can I config xmlUnit to make it work? (I'm using xmlunit 2.6.3)

Thanks

Leon

anuni
  • 889
  • 10
  • 26

1 Answers1

1

This is pretty similar to the running example of the "Selecting Nodes" part of XMLUnit's User Guide.

You need to use an ElementSelector that picks the correct propertyValues element when looking at the list of elements and then decides to compare the elements that contain the same nested text inside the only child element named propertyName. This directly translates into

ElementSelectors.conditionalBuilder()
    .whenElementIsNamed("propertyValues")
    .thenUse(ElementSelectors.byXPath("./propertyName", ElementSelectors.byNameAndText))
   ...

and then you need to add whatever other rules are required to make the rest work. Looking at the visible rest of your example there are no ambiguous children and a simple

   ...
    .elseUse(ElementSelectors.byName)
    .build();

will do.

Stefan Bodewig
  • 3,260
  • 15
  • 22
  • btw, your sample just pickup the 'propertyName' child node, how about the value node? – anuni Sep 05 '19 at 10:07
  • should it be: .thenUse(ElementSelectors.byXPath("./propertyName|./value/text/value", ElementSelectors.byNameAndText)) ? – anuni Sep 05 '19 at 10:25
  • It picks only `propertyName` because I assumed this is what identifies the `propertyValues` that need to be compared. If you need `vaues` as well to make XMLUnit pick the correct pairs then you need to adjust. This is really **only** about what it takes to make XMLUnit pick the right `propertyValues` - for all other `byName` is used and seems to be sufficient. – Stefan Bodewig Sep 07 '19 at 09:13
  • I just found that I can use ElementSelectors.and() to combine selector for propertyName and selector for value, thanks a lot! – anuni Sep 11 '19 at 07:09