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