shibendra chakraborty
shibendra chakraborty

Reputation: 131

XSD Validation Catching ALL Errors within each complexType elemnt - FAILS to catch all

My objective is to catch All error in XSD validation for each and every "complexType" in JAVA - But it is failing when xml has more than one error under a complexType element.

I am using the Example code from - https://www.baeldung.com/java-validate-xml-xsd

XSD used -

<?xml version="1.0" encoding="UTF-8" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="individual">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="name" type="xs:string" />
                <xs:element name="address">
                    <xs:complexType>
                        <xs:sequence>
                            <xs:element name="zip" type="xs:positiveInteger" />
                            <xs:element name="city" type="xs:string" />
                        </xs:sequence>
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

XML used with errors introduced -

<?xml version="1.0" encoding="UTF-8" ?>
<individual>
    <name>Baeldung</name>
    <age></age> <!-- error1 -->
    <salary></salary> <!-- error2 -->
    <address>
        <zip>00001</zip>
        <state></state> <!-- error3 -->
        <city>New York</city>
        <country></country> <!-- error4 -->
    </address>
</individual>

java code has the implementation of ErrorHandler from the above link .

public class XmlErrorHandler implements ErrorHandler ....

Now, as we see, in XML there are 4 new elements under 2 complexTypes. The Validation list reports only has one error each from complexTypes. It cannot catch all in ErrorHandler implementation.

list from Errorhandler implementation - Only 2 errors out of 4 !!

cvc-complex-type.2.4.a: Invalid content was found starting with element 'age'. One of '{address}' is expected.

cvc-complex-type.2.4.a: Invalid content was found starting with element 'state'. One of '{city}' is expected.

Can anyone help me? How to get all errors from each complexType fixed?

Thanks.

Upvotes: 2

Views: 44

Answers (1)

Michael Kay
Michael Kay

Reputation: 163625

Recovery from errors when parsing content against a grammar is a difficult art. Compare with parsing Java. Given input like

String s := "10" - 3f4 ;

how many errors are there? Perhaps "-" should have been "+"? Perhaps "10" should have been 10 without the quotes? Perhaps "3f4" should have been "3e4", or perhaps it should have been "3+4"? It's like the matchstick puzzles: what's the minimum number of matchsticks you have to move to get from a wrong arrangement to a valid arrangement? You can regard each of those moves as an error, and you could ask the processor to report the minimum number of changes needed to turn what's written into something that's valid. But parsers aren't usually that smart, and people don't expect them to be. Parsers report the first thing they see that isn't a valid continuation (here, the "-" sign), and then they skip forward to a point where they can be reasonably sure they know where they are - in Java, that will generally be the semicolon. If you don't do that, you risk reporting spurious errors because you've guessed wrong about the true nature of the first error, and users get frustrated because the subsequent errors make no sense.

Upvotes: 1

Related Questions