"simpleType" Components with "restriction" Child Components

This section describes the 'restriction' child component in 'simpleType' component. 'restriction' child components are used to reduce the value space on a base datatype by changing its constraining facets.

In earlier tutorials, we learned datatype facets can be modified to create new datatypes using "restriction" components. Now let's take closer look at the "restriction" component.

Here is an XSD template showing you how to use the "restriction" as a child component in a "simpleType" component:

Format 1:
<simpleType ...>
  <restriction base="base_type">
    facet component
    facet component
    ...
  </restriction>
</simpleType>

Format 2:
<simpleType ...>
  <restriction>
    <simpleType ...>
      definition of the base datatype
    </simpleType>

    facet component
    facet component
    ...
  </restriction>
</simpleType>

Some notes on using the "restriction" component:

For example, the following XSD document uses "restriction" components to define 2 new simple datatypes:

<?xml version="1.1"?>
<!-- restriction_simpleType_test.xsd
 - Copyright (c) 2002-2013 HerongYang.com. All Rights Reserved.
-->
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Restriction_SimpleType_Test">
  <xs:complexType>
    <xs:sequence>

      <xs:element name="IPAddress" maxOccurs="unbounded">
        <!-- "restriction" with no "base" attribute -->
        <xs:simpleType>
          <xs:restriction>
            <xs:simpleType>
              <xs:restriction base="xs:string">
<xs:pattern value="\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}"/>
              </xs:restriction>
            </xs:simpleType>
            <xs:maxLength value="15"/>
          </xs:restriction>
        </xs:simpleType>
      </xs:element>

      <xs:element name="Port" maxOccurs="unbounded">
        <!-- "restriction" with "base" attribute -->
        <xs:simpleType>
          <xs:restriction base="xs:unsignedByte">
            <xs:enumeration value="80"/>
            <xs:enumeration value="23"/>
          </xs:restriction>
        </xs:simpleType>
      </xs:element>

    </xs:sequence>
  </xs:complexType>
</xs:element>

</xs:schema>

Here is a sample XML document to test the above XSD document:

<?xml version="1.1"?>
<!-- restriction_simpleType_test.xml
 - Copyright (c) 2002-2013 HerongYang.com. All Rights Reserved.
-->
<Restriction_SimpleType_Test>

   <!-- Valid "IPAddress" values -->
   <IPAddress>127.0.0.1</IPAddress>

   <!-- Invalid "IPAddress" values -->
   <IPAddress>::1:</IPAddress>

   <!-- Valid "Port" values -->
   <Port>80</Port>

   <!-- Invalid "Port" values -->
   <Port>8080</Port>

</Restriction_SimpleType_Test>

When validating this XML document with my XsdSchemaValidator.java program presented earlier in the book, I get these errors:

herong> java XsdSchemaValidator
^^^ restriction_simpleType_test.xsd restriction_simpleType_test.xml

Error:
   Line number: 11
   Column number: 31
   Message: cvc-pattern-valid: Value '::1:' is not facet-valid with
   respect to pattern '\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}' for type
   '#AnonType_IPAddressRestriction_SimpleType_Test'.

Error:
   Line number: 11
   Column number: 31
   Message: cvc-type.3.1.3: The value '::1:' of element 'IPAddress' is
   not valid.

Error:
   Line number: 17
   Column number: 21
   Message: cvc-enumeration-valid: Value '8080' is not facet-valid
   with respect to enumeration '[80, 23]'. It must be a value from the
   enumeration.

Error:
   Line number: 17
   Column number: 21
   Message: cvc-type.3.1.3: The value '8080' of element 'Port' is not
   valid.

Failed with errors: 4

Table of Contents

 About This Book

 Introduction to XML Schema

 XML Editor and Schema Processor - XMLPad

 Java API for XML Processing - JAXP

 JAXP - XML Schema (XSD) Validation

 Xerces2 Java Parser - Java API of XML Parsers

 Using Xerces2 Java APIs

 XML Schema Language - Basics

 Introduction of XSD Built-in Datatypes

 "string" and Its Derived Datatypes

 "decimal" and Its Derived Datatypes

 "dateTime" and Its Related Datatypes

 Miscellaneous Built-in Datatypes

 Facets, Constraining Facets and Restriction Datatypes

"simpleType" - Defining Your Own Simple Datatypes

 What Is Simple Datatype?

 "simpleType" Components - User-Defined Simple Datatypes

"simpleType" Components with "restriction" Child Components

 "simpleType" Components with "union" Child Components

 "simpleType" Components with "list" Child Components

 Atomic, List, Atomic Union and List Union Datatypes

 Constraining Facets on List Datatypes

 Constraining Facets on Union Datatypes

 Nested List Datatypes - Not Allowed

 Nested Atomic and List Union Datatypes

 Complex Element Declaration

 Identity-Constraints: unique, key and keyref

 Assertion as Custom Validation Rules

 XML Schema Location and Namespace in XML Documents

 Overriding Element Types in XML Documents

 Linking Multiple Schema Documents Together

 Glossary

 Archived Tutorials

 References

 Full Version in PDF/EPUB