Home arrow XML arrow Page 3 - Understanding XML Schema

An Evening At The Moulin Rouge - XML

Imagine a technology that offered you all the power of a DTD without the associated unpleasantness of those strange symbols and arcane commands. Sounds too good to be true? Say hello to XML Schema.

  1. Understanding XML Schema
  2. The Next Generation
  3. An Evening At The Moulin Rouge
  4. Simple Simon
  5. A Complex Web
  6. Nesting Season
  7. Battle Of The Sexes
  8. Dealing With The Ref
  9. Different Flavours
  10. When In Rome...
  11. Test Drive
  12. The Next Step
By: Harish Kamath, (c) Melonfire
Rating: starstarstarstarstar / 3
October 15, 2001

print this article


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

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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Google Docs and Xpath Data Functions
- Flex Array Collection Sort and Filtering
- The Flex Tree Control
- Flex List Controls
- Working with Flex and Datagrids
- 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...

Developer Shed Affiliates


Dev Shed Tutorial Topics: