XML technologies / XML Schema / Creating extensible schemas / Extensible schemas

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

Schema extensibility depends strongly on style: which elements, attributes, groups and simple or complex types are global; the use of attributes final and fixed; and optionally distributing components among schema documents. Let us examine these three factors.

Global components

A simple example often beats a long explanation. To compare styles, we shall use our library to examine simple- and complex-type elements and attributes.

Elements

Consider defining the element book in our library. Four basic definitions validate the same instance elements, but not the same instance documents: a global element can serve as document element. A Russian doll design defines element book and its type locally within element library. I use the same style for child elements of book to keep the schema short, focusing on the definition of book:

<xs:element name="library">
   <xs:complexType>
      <xs:sequence>
         <xs:element name="book" maxOccurs="unbounded">
            <xs:complexType>
               <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="id"/>
               <xs:attribute ref="available"/>
            </xs:complexType>
         </xs:element>
      </xs:sequence>
   </xs:complexType>
</xs:element>

Or define a global element book and reference it in the library content model:

<xs:element name="book">
   <xs:complexType>
      <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="id"/>
      <xs:attribute ref="available"/>
   </xs:complexType>
</xs:element>
<xs:element name="library">
   <xs:complexType>
      <xs:sequence>
         <xs:element ref="book" maxOccurs="unbounded"/>
      </xs:sequence>
   </xs:complexType>
</xs:element>

The third traditional approach defines a complex type bookType for the content model of element book. I could call the type book but find bookType less confusing:

<xs:complexType name="bookType">
   <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="id"/>
   <xs:attribute ref="available"/>
</xs:complexType>
<xs:element name="library">
   <xs:complexType>
      <xs:sequence>
         <xs:element name="book" type="bookType" maxOccurs="unbounded"/>
      </xs:sequence>
   </xs:complexType>
</xs:element>

Finally, define a group containing our element book:

<xs:group name="bookGroup">
   <xs:sequence>
      <xs:element name="book">
         <xs:complexType>
            <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="id"/>
            <xs:attribute ref="available"/>
         </xs:complexType>
      </xs:element>
   </xs:sequence>
</xs:group>
<xs:element name="library">
   <xs:complexType>
      <xs:sequence>
         <xs:group ref="bookGroup" maxOccurs="unbounded"/>
      </xs:sequence>
   </xs:complexType>
</xs:element>

These four basic styles can of course be combined. An extreme example is:

<xs:complexType name="bookType">
   <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="id"/>
   <xs:attribute ref="available"/>
</xs:complexType>
<xs:element name="book" type="bookType"/>
<xs:group name="bookGroup">
   <xs:sequence>
      <xs:element ref="book"/>
   </xs:sequence>
</xs:group>
<xs:element name="library">
   <xs:complexType>
      <xs:sequence>
         <xs:group ref="bookGroup" maxOccurs="unbounded"/>
      </xs:sequence>
   </xs:complexType>
</xs:element>

Though excessive, this is also the most extensible example: it supports all reuse and derivation methods for the three compositors. Let us compare the four styles for reusability and derivability.

The Russian doll is clearly least extensible: definitions of both element book and its content model are local. They cannot be referenced elsewhere or modified through derivation, xs:redefine or substitution groups. Russian doll style blocks more effectively than any attribute block. Changing or reusing element book or its model requires associating an entirely different schema with the instance or using an attribute xsi:type there.

A flat model with global element definitions offers basic flexibility: reuse anywhere in any schema, use as a document element, and use as a substitution group head. Combined with a local complex-type definition, as here, it does not allow changing the model. Only the group-head use can be blocked, through an attribute block. The other two uses are unrestricted. Elements cannot be redefined, and the model of our element book can change only through substitution using xsi:type in the instance.

A global complex type for the content model of element book opens two doors. The content model of element book can be reused to derive extended or restricted models elsewhere, and the complex type can be redefined through xs:redefine. As seen in Building further components with object-oriented design, derivation can be blocked with attribute final, but redefinition cannot be controlled.

Finally, placing the definition of element book inside a group permits reuse elsewhere, for example in our flat model, while hiding the definition of element book when needed so it cannot serve as a document element. This also blocks group-head use. A group can be redefined through xs:redefine to change occurrence counts, add elements or even change the model if a global complex type is used. This use resembles RELAX NG and gains some of its flexibility. However, element groups cannot be recursive. Defining recursive models still requires a global element for references, limiting groups when that is impossible or undesirable, for example with two recursive models using the same element name for different content.

Which approach is right? There is no single definitive answer. Each style has its own extension options, and choosing or combining styles strongly affects reuse and derivation. The table helps show the differences; remember that all styles can be combined.

Table: Styles for complex types

Style

Element reference

Content model reference

Derivation

Substitution group

Document element

Redefinition

Russian doll

No

No

No

No

No

No

Flat

Yes

No

No

Yes

Yes

No

Complex type

No

Yes

Yes

No

No

Yes

Group

Yes

No

No

No

No

Yes

Simple-type elements behave much like complex-type ones, except complex definitions are replaced by simple-type definitions like those for attributes, discussed next.

Attributes

As discussed in Controlling namespaces, attributes differ from elements by usually being unqualified, so they cannot be global. Otherwise attributes, simple types and attribute groups resemble elements and complex types. The other exception is that attributes have no equivalent of substitution groups or xsi:type. For attribute lang in element title restricted to en or de, a Russian doll design defines both attribute and type locally:

<xs:element name="title">
   <xs:complexType>
      <xs:simpleContent>
         <xs:extension base="xs:token">
            <xs:attribute name="lang">
               <xs:simpleType>
                  <xs:restriction base="xs:language">
                     <xs:enumeration value="en"/>
                     <xs:enumeration value="de"/>
                  </xs:restriction>
               </xs:simpleType>
            </xs:attribute>
         </xs:extension>
      </xs:simpleContent>
   </xs:complexType>
</xs:element>

Or use a flat design with a globally defined attribute:

<xs:attribute name="lang">
   <xs:simpleType>
      <xs:restriction base="xs:language">
         <xs:enumeration value="en"/>
         <xs:enumeration value="de"/>
      </xs:restriction>
   </xs:simpleType>
</xs:attribute>
<xs:element name="title">
   <xs:complexType>
      <xs:simpleContent>
         <xs:extension base="xs:token">
            <xs:attribute ref="lang"/>
         </xs:extension>
      </xs:simpleContent>
   </xs:complexType>
</xs:element>

A global simple type can also be defined:

<xs:simpleType name="langType">
   <xs:restriction base="xs:language">
      <xs:enumeration value="en"/>
      <xs:enumeration value="de"/>
   </xs:restriction>
</xs:simpleType>
<xs:element name="title">
   <xs:complexType>
      <xs:simpleContent>
         <xs:extension base="xs:token">
            <xs:attribute name="lang" type="langType"/>
         </xs:extension>
      </xs:simpleContent>
   </xs:complexType>
</xs:element>

The attribute can be hidden in an attribute group:

<xs:attributeGroup name="langGroup">
   <xs:attribute name="lang">
      <xs:simpleType>
         <xs:restriction base="xs:language">
            <xs:enumeration value="en"/>
            <xs:enumeration value="de"/>
         </xs:restriction>
      </xs:simpleType>
   </xs:attribute>
</xs:attributeGroup>
<xs:element name="title">
   <xs:complexType>
      <xs:simpleContent>
         <xs:extension base="xs:token">
            <xs:attributeGroup ref="langGroup"/>
         </xs:extension>
      </xs:simpleContent>
   </xs:complexType>
</xs:element>

All these approaches can be combined:

<xs:simpleType name="langType">
   <xs:restriction base="xs:language">
      <xs:enumeration value="en"/>
      <xs:enumeration value="de"/>
   </xs:restriction>
</xs:simpleType>
<xs:attribute name="lang" type="langType"/>
<xs:attributeGroup name="langGroup">
   <xs:attribute ref="lang"/>
</xs:attributeGroup>
<xs:element name="title">
   <xs:complexType>
      <xs:simpleContent>
         <xs:extension base="xs:token">
            <xs:attributeGroup ref="langGroup"/>
         </xs:extension>
      </xs:simpleContent>
   </xs:complexType>
</xs:element>

These choices have almost the same effects as for complex-type elements, except for substitution groups and document-element use. The table describes the options.

Table: Styles for attributes

Style

Attribute reference

Data type reference

Derivation

Redefinition

Russian doll

No

No

No

No

Flat

Yes

No

No

No

Simple type

No

Yes

Yes

Yes

Attribute group

Yes

No

No

Yes

Attributes final and fixed

Discussed in Building further components with object-oriented design, these attributes clearly affect reuse of simple and complex definitions by blocking some or all derivation. They affect the schema’s flexibility. Their companions block and abstract affect openness rather than the set of instance documents.

Dividing schema components

The final factor is distributing components among documents. Some designers recommend one schema document per class or component, including or importing what is needed for a complete schema. This may seem excessive but offers fine granularity and bypasses limitations of xs:redefine: to redefine a component, omit its old definition and write a new one.

The main difficulties are managing a rapidly growing number of documents and their many dependencies. These must be considered when designing schema libraries. They can be hard to trace because the connections between including and included documents are multidirectional: a component in an included schema can reference components defined in any other schema processed by the schema processor.

Recall how a processor builds a global schema from imports, inclusions and redefinitions. It first consolidates all components from processed documents, then resolves references between components. Simple and powerful for inclusion, this becomes less pleasant with imports and redefinitions. Start with xs:include.

The semantics of xs:include differ from include statements in languages such as C. It is conditional inclusion: an xs:include asks to read a schema if not already read, add previously undefined components to the consolidated schema, ignore identical duplicates, and raise an error for conflicting duplicates. Direct loops, A includes B includes A, and indirect duplicate inclusions, A includes B and C while C includes B, are valid. Inclusion paths can be arbitrarily complex.

The meaning of xs:redefine is similar but permits redefinitions. This prohibits loops where A redefines B and B redefines or includes A. Inclusion forms a graph, but redefinition forms a tree. Processing is recursive: including or redefining a document actually includes its consolidated schema. Inclusion loops can still occur within redefinition branches: A redefines B, B includes C, and C includes B.

Some designers rely on a schema without a target namespace included or redefined in a schema with a target namespace borrowing its includer’s target namespace. Neutral, namespace-free components can thus serve as building blocks. They adopt the includer’s namespace like a chameleon takes its surroundings’ color, hence the name “chameleon schemas”. Simple and sometimes useful, this can confuse by defining similar components, types and models in different namespaces rather than a shared namespace that makes their identity clear.

xs:import behaves somewhat like xs:include: no redefinition occurs, so A for namespace A can import B for namespace B which imports A. Crucially, xs:import has two purposes: importing a schema and declaring references to a namespace’s components. If A imports B and B needs components from A, B must contain an instruction xs:import declaring use of namespace A. The attribute schemaLocation is optional and may be omitted here.

All three mechanisms can be combined, including chameleon schemas with imports. The same chameleon may contribute several times to a global schema under different namespaces. If A for namespace A includes namespace-free B and imports C for namespace C, which also includes B, the two inclusions of B belong to different namespaces and are considered distinct.

We can now invent combinations of inclusion/import graphs and redefinition trees. Remember simplicity’s appeal: without self-imposed limits, humans may get lost sooner than their favorite schema processor.

   

back to top

 

 

 

back to top