SunQuest
 
       XML
  Home arrow XML arrow Page 3 - 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 
Moblin 
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

    AT&T devCentral & BlackBerry(r) Webcast Series: BlackBerry and GPS -Build Location Awareness into your BlackBerry Applications, July 10th -1:00PM EST. Register Today!

    Understanding XML Schema - An Evening At The Moulin Rouge


    (Page 3 of 12 )

    Perhaps the best way to illustrate the difference between a DTD and a schema is with an example - consider the following XML document,

    <?xml version="1.0"?> <movie> <title>Moulin Rouge</title> <director>Baz Luhrmann</director> </movie>
    and its associated DTD.

    <!ELEMENT movie (title, director)> <!ELEMENT director (#PCDATA)> <!ELEMENT title (#PCDATA)>
    If I were to convert this DTD into an XML Schema, here's what it 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> <xsd:element name="title" type="xsd:string"/> <xsd:element name="director" type="xsd:string"/> </xsd:sequence> </xsd:complexType> </xsd:element> </xsd:schema>
    Let's dissect this a little:

    A schema is a well-formed XML document. It even begins with the standard XML document prolog, which specifies the XML version and document character set.

    <?xml version="1.0"?>
    The outermost element in a schema is the <xsd:schema> element, which includes a namespace declaration to associate the document with the XML Schema namespace.

    <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    This namespace declaration tells the parser that elements specific to the schema will be declared with the "xsd:" prefix; this is necessary both to avoid clashes with user-defined element names, and to allow the parser to distinguish between schema instructions and non-schema elements.

    The outermost <xsd:schema> element is a container; it encloses the element and attribute definitions which constitute the validation rules of the schema. Don't worry about these for the moment, I'll be discussing them shortly.

    Once all the validation rules have ended, the document is closed with a

    </xsd:schema>
    Now, the schema itself is only one part of the puzzle - the other part is the document instance which will be validated against the schema. Typically, the two are linked together by adding a special attribute indicating the location of the schema to the XML document instance. Using the document instance above, we have

    <movie xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="movie.xsd"> <title>Moulin Rouge</title> <director>Baz Luhrmann</director> </movie>
    The "xsi:noNamespaceSchemaLocation" attribute holds the location of the schema against which this document is to be validated. This attribute is used to validate specific document instances against a schema (which is why it comes from the XML Schema Instance namespace.)

    An alternative to this is the "xsi:schemaLocation" attribute, which specifies a schema location with a corresponding namespace location (a detailed explanation of this is beyond the scope of this tutorial, but take a look at the XML Schema specification for examples and more information.)

    <movie xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.mydomain.com/movie/movie-namespace http://www.mydomain.com/movie/movie.xsd"> <title>Moulin Rouge</title> <director>Baz Luhrmann</director> </movie>
    In addition to these two methods, different XML processors may allow you to specify schema locations in different ways (for example, on the command line.)

    Finally, with your schema and document instance linked together, it's time to take them for a test drive. A good processor to use is XSV, an open-source XML Schema Validator from the University of Edinburgh and the W3C - you can download it from http://www.w3.org/XML/Schema#Tools. Alternatively, take a look at XMLSpy, a powerful and very cool XML editor available for download at http://www.xmlspy.com/, or visit the W3C's software page at http://www.w3.org/XML/Schema#Tools

    Here's what XSV has to say when I run it on my "movie.xml" document instance above:

    $ xsv movie.xml <?xml version='1.0'?> <xsv docElt='{None}movie' instanceAssessed='true' instanceErrors='0' rootType='[Anonymous]' schemaErrors='0' schemaLocs='None -> movie.xsd' target='/usr/local/apache/htdocs/movie.xml' validation='strict' version='XSV 1.202/1.105 of 2001/08/30 16:12:04' xmlns='http://www.w3.org/2000/05/xsv'> <schemaDocAttempt URI='/usr/local/apache/htdocs/movie.xsd' outcome='success' source='schemaLoc'/> </xsv>

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