Skip to content

XMLUnit 2 API to specify XSD for similarity for ATTR_VALUE_EXPLICITLY_SPECIFIED #88

@tarilabs

Description

@tarilabs

Hello, I'm trying to compare two XML documents for which "control" implies a default attribute, while "test" make the attribute explicit with default value.

I have noted the DifferenceEvaluators.Default specifies for ComparisonType.ATTR_VALUE_EXPLICITLY_SPECIFIED the outcome is indeed ComparisonResult.SIMILAR as I would expected.

However I'm unable to obtain the desired output while using XMLUnit 2 API, as it does not appear to be a way to specify the XSD schema where this definition (attribute is optional with default value) is actually specified.

This is my XSD:

<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://www.example.org/schema/" targetNamespace="http://www.example.org/schema/">
    <element name="myelement">
      <complexType>
        <attribute name="myAttribute" type="string" default="myDefaultValue" use="optional"></attribute>
      </complexType>
    </element>
</schema>

This is "control" xml which implies a default attribute:

<?xml version="1.0" encoding="UTF-8"?>
<myelement  xmlns="http://www.example.org/schema/" />

This is "test" xml where the attribute is explicit with default value:

<?xml version="1.0" encoding="UTF-8"?>
<myelement  myAttribute="myDefaultValue"
            xmlns="http://www.example.org/schema/" />

I would have expected if I XMLUnit Diff-them, they should be "similar", but result actually is "different".

Here is snippet my JUnit test inlined for convenience:

@Test
public void testInline() {
    final String XSD = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + 
            "<schema xmlns=\"http://www.w3.org/2001/XMLSchema\" xmlns:tns=\"http://www.example.org/schema/\" targetNamespace=\"http://www.example.org/schema/\">\n" + 
            "    <element name=\"myelement\">\n" + 
            "      <complexType>\n" +
            "        <attribute name=\"myAttribute\" type=\"string\" default=\"myDefaultValue\" use=\"optional\"></attribute>\n" + 
            "      </complexType>\n" + 
            "    </element>\n" + 
            "</schema>"; 

    final String explicitXML = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + 
            "<myelement  myAttribute=\"myDefaultValue\"\n" + 
            "            xmlns=\"http://www.example.org/schema/\" />";

    final String implicitXML = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + 
            "<myelement  xmlns=\"http://www.example.org/schema/\" />";

    validateAgainstXSD(XSD, explicitXML);
    validateAgainstXSD(XSD, implicitXML);

    Diff myDiff = DiffBuilder.compare( Input.fromString( implicitXML ).build() )
            .withTest( Input.fromString( explicitXML ).build() )
            .ignoreWhitespace()
            .checkForSimilar()
            .build()
            ;
    for (Difference diff : myDiff.getDifferences()) {
        System.out.println(diff);
    }
    Assert.assertFalse(myDiff.toString(), myDiff.hasDifferences());


}

private void validateAgainstXSD(final String xsdContent, final String xmlContent) {
    Validator v = Validator.forLanguage(Languages.W3C_XML_SCHEMA_NS_URI);
    v.setSchemaSource( Input.fromString( xsdContent ).build() );
    ValidationResult r = v.validateInstance( Input.fromString( xmlContent ).build()  );
    if (!r.isValid()) {
        for (ValidationProblem p : r.getProblems()) {
            System.err.println(p);
        }
    }
    assertTrue(r.isValid());
}

Both XML file validate against the XSD Schema, however test fails with result "different".

I've also checked with included JUnit test suite of XMLUnit, but the only relevant test case I found is DefaultComparisonFormatterTest.testComparisonType_ATTR_VALUE_EXPLICITLY_SPECIFIED() here: https://github.com/xmlunit/xmlunit/blob/master/xmlunit-core/src/test/java/org/xmlunit/diff/DefaultComparisonFormatterTest.java#L409
however this test is using DTD.

Is it possible to achieve the same result instead of DTD by using the XSD schema as described above, please?

ps: thank you for XMLUnit is great ! =)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions