Home arrow XML arrow Page 4 - Doing More With XML Schemas (part 1)

Extending Yourself - XML

Get into the more advanced aspects of XML Schema design with a discussion of simple and complex datatypes, and learn to apply the basic OOP concepts of extensibility and inheritance to your schemas.

TABLE OF CONTENTS:
  1. Doing More With XML Schemas (part 1)
  2. A Complex Web
  3. Nesting Season
  4. Extending Yourself
  5. Filing It All Away
By: Harish Kamath, (c) Melonfire
Rating: starstarstarstarstar / 6
January 07, 2003

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement
One of the coolest things about schemas is that they allow you to extend previously defined datatypes to create new sub-types, in much the same way as OOP programmers extend existing classes. These sub-types will automatically inherit all the characteristics of the base type, but may also be further customized to specific requirements.

In order to better understand this, let's modify the example on the previous page to derive a new datatype from the base "starWarsEntity" type.

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <!-- define a complex type --> <xsd:complexType name="starWarsEntity"> <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:sequence> </xsd:complexType> <!-- extend base to derive a new sub-type --> <xsd:complexType name="Wookie"> <xsd:complexContent> <xsd:extension base="starWarsEntity"> </xsd:extension> </xsd:complexContent> </xsd:complexType> <!-- define the root element and its contents --> <xsd:element name="gallery"> <xsd:complexType> <xsd:sequence> <xsd:element name="character" type="starWarsEntity" maxOccurs="unbounded"/> </xsd:sequence> </xsd:complexType> </xsd:element> </xsd:schema>
In order to extend an existing datatype, you need to use the <xsd:extension> element, which goes hand-in-hand with a "base" attribute - this "base" attribute contains the name of the parent type (which must, obviously, exist).

What use is this, you ask? Not much at the moment...but look how easy it becomes to add species-specific attributes to the derived datatype:

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <!-- define a complex type --> <xsd:complexType name="starWarsEntity"> <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:sequence> </xsd:complexType> <!-- extend base to derive a new sub-type --> <xsd:complexType name="Wookie"> <xsd:complexContent> <xsd:extension base="starWarsEntity"> <xsd:sequence> <xsd:element name="battlecry" type="xsd:string"/> </xsd:sequence> </xsd:extension> </xsd:complexContent> </xsd:complexType> <!-- define the root element and its contents --> <xsd:element name="gallery"> <xsd:complexType> <xsd:sequence> <xsd:element name="character" type="starWarsEntity" maxOccurs="unbounded"/> </xsd:sequence> </xsd:complexType> </xsd:element> </xsd:schema>
So Wookies now have an additional characteristic - "battlecry".

Since it's so easy, let's derive a couple more:

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <!-- define a complex type --> <xsd:complexType name="starWarsEntity"> <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:sequence> </xsd:complexType> <!-- extend base to derive a new sub-type --> <xsd:complexType name="Wookie"> <xsd:complexContent> <xsd:extension base="starWarsEntity"> <xsd:sequence> <xsd:element name="battlecry" type="xsd:string"/> </xsd:sequence> </xsd:extension> </xsd:complexContent> </xsd:complexType> <xsd:complexType name="Human"> <xsd:complexContent> <xsd:extension base="starWarsEntity"> <xsd:sequence> <xsd:element name="gender" type="xsd:string"/> </xsd:sequence> </xsd:extension> </xsd:complexContent> </xsd:complexType> <xsd:complexType name="Ewok"> <xsd:complexContent> <xsd:extension base="starWarsEntity"> <xsd:sequence> <xsd:element name="vehicle" type="xsd:string"/> <xsd:element name="society" type="xsd:string"/> </xsd:sequence> </xsd:extension> </xsd:complexContent> </xsd:complexType> <!-- define the root element and its contents --> <xsd:element name="gallery"> <xsd:complexType> <xsd:sequence> <xsd:element name="character" type="starWarsEntity" maxOccurs="unbounded"/> </xsd:sequence> </xsd:complexType> </xsd:element> </xsd:schema>
So Humans now include the additional characteristic "gender", while Ewoks include the characteristics "vehicle" and "society". Each of these derived datatypes thus carries its own special modifications, while simultaneously including all the characteristics of the base "starWarsEntity" datatype.

Now, how do I make my XML document use these derived datatypes? Pretty simple - just add the "type" attribute to each "character" element, so that the XML validator knows to look at the derived datatype rather than the base type. Here's how:

<?xml version="1.0" encoding="UTF-8"?> <gallery xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="starwars.xsd"> <character xsi:type="Human"> <name>Luke Skywalker</name> <species>Human</species> <language>Basic</language> <home>Tatooine</home> <gender>Male</gender> </character> <character xsi:type="Wookie"> <name>Chewbacca</name> <species>Wookie</species> <language>Shyriiwook</language> <home>Kashyyyk</home> <battlecry>AARGH!</battlecry> </character> <character xsi:type="Ewok"> <name>Chief Chirpa</name> <species>Ewok</species> <language>Ewok</language> <home>Endor</home> <vehicle>Glider</vehicle> <society>Tribal</society> </character> </gallery>
Simple, huh?

 
 
>>> More XML Articles          >>> More By Harish Kamath, (c) Melonfire
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

XML ARTICLES

- Google Docs and Xpath Data Functions
- Flex Array Collection Sort and Filtering
- The Flex Tree Control
- Flex List Controls
- Working with Flex and Datagrids
- How to Set Up Podcasting and Vodcasting
- Creating an RSS Reader Application
- Building an RSS File
- An Introduction to XUL Part 6
- An Introduction to XUL Part 5
- An Introduction to XUL Part 4
- An Introduction to XUL Part 3
- An Introduction to XUL Part 2
- An Introduction to XUL Part 1
- XML Matters: Practical XML Data Design and M...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: