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.
With the broad overview out of the way, let's now focus on the different elements (pun definitely intended!) that make up a schema.
The XML Schema specification makes a basic distinction between "simple" and "complex" elements. Simple elements cannot contain other elements or possess additional attributes; complex elements can have additional attributes and serve as containers for other elements (which themselves may be either simple or complex).
Within a schema, these two element types are represented by the <xsd:simpleType> and <xsd:complexType> elements respectively.
Simple elements can be represented in two ways. The first (and simplest) method is to use the <xsd:element> declaration with a built-in datatype - the following simple element
When the datatype name is preceded by the "xsd:" prefix, it indicates a predefined
datatype and not a new, user-defined type. The XML Schema specification lists about forty different built-in datatypes, including "string", "integer", "decimal", "float", "boolean", "time", "date", "dateTime" and "anyURI". However, in case these are too generic for you, it's also possible to derive your own custom datatype from the built-in ones, and then declare simple elements using this custom datatype.
Consider the following schema definition, which is equivalent to the one above:
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<!-- define
a new datatype -->
<xsd:simpleType name="simpleDType">
<xsd:restriction
base="xsd:string"></xsd:restriction>
</xsd:simpleType>
<!-- declare
an element of this type -->
<xsd:element name="title" type="simpleDType"/>
</xsd:schema>
Typically, this second method is used only when a schema author needs to restrict
the values of a particular simple element over and above the constraints inherent in a specific datatype. Here's a more constructive definition, which restricts the values of the "rating" element to an integer between 1 and 10.
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<!-- define
a new datatype -->
<xsd:simpleType name="simpleDType">
<xsd:restriction
base="xsd:integer">
<!-- restrict integer values between 1 and 10 -->
<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>
I'll be discussing the derivation of new datatypes in more detail a little further
down, so don't worry too much if the syntax seems a little unfamiliar. The important thing to note here is the two different options open to you while defining a simple element.