2

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?

kjhughes
  • 106,133
  • 27
  • 181
  • 240
Gaurav_Bhide
  • 105
  • 1
  • 7

1 Answers1

2

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

kjhughes
  • 106,133
  • 27
  • 181
  • 240
  • I think you meant to say "Your expectation that your XML is INVALID is correct". – Michael Kay May 06 '20 at 15:13
  • You're right, and I was in the process of fixing when your comment came in. Thank you! – kjhughes May 06 '20 at 15:15
  • @MichaelKay: BTW, I believe less error-prone naming/semantics would have been to use `xsi:namespaceSchemaLocation` for namespace-based associations and `xsi:schemaLocation` for namespaceless associations. – kjhughes May 06 '20 at 15:25
  • Personally, I think `xsi:schemaLocation` should never be used. If you don't trust a document to be valid, why should you trust it to tell you where the schema is to be found? – Michael Kay May 06 '20 at 15:31
  • Because `xsi:schemaLocation` in practice is used as a validator-independent mechanism for XML-XSD association between cooperating, not antagonistic, parties who trust the assignment of an XSD but wish to be informed when validation failures occur given the declared assignment. – kjhughes May 06 '20 at 15:47