XML
  Home arrow XML arrow Designing Your own XML Schema: Constrainin...
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 
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

Designing Your own XML Schema: Constraining with Restrictions
By: Jagadish Chaterjee
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 5
    2006-03-13

    Table of Contents:
  • Designing Your own XML Schema: Constraining with Restrictions
  • Working with other data types in XML Schema
  • Constraining values (restricting a range)
  • Restricting lengths in XML Schema
  • Restrictions based on lists (or enumerations) in XML Schema
  • Defining a Primary Key in XML Schema

  • 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


    Designing Your own XML Schema: Constraining with Restrictions


    (Page 1 of 6 )

    This is second article in a series which guides you in designing XML Schemas right from the basics without any hurdles.

    If you are new to XML Schema, I strongly suggest you go through my first article in this series, which you can check out here

    Declaring attributes in XML Schema

    Since I already explained certain aspects of XML Schemas (defining them, simple types, complex types, and so on) in my previous article, I shall not repeat them again in this article.  Before discussing “constraints” (or restrictions), I need to cover two more topics.  Let us first complete them and then proceed to “restrictions” in XML schema.

    An “attribute” is very similar to characteristics of an element. The best example would be the <IMG> tag in HTML.  It accepts the file name of the image with the help of the “SRC” attribute.  Similarly, you can also define your own attributes (reasonably) within an XML Schema.

    Let us have a look at the following XML schema now:

    <?xml version="1.0" encoding="UTF-8"?>
    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
          <xs:element name="Employees">
                <xs:annotation>
                      <xs:documentation>Comment describing your root
    element
    </xs:documentation>
                </xs:annotation>
                <xs:complexType>
                      <xs:sequence>
                            <xs:element name="Employee"
    maxOccurs="unbounded">
                                  <xs:complexType>
                                        <xs:sequence>
                                              <xs:element name="Name"
    type="xs:string"/>
                                              <xs:element name="Age" type="xs:int"/>
                                        </xs:sequence>
                                        <xs:attribute name="ID"
    type="xs:int" use="required"/>
                                  </xs:complexType>
                            </xs:element>
                      </xs:sequence>
                </xs:complexType>
          </xs:element>
    </xs:schema>



    According to the above example, the document root element would be “Employees.”  The element “Employees” internally can contain “Employee” elements.  The element “Employee” internally contains two more elements, “Name” and “Age.”  You should observe that there could be any number of “Employee” elements within “Employee.” 

    You should also observe that “Name” and “Age” are defined with “simple types” rather than with “complex types.”  The only complex elements are “Employees” and “Employee.”  The most important declaration within the above schema is as follows:

    <xs:attribute name="ID" type="xs:int" use="required"/>

    The above statement makes sure that every “Employee” element is provided with the attribute “ID” (which is of type “int”).

    Now, how would the XML document look when it conformed to the above XML Schema?  The following would be a sample XML document which conforms to the XML Schema above.

    <?xml version="1.0" encoding="UTF-8"?>
    <Employees xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="C:\SampleAttributes.xsd">
          <Employee ID="1001">
                <Name>Jag</Name>
                <Age>27</Age>
          </Employee>
          <Employee ID="1002">
                <Name>Winner</Name>
                <Age>20</Age>
          </Employee>
    </Employees>

    More XML Articles
    More By Jagadish Chaterjee


       · Hai, this is another extension of the XML Schema series. Enjoy.
     

    XML ARTICLES

    - 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
    - Simple Web Syndication with RSS 2.0
    - Java UI Design with an IDE







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek