XML technologies / XML Schema / Building blocks / Schema inclusion

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

The simplest way to build schema libraries is inclusion, much like inclusion in languages such as C. Compared with physical inclusion through expansion of an external entity reference or through XInclude, described in “XInclude”, schema inclusion is logical and controls inclusion semantics. It is also a special case of schema redefinition. Inclusion and redefinition operate within one namespace or none. Schema import, covered in Controlling namespaces, is needed to import definitions into other namespaces.

Includes must be top-level children of element xs:schema. They include all top-level declarations from the other schema, which need not be complete, treating them as top-level elements of the result. No priority rules apply: conflicting definitions are errors. We could put all simple-type definitions in a separate schema:

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
   <xs:simpleType name="string255">
      <xs:restriction base="xs:token">
         <xs:maxLength value="255"/>
      </xs:restriction>
   </xs:simpleType>
   <xs:simpleType name="string32">
      <xs:restriction base="xs:token">
         <xs:maxLength value="32"/>
      </xs:restriction>
   </xs:simpleType>
   <xs:simpleType name="isbn">
      <xs:restriction base="xs:NMTOKEN">
         <xs:length value="10"/>
         <xs:pattern value="[0-9]{9}[0-9X]"/>
      </xs:restriction>
   </xs:simpleType>
   <xs:simpleType name="bookID">
      <xs:restriction base="xs:ID">
         <xs:pattern value="b[0-9]{9}[0-9X]"/>
      </xs:restriction>
   </xs:simpleType>
   <xs:simpleType name="supportedLanguages">
      <xs:restriction base="xs:language">
         <xs:enumeration value="en"/>
         <xs:enumeration value="de"/>
      </xs:restriction>
   </xs:simpleType>
   <xs:simpleType name="date">
      <xs:restriction base="xs:date">
         <xs:pattern value="[^:Z]*"/>
      </xs:restriction>
   </xs:simpleType>
</xs:schema>

Our main schema can include it with:

<xs:include schemaLocation="simple-types.xsd"/>

Here the dependency is one-way: simple types are defined in simple-types.xsd and used by the main schema. The included schema declares no elements and cannot validate documents alone, so it is not useful by itself. Yet it is complete, referencing only predefined simple types. Completeness is not required, as including complex-type definitions shows:

<?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="bookTmp">
      <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>

Both fragments can now be included in the main schema:

<xs:include schemaLocation="simple-types.xsd"/>
<xs:include schemaLocation="complex-types.xsd"/>

We now have an included schema (complex-types.xsd) referencing elements such as author, character or dead, defined in the main schema using data types defined in either simple-types.xsd or complex-types.xsd . This is permitted in W3C XML Schema, which collects everything needed before checking references, or nearly everything, since wildcards can be exceptions, as shown in Object-oriented construction of further building blocks. This powerful flexibility supports flexible libraries but can be error-prone. A complex type such as personType always has the same children, but their content models vary with the schema including complex-types.xsd . Carefully track these mutual dependencies when using this mechanism.

   

 

 

 

back to top