Home arrow XML arrow Designing Your own XML Schema: Restrictions and User Defined Types
SEARCH DEVARTICLES

TOOLS YOU CAN USE

advertisement
Designing Your own XML Schema: Restrictions and User Defined Types
(Page 1 of 7 )

If you are new to XML Schema, I strongly suggest you to go through my first article in the same series, which you can find here

Restricting the digits in an XML Schema

As, I already explained various aspects of XML Schemas, including their definition, simple types and complex types, restrictions and so forth in my previous articles, I shall not repeat them again in this article. 

Let us consider that we would like to have any employee's salary to be restricted to only certain digits (towards both precision and scale).  This is possible with an existing schema type, “decimal.”  Let us look at the following complete schema:

<?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="ID" type="xs:string" />
              <xs:element name="Name" type="xs:string" />
              <xs:element name="Salary">
                <xs:simpleType>
                  <xs:restriction base="xs:decimal">
                    <xs:totalDigits value="7" />
                    <xs:fractionDigits value="2" />
                  </xs:restriction>
                </xs:simpleType>
              </xs:element>
            </xs:sequence>
          </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="Salary">
  <xs:simpleType>
    <xs:restriction base="xs:decimal">
      <xs:totalDigits value="7" />
      <xs:fractionDigits value="2" />
    </xs:restriction>
  </xs:simpleType>
</xs:element>

The most important parts are the “totalDigits” and “fractionDigitis” which do our restriction.


blog comments powered by Disqus
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

Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 



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