HomeXML Page 4 - Doing More With XML Schemas (part 1)
Extending Yourself - XML
Get into the more advanced aspects of XML Schema design with a discussion of simple and complex datatypes, and learn to apply the basic OOP concepts of extensibility and inheritance to your schemas.
One of the coolest things about schemas is that they allow you to extend previously defined datatypes to create new sub-types, in much the same way as OOP programmers extend existing classes. These sub-types will automatically inherit all the characteristics of the base type, but may also be further customized to specific requirements.
In order to better understand this, let's modify the example on the previous page to derive a new datatype from the base "starWarsEntity" type.
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<!-- define
a complex type -->
<xsd:complexType name="starWarsEntity">
<xsd:sequence>
<xsd:element
name="name" type="xsd:string"/>
<xsd:element name="species" type="xsd:string"/>
<xsd:element
name="language" type="xsd:string"/>
<xsd:element name="home" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
<!--
extend base to derive a new sub-type -->
<xsd:complexType name="Wookie">
<xsd:complexContent>
<xsd:extension
base="starWarsEntity">
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
<!--
define the root element and its contents -->
<xsd:element name="gallery">
<xsd:complexType>
<xsd:sequence>
<xsd:element
name="character"
type="starWarsEntity" maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
In order to extend an existing datatype, you need to use the <xsd:extension>
element, which goes hand-in-hand with a "base" attribute - this "base" attribute contains the name of the parent type (which must, obviously, exist).
What use is this, you ask? Not much at the moment...but look how easy it becomes to add species-specific attributes to the derived datatype:
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<!-- define
a complex type -->
<xsd:complexType name="starWarsEntity">
<xsd:sequence>
<xsd:element
name="name" type="xsd:string"/>
<xsd:element name="species" type="xsd:string"/>
<xsd:element
name="language" type="xsd:string"/>
<xsd:element name="home" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
<!--
extend base to derive a new sub-type -->
<xsd:complexType name="Wookie">
<xsd:complexContent>
<xsd:extension
base="starWarsEntity">
<xsd:sequence>
<xsd:element name="battlecry"
type="xsd:string"/>
</xsd:sequence>
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
<!--
define the root element and its contents -->
<xsd:element name="gallery">
<xsd:complexType>
<xsd:sequence>
<xsd:element
name="character"
type="starWarsEntity" maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
So Wookies now have an additional characteristic - "battlecry".
Since it's so easy, let's derive a couple more:
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<!-- define
a complex type -->
<xsd:complexType name="starWarsEntity">
<xsd:sequence>
<xsd:element
name="name" type="xsd:string"/>
<xsd:element name="species" type="xsd:string"/>
<xsd:element
name="language" type="xsd:string"/>
<xsd:element name="home" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
<!--
extend base to derive a new sub-type -->
<xsd:complexType name="Wookie">
<xsd:complexContent>
<xsd:extension
base="starWarsEntity">
<xsd:sequence>
<xsd:element name="battlecry"
type="xsd:string"/>
</xsd:sequence>
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
<xsd:complexType
name="Human">
<xsd:complexContent>
<xsd:extension base="starWarsEntity">
<xsd:sequence>
<xsd:element
name="gender"
type="xsd:string"/>
</xsd:sequence>
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
<xsd:complexType
name="Ewok">
<xsd:complexContent>
<xsd:extension base="starWarsEntity">
<xsd:sequence>
<xsd:element
name="vehicle"
type="xsd:string"/>
<xsd:element name="society"
type="xsd:string"/>
</xsd:sequence>
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
<!--
define the root element and its contents -->
<xsd:element name="gallery">
<xsd:complexType>
<xsd:sequence>
<xsd:element
name="character"
type="starWarsEntity" maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
So Humans now include the additional characteristic "gender", while Ewoks include
the characteristics "vehicle" and "society". Each of these derived datatypes thus carries its own special modifications, while simultaneously including all the characteristics of the base "starWarsEntity" datatype.
Now, how do I make my XML document use these derived datatypes? Pretty simple - just add the "type" attribute to each "character" element, so that the XML validator knows to look at the derived datatype rather than the base type. Here's how: