XML
  Home arrow XML arrow Page 3 - Doing More With XML Schemas (part 2)
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 2)
By: Harish Kamath, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 5
    2003-01-23

    Table of Contents:
  • Doing More With XML Schemas (part 2)
  • Feeling The Force
  • The Next Level
  • Big Brother Is Watching...
  • Speaking In The Abstract
  • Going Local

  • 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 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".

    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 4 hosted by Hostway