Home arrow XML arrow Page 2 - Designing Your own XML Schema: Constraining with Restrictions
XML

Designing Your own XML Schema: Constraining with Restrictions


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

Author Info:
By: Jagadish Chaterjee
Rating: 5 stars5 stars5 stars5 stars5 stars / 9
March 13, 2006
TABLE OF CONTENTS:
  1. · Designing Your own XML Schema: Constraining with Restrictions
  2. · Working with other data types in XML Schema
  3. · Constraining values (restricting a range)
  4. · Restricting lengths in XML Schema
  5. · Restrictions based on lists (or enumerations) in XML Schema
  6. · Defining a Primary Key in XML Schema

print this article
SEARCH DEVARTICLES

Designing Your own XML Schema: Constraining with Restrictions - Working with other data types in XML Schema
(Page 2 of 6 )

In this section, we shall go through an example covering most of the important data types available in the XML Schema definition.  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="HireDate" type="xs:date"/>
                                          <xs:element name="isMarried" type="xs:boolean" minOccurs="0"/>
                                          <xs:element name="Salary" type="xs:float"/>
                                          <xs:element name="Age" type="xs:integer"/>
                                    </xs:sequence>
                                    <xs:attribute name="ID" type="xs:string" 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 a few more elements: “Name,” “Hiredate,” “isMarried,” “Salary” and “Age.”  You should observe that there could be any number of “Employee” elements within “Employee.” 

You should also observe that “Name,” “HireDate,” “isMarried,” “Salary” and “Age” are defined with different simple data types rather than with “complex types.”  The only complex elements are “Employees” and “Employee.”  I also declared an attribute ID for the “Employee” element as follows:

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

Another important point to observe is that the “isMarried” element is declared as optional.  This is possible by making “minOccurs” be “0” (default is “1”).

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:\Documents and
Settings\Administrator\Desktop\ForDec\ForDec\XSLT\XMLExamples\02
\SampleDataTypes.xsd
">
      <Employee ID="1001">
            <Name>Jag</Name>
            <HireDate>1978-06-25</HireDate>
            <isMarried>false</isMarried>
            <Salary>2500</Salary>
            <Age>27</Age>
      </Employee>
      <Employee ID="1002">
            <Name>Winner</Name>
            <HireDate>1985-09-08</HireDate>
            <Salary>2500</Salary>
            <Age>20</Age>
      </Employee>
</Employees>



blog comments powered by Disqus
XML ARTICLES

- 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 
Support 

Developer Shed Affiliates

 




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