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"
  <include schemaLocation=
      <element name="doc" type="Body"/>
      <complexType name="Body">
          <element name="body">
              <attribute name="bodyText" type="string"/>
      <complexType name="HTMLBodyCT">
          <element name="HTMLBody">
                  <element name="h1" type="string" content="text"/>

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=
      xsi:type="HTMLBodyCT" >
          <h1>"Hello, world"</h1>

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.


Schemas enable you to associate data types with attributes, create your own data types, and define the structure of your document using well-formed XML. Schemas are used to define elements that are associated with a name and a type. The type is either a data type or one or more attributes or elements. Elements can be grouped together in group elements, and attributes can be grouped together in attributeGroup elements. The group and attributeGroup elements can either be used locally or they can have document level scope.

Schemas provide many advantages over DTDs; namely, they use namespaces, they utilize a wide range of data types, and they are written in XML. It's likely that schemas will gradually replace DTDs over the next few years. Schemas will be discussed in more detail when we look at BizTalk in Chapter 8 and the Document Object Model in Chapter 11.