Home arrow XML arrow Page 7 - Understanding XML Schema

Battle Of The Sexes - 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
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
 

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: