<xs:complexType name="bookTitleAttribute">
<xs:complexContent>
<xs:restriction base="bookBase">
<xs:sequence>
<xs:element ref="isbn"/>
<xs:element ref="author" minOccurs="0"
maxOccurs="unbounded"/>
<xs:element ref="character" minOccurs="0"
maxOccurs="unbounded"/>
</xs:sequence>
</xs:restriction>
</xs:complexContent>
</xs:complexType>
We can derive a second type that accepts only titles defined as one
or more title elements:
<xs:complexType name="bookTitleElements">
<xs:complexContent>
<xs:restriction base="bookBase">
<xs:sequence>
<xs:element ref="isbn"/>
<xs:element ref="title" minOccurs="1" maxOccurs="unbounded"/>
<xs:element ref="author" minOccurs="0"
maxOccurs="unbounded"/>
<xs:element ref="character" minOccurs="0"
maxOccurs="unbounded"/>
</xs:sequence>
<xs:attribute ref="title" use="prohibited"/>
</xs:restriction>
</xs:complexContent>
</xs:complexType>
Now that we have all our building blocks, we can use them in the
schema to define the book element as having a type
bookBase:
<xs:element name="book" type="bookBase"/>
Then we can use them in the instance documents to declare which
derived type we are using:
<book id="b0836217462" available="true" xsi:type="bookTitleElements">
<isbn>
0836217462
</isbn>
<title lang="en">
Being a Dog Is a Full-Time Job
</title>
<title lang="fr">
Etre un chien est un travail à plein temps.
</title>
.../...
</book>
or:
<book id="b0836217462" available="true" title="Being a Dog Is a
Full-TimeJob" xsi:type="bookTitleAttribute">
.../...
</book>
However, this allows instance documents to use the base type, which
may not be something we want, since we can have either no title at
all or an attribute and one or more elements (something we want to
avoid). We can forbid the use of the base type by defining it as
"abstract." Setting this attribute
of the complex type definition blocks instance documents from using
it. They will have to specify one of its derived types through a
xsi:type attribute.