We will see, in the course of this book, that there are many
different styles for writing a schema, and there are even more
approaches to deriving a schema from an instance document. For our
first schema, we will adopt a style that is familiar to those of you
who have already worked with DTDs. We'll start by
creating a classified list of the elements and attributes found in
the schema.
Before we start, we need to classify the elements and, for this
exercise, give some key definitions for understanding how W3C XML
Schema does this classification. (We will see these definitions in
more detail in the chapters about simple and complex types.)
Elements such as name, born,
and title have simple content models:
.../...
<title lang="en">
Being a Dog Is a Full-Time Job
</title>
.../...
<name>
Charles M Schulz
</name>
<born>
1922-11-26
</born>
.../...
Elements such as library or
character have complex content models:
<library>
<book id="b0836217462" available="true">
.../...
</book>
</library>
<character id="Lucy">
<name>
Lucy
</name>
<born>
1952-03-03
</born>
<qualification>
bossy, crabby and selfish
</qualification>
</character>
Within elements that have a simple content model, we can distinguish
those which have attributes and those which cannot have any
attributes. Later chapters discuss how W3C XML Schema can also
represent empty and mixed content models.
<author id="CMS">
<name>
Charles M Schulz
</name>
<born>
1922-11-26
</born>
<dead>
2000-02-12
</dead>
</author>
.../...
<title lang="en">
Being a Dog Is a Full-Time Job
</title>
While elements such as born or
qualification (and, of course, all the attributes)
have a simple type:
<born>
1922-11-26
</born>
.../...
<qualification>
brought classical music to the Peanuts strip
</qualification>
.../...
<book available="true"/>
Now that we have criteria to classify our components, we can define
each of them. Let's start with the simplest one by
taking a type element, such as the name element
that can be found in author or
character:
<name>
Charles M Schulz
</name>
The library element, the most straightforward of
them, is defined as:
<xs:element name="library">
<xs:complexType>
<xs:sequence>
<xs:element ref="book" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
This definition can be read as "the element named
library is a complex type composed of a sequence
of 1 to many occurrences (note the maxOccurs
attribute) of elements defined as having a name
book."
The element author, which has an attribute and for
which we may consider the date of death as optional, could be:
<xs:element name="author">
<xs:complexType>
<xs:sequence>
<xs:element ref="name"/>
<xs:element ref="born"/>
<xs:element ref="dead" minOccurs="0"/>
</xs:sequence>
<xs:attribute ref="id"/>
</xs:complexType>
</xs:element>
This means the element named author is a complex
type composed of a sequence of three elements
(name, born, and
dead), and id. The
dead element is optional- it may occur zero
times.