Stylus Studio XML Editor

Table of contents

Appendices

4.8 Controlling the Creation & Use of Derived Types

Controlling the Creation & Use of Derived Types

ref27So far, we have been able to derive new types and use them in instance documents without any restraints. In reality, schema authors will sometimes want to control derivations of particular types, and the use of derived types in instances.

XML Schema provides a couple of mechanisms that control the derivation of types. One of these mechanisms allows the schema author to specify that for a particular complex type, new types may not be derived from it, either (a) by restriction, (b) by extension, or (c) at all. To illustrate, suppose we want to prevent any derivation of the Address type by restriction because we intend for it only to be used as the base for extended types such as USAddress and UKAddress. To prevent any such derivations, we slightly modify the original definition of Address as follows:

NOTE: 

Preventing Derivations by Restriction of Address

<complexType name="Address" final="restriction">
  <sequence>
    <element name="name"   type="string"/>
    <element name="street" type="string"/>
    <element name="city"   type="string"/>
  </sequence>
</complexType>

ref43The restriction value of the final attribute prevents derivations by restriction. Preventing derivations at all, or by extension, are indicated by the values #all and extension respectively. Moreover, there exists an optional finalDefault attribute on the schema element whose value can be one of the values allowed for the final attribute. The effect of specifying the finalDefault attribute is equivalent to specifying a final attribute on every type definition and element declaration in the schema.

ref44Another type-derivation mechanism controls which facets can be applied in the derivation of a new simple type. When a simple type is defined, the fixed attribute may be applied to any of its facets to prevent a derivation of that type from modifying the value of the fixed facets. For example, we can define a Postcode simple type as:

NOTE: 

Preventing Changes to Simple Type Facets

<simpleType name="Postcode">
  <restriction base="string">
    <length value="7" fixed="true"/>
  </restriction>
</simpleType>

Once this simple type has been defined, we can derive a new postal code type in which we apply a facet not fixed in the base definition, for example:

NOTE: 

Legal Derivation from Postcode

<simpleType name="UKPostcode">
  <restriction base="ipo:Postcode">
    <pattern value="[A-Z]{2}\d\s\d[A-Z]{2}"/>
  </restriction>
</simpleType>

However, we cannot derive a new postal code in which we re-apply any facet that was fixed in the base definition:

NOTE: 

Illegal Derivation from Postcode

<simpleType name="UKPostcode">
  <restriction base="ipo:Postcode">
    <pattern value="[A-Z]{2}\d\d[A-Z]{2}"/>
    <!-- illegal attempt to modify facet fixed in base type -->
    <length value="6" fixed="true"/>
  </restriction>
</simpleType>

ref28In addition to the mechanisms that control type derivations, XML Schema provides a mechanism that controls which derivations and substitution groups may be used in instance documents. In [Using Derived Types in Instance Documents], we described how the derived types, USAddress and UKAddress, could be used by the shipTo and billTo elements in instance documents. These derived types can replace the content model provided by the Address type because they are derived from the Address type. However, replacement by derived types can be controlled using the block attribute in a type definition. For example, if we want to block any derivation-by-restriction from being used in place of Address (perhaps for the same reason we defined Address with final="restriction"), we can modify the original definition of Address as follows:

NOTE: 

Preventing Derivations by Restriction of Address in the Instance

<complexType name="Address" block="restriction">
  <sequence>
    <element name="name"   type="string"/>
    <element name="street" type="string"/>
    <element name="city"   type="string"/>
  </sequence>
</complexType>

ref42The restriction value on the block attribute prevents derivations-by-restriction from replacing Address in an instance. However, it would not prevent UKAddress and USAddress from replacing Address because they were derived by extension. Preventing replacement by derivations at all, or by derivations-by-extension, are indicated by the values #all and extension respectively. As with final, there exists an optional blockDefault attribute on the schema element whose value can be one of the values allowed for the block attribute. The effect of specifying the blockDefault attribute is equivalent to specifying a block attribute on every type definition and element declaration in the schema.