XML technologies / XML Schema / Building blocks / Schema inclusion with redefinition

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

Inclusion provides no means of modifying the definitions it incorporates. Moreover, since they are considered global definitions after being imported, they cannot subsequently be changed either. W3C XML Schema does, however, allow derivation from global type and group definitions during inclusion, while retaining the same name afterwards. The semantics of this redefinition are therefore: “Use this definition instead of the one found in the included schema, but ensure that it is a valid derivation so that applications are not too surprised by the change.” This is implemented by the element xs:redefine together with a schemaLocation attribute (just as with xs:include). Its children are component definitions that replace the definitions found in the included schema. Definitions not listed in the element xs:redefine are incorporated unchanged. This means that an xs:redefine without child elements is completely equivalent to an xs:include.

Note that redefinition affects the resulting schema globally. All references to redefined components are affected by the changes to those components, without exception, even references made in the redefined schema.

Redefining simple and complex types

Simple and complex types are redefined by deriving from them inside the element xs:redefine (by restriction for simple types, by restriction or extension for complex types). We can apply this to our previous example. The definition of bookTmp is currently used to describe the element book by derivation:

<xs:element name="book">
   <xs:complexType>
      <xs:complexContent>
         <xs:restriction base="bookTmp">
            <xs:sequence>
               <xs:element ref="isbn"/>
               <xs:element ref="title"/>
               <xs:element ref="author" minOccurs="0" maxOccurs="unbounded"/>
               <xs:element ref="character" minOccurs="0" maxOccurs="unbounded"/>
            </xs:sequence>
            <xs:attribute name="id" type="bookID"/>
            <xs:attribute ref="available"/>
         </xs:restriction>
      </xs:complexContent>
   </xs:complexType>
</xs:element>

Instead, we can redefine the complex type book. The new schema defining the complex types is then:

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
   <xs:complexType name="elementWithID">
      <xs:attribute ref="id"/>
   </xs:complexType>
   <xs:complexType name="book">
      <xs:complexContent>
         <xs:extension base="elementWithID">
            <xs:sequence>
               <xs:element ref="isbn"/>
               <xs:element ref="title"/>
               <xs:element ref="author" minOccurs="0" maxOccurs="unbounded"/>
               <xs:element ref="character" minOccurs="0" maxOccurs="unbounded"/>
            </xs:sequence>
            <xs:attribute ref="available"/>
         </xs:extension>
      </xs:complexContent>
   </xs:complexType>
   <xs:complexType name="personType">
      <xs:complexContent>
         <xs:extension base="elementWithID">
            <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:extension>
      </xs:complexContent>
   </xs:complexType>
</xs:schema>

The redefinition—notice how a complex type book is redefined using a base type of the same name, which would otherwise always be forbidden—and the use of the element book then look like this:

<xs:redefine schemaLocation="complex-types2.xsd">
   <xs:complexType name="book">
      <xs:complexContent>
         <xs:restriction base="book">
            <xs:sequence>
               <xs:element ref="isbn"/>
               <xs:element ref="title"/>
               <xs:element ref="author" minOccurs="0" maxOccurs="unbounded"/>
               <xs:element ref="character" minOccurs="0" maxOccurs="unbounded"/>
            </xs:sequence>
            <xs:attribute name="id" type="bookID"/>
            <xs:attribute ref="available"/>
         </xs:restriction>
      </xs:complexContent>
   </xs:complexType>
</xs:redefine>
<xs:element name="book" type="book"/>

Redefining element and attribute groups

Redefining complex and simple types seems quite natural and should be no great surprise, since it builds on what we discussed in detail in previous chapters. What is new about xs:redefine is that element and attribute groups, which cannot be derived from, can also be redefined. Redefining these groups requires no additional schema element: a group redefinition containing a reference to itself is considered an extension; otherwise it is a restriction. These two methods have their own rules and semantics, similar but not identical to those for deriving complex types. They deserve a separate description. As we shall see, the underlying principles are the same, and the asymmetry between extension and restriction persists in group redefinitions.

Extension

A group extension is implemented by referencing the group somewhere in its redefinition. Its semantics therefore resemble derivation by extension for complex types with complex content: new content is added to the base type, but with greater flexibility. During group extension, we can choose where to insert the base type’s content. This contrasts with extending a complex type with complex content, where new content always follows that of the base type. For example, consider this group definition:

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
   <xs:group name="character">
      <xs:sequence>
         <xs:element ref="born"/>
         <xs:element ref="qualification"/>
      </xs:sequence>
   </xs:group>
</xs:schema>

When redefining it, we can add the element name, missing from the start of the content:

<xs:redefine schemaLocation="character-group.xsd">
   <xs:group name="character">
      <xs:sequence>
         <xs:element ref="name"/>
         <xs:group ref="character"/>
      </xs:sequence>
   </xs:group>
</xs:redefine>
<xs:element name="character">
   <xs:complexType>
      <xs:group ref="character"/>
      <xs:attribute ref="id"/>
   </xs:complexType>
</xs:element>

We were able to choose the insertion point for the base group’s content so that it follows the name element. This is an improvement over derivation for complex types with complex content.

This way of extending element or attribute groups is clearly underspecified in the Recommendation and should be used only in its simplest form to avoid interoperability problems. The Recommendation requires the reference’s attributes minOccurs and maxOccurs to be exactly one, expressing the intention to include the base group’s content exactly once during extension. However, the wording does not prohibit embedding this reference in a branch with a different occurrence count:

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

Functionally, this corresponds to the value 0 of the attribute minOccurs on the group reference, thus allowing content models without any occurrence of the base group. Since this contradicts the underlying intention of derivation by extension, this kind of structure should not be used. Similarly, the Recommendation does not prohibit using a compositor other than xs:sequence when redefining a group. However, using xs:choice instead of xs:sequence produces groups in which the base content can be omitted, so this is certainly something to avoid.

References used to extend groups during redefinition must be at the top level of the group definition. Finally, when extending element groups, note that self-references in ordinary global group definitions cannot define recursive content models, even though the redefinition syntax contains a group reference to the group being defined. These must be implemented at a lower level, for example:

<xs:group name="group">
   <!-- Diese Gruppendefinition ist *nicht* zulässig -->
   <xs:sequence>
      <xs:element name="foo">
         <xs:complexType>
            <xs:group ref="group" minOccurs="0"/>
         </xs:complexType>
      </xs:element>
      <xs:element name="bar" type="xs:token"/>
   </xs:sequence>
</xs:group>

Restriction

Redefining attribute and element groups by restriction is similar in principle to deriving complex types with complex content by restriction. A new group definition is provided; it must meet the same criteria as a restriction of complex types with complex content and must be a valid restriction of the base group. Content matching the redefined group must always also match the base group, and elements used in the new definition must be explicit restrictions of the elements used in the base group. Consider this group definition:

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
   <xs:group name="author">
      <xs:sequence>
         <xs:element ref="name"/>
         <xs:element ref="born"/>
         <xs:element ref="dead" minOccurs="0"/>
         <xs:element name="nationality" type="xs:NMTOKEN" minOccurs="0"/>
      </xs:sequence>
   </xs:group>
</xs:schema>

We can define this group so that the optional element nationality is removed:

<xs:redefine schemaLocation="author.xsd">
   <xs:group name="author">
      <xs:sequence>
         <xs:element ref="name"/>
         <xs:element ref="born"/>
         <xs:element ref="dead" minOccurs="0"/>
      </xs:sequence>
   </xs:group>
</xs:redefine>
<xs:element name="author">
   <xs:complexType>
      Andere Alternativen | 147
      <xs:group ref="author"/>
      <xs:attribute ref="id"/>
   </xs:complexType>
</xs:element>

Before leaving this topic, note that the rules for restricting attribute groups differ from those for restricting complex types. The attribute list must name every attribute to retain. This differs from complex-type restrictions, where unmentioned attributes are considered retained unchanged. Consider an attribute group such as:

<xs:attributeGroup name="commonAttributes">
   <xs:attribute name="id" type="xs:ID"/>
   <xs:attribute name="available" type="xs:boolean"/>
   <xs:attribute name="lang" type="xs:language"/>
</xs:attributeGroup>
        

If we want to restrict it by redefinition so that the attribute available is removed, we must repeat the definitions of the other two attributes:

<xs:redefine schemaLocation="attributes.xsd">
   <xs:attributeGroup name="commonAttributes">
      <xs:attribute name="id" type="xs:ID"/>
      <xs:attribute name="lang" type="xs:language"/>
   </xs:attributeGroup>
</xs:redefine>

   

 

 

 

back to top