Home arrow XML arrow Page 5 - 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 - Restrictions based on lists (or enumerations) in XML Schema
(Page 5 of 6 )

Let us consider that we would like to have a department assigned (or specified) to every employee in our XML document.  The department sometimes could be misspelled.  So we need to provide a “list of values” (or enumeration) which are the only valid values to accept!

The following complete schema would do the same:

<?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="Department">
                                                <xs:simpleType>
                                                      <xs:restriction base="xs:string">
                                                            <xs:enumeration value="Production" />
                                                            <xs:enumeration value="Sales" />
                                                            <xs:enumeration value="Accounting" />
                                                      </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>



You need to observe the following fragment within the above complete schema, which actually does the restriction:

<xs:element name="Department">
      <xs:simpleType>
            <xs:restriction base="xs:string">
                  <xs:enumeration value="Production" />
                  <xs:enumeration value="Sales" />
                  <xs:enumeration value="Accounting" />
            </xs:restriction>
      </xs:simpleType>
</xs:element>

You can include as many enumerations as possible within an XML schema.  In the above fragment, I specified only “string” based enumerations.  You can also work with “integer,” “date” and other types of enumerations as well.


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