XML
  Home arrow XML arrow Page 3 - An Introduction to XML Schemas
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
XML

An Introduction to XML Schemas
By: Dan Wellman
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 12
    2004-11-03

    Table of Contents:
  • An Introduction to XML Schemas
  • Choose Your Schema
  • Element Declarations
  • Definitions and Elements
  • The Complete File
  • Validating a Document
  • A Few Final Tips

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    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:complexType>
      <xsd:sequence maxOccurs="unbounded">
        <xsd:element ref="article"/>
      </xsd:sequnce>
      </xsd:complexType>
    </xsd:element>

    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:complexType>
        <xsd:sequence minOccurs="1" maxOccurs="1">
          <xsd:element ref="headline"/>
          <xsd:element ref="story"/>
        </xsd:sequence>
      </xsd:complexType>
    </xsd:element>

    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"/>

    More XML Articles
    More By Dan Wellman


       · Hi,On page 3 of 7,...
       · Thanks for pointing that one out, a small error but one that will no doubt have...
     

    XML ARTICLES

    - 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
    - Simple Web Syndication with RSS 2.0
    - Java UI Design with an IDE
    - UI Design with Java and XML Toolkits
    - Displaying ADO Retrieved Data with XML Islan...
    - Widget Walkthrough
    - Introduction to Widgets
    - The Why and How of XML Data Islands






    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
    Stay green...Green IT