XML
  Home arrow XML arrow Page 4 - Doing More With XML Schemas (part 1)
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Moblin 
JMSL Numerical Library 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
XML

Doing More With XML Schemas (part 1)
By: Harish Kamath, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 5
    2003-01-07

    Table of Contents:
  • Doing More With XML Schemas (part 1)
  • A Complex Web
  • Nesting Season
  • Extending Yourself
  • Filing It All Away

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Doing More With XML Schemas (part 1) - Extending Yourself


    (Page 4 of 5 )

    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


     

       

    XML ARTICLES

    - 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...
    - Practical XML Data Design and Manipulation f...
    - SimpleXML
    - XForms Basics, Part 3
    - XForms Basics, Part 2
    - XForms Basics





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway