Home arrow XML arrow Page 9 - Understanding XML Schema

Different Flavours - 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
Now, if your validation rules are not very stringent, you don't usually need to derive new datatypes; the built-in ones suffice for most requirements. If, on the other hand, you need to restrict your data to specific ranges or values, you will find it necessary to create new datatypes and use them within your schema definition. Let's move on to a closer examination of how this is accomplished.

The XML Schema specification allows for the derivation of new datatypes from the built-in types. Typically, these new derivations are created by placing a restriction on the allowed range of values for a built-in datatype, via the <xsd:restriction> element.

You've already seen this element in some of the previous examples - in fact, here's a quick snippet to jog your memory:

<?xml version="1.0"?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <!-- other definitions - snip! --> <!-- create a new datatype --> <xsd:simpleType name="simpleDType"> <xsd:restriction base="xsd:integer"> <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>
In this case, a new datatype named "simpleDType" has been created using the built-in "integer" datatype as a base. The <xsd:minInclusive> and <xsd:maxInclusive> elements are used to specify an allowable range of values for this new datatype; these restrictions are referred to in schema jargon as "facets".

It's also possible to create restrictions on the basis of patterns or regular expressions - consider the following element definition, which defines a datatype for temperature values using the <xsd:pattern> facet. This derived datatype allows for a value containing a maximum of three digits, prefixed by an optional minus sign.

<?xml version="1.0"?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <!-- other definitions - snip! --> <!-- create a new datatype --> <xsd:simpleType name="temperatureDType"> <xsd:restriction base="xsd:string"> <xsd:pattern value="-?[0-9]{1,3}"/> </xsd:restriction> </xsd:simpleType> <!-- declare an element of this type --> <xsd:element name="temp" type="temperatureDType"/> </xsd:schema>
You can restrict an element to certain values with the <xsd:enumeration> facet - the following example creates a datatype which, when applied to an element declaration, restricts it to one of four values:

<?xml version="1.0"?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <!-- other definitions - snip! --> <!-- create a new datatype --> <xsd:simpleType name="flavourDType"> <xsd:restriction base="xsd:string"> <xsd:enumeration value="vanilla"/> <xsd:enumeration value="chocolate"/> <xsd:enumeration value="strawberry"/> <xsd:enumeration value="peach"/> </xsd:restriction> </xsd:simpleType> <!-- declare an element of this type --> <xsd:element name="flavour" type="flavourDType"/> </xsd:schema>
Why stop at elements? You can use the datatype above to restrict attributes to specific values as well. Consider the following derived datatype, which restricts element or attribute values to specific genres of film:

<?xml version="1.0"?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <!-- other definitions - snip! --> <!-- create a new datatype --> <xsd:simpleType name="genreDType"> <xsd:restriction base="xsd:NMTOKEN"> <xsd:enumeration value="romance"/> <xsd:enumeration value="comedy"/> <xsd:enumeration value="drama"/> <xsd:enumeration value="action"/> <xsd:enumeration value="horror"/> </xsd:restriction> </xsd:simpleType> <!-- declare an attribute of this type --> <xsd:attribute name="genre" type="genreDType"/> </xsd:schema>
There are an infinite number of creative things you can do with power like this - for more examples and information on the numerous facets available for the different datatypes, take a look at http://www.w3.org/TR/xmlschema-2/

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