Skip to content

XMLUnit comparison is not working if namespace details are included in the root element #222

@sureshthota

Description

@sureshthota

I am trying to compare two XMLs which has the same data but in different order. When root element has no namespace and other attributes the comparison works fine. It says both are identical. However when I include namespace details it is complaining about the elements which are not in order. Please note that I tried to downgrade all the relevant differences. But it did not work.

Below are code and XML details along with the error.

XML 1:


<root xmlns:core="http://www.testeng.test.com/core"
  xmlns="http://www.company.test.com/schemas/atlas"
  xmlns:pdt="http://www.testeng.test.com/people/UserDescriptives"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.company.test.com/schemas/atlas MyAppV4.xsd http://www.testeng.test.com/people/UserDescriptives UserDescriptiveTypes-v1-1a.xsd http://www.testeng.test.com/core CommonSimpleTypes-v1-3.xsd">

  <A1>
      <B1>
        <Keys>
          <Value1 Key="1">Example1</Value1>
          <Value2 Key="2">Example2</Value2>
          <Value3 Key="3">Example3</Value3>
        </Keys>

        <Keys>
          <Value1 Key="1">Example5</Value1>
          <Value2 Key="2">Example8</Value2>
          <Value3 Key="3">Example6</Value3>
        </Keys>
      </B1>
    </A1>
</root>


XML 2:

<root xmlns:core="http://www.testeng.test.com/core"
  xmlns="http://www.company.test.com/schemas/atlas"
  xmlns:pdt="http://www.testeng.test.com/people/UserDescriptives"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.company.test.com/schemas/atlas MyAppV4.xsd http://www.testeng.test.com/people/UserDescriptives UserDescriptiveTypes-v1-1a.xsd http://www.testeng.test.com/core CommonSimpleTypes-v1-3.xsd">

  <A1>
    <B1>
      <Keys xmlns:Data="foo">
        <Value1 Key="1">Example5</Value1>
        <Value2 Key="2">Example8</Value2>
        <Value3 Key="3">Example6</Value3>
      </Keys>
      <Keys xmlns:Data="foo">
        <Value1 Key="1">Example1</Value1>
        <Value2 Key="2">Example2</Value2>
        <Value3 Key="3">Example3</Value3>
      </Keys>
    </B1>
  </A1>
</root>

Code:

public class CompareXmls {


    public static void main(String[] args) {
        DifferenceEvaluator evaluator = DifferenceEvaluators.chain(DifferenceEvaluators.Default, new MyEvaluator(),
            DifferenceEvaluators.downgradeDifferencesToEqual(ComparisonType.CHILD_NODELIST_SEQUENCE),
            DifferenceEvaluators.downgradeDifferencesToEqual(ComparisonType.NAMESPACE_PREFIX),
            DifferenceEvaluators.downgradeDifferencesToEqual(ComparisonType.NO_NAMESPACE_SCHEMA_LOCATION),
            DifferenceEvaluators.downgradeDifferencesToEqual(ComparisonType.NAMESPACE_URI),
            DifferenceEvaluators.downgradeDifferencesToEqual(ComparisonType.SCHEMA_LOCATION),
            DifferenceEvaluators.downgradeDifferencesToEqual(ComparisonType.DOCTYPE_NAME),
            DifferenceEvaluators.downgradeDifferencesToEqual(ComparisonType.DOCTYPE_PUBLIC_ID),
            DifferenceEvaluators.downgradeDifferencesToEqual(ComparisonType.DOCTYPE_SYSTEM_ID));

        InputStream resourceAsStream1 = TestXmlComparison.class.getClassLoader().getResourceAsStream("X1.xml");
        InputStream resourceAsStream2 = TestXmlComparison.class.getClassLoader().getResourceAsStream("X2.xml");

        Diff diff = DiffBuilder.compare(Input.from(resourceAsStream1))
            .withTest(Input.from(resourceAsStream2))
            .ignoreComments()
            .ignoreWhitespace()
            .normalizeWhitespace()
            .checkForSimilar()
            .withDifferenceEvaluator(evaluator)
            .withNodeMatcher(new DefaultNodeMatcher(ElementSelectors.conditionalBuilder()
                .whenElementIsNamed("Keys")
                .thenUse(ElementSelectors.byXPath("./Value1", ElementSelectors.byNameAndText))
                .elseUse(ElementSelectors.byName)
                .build()
             )).build()
            ;
        System.err.println(diff);
    }
}

Error/Differences:

Expected text value 'Example1' but was 'Example5' - comparing <Value1 ...>Example1</Value1> at /root[1]/A1[1]/B1[1]/Keys[1]/Value1[1]/text()[1] to <Value1 ...>Example5</Value1> at /root[1]/A1[1]/B1[1]/Keys[1]/Value1[1]/text()[1]

As I mentioned earlier, If I just use <root> with out namespace details I am getting response as Identical.

Please help.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions