XML technologies / XML Schema / Building further components with object-oriented design / Controlling derivation

(Excerpt from “XML Schema” by Eric van der Vlist)

We now revisit earlier constructions and introduce new ones to explain how to control derivation of the various compositors. These constructions are essentially attributes allowing further derivation to be blocked or required. The available granularity depends on the compositor concerned.

Attributes

Attributes have no substitution groups, derivation mechanism or means of specifying their type in instance documents. No derivation controls are therefore needed. Strictly speaking, attributes can be derived indirectly by restricting their parent element or redefining attribute groups.

Elements

When discussing an xs:element, we must distinguish global definitions, local definitions and references, whose derivation behavior differs. Derivation is therefore controlled as shown in the table.

Table: Controlling element derivation

Attribute

Global element

Local element definition

Element reference

block

Yes

Yes

No

abstract

Yes

No

No

final

Yes

No

No

The block attribute

The attribute block controls type substitution in instances through the attribute xsi:type and substitution groups. This attribute contains a list of tokens separated by whitespace, with possible values “restriction”, “extension” and “substitution”, or the special value #all representing all three. Its default can be set by attribute blockDefault on document element xs:schema.

The first two values, “restriction” and “extension”, control all substitutions through xsi:type or substitution groups, blocking substitution by data types derived by restriction or extension from the declared data type. The third value, “substitution”, applies only to substitution groups and determines whether a member of a group headed by the element is allowed. Only global elements can participate in substitution groups, so this value matters only for global definitions.

Using attribute block for both type substitution and substitution groups can be misleading, particularly “restriction” and “extension”, which affect both. Consider a complex-type definition, personType, describing a person with a name, a required birth date, an optional death date and an optional description:

<xs:complexType name="personType">
   <xs:sequence>
      <xs:element ref="name"/>
      <xs:element ref="born"/>
      <xs:element ref="dead" minOccurs="0"/>
      <xs:element ref="qualification" minOccurs="0"/>
   </xs:sequence>
   <xs:attribute ref="id"/>
</xs:complexType>

By extension, we can derive a data type describing an author as a person with a book list:

<xs:complexType name="authorType">
   <xs:complexContent>
      <xs:extension base="personType">
         <xs:sequence>
            <xs:element name="book" type="xs:token" minOccurs="1" maxOccurs="unbounded"/>
         </xs:sequence>
      </xs:extension>
   </xs:complexContent>
</xs:complexType>

By restriction, we can derive a type describing a fictional character as a person without a death date but with a required description:

<xs:complexType name="characterType">
   <xs:complexContent>
      <xs:restriction base="personType">
         <xs:sequence>
            <xs:element ref="name"/>
            <xs:element ref="born"/>
            <xs:element ref="qualification"/>
         </xs:sequence>
      </xs:restriction>
   </xs:complexContent>
</xs:complexType>

Attribute block in element definitions first controls type substitution through xsi:type, controlling instance substitutions such as the following, if the element person is defined with type personType:

<person xsi:type="authorType" id="CMS" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   <name>
      Charles M. Schulz
   </name>
   <born>
      1922-11-26
   </born>
   <dead>
      2000-02-12
   </dead>
   <book>
      Auf den Hund gekommen
   </book>
</person>
<person xsi:type="characterType" id="Snoopy" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   <name>
      Snoopy
   </name>
   <born>
      1950-10-04
   </born>
   <qualification>
      extrovertierter Beagle
   </qualification>
</person>

The first substitution uses type authorType, derived by extension from personType, and can be blocked by specifying an attribute block containing the value extension. The second uses type characterType, derived by restriction from personType, and can be blocked by specifying an attribute block containing the value restriction . Both can be blocked together with an attribute block whose value is #all .

This shows that the two derivation methods affect applications quite differently. Restriction substitution has virtually no effect because content matching the restricted type must also match the original. Extension, however, allows content forbidden by the original, such as adding the element book in the example. Applications not expecting these additions are therefore more likely to break. A conservative approach is to include extension in the default for block in your schemas.

We have still seen only one side of attribute block. It has another distinct but related purpose: limiting substitution groups. To illustrate, define three member elements in a group headed by person . The third is simply a synonym of element person with the same type:

<xs:element name="person" type="personType"/>

<xs:element name="author" type="authorType" substitutionGroup="person"/>

<xs:element name="character" type="characterType" substitutionGroup="person"/>

<xs:element name="human" type="personType" substitutionGroup="person"/>

Defining this group without blocking anything allows not only block and the type substitutions above, but also element substitutions such as:

<author id="CMS">
   <name>
      Charles M. Schulz
   </name>
   <born>
      1922-11-26
   </born>
   <dead>
      2000-02-12
   </dead>
   <book>
      Auf den Hund gekommen
   </book>
</author>
<character id="PP">
   <name>
      Peppermint Patty
   </name>
   <born>
      1966-08-22
   </born>
   <qualification>
      kühn, dreist und draufgängerisch
   </qualification>
</character>
<human id="CMS.">
   <name>
      Charles M. Schulz
   </name>
   <born>
      1922-11-26
   </born>
   <dead>
      2000-02-12
   </dead>
</human>

The first substitutes a group member whose type extends the head’s type. It can be blocked with an attribute block containing the value extension. The second substitutes a member whose type restricts the head’s type. It can be blocked with an attribute block containing the value restriction. These resemble the earlier type substitutions, but the element name distinguishes the type instead of attribute xsi:type. The third is new: its type equals the head’s, and only an attribute block containing the value substitution can block it. Including this value in an attribute block blocks every element substitution, also showing that not all combinations are expressible. To block everything, define author as follows:

<xs:element name="author" type="authorType" block="#all"/>

This should not suggest that blocking type substitution necessarily blocks substitution groups. Type substitution can also be blocked in complex-type definitions, as discussed later under “Complex types”.

Final elements

Like block, final affects substitution groups, but at a different level: it constrains the schema itself rather than instance documents. final accepts a list containing restriction and extension or the special value #all. Attribute finalDefault on xs:schema sets its default. No value substitution is needed, since unlike block, final concerns substitution groups, and #all can block all substitutions and only those.

The effect of final is more radical than block: whereas block blocks the use of substitution groups, final forbids using the element as a group head. If the element person is defined with attribute final set, it cannot head a group for author, character or both.

Abstract elements

The last attribute is abstract, the opposite of block. It prevents direct use of the element in instances; a substitution group must replace it. Defining the element person as abstract forbids its use in instances. They must use a group member such as author, character or even human.

Complex types

Complex types use the same attributes as elements, but with slightly different meanings. One attribute, block, affects substitution of elements using the type, as above; the others, final and abstract, affect derivation of the complex type itself.

Blocking complex types

Applying the attribute block in complex-type definitions still blocks type substitutions for elements defined with that type. The difference is that block now concerns type substitutions rather than substitution-group element substitutions. These attributes block resemble electrical switches in series, each able to block derivation on its own circuit, as illustrated below.

For each of restriction and extension, element and type substitutions can first be disabled together with attribute block in the element definition. Type substitutions can additionally be blocked by attribute block in the complex-type definition. The value substitution in attribute block of an element definition acts as a global switch for all element substitutions, including a third circuit permitting same-type element substitutions, which cannot be blocked separately.

The schema element’s default applies at both levels. It can differ between element and complex-type definitions if they belong to different schemas and have different ancestors xs:schema. Applying the default defined on an ancestor xs:schema at both levels means it may block derivation at both, opening two switches. To override a nonempty default, specify an attribute block in both element and complex-type definitions.

block attribute for element and complex-type definitions

Figure: block attribute for element and complex-type definitions

Final complex types

The attribute final in a complex-type definition controls whether new complex types may derive from it by restriction or extension.

Unlike attribute block, linked to its element-definition counterpart, attribute final in a complex-type definition applies only to that type itself. Like attribute abstract in element definitions, it acts at schema level and has no effect on instances.

Abstract complex types

The attribute abstract in complex-type definitions operates on instance documents. When an abstract type defines elements, its type must be replaced in instances with an attribute xsi:type.

Note carefully that abstract alone does not prohibit using a complex type in a content model. If used, however, it must be replaced in instances through xsi:type. To prohibit its use in content models, specify both attributes final and block; the complex type can then serve only as a base for derivations.

Simple types

Derivation control really is simpler for simple types: at most, they can be final. Their attribute final accepts the values list, restriction, union or the special value #all representing all three. The default is set by the schema element’s attribute finalDefault, which also applies to elements and complex types.

Simple types cannot be abstract or blocked, avoiding problems when defining attributes, where these concepts have no meaning. If needed for defining elements, create a complex type with simple content based on the simple type. This complex type can accept the attributes abstract and block.

Although final is the only derivation-control attribute for simple-type elements available on the element xs:simpleType, global definition, finer control is available through attribute fixed available on each facet, as explained in Creating simple data types.

Other components and redefinitions

Other components, such as attributes and element or attribute groups, cannot be derived directly. Attributes cannot be derived at all, and groups only through redefinition, so these cases have no derivation controls. Redefining groups through xs:redefine likewise exceeds this construction’s capabilities and cannot be controlled. Although the Recommendation is unclear, it is safer to assume attribute final of complex types also applies to their redefinitions, treated as implicit derivations.

Interpretation of attribute block is not uniform. The current view of the W3C XML Schema Working Group is in the specification errata list.

   

back to top

 

 

 

back to top