Home arrow XML arrow Page 4 - Understanding XML Schema

Simple Simon - 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.

TABLE OF CONTENTS:
  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
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement
With the broad overview out of the way, let's now focus on the different elements (pun definitely intended!) that make up a schema.

The XML Schema specification makes a basic distinction between "simple" and "complex" elements. Simple elements cannot contain other elements or possess additional attributes; complex elements can have additional attributes and serve as containers for other elements (which themselves may be either simple or complex).

Within a schema, these two element types are represented by the <xsd:simpleType> and <xsd:complexType> elements respectively.

Simple elements can be represented in two ways. The first (and simplest) method is to use the <xsd:element> declaration with a built-in datatype - the following simple element

<title>Moulin Rouge</title>
would be represented in a schema by

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <xsd:element name="title" type="xsd:string"/> </xsd:schema>
When the datatype name is preceded by the "xsd:" prefix, it indicates a predefined datatype and not a new, user-defined type. The XML Schema specification lists about forty different built-in datatypes, including "string", "integer", "decimal", "float", "boolean", "time", "date", "dateTime" and "anyURI". However, in case these are too generic for you, it's also possible to derive your own custom datatype from the built-in ones, and then declare simple elements using this custom datatype.

Consider the following schema definition, which is equivalent to the one above:

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <!-- define a new datatype --> <xsd:simpleType name="simpleDType"> <xsd:restriction base="xsd:string"></xsd:restriction> </xsd:simpleType> <!-- declare an element of this type --> <xsd:element name="title" type="simpleDType"/> </xsd:schema>
Typically, this second method is used only when a schema author needs to restrict the values of a particular simple element over and above the constraints inherent in a specific datatype. Here's a more constructive definition, which restricts the values of the "rating" element to an integer between 1 and 10.

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <!-- define a new datatype --> <xsd:simpleType name="simpleDType"> <xsd:restriction base="xsd:integer"> <!-- restrict integer values between 1 and 10 --> <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>
I'll be discussing the derivation of new datatypes in more detail a little further down, so don't worry too much if the syntax seems a little unfamiliar. The important thing to note here is the two different options open to you while defining a simple element.

 
 
>>> More XML Articles          >>> More By Harish Kamath, (c) Melonfire
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

XML ARTICLES

- 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: