Overriding Data Types
Using namespaces, we have managed to build a schema from other schemas and include data types from other schemas. In both of these cases, the instance document uses only the top-level schema. You can also declare an element as being one particular data type and then override that data type in the instance document. Consider the following top-level schema:
<schema targetNamespace="http://www.northwindtraders.com/Message"
xmlns:northwindMessage="http://www.northwindtraders.com/Message"
xmlns="http://www.w3.org/1999/XMLSchema">
<include schemaLocation=
"http://www.northwindtraders.com/HTMLMessage.xsd"/>
<element name="doc" type="Body"/>
<complexType name="Body">
<element name="body">
<attribute name="bodyText" type="string"/>
</element>
</complexType>
<complexType name="HTMLBodyCT">
<element name="HTMLBody">
<complexType>
<element name="h1" type="string" content="text"/>
</complexType>
</element>
</complexType>
</schema>
|
This schema has defined the doc element as being a Body data type, and the doc element will contain a body child element that has a bodyText attribute. Now suppose you also want to be able to create messages from other body data types, such as the HTMLBodyCT data type defined in the schema. You could do this by creating a group element with choices.
Another option is to declare the schema as above and then substitute the HTMLBodyCT data type for the Body data type in the instance document. To do this, you will need to reference the schema instance namespace in the instance document. To use the HTMLBodyCT data type, you would need to create an instance document such as this:
<?xml version="1.0"?>
<northwindMessage:doc xmlns:northwindMessage=
"http://www.northwindtraders.com/Message"
xmlns:xsi="http://www.w3.org/1999/XMLSchema/instance"
xsi:type="HTMLBodyCT" >
<HTMLBody>
<h1>"Hello, world"</h1>
</HTMLBody>
</northwindMessage:doc>
|
In this example, you have used the xsi:type attribute to reference a type defined in the schema (HTMLBodyCT). The xsi:type is part of the schema instance namespace and is used to override an element's type with another type that is defined in the schema. In this example, you have now redefined the doc element as being of HTMLBodyCT data type instead of a Body data type. You could also have defined the HTMLBodyCT data type in a separate schema and used the include element in the top-level schema.