Imagine a technology that offered you all the power of a DTD without the associated unpleasantness of those strange symbols and arcane commands. Sounds too good to be true? Say hello to XML Schema.
Now, if your validation rules are not very stringent, you don't usually need to derive new datatypes; the built-in ones suffice for most requirements. If, on the other hand, you need to restrict your data to specific ranges or values, you will find it necessary to create new datatypes and use them within your schema definition. Let's move on to a closer examination of how this is accomplished.
The XML Schema specification allows for the derivation of new datatypes from the built-in types. Typically, these new derivations are created by placing a restriction on the allowed range of values for a built-in datatype, via the <xsd:restriction> element.
You've already seen this element in some of the previous examples - in fact, here's a quick snippet to jog your memory:
<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<!--
other definitions - snip! -->
<!-- create a new datatype -->
<xsd:simpleType
name="simpleDType">
<xsd:restriction base="xsd:integer">
<xsd:minInclusive
value="1"/>
<xsd:maxInclusive value="10"/>
</xsd:restriction>
</xsd:simpleType>
<!--
declare an element of this type -->
<xsd:element name="rating" type="simpleDType"/>
</xsd:schema>
In this case, a new datatype named "simpleDType" has been created using the built-in
"integer" datatype as a base. The <xsd:minInclusive> and <xsd:maxInclusive> elements are used to specify an allowable range of values for this new datatype; these restrictions are referred to in schema jargon as "facets".
It's also possible to create restrictions on the basis of patterns or regular expressions - consider the following element definition, which defines a datatype for temperature values using the <xsd:pattern> facet. This derived datatype allows for a value containing a maximum of three digits, prefixed by an optional minus sign.
<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<!--
other definitions - snip! -->
<!-- create a new datatype -->
<xsd:simpleType
name="temperatureDType">
<xsd:restriction base="xsd:string">
<xsd:pattern
value="-?[0-9]{1,3}"/>
</xsd:restriction>
</xsd:simpleType>
<!--
declare an element of this type -->
<xsd:element name="temp" type="temperatureDType"/>
</xsd:schema>
You can restrict an element to certain values with the <xsd:enumeration> facet
- the following example creates a datatype which, when applied to an element declaration, restricts it to one of four values:
<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<!--
other definitions - snip! -->
<!-- create a new datatype -->
<xsd:simpleType
name="flavourDType">
<xsd:restriction base="xsd:string">
<xsd:enumeration
value="vanilla"/>
<xsd:enumeration value="chocolate"/>
<xsd:enumeration
value="strawberry"/>
<xsd:enumeration value="peach"/>
</xsd:restriction>
</xsd:simpleType>
<!-- declare an element of this type -->
<xsd:element name="flavour" type="flavourDType"/>
</xsd:schema>
Why stop at elements? You can use the datatype above to restrict attributes to
specific values as well. Consider the following derived datatype, which restricts element or attribute values to specific genres of film:
<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<!--
other definitions - snip! -->
<!-- create a new datatype -->
<xsd:simpleType
name="genreDType">
<xsd:restriction base="xsd:NMTOKEN">
<xsd:enumeration
value="romance"/>
<xsd:enumeration value="comedy"/>
<xsd:enumeration
value="drama"/>
<xsd:enumeration value="action"/>
<xsd:enumeration
value="horror"/>
</xsd:restriction>
</xsd:simpleType>
<!-- declare
an attribute of this type -->
<xsd:attribute name="genre" type="genreDType"/>
</xsd:schema>
There are an infinite number of creative things you can do with power like this
- for more examples and information on the numerous facets available for the different datatypes, take a look at http://www.w3.org/TR/xmlschema-2/