|
Resources | Buyer's Guide | Newsletter | Safari Bookshelf | ||
Edit and debug XSLT 1.0/2.0 and XQuery code - Download Altova XMLSpy 2005 today! | ||
Annotated XML What is XML? What is XSLT? What is XSL-FO? What is XLink? What is XML Schema? What is XQuery? What is RDF? What is RSS? What are Topic Maps? What are Web Services? What are XForms? XSLT Recipe of the Day
Manage Your Account
Syntax Checker |
Using W3C XML Schemaby Eric van der VlistOctober 17, 2001 This article is an introduction to using W3C XML Schemas, and also includes a comprehensive reference to the Schema datatypes and structures. (Editor's note: this tutorial has been updated since its first publication in 2000, to reflect the finalization of W3C XML Schema as a Recommendation.) Introducing our First SchemaLet's start by having a look at this simple document which describes a book: <?xml version="1.0" encoding="UTF-8"?> <book isbn="0836217462"> <title> Being a Dog Is a Full-Time Job </title> <author>Charles M. Schulz</author> <character> <name>Snoopy</name> <friend-of>Peppermint Patty</friend-of> <since>1950-10-04</since> <qualification> extroverted beagle </qualification> </character> <character> <name>Peppermint Patty</name> <since>1966-08-22</since> <qualification>bold, brash and tomboyish</qualification> </character> </book> Get a copy of library1.xml for reference. To write a schema for this document, we could simply follow its structure
and define each element as we find it. To start, we open a <?xml version="1.0" encoding="UTF-8"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> .../... </xs:schema> The To match the start tag for the <xs:element name="book"> <xs:complexType> <xs:sequence> .../... </xs:sequence> .../... </xs:complexType> </xs:element> The Now we can define the title and author elements as simple types -- they don't
have attributes or non-text children and can be described directly within a
degenerate <xs:element name="title" type="xs:string"/> <xs:element name="author" type="xs:string"/> Now, we must deal with the <xs:element name="character" minOccurs="0" maxOccurs="unbounded"> <xs:complexType> <xs:sequence> .../... </xs:sequence> </xs:complexType> </xs:element> Unlike other schema definition languages, W3C XML Schema lets us define the
cardinality of an element (i.e. the number of its possible occurrences) with
some precision. We can specify both We specify then the list of all its children in the same way: <xs:element name="name" type="xs:string"/> <xs:element name="friend-of" type="xs:string" minOccurs="0" maxOccurs="unbounded"/> <xs:element name="since" type="xs:date"/> <xs:element name="qualification" type="xs:string"/>
And we terminate its description by closing the We can now declare the attributes of the document elements, which must always come last. There appears to be no special reason for this, but the W3C XML Schema Working Group has considered that it was simpler to impose a relative order to the definitions of the list of elements and attributes within a complex type, and that it was more natural to define the attributes after the elements. <xs:attribute name="isbn" type="xs:string"/> And close all the remaining elements. That's it! This first design, sometimes known as "Russian Doll Design" tightly follows the structure of our example document. One of the key features of such a design is to define each element and attribute within its context and to allow multiple occurrences of a same element name to carry different definitions. Complete listing of this first example: <?xml version="1.0" encoding="utf-8"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="book"> <xs:complexType> <xs:sequence> <xs:element name="title" type="xs:string"/> <xs:element name="author" type="xs:string"/> <xs:element name="character" minOccurs="0" maxOccurs="unbounded"> <xs:complexType> <xs:sequence> <xs:element name="name" type="xs:string"/> <xs:element name="friend-of" type="xs:string" minOccurs="0" maxOccurs="unbounded"/> <xs:element name="since" type="xs:date"/> <xs:element name="qualification" type="xs:string"/> </xs:sequence> </xs:complexType> </xs:element> </xs:sequence> <xs:attribute name="isbn" type="xs:string"/> </xs:complexType> </xs:element> </xs:schema> Download this schema: library1.xsd The next section explores how to subdivide schema designs to make them more readable and maintainable. |
Sponsored By: |
|
|
Contact Us | Our Mission | Privacy Policy | Advertise With Us | | Submissions Guidelines | |
Copyright © 2005 O'Reilly Media, Inc. |