XML technologies / XML Schema / Building further components with object-oriented design / Substitution groups

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

A vocabulary often needs to accept very different content models. We can try one generic element name or define a schema intelligent enough to handle the possible model. Since consistent declaration prevents several different models for one element, we must either use attributes xsi:type in instances or define a model broad enough to cover every possibility, probably also accepting unwanted combinations.

The simplest way to handle different types in W3C XML Schema is to give each case a different element name. We have seen that the compositor xs:choice outside a group lets a node accept an element chosen from a list, but the list is fixed in the complex-type definition. It cannot be extended under the rules for complex-type extension. Substitution groups provide a flexible way to construct and extend a compositor xs:choice outside a group from individual elements or references. More simply, they are lists of interchangeable elements in documents. One important limitation: substitution groups apply only to global elements.

Substitution groups can be seen as extensible element groups. First, let us revisit traditional element groups to highlight the difference. The Recommendation is particularly unclear on extending element groups and restricting substitution groups, so I use a conservative interpretation intended to avoid interoperability problems. The various interpretations are discussed at the chapter’s end.

Using a traditional group

Return to defining a name. Universal names are among the most controversial standardization topics and make useful examples. Instead of playing with data types, we can use different element names. A name can then be a simple name such as

<simple-name>
   Snoopy
</simple-name>

or a full name such as

<full-name>
   <last>
      Schulz
   </last>
   <first>
      Charles
   </first>
   <middle>
      M.
   </middle>
</full-name>

We already know how to define a flexible schema matching both documents. We can create a reusable group with a compositor xs:choice allowing either element wherever a name is required. The steps are to define both elements, full-name and simple- name, create a group, and use it to define elements author and character:

<xs:element name="full-name">
   <xs:complexType>
      <xs:all>
         <xs:element name="first" type="string32" minOccurs="0"/>
         <xs:element name="middle" type="string32" minOccurs="0"/>
         <xs:element name="last" type="string32"/>
      </xs:all>
   </xs:complexType>
</xs:element>
<xs:element name="simple-name" type="string32"/>
<xs:group name="name">
   <xs:choice>
      <xs:element ref="simple-name"/>
      <xs:element ref="full-name"/>
   </xs:choice>
</xs:group>
<xs:element name="author">
   <xs:complexType>
      <xs:sequence>
         <xs:group ref="name"/>
         <xs:element ref="born"/>
         <xs:element ref="dead" minOccurs="0"/>
      </xs:sequence>
      <xs:attribute ref="id"/>
   </xs:complexType>
</xs:element>
<xs:element name="character">
   <xs:complexType>
      <xs:sequence>
         <xs:group ref="name"/>
         <xs:element ref="born"/>
         <xs:element ref="qualification"/>
      </xs:sequence>
      <xs:attribute ref="id"/>
   </xs:complexType>
</xs:element>

Here we can use xs:all because the elements concerned are isolated inside element full-name. Note that xs:all does not make order irrelevant; it merely validates all combinations. Here,

<full-name>
   <first>
      Eric
   </first>
   <last>
      van der Vlist
   </last>
</full-name>

or

<full-name>
   <last>
      van der Vlist
   </last>
   <first>
      Eric
   </first>
</full-name>

may express whether I prefer “Eric van der Vlist” or “van der Vlist Eric”. Applications can readily access full-name components, but those requiring a full-name must respect document order.

Substitution groups

Using substitution groups

How can substitution groups define the same model? First define an element from which both full-name and simple-name can derive. One is a simple type, the other a complex type with complex content; no type can be extended into both. We must start with the universal type accepting all content. This special type, xs:anyType, is also the default when no type is specified. We can define a generic element name with no type definition, making it as open as possible:

<xs:element name="name"/>

This becomes the substitution group’s head. Without changing it, other elements can declare themselves usable wherever the schema references the head. These are group members, whose only constraint is that their types validly derive from the head’s type. Each interchangeable element declares this through an attribute substitutionGroup referencing the head, for example:

<xs:element name="simple-name" type="string32" substitutionGroup="name"/>
<xs:element name="full-name" substitutionGroup="name">
   <xs:complexType>
      <xs:all>
         <xs:element name="first" type="string32" minOccurs="0"/>
         <xs:element name="middle" type="string32" minOccurs="0"/>
         <xs:element name="last" type="string32"/>
      </xs:all>
   </xs:complexType>
</xs:element>

These declarations allow both elements wherever the schema names their head, for example in the definitions of elements character and author :

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

Abstract elements

As written, the schema also allows the head in instances. Since our head accepts arbitrary content, that is probably undesirable. We need a mechanism like the abstract types encountered for similar problems in Creating complex data types with xsi:type. We make the head abstract with attribute abstract in its definition:

<xs:element name="name" abstract="true"/>

Trees of substitution groups

Suppose our German branch defines an element composed-name resembling full-name but without subelement middle. We could add it directly to our group, but if we make element name the head, the similarities between it and element full-name remain hidden. Some applications may also want to accept either full-name or composed-name. The solution is to use full-name as head of a new group. Define the type of element full-name globally to show the explicit derivation between the elements:

<xs:complexType name="full-name-type">
   <xs:all>
      <xs:element name="first" type="string32" minOccurs="0"/>
      <xs:element name="middle" type="string32" minOccurs="0"/>
      <xs:element name="last" type="string32"/>
   </xs:all>
</xs:complexType>
<xs:element name="full-name" substitutionGroup="name" type="full-name-type"/>
<xs:element name="composed-name" substitutionGroup="full-name">
   <xs:complexType>
      <xs:complexContent>
         <xs:restriction base="full-name-type">
            <xs:all>
               <xs:element name="first" type="string32" minOccurs="0"/>
               <xs:element name="last" type="string32"/>
            </xs:all>
         </xs:restriction>
      </xs:complexContent>
   </xs:complexType>
</xs:element>
                
          

We now have two groups headed by name and full-name, and a tree of groups, since permitted substitutions for name include full-name and simple-name as well as composed-name.

Traditional declarations or substitution groups?

Comparing our solutions to the same problem, substitution groups are more extensible than traditional groups with a compositor xs:choice. An element group can be derived only by inclusion with xs:redefine, while a substitution group is extended simply by defining new possible elements. These can belong to any namespace, provided their type equals or validly derives from the head’s type. This constraint helps ensure that unexpected models do not surprise applications too much.

Another difference: deriving a model with compositor xs:choice cannot add alternatives to the choice. Substitution groups behave almost oppositely. Although the Recommendation says to validate them as choices, it leaves element order in the equivalent choice undefined. I recommend not restricting substitution groups in practice, to avoid interoperability problems between processors.

Paradoxically, one mechanism, xs:choice, can only be restricted, while substitution groups can only be extended, yet the Recommendation calls them equivalent for validation. Consider this when choosing an approach.

The table summarizes the differences. “Not recommended” means it may work with some processors but relies on a loose interpretation of the Recommendation and risks interoperability problems.

Table: Comparing element groups and substitution groups

Construction

Element groups with compositor xs:choice outside a group

Substitution groups

Definition

Centralized, using xs:group, definition, and xs:choice outside a group

Distributed across global element definitions, using attribute substitutionGroup

Constraints on alternatives

None; elements may differ completely

Element types must explicitly derive from the head’s type

Allows global elements

Yes

Yes

Allows local elements

Yes

No

Restriction to remove alternatives

Yes, with xs:redefine

Not recommended

Extension to add alternatives

Not recommended

Yes, by adding new elements with the same head

Extension to add elements to the sequence

Yes, with xs:redefine

No

Ambiguities in the Recommendation

Both extending xs:choice through group redefinition and restricting substitution groups are unclear in the Recommendation and need explanation.

Extending xs:choice through group redefinition

Return to our group defined as follows:

<xs:group name="name">
   <xs:choice>
      <xs:element ref="simple-name"/>
      <xs:element ref="full-name"/>
   </xs:choice>
</xs:group>

Nothing in the Recommendation seems explicitly to prohibit adding an alternative element through redefinition, such as:

<xs:redefine schemaLocation="foo.xsd">
   <xs:group name="name">
      <xs:choice>
         <xs:group ref="name"/>
         <xs:element ref="bar"/>
      </xs:choice>
   </xs:group>
</xs:redefine>

This would allow a new element, bar, instead of simple-name and full-name . That would be useful. However, redefinition by restriction, restricting group content, follows the same principles as complex-type restriction. We can infer that the Working Group likewise intends redefinition by extension to follow complex-type extension, which explicitly forbids adding particles to xs:choice outside a group.

Although some processors and specialists support this construction, I advise against it: it appears to violate the Recommendation’s intent, if not its wording.

Restricting substitution groups

This is nearly the opposite situation. The Working Group seems to intend to allow restrictions, but the wording leaves the result undefined.

The Recommendation clearly says that, when checking particle restriction, substitution groups should be treated as xs:choice. This suggests restricting them like complex types. Consider the complex-type definition of element author using the group headed by name as defined above:

<xs:complexType name="authorType">
   <xs:sequence>
      <xs:choice>
         <xs:element ref="name"/>
         <xs:element ref="simple-name"/>
         <xs:element ref="full-name"/>
      </xs:choice>
      <xs:element ref="born"/>
      <xs:element ref="dead" minOccurs="0"/>
   </xs:sequence>
   <xs:attribute ref="id"/>
</xs:complexType>

If substitution groups are treated as xs:choice, this definition is equivalent to the following, provided the head is not abstract:

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

It should be possible to derive this complex type by restriction, for example:

<xs:complexType name="restrictedAuthorType">
   <xs:complexContent>
      <xs:restriction base="authorType">
         <xs:sequence>
            <xs:choice>
               <xs:element ref="simple-name"/>
               <xs:element ref="full-name"/>
            </xs:choice>
            <xs:element ref="born"/>
            <xs:element ref="dead" minOccurs="0"/>
         </xs:sequence>
      </xs:restriction>
   </xs:complexContent>
</xs:complexType>

However, the Recommendation also requires, during restriction of a compositor xs:choice, a complete order-preserving functional mapping between particles defining the derived and original xs:choice. It does not define particle order when mapping substitution groups to xs:choice. Depending on the processor’s ordering when constructing the xs:choice from the group, our derivation may therefore be valid or invalid.

   

 

 

 

back to top