Home arrow XML arrow Page 3 - An Introduction to XML Schemas

An Introduction to XML Schemas

XML Schemas help you control what elements appear in your XML documents. Similar to Document Type Definitions, they offer distinct advantages over the older syntax. Dan Wellman provides a basic overview, with examples to get you started working on your own.

Author Info:
By: Dan Wellman
Rating: 5 stars5 stars5 stars5 stars5 stars / 13
November 03, 2004
  1. · An Introduction to XML Schemas
  2. · Choose Your Schema
  3. · Element Declarations
  4. · Definitions and Elements
  5. · The Complete File
  6. · Validating a Document
  7. · A Few Final Tips

print this article

An Introduction to XML Schemas - Element Declarations
(Page 3 of 7 )

Next come the element declarations, which list the elements that must appear, may appear and in which order they appear. If you’ve read my Introduction to XML article, the news story xml document will be familiar to you; if you haven’t read it, the XML Schema document we’ll create will define an XML document consisting of a root element of <news> containing multiple child elements called <article> which in turn have child elements called <headline> and <story>.

When it comes to element declarations in schema documents, you can create either simple or complex definitions. Simple definitions declare elements that do not have any children or attributes and can only contain text, while complex definitions declare elements that can have children and attributes as well as text.

For our news document then, we will need to define the root element directly after the XML declaration. Add the following code:

<xsd:element name="news">
  <xsd:sequence maxOccurs="unbounded">
    <xsd:element ref="article"/>

What this markup does is declare the root element of the XML document with a complex type definition. As <news> is the root element, it will clearly contain other elements, in this case, the <article> element, which is declared using the <element ref> element.   The value of this element must be the same as the value of the name attribute in each child elements declaration. You’ll see what I mean in the next section of code. The <sequence> declaration also dictates the order that the XML elements must appear in.

You’ll notice also that each element has a prefix of xsd: which is in accordance with the schema and namespace declaration.

Declare Your Children

You declare child elements using sequences which specify which elements should appear, in the order that they appear. The maxOccurs attribute value of unbounded tells us that it can appear one to many times, with no upper limit. If this wasn’t specified, the default value for this and the minOccurs attribute is one.

The sequence element is what is known as a compositor, which is an element that contains references to other elements. In addition to the <sequence> compositor, there are also <choice> and <all> compositors; choice compositors give a choice of element references, any one of which may be used, and all compositors declare that any or all of the referenced elements may occur. 

We now need another complex definition for the <article> element, as this element will also contain child elements:

<xsd:element name="article">
    <xsd:sequence minOccurs="1" maxOccurs="1">
      <xsd:element ref="headline"/>
      <xsd:element ref="story"/>

As we only want one headline and one story per article, we set both the min and max Occurs values to 1.

We’ve now declared our two bottom level child elements. As both the <headline> and <story> elements in the XML document contain only text, and no other elements or attributes, we can declare both of these as simple definitions directly below the above code:

<xsd:element name="headline"/>
<xsd:element name="story"/>

blog comments powered by Disqus

- Open XML Finally Supported by MS Office
- XML Features Added to Two Systems
- Using Regions with XSL Formatting Objects
- Using XSL Formatting Objects
- More Schematron Features
- Schematron Patterns and Validation
- Using Schematron
- Datatypes and More in RELAX NG
- Providing Options in RELAX NG
- An Introduction to RELAX NG
- Path, Predicates, and XQuery
- Using Predicates with XQuery
- Navigating Input Documents Using Paths
- XML Basics
- Introduction to XPath

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2019 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials