XML
  Home arrow XML arrow Page 9 - Understanding XML Schema
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 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Mobile Linux 
App Generation ROI 
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

Understanding XML Schema
By: Harish Kamath, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 3
    2001-10-15

    Table of Contents:
  • Understanding XML Schema
  • The Next Generation
  • An Evening At The Moulin Rouge
  • Simple Simon
  • A Complex Web
  • Nesting Season
  • Battle Of The Sexes
  • Dealing With The Ref
  • Different Flavours
  • When In Rome...
  • Test Drive
  • The Next Step

  • 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


    Understanding XML Schema - Different Flavours


    (Page 9 of 12 )

    Now, if your validation rules are not very stringent, you don't usually need to derive new datatypes; the built-in ones suffice for most requirements. If, on the other hand, you need to restrict your data to specific ranges or values, you will find it necessary to create new datatypes and use them within your schema definition. Let's move on to a closer examination of how this is accomplished.

    The XML Schema specification allows for the derivation of new datatypes from the built-in types. Typically, these new derivations are created by placing a restriction on the allowed range of values for a built-in datatype, via the <xsd:restriction> element.

    You've already seen this element in some of the previous examples - in fact, here's a quick snippet to jog your memory:

    <?xml version="1.0"?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <!-- other definitions - snip! --> <!-- create a new datatype --> <xsd:simpleType name="simpleDType"> <xsd:restriction base="xsd:integer"> <xsd:minInclusive value="1"/> <xsd:maxInclusive value="10"/> </xsd:restriction> </xsd:simpleType> <!-- declare an element of this type --> <xsd:element name="rating" type="simpleDType"/> </xsd:schema>
    In this case, a new datatype named "simpleDType" has been created using the built-in "integer" datatype as a base. The <xsd:minInclusive> and <xsd:maxInclusive> elements are used to specify an allowable range of values for this new datatype; these restrictions are referred to in schema jargon as "facets".

    It's also possible to create restrictions on the basis of patterns or regular expressions - consider the following element definition, which defines a datatype for temperature values using the <xsd:pattern> facet. This derived datatype allows for a value containing a maximum of three digits, prefixed by an optional minus sign.

    <?xml version="1.0"?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <!-- other definitions - snip! --> <!-- create a new datatype --> <xsd:simpleType name="temperatureDType"> <xsd:restriction base="xsd:string"> <xsd:pattern value="-?[0-9]{1,3}"/> </xsd:restriction> </xsd:simpleType> <!-- declare an element of this type --> <xsd:element name="temp" type="temperatureDType"/> </xsd:schema>
    You can restrict an element to certain values with the <xsd:enumeration> facet - the following example creates a datatype which, when applied to an element declaration, restricts it to one of four values:

    <?xml version="1.0"?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <!-- other definitions - snip! --> <!-- create a new datatype --> <xsd:simpleType name="flavourDType"> <xsd:restriction base="xsd:string"> <xsd:enumeration value="vanilla"/> <xsd:enumeration value="chocolate"/> <xsd:enumeration value="strawberry"/> <xsd:enumeration value="peach"/> </xsd:restriction> </xsd:simpleType> <!-- declare an element of this type --> <xsd:element name="flavour" type="flavourDType"/> </xsd:schema>
    Why stop at elements? You can use the datatype above to restrict attributes to specific values as well. Consider the following derived datatype, which restricts element or attribute values to specific genres of film:

    <?xml version="1.0"?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <!-- other definitions - snip! --> <!-- create a new datatype --> <xsd:simpleType name="genreDType"> <xsd:restriction base="xsd:NMTOKEN"> <xsd:enumeration value="romance"/> <xsd:enumeration value="comedy"/> <xsd:enumeration value="drama"/> <xsd:enumeration value="action"/> <xsd:enumeration value="horror"/> </xsd:restriction> </xsd:simpleType> <!-- declare an attribute of this type --> <xsd:attribute name="genre" type="genreDType"/> </xsd:schema>
    There are an infinite number of creative things you can do with power like this - for more examples and information on the numerous facets available for the different datatypes, take a look at http://www.w3.org/TR/xmlschema-2/

    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

     
    Application Delivery: Everything You Wanted to Know, but Didn`t Know You Needed to Ask
    A comprehensive guide to examining the topics of Wide-area Data Services and app....

     
    Best Practices: Safe and Secure Hardware Asset Recovery
    Companies increasingly must meet EPA and local requirements for the disposal of ....

     
    Managing SSL Security in Multi-Server Environments
    Read this white paper to learn how to simplify management of your organization's....

     
    Open Source Security Myths
    Open Source Software (OSS) is computer software whose source code is available t....

     
    Power and Cooling Capacity Management for Data Centers
    This paper describes the principles for achieving power and cooling capacity man....

     




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