Home arrow XML arrow Page 4 - Designing Your own XML Schema: Restrictions and User Defined Types

Designing Your own XML Schema: Restrictions and User Defined Types

This is the third article in a series which guides you in designing XML Schemas. Hopefully this series will take you from the most basic ideas to the more advanced topics without any hurdles.

Author Info:
By: Jagadish Chaterjee
Rating: 5 stars5 stars5 stars5 stars5 stars / 11
March 20, 2006
  1. · Designing Your own XML Schema: Restrictions and User Defined Types
  2. · Restrictions based on patterns in XML Schema
  3. · How to define your own simple type (user-defined data type) in XML Schema
  4. · Understanding the user-defined simple type in XML Schema
  5. · User-defined Simple Type with enumeration in XML Schema
  6. · Understanding the simple type with enumeration in XML Schema
  7. · User-defined Complex Type in XML Schema

print this article

Designing Your own XML Schema: Restrictions and User Defined Types - Understanding the user-defined simple type in XML Schema
(Page 4 of 7 )

Instead of explaining the whole XML schema at once (demonstrated in the previous section), let me elaborate in a few steps.  Let us consider the following fragment first:

      <xs:simpleType name="AgeType">
        <xs:restriction base="xs:integer">
          <xs:minInclusive value="1" />
          <xs:maxInclusive value="100" />

The above fragment declares a new data type (of the simple type in XML schema).  The name of the new simple data type is “AgeType.”  Once declared, I can use this “AgeType” as a data type for any element in my XML Schema.

In general, every new user-defined type will get inherited from an existing XML Schema type.  According to the above scenario, our “AgeType” is getting inherited from the “integer” type (xs:integer).  This means all characteristics of “xs:integer” are available to our new data type, “AgeType.” 

Once inherited from an existing type, we can refine it with our own restrictions, along with facets (if necessary).  According to the above definition, I restricted the “AgeType” in such a way that it would accept only the values between one and one hundred (both inclusive).  And that completes our user-defined type at the moment.

Now I need to use the new data type in my element definitions (in our sense, it would be the “Age” element).  Let us look at the following fragment:

      <xs:element name="Age" type="AgeType" />

The above line declares a new element, “Age,” which is based on our own user-defined simple type, “AgeType.”  And thus all the restrictions available for “AgeType,” get carried to the “Age” element accordingly for validations.  You can use the same “AgeType” any number of times within the same schema.  Availability of the “AgeType” to external schemas is also possible through namespaces and other means. I will cover this in future articles.

Apart from all of the above uses, there exists another advantage to the user-defined data types.  If we modify the “AgeType” definition (for instance, changed its restrictions), it would automatically apply to every element declared with that respective data type!

blog comments powered by Disqus

- 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 

Developer Shed Affiliates


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