Search code examples
xmleclipsexsdxsd-validationxml-validation

Why is this XML valid against this XSD?


I am new to XML and now working on parsing XML file. While validating XML file, we are receiving an error:

Invalid content was found starting with element 'nxce:element'. No child element is expected at this point.

The XSD used choice indicator with 4 elements. XML file has entries for all 4 elements hence we believe that the error is occurred because of wrong use of choice indicator. The actual XML is very complex, so to check working of choice indicator and to reproduce same error with simpler file, I developed a XML file and an XSD file. I was expecting error when I validated XML file, but XML file was validated without any error.

XML File

<?xml version="1.0" encoding="UTF-8"?>
<employee xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="file:///EmployeeSchema.xsd">
<emp>
    <empid>3</empid>
    <name>Name</name>
    <address>
        <building>Building</building>
        <pincode>Pincode</pincode>
        <street>Street</street>
        <area>Area</area>
    </address>
</emp>
</employee>

XSD File

<?xml version="1.0" encoding="UTF-8" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="employee">
<xs:complexType>
    <xs:sequence>
        <xs:element name="emp">
            <xs:complexType>
                <xs:sequence>
                    <xs:element name="empid" type="xs:string"/>
                    <xs:element name="name" type="xs:string"/>
                    <xs:element name="address">
                        <xs:complexType>
                            <xs:choice>
                                <xs:sequence>
                                    <xs:element name="street" type="xs:string" />
                                    <xs:element name="area" type="xs:string" />
                                </xs:sequence>
                                <xs:sequence>
                                    <xs:element name="building" type="xs:string" />
                                    <xs:element name="pincode" type="xs:string" />
                                </xs:sequence>
                            </xs:choice>    
                        </xs:complexType>
                    </xs:element>
                </xs:sequence>      
        </xs:complexType>   
    </xs:element>
</xs:sequence>  
</xs:complexType>
</xs:element>
</xs:schema>

As the choice indicator specifies that either one child element or another can occur, I was expecting to get error as in address tag, I used all 4 elements. But XML was validated without any error in eclipse. Could you please let me know, why I am not receiving error while validating XML?


Solution

  • Your expectation that your XML should be invalid against your XSD is correct.

    Your XML is likely not even being validated against your intended XSD because your XML specifies

    xsi:schemaLocation="file:///EmployeeSchema.xsd"
    

    where it should specify:

    xsi:noNamespaceSchemaLocation="file:///EmployeeSchema.xsd"
    

    Note that xsi:schemaLocation takes namespace-URL pairs, not just a URL to the XSD, to locate XSDs on a per namespace basis. Since your XML uses no namespaces, use xsi:noNamespaceSchemaLocation as shown above rather than xsi:schemaLocation.

    See also