Doing More With XML Schemas (part 2) - Speaking In The Abstract
(Page 5 of 6 )
While on the subject of controlling the manner in which type definitions can be used, it's instructive to also look at abstract type definitions. If a base type spawns several new sub-types, as in the example below,
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element
name="gallery">
<xsd:complexType>
<xsd:sequence>
<xsd:element
name="character"
type="starWarsEntity" maxOccurs="unbounded" />
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<!--
base definition -->
<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>
<!--
derived definition -->
<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>
<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>
<!--
and so on -->
</xsd:schema>
schema authors can force document authors to be more precise in their usage of
these types by declaring the base type as abstract.
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<!--
base definition -->
<xsd:complexType name="starWarsEntity" abstract="true">
<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>
</xsd:schema>
This requires document authors to specifically name the sub-type whenever they
use it in a document instance. Failure to do so will result in XML validation errors. For example, while the following XML document instance is certainly conformant to the rules laid down for the base type "starWarsEntity",
<?xml version="1.0" encoding="UTF-8"?>
<gallery xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<character>
<name>Luke
Skywalker</name>
<species>Human</species>
<language>Basic</language>
<home>Tatooine</home>
</character>
</gallery>
the XML validator will still generate errors while parsing it, as "starWarsEntity"
has been defined as an abstract type. It is only when the document author specifies a type via the "type" attribute
<?xml version="1.0" encoding="UTF-8"?>
<gallery xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<character
xsi:type="Human">
<name>Luke Skywalker</name>
<species>Human</species>
<language>Basic</language>
<home>Tatooine</home>
<gender>Male</gender>
</character>
</gallery>
that validation will take place without errors.
Again, this mechanism assists in reducing the risk of errors, and in controlling the manner in which schema definitions are used by document authors. It's also possible to declare specific elements (rather than types) as abstract - all you need is a substitution group, which you can read about at
http://www.w3.org/TR/xmlschema-0/#SubsGroups Next: Going Local >>
More XML Articles
More By Harish Kamath, (c) Melonfire