Understanding XML Schema - Battle Of The Sexes
(Page 7 of 12 )
So that takes care of elements - but what about attributes?
Attribute definitions typically follow element definitions, and are declared with the <xsd:attribute> element.
<xsd:attribute name="genre" type="xsd:string" />
An optional "use" attribute can be used to specify whether or not the attribute
is optional.
<!-- this attribute is optional -->
<xsd:attribute name="genre" type="xsd:string"
use="optional" />
<!-- this attribute is required-->
<xsd:attribute name="id"
type="xsd:integer" use="required" />
<!-- this attribute is not allowed -->
<xsd:attribute
name="address" type="xsd:string" use="prohibited" />
In order to see this in action, let's suppose I altered my document instance
to include an attribute:
<?xml version="1.0"?>
<movie genre="romance">
<title>Moulin Rouge</title>
<cast>
<person>Nicole
Kidman</person>
<person>Ewan McGregor</person>
</cast>
<rating>5</rating>
</movie>
Here's what my schema would look like:
<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element
name="movie">
<xsd:complexType>
<xsd:sequence>
<!-- simple element
definition -->
<xsd:element name="title" type="xsd:string"/>
<!--
complex element definition -->
<xsd:element name="cast">
<xsd:complexType>
<xsd:sequence>
<xsd:element
name="person" type="xsd:string" maxOccurs="unbounded" />
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<!--
simple element definition -->
<xsd:element name="release_date" type="xsd:date"
maxOccurs="1"
minOccurs="0"/>
<!-- simple element definition -->
<xsd:element
name="rating" type="xsd:integer"/>
</xsd:sequence>
<!-- attribute
definition -->
<xsd:attribute name="genre" type="xsd:string" />
</xsd:complexType>
</xsd:element>
</xsd:schema>
How about adding a twist? Let's suppose every "person" element needs an additional
"sex" attribute, like this:
<?xml version="1.0"?>
<movie genre="romance">
<title>Moulin Rouge</title>
<cast>
<person
sex="female">Nicole Kidman</person>
<person sex="male">Ewan McGregor</person>
</cast>
<rating>5</rating>
</movie>
This apparently minor change has quite a significant impact on the schema definition,
because the "person" element, previously defined as a simple type, must now be defined as a complex type. Since the "person" element does not have any nested child elements, we can pass up the <xsd:sequence> instruction within the element definition in favour of the <xsd:simpleContent> element and include an attribute definition within this element.
Here's the revised schema definition:
<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element
name="movie">
<xsd:complexType>
<xsd:sequence>
<!-- simple element
definition - title -->
<xsd:element name="title" type="xsd:string"/>
<!--
complex element definition - cast -->
<xsd:element name="cast">
<xsd:complexType>
<xsd:sequence>
<!--
since person has an attribute, it must be defined as a complex
element -->
<xsd:element
name="person" maxOccurs="unbounded">
<xsd:complexType>
<xsd:simpleContent>
<xsd:restriction
base="xsd:string">
<!-- attribute definition - sex -->
<xsd:attribute
name="sex" type="xsd:string" />
</xsd:restriction>
</xsd:simpleContent>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<!--
simple element definition - release date -->
<xsd:element name="release_date"
type="xsd:date" maxOccurs="1"
minOccurs="0"/>
<!-- simple element definition
- rating -->
<xsd:element name="rating" type="xsd:integer"/>
</xsd:sequence>
<!--
attribute definition for movie genre -->
<xsd:attribute name="genre" type="xsd:string"
/>
</xsd:complexType>
</xsd:element>
</xsd:schema>
Next: Dealing With The Ref >>
More XML Articles
More By Harish Kamath, (c) Melonfire