Constraining Facets on List Datatypes

This section describes fundamental and constraining facets on list datatypes: 'length', 'minLength', 'maxLength', 'pattern', 'enumeration' and 'assertions'. New simple datatype can be defined with a 'restriction' component using a list datatype as the base.

From the previous tutorial, we know that a list datatype is defined as a datatype where the entire lexical representation is mapped to a list of items separated by whitespaces. And its item datatype is another atomic datatype or atomic union datatype. Now let's see what facets are supported on list datatypes.

A list construction supports 2 groups of facets: fundamental facets and constraining facets:

A list datatype has the following settings for its fundamental facets defined:

    ordered = false
    bounded = false
    cardinality = countably infinite
    numeric = false

A list datatype supports the following constraining facets:

    whiteSpace = collapse
    length = undefined
    minLength = 1
    maxLength = undefined
    pattern = undefined
    enumeration = undefined
    assertions = undefined

That means we can define a new simple datatype with a "restriction" construction component using a list datatype as the base. Some notes on how constraining facets are applied on list values:

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

<?xml version="1.1"?>
<!-- list_facet_test.xsd
 - Copyright (c) 2013, HerongYang.com, All Rights Reserved.
-->
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

<xs:simpleType name="strList">
  <xs:list itemType="xs:string"/>
</xs:simpleType>

<xs:simpleType name="intList">
  <xs:list>
    <xs:simpleType>
      <xs:restriction base="xs:unsignedByte">
        <xs:minInclusive value="1"/>
        <xs:maxInclusive value="49"/>
      </xs:restriction>
    </xs:simpleType>
  </xs:list>
</xs:simpleType>

<xs:element name="List_Facet_Test">
  <xs:complexType>
    <xs:sequence>

      <xs:element name="Winners" maxOccurs="unbounded">
        <!-- "restriction" on "list" datatype -->
        <xs:simpleType>
          <xs:restriction base="strList">
            <xs:length value="3"/>
            <xs:pattern value="\w{6} \w{5} \w{4}"/>
          </xs:restriction>
        </xs:simpleType>
      </xs:element>

      <xs:element name="WinningNumbers" maxOccurs="unbounded">
        <!-- "restriction" on "list" datatype -->
        <xs:simpleType>
          <xs:restriction base="intList">
            <xs:length value="6"/>
          </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"?>
<!-- list_facet_test.xml
 - Copyright (c) 2013, HerongYang.com, All Rights Reserved.
-->
<List_Facet_Test>

   <!-- Valid "Winners" list of 3 items -->
   <Winners>  Herong Maria Bill  </Winners>

   <!-- Invalid "Winners" values -->
   <Winners>  Lee    Maria     Bill </Winners>
   <Winners>  <![CDATA[B. Lee]]> Maria Bill </Winners>
   
   <!-- Valid "WinningNumbers" list values -->
   <WinningNumbers>  1 20 24 33 37 43  </WinningNumbers>

   <!-- Invalid "WinningNumbers" values -->
   <WinningNumbers>  1 20 24 33 37 43     49 </WinningNumbers>
</List_Facet_Test>

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

c:\Progra~1\Java\jdk1.7.0_07\bin\java XsdSchemaValidator 
   list_facet_test.xsd list_facet_test.xml
   
Error:
   Line number: 11
   Column number: 47
   Message: cvc-pattern-valid: Value 'Lee Maria Bill' is not facet-
   valid with respect to pattern '\w{6} \w{5} \w{4}' for type 
   '#AnonType_WinnersList_Facet_Test'. 
   (6 characters are needed on the first item)

Error:
   Line number: 11
   Column number: 47
   Message: cvc-type.3.1.3: The value 'Lee Maria Bill' of element 
   'Winners' is not valid.

Error:
   Line number: 12
   Column number: 55
   Message: cvc-pattern-valid: Value 'B. Lee Maria Bill' is not facet-
   valid with respect to pattern '\w{6} \w{5} \w{4}' for type 
   '#AnonType_WinnersList_Facet_Test'.
   (CDATA block is not able to "glue" togeter "B. Lee" as 1 item)

Error:
   Line number: 12
   Column number: 55
   Message: cvc-type.3.1.3: The value 'B. Lee Maria Bill' of element
   'Winners' is not valid.

Error:
   Line number: 18
   Column number: 63
   Message: cvc-length-valid: Value '1 20 24 33 37 43 49' with length
   = '7' is not facet-valid with respect to length '6' for type 
   '#AnonType_WinningNumbersList_Facet_Test'.
   (only 6 items are allowed)

Error:
   Line number: 18
   Column number: 63
   Message: cvc-type.3.1.3: The value '1 20 24 33 37 43 49' of element
   'WinningNumbers' is not valid.

Failed with errors: 6

Last update: 2013.

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 API

 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

 References

 PDF Printing Version