XML
  Home arrow XML arrow Page 3 - 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  
Dedicated Servers  
Moblin 
JMSL Numerical Library 
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

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 / 3
    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 - Constraining values (restricting a range)


    (Page 3 of 6 )

    Those who design databases should definitely know about “constraints.”  It is the most important aspect of “data integrity” in any of the relational databases.  A constraint is basically a type of “condition” to accept proper values.  Fox example, employee ids should never repeat; age cannot be more than 120; salary cannot be more than $10,000 per month; and so on.

    Such types of conditions are quite common in any scenario and especially in databases.  They are common not only for databases, but also quite common for XML Schemas!

    Now let us work with a simple practical scenario.  I would like to have an element “AGE” be present in my XML Schema (very similar to the examples in the previous sections).  I can assign a data type “integer” to it.  But this is not sufficient,  because I would like to have the value restricted to between 0 and 100 (whereas an integer would accept more than that).  This means a “restriction” is necessary.

    Let us see the XML Schema supporting the age “restriction:”

    <?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>Contains All Employee information</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">
                                                    <xs:simpleType>
                                                          <xs:restriction base="xs:integer">
                                                                <xs:minInclusive value="0" />
                                                                <xs:maxInclusive value="100" />
                                                          </xs:restriction>
                                                    </xs:simpleType>
                                              </xs:element>
                                        </xs:sequence>
                                        <xs:attribute name="ID" type="xs:string" use="required" />
                                  </xs:complexType>
                            </xs:element>
                      </xs:sequence>
                </xs:complexType>
          </xs:element>
    </xs:schema>



    Let us concentrate on the “age” element.  It stays under the “Employee” element.  Its “simpleType” (or data type) is defined as an integer type.  And finally, we applied the “restriction” by specifying minimum and maximum values as follows:

    <xs:element name="age">
          <xs:simpleType>
                <xs:restriction base="xs:integer">
                      <xs:minInclusive value="0" />
                      <xs:maxInclusive value="100" />
                </xs:restriction>
          </xs:simpleType>
    </xs:element>

    We can also modify the above fragment to accept values between 1 and 99 (both inclusive) as follows:

    <xs:element name="age">
          <xs:simpleType>
                <xs:restriction base="xs:integer">
                      <xs:minExclusive value="0" />
                      <xs:maxExclusive value="100" />
                </xs:restriction>
          </xs:simpleType>
    </xs:element>

    You can use “Exclusive” or “Inclusive” according to your requirements (they can even be interchanged).  But care should be taken while defining the same (using tools).

    More XML Articles
    More By Jagadish Chaterjee


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

    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 1 hosted by Hostway