Message model objects: type inheritance
The XML Schema language allows a type definition to be based on another type definition. In this way, a hierarchy of types can be constructed.
This topic outlines the concepts of type inheritance, and highlights some important issues relating to substitution.
A full discussion of XML Schema type inheritance can be found on the World Wide Web Consortium (W3C) website, or in numerous books about XML Schema.
Restriction and extension
A type is a restriction of its base type, if elements of the derived type have a smaller range of valid values (or valid type members) than elements of the base type.
-
For example, a restriction of a complex type might reduce the number of occurrences of one of its type members, or might omit that type member completely.
-
Similarly, a restriction of a simple type might lower the Max Inclusive facet value, or raise the Min Inclusive facet value.
A type is an extension of its base type if elements of the derived type have a wider range of valid values (or valid type members) than elements of the base type.
-
For example, an extension of a complex type might add type members that were not present in the base type, or might allow a type member to repeat.
-
Similarly, an extension of a simple type must always be a complex type that is based on the simple type; you cannot extend a simple type by widening its range of valid values.
Special rules apply to the derivation of simple types. A simple type cannot extend another simple type. This ensures that restrictions that are imposed by a simple type cannot be removed by deriving another simple type from it.
However, a complex type can extend a simple type. This does not affect the range of valid values of the simple type, but it does allow attributes to be added. The result of extending a simple type is always a complex type that contains zero or more attributes.
Controlling type inheritance
The final attribute on a complex type can take three values, with the following effects:
- restriction: It is not valid to derive another complex type from this type by restriction.
- extension: It is not valid to derive another complex type from this type by extension.
- all: It is not valid to derive another complex type from this type by either extension or restriction
Type inheritance and substitution
XML Schema provides two different substitution mechanisms, both of which use type inheritance information to allow or disallow substitutions.
Element substitution is controlled by substitution groups, and element substitution can be blocked or allowed for extension and restriction by settings on either the element itself or the type of the element.
Type substitution allows the type of the element to be defined within the instance document, using the xsi:type attribute on the element, so that the real type of the element is not known until the element has been partly parsed. This mechanism can also be blocked or allowed based on the derivation method of the types involved.