Compare XML using xmlunit when order of Attributes is different is not working
Asked Answered
R

1

0

I have 2 xmls : Basically two XSD schemas

<?xml version="1.0" encoding="UTF-8"?>

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <xsd:element name="my_export_file">
    <xsd:complexType>
      <xsd:sequence minOccurs="0" maxOccurs="unbounded">
        <xsd:element name="row">
          <xsd:complexType>
            <xsd:attributeGroup ref="rowattr" />
          </xsd:complexType>
        </xsd:element>
      </xsd:sequence>
      <xsd:attributeGroup ref="docelattr" />
    </xsd:complexType>
  </xsd:element>
  <xsd:attributeGroup name="rowattr">
    <xsd:attribute name="subject_level_ind" type="Str.1" use="optional" />
    **<xsd:attribute name="object_level_ind" type="Str.1" use="optional" />**
    <xsd:attribute name="src_system_id" type="Str.80" use="required" />   
  </xsd:attributeGroup>
  <xsd:attributeGroup name="docelattr">
    <xsd:attribute name="reporting_date" type="xsd:string" />
    <xsd:attribute name="interface_type" type="xsd:string" /> 
  </xsd:attributeGroup>
  <xsd:simpleType name="Str.1">
    <xsd:restriction base="xsd:string">
      <xsd:maxLength value="1" />
    </xsd:restriction>
  </xsd:simpleType>
    <xsd:simpleType name="Str.80">
    <xsd:restriction base="xsd:string">
      <xsd:maxLength value="80" />
    </xsd:restriction>
  </xsd:simpleType>
</xsd:schema>

and

<?xml version="1.0" encoding="UTF-8"?>

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <xsd:element name="my_export_file">
    <xsd:complexType>
      <xsd:sequence minOccurs="0" maxOccurs="unbounded">
        <xsd:element name="row">
          <xsd:complexType>
            <xsd:attributeGroup ref="rowattr" />
          </xsd:complexType>
        </xsd:element>
      </xsd:sequence>
      <xsd:attributeGroup ref="docelattr" />
    </xsd:complexType>
  </xsd:element>
  <xsd:attributeGroup name="rowattr">
    <xsd:attribute name="subject_level_ind" type="Str.1" use="optional" />
    <xsd:attribute name="src_system_id" type="Str.80" use="required" />
    **<xsd:attribute name="object_level_ind" type="Str.1" use="optional" />**       
  </xsd:attributeGroup>
  <xsd:attributeGroup name="docelattr">
    <xsd:attribute name="reporting_date" type="xsd:string" />
    <xsd:attribute name="interface_type" type="xsd:string" /> 
  </xsd:attributeGroup>
  <xsd:simpleType name="Str.1">
    <xsd:restriction base="xsd:string">
      <xsd:maxLength value="1" />
    </xsd:restriction>
  </xsd:simpleType>
    <xsd:simpleType name="Str.80">
    <xsd:restriction base="xsd:string">
      <xsd:maxLength value="80" />
    </xsd:restriction>
  </xsd:simpleType>
</xsd:schema>

I want to compare those and give differences. My code works fine but the only issue is if order of attributes is different it doesn't treat them as "SIMILAR". As you can see in the example, my xmls are same with just one change - order of object_level_ind is different. I want my code to not return this difference.

Code

var fis1 = new FileReader("C:\\Users\\test1.xsd ");
 var fis2 = new FileReader("C:\\Users\\test2.xsd");
 XMLUnit.setIgnoreWhitespace(true);
 XMLUnit.setIgnoreAttributeOrder(true);
 DetailedDiff diff =  new DetailedDiff(XMLUnit.compareXML(fis1,fis2));
 diff.overrideElementQualifier(new ElementNameAndTextQualifier());
 List<?> allDifferences = diff.getAllDifferences();
 System.out.println(allDifferences);

I Also tried:

DifferenceEvaluator evaluator = DifferenceEvaluators
                .downgradeDifferencesToEqual(ComparisonType.CHILD_NODELIST_SEQUENCE);

        Diff diff = DiffBuilder.compare(fis1)
                .withTest(fis2).ignoreComments()
                .ignoreWhitespace()
                .withNodeMatcher(new DefaultNodeMatcher(ElementSelectors.byName))
                .withDifferenceEvaluator(evaluator)
                .checkForSimilar()
                .build();
        System.out.println("Differences: " + diff);

I Also tried solution given in comparing two xmls using xmlunit ignorng their order

But for my xml it gives:

identical: false
similar  : false

Please let me know if any pointers.

Best Regards,

Abhi

Rickety answered 19/12, 2022 at 9:33 Comment(0)
T
0

This is happening because setIgnoteAttributeOrder ignores the order of the attributes of a node/element and not the actual order of node/element in the XML document. So, while <xsd:attribute name="object_level_ind" type="Str.1" use="optional" /> and <xsd:attribute use="optional" name="object_level_ind" type="Str.1" /> are considered same, the order of the elements is not ignored.

This answer here may have more details on how to compare ignoring element order - Compare two XML strings ignoring element order

Ticktacktoe answered 19/12, 2022 at 9:55 Comment(2)
It didn't work for me. I tried - Diff diff = DiffBuilder.compare(Input.fromFile(fis1)) .withTest(Input.fromFile(fis2)) .ignoreWhitespace() .normalizeWhitespace() .withNodeMatcher(new DefaultNodeMatcher(ElementSelectors.byNameAndText)) .checkForSimilar() .build(); But it gave me result as - Expected attribute value 'object_level_ind' but was 'src_system_id'Rickety
: setIgnoteAttributeOrder is also one of my requirement so that won't do a harm in comparison.Rickety

© 2022 - 2024 — McMap. All rights reserved.