SunQuest
 
       XML
  Home arrow XML arrow Page 7 - 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 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Actuate Whitepapers 
VeriSign Whitepapers 
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

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    Understanding XML Schema - Battle Of The Sexes


    (Page 7 of 12 )

    So that takes care of elements - but what about attributes?

    Attribute definitions typically follow element definitions, and are declared with the <xsd:attribute> element.

    <xsd:attribute name="genre" type="xsd:string" />
    An optional "use" attribute can be used to specify whether or not the attribute is optional.

    <!-- this attribute is optional --> <xsd:attribute name="genre" type="xsd:string" use="optional" /> <!-- this attribute is required--> <xsd:attribute name="id" type="xsd:integer" use="required" /> <!-- this attribute is not allowed --> <xsd:attribute name="address" type="xsd:string" use="prohibited" />
    In order to see this in action, let's suppose I altered my document instance to include an attribute:

    <?xml version="1.0"?> <movie genre="romance"> <title>Moulin Rouge</title> <cast> <person>Nicole Kidman</person> <person>Ewan McGregor</person> </cast> <rating>5</rating> </movie>
    Here's what my schema would look like:

    <?xml version="1.0"?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <xsd:element name="movie"> <xsd:complexType> <xsd:sequence> <!-- simple element definition --> <xsd:element name="title" type="xsd:string"/> <!-- complex element definition --> <xsd:element name="cast"> <xsd:complexType> <xsd:sequence> <xsd:element name="person" type="xsd:string" maxOccurs="unbounded" /> </xsd:sequence> </xsd:complexType> </xsd:element> <!-- simple element definition --> <xsd:element name="release_date" type="xsd:date" maxOccurs="1" minOccurs="0"/> <!-- simple element definition --> <xsd:element name="rating" type="xsd:integer"/> </xsd:sequence> <!-- attribute definition --> <xsd:attribute name="genre" type="xsd:string" /> </xsd:complexType> </xsd:element> </xsd:schema>
    How about adding a twist? Let's suppose every "person" element needs an additional "sex" attribute, like this:

    <?xml version="1.0"?> <movie genre="romance"> <title>Moulin Rouge</title> <cast> <person sex="female">Nicole Kidman</person> <person sex="male">Ewan McGregor</person> </cast> <rating>5</rating> </movie>
    This apparently minor change has quite a significant impact on the schema definition, because the "person" element, previously defined as a simple type, must now be defined as a complex type. Since the "person" element does not have any nested child elements, we can pass up the <xsd:sequence> instruction within the element definition in favour of the <xsd:simpleContent> element and include an attribute definition within this element.

    Here's the revised schema definition:

    <?xml version="1.0"?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <xsd:element name="movie"> <xsd:complexType> <xsd:sequence> <!-- simple element definition - title --> <xsd:element name="title" type="xsd:string"/> <!-- complex element definition - cast --> <xsd:element name="cast"> <xsd:complexType> <xsd:sequence> <!-- since person has an attribute, it must be defined as a complex element --> <xsd:element name="person" maxOccurs="unbounded"> <xsd:complexType> <xsd:simpleContent> <xsd:restriction base="xsd:string"> <!-- attribute definition - sex --> <xsd:attribute name="sex" type="xsd:string" /> </xsd:restriction> </xsd:simpleContent> </xsd:complexType> </xsd:element> </xsd:sequence> </xsd:complexType> </xsd:element> <!-- simple element definition - release date --> <xsd:element name="release_date" type="xsd:date" maxOccurs="1" minOccurs="0"/> <!-- simple element definition - rating --> <xsd:element name="rating" type="xsd:integer"/> </xsd:sequence> <!-- attribute definition for movie genre --> <xsd:attribute name="genre" type="xsd:string" /> </xsd:complexType> </xsd:element> </xsd:schema>

    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

    Click Here




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