Stylus Studio XML Editor

Table of contents

Appendices

4.6 Substitution Groups

Substitution Groups

ref25XML Schema provides a mechanism, called substitution groups, that allows elements to be substituted for other elements. More specifically, elements can be assigned to a special group of elements that are said to be substitutable for a particular named element called the head element. (Note that the head element as well as the substitutable elementsmust be declared as a global elements.) To illustrate, we declare two elements called customerComment and shipComment and assign them to a substitution group whose head element is comment, and so customerComment and shipComment can be used anyplace that we are able to use comment. Elements in a substitution group must have the same type as the head element, or they can have a type that has been derived from the head element's type. To declare these two new elements, and to make them substitutable for the comment element, we use the following syntax:

NOTE: 

Declaring Elements Substitutable for comment

<element name="shipComment" type="string"
         substitutionGroup="ipo:comment"/>
<element name="customerComment" type="string"
         substitutionGroup="ipo:comment"/>

When these declarations are added to the international purchase order schema, shipComment and customerComment can be substituted for comment in the instance document, for example:

NOTE: 

Snippet of ipo.xml with Substituted Elements

....
<items>
  <item partNum="833-AA">
    <productName>Lapis necklace</productName>
    <quantity>1</quantity>
    <USPrice>99.95</USPrice>
    <ipo:shipComment>
      Use gold wrap if possible
    </ipo:shipComment>
    <ipo:customerComment>
      Want this for the holidays!
    </ipo:customerComment>
    <shipDate>1999-12-05</shipDate>
  </item>
</items>
....

Note that when an instance document contains element substitutions whose types are derived from those of their head elements, it is not necessary to identify the derived types using the xsi:type construction that we described in [Using Derived Types in Instance Documents].

The existence of a substitution group does not require any of the elements in that class to be used, nor does it preclude use of the head element. It simply provides a mechanism for allowing elements to be used interchangeably.