Doing More With XML Schemas (part 2) - The Next Level
(Page 3 of 6 )
Now, deriving a new datatype by extending the characteristics of an existing one is just one way of getting the job done. The XML Schema specification also supports one other way of deriving new datatypes: by restricting, or constraining, the characteristics of existing types.
In order to understand this, let's go back to the analogy on the previous page, and consider using the "Jedi" datatype as the base for another datatype: "JediMaster" (according to http://www.starwars.com/databank/organization/thejediorder/index.html, Jedi Masters are "those who have shown exceptional devotion and skill in the Force.")
In other words, a "JediMaster" possesses all the characteristics of a "Jedi"...with one additional constraint: a very high midi-chlorian count. Therefore, it is possible to define a "JediMaster" datatype simply by adding a restriction to the definition of the "Jedi" datatype - as demonstrated below:
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<!--
other definitions -->
<xsd:complexType name="JediMaster">
<xsd:complexContent>
<xsd:restriction
base="Jedi">
<xsd:sequence>
<xsd:element name="name"
type="xsd:string"/>
<xsd:element
name="species"
type="xsd:string"/>
<xsd:element name="language"
type="xsd:string"/>
<xsd:element
name="home"
type="xsd:string"/>
<xsd:element name="gender"
type="xsd:string"/>
<xsd:element
name="midichlorian-count"
>
<xsd:simpleType>
<xsd:restriction
base="xsd:integer">
<xsd:minInclusive
value="10000" />
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
</xsd:sequence>
</xsd:restriction>
</xsd:complexContent>
</xsd:complexType>
</xsd:schema>
Want to verify that I'm speaking the truth? Take the XML sample on the previous
page, alter it to use the new "JediMaster" datatype, and pass it through an XML validator.
<?xml version="1.0" encoding="UTF-8"?>
<gallery xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<character
xsi:type="JediMaster">
<name>Luke Skywalker</name>
<species>Human</species>
<language>Basic</language>
<home>Tatooine</home>
<gender>Male</gender>
<midichlorian-count>9000</midichlorian-count>
</character>
</gallery>
You should see an error, since the value of the <midiclorian-count> element
is less than the defined minimum value in the "JediMaster" datatype.
Now, alter the value of the <midiclorian-count> element in your XML sample, and validate it again.
<?xml version="1.0" encoding="UTF-8"?>
<gallery xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<character
xsi:type="JediMaster">
<name>Luke Skywalker</name>
<species>Human</species>
<language>Basic</language>
<home>Tatooine</home>
<gender>Male</gender>
<midichlorian-count>18000</midichlorian-count>
</character>
</gallery>
This time, you should see no errors - indicating that the restriction you've
imposed while deriving the new datatype is in effect.
Note also that when deriving by restriction, it is necessary to repeat the definition of all elements in the derived complex type from my original type. As a result, all elements of type "JediMaster" will also be acceptable as elements of type "Jedi".
Next: Big Brother Is Watching... >>
More XML Articles
More By Harish Kamath, (c) Melonfire