XSD Tutorials - Herong's Tutorial Examples - Version 5.11, by Dr. Herong Yang
Overriding the Base Datatype - Errors
This section describes a tutorial example on incorrect use of 'xsi:type' when overriding element's base datatype.
If you don't follow those rules mentioned in the previous section when overriding the base datatype of a element with a derived datatype, you will get validation errors. Here is a sample XML document, directory_error.xml, that uses "xsi:type" incorrectly:
<?xml version="1.0"?> <directory xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <!-- directory_error.xml - Copyright (c) 2013, HerongYang.com, All Rights Reserved. --> <!-- error: "phoneContactType" requires "phone" --> <contact xsi:type="phoneContactType"> <name>Herong Yang</name> </contact> <!-- error: the base datatype takes "name" only --> <contact> <name>Traffic Hotline</name> <phone>1-866-MY-TRAFC</phone> </contact> <!-- error: "webContactType" is not derived from "contactType" --> <contact xsi:type="webContactType"> <name>Search Engine</name> <url>http://www.google.com</url> </contact> </directory>
Here are the validation errors you will get:
>java XsdSchemaValidator directory.xsd directory_error.xml Error: Line number: 5 Column number: 12 Message: cvc-complex-type.2.4.b: The content of element 'contact' is not complete. One of '{phone}' is expected. Error: Line number: 8 Column number: 10 Message: cvc-complex-type.2.4.d: Invalid content was found starting with element 'phone'. No child element is expected at this point. Error: Line number: 10 Column number: 37 Message: cvc-elt.4.3: Type 'webContactType' is not validly derived from the type definition, 'contactType', of element 'contact'. Failed with errors: 3
Last update: 2013.
Table of Contents
XML Editor and Schema Processor - XMLPad
Java API for XML Processing - JAXP
JAXP - XML Schema (XSD) Validation
Xerces2 Java Parser - Java API of XML Parsers
Introduction of XSD Built-in Datatypes
"string" and Its Derived Datatypes
"decimal" and Its Derived Datatypes
"dateTime" and Its Related Datatypes
Miscellaneous Built-in Datatypes
Facets, Constraining Facets and Restriction Datatypes
"simpleType" - Defining Your Own Simple Datatypes
Identity-Constraints: unique, key and keyref
Assertion as Custom Validation Rules
XML Schema Location and Namespace in XML Documents
►Overriding Element Types in XML Documents
Overriding Element Type to Empty Content - nillable
Overriding Element Type to Empty Content - xsi:nil
Defining a Derived Datatype from a Base Datatype
Overriding the Base Datatype - xsi:type
►Overriding the Base Datatype - Errors