8.2.1. Redefining of Simple and Complex Types
Simple
and
complex types are redefined by deriving
them (by restriction for simple types and by restriction or extension
for complex types) inside the xs:redefine element. We can apply this to our
last example. The definition of bookTmp is
currently used to describe the book element though
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 of doing this, we can also redefine the definition of the
book complex type. The new schema to define 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--note how a book complex type is
redefined using a base type with the same name, which would be
forbidden anywhere else--and usage of the
book element looks like:
<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"/>