Constraining Facets on Union Datatypes

This section describes constraining facets on union datatypes. New simple datatype can be defined with a 'restriction' construction component using a union datatype as the base.

From the previous tutorial, we know that there are 2 categories of union datatypes:

Both atomic union datatypes and list union datatypes support the following constraining facets:

    pattern = undefined
    enumeration = undefined
    assertions = undefined

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

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

<xs:simpleType name="atomicUnion">
  <xs:union memberTypes="xs:positiveInteger xs:negativeInteger"/>
</xs:simpleType>

<xs:simpleType name="listUnion">
  <xs:union memberTypes="xs:gYear">
    <xs:simpleType>
      <xs:list itemType="xs:unsignedByte"/>
    </xs:simpleType>
  </xs:union>
</xs:simpleType>

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

      <xs:element name="AtomicUnion" maxOccurs="unbounded">
        <!-- "restriction" on atomic list datatype -->
        <xs:simpleType>
          <xs:restriction base="atomicUnion">
            <!-- "maxInclusive" not allowed on union
            <xs:maxInclusive value="999"/> -->
            <xs:pattern value="-?\d\d\d|\d"/>
          </xs:restriction>
        </xs:simpleType>
      </xs:element>

      <xs:element name="ListUnion" maxOccurs="unbounded">
        <!-- "restriction" on list union datatype -->
        <xs:simpleType>
          <xs:restriction base="listUnion">
            <xs:pattern value="\d\d\d\d|\d\d \d\d"/>
          </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"?>
<!-- union_facet_test.xml
 - Copyright (c) 2013, HerongYang.com, All Rights Reserved.
-->
<Union_Facet_Test>

   <!-- Valid "AtomicUnion" list of 3 items -->
   <AtomicUnion>  999  </AtomicUnion>
   <AtomicUnion>  100  </AtomicUnion>

   <!-- Invalid "AtomicUnion" values -->
   <AtomicUnion>  1000 </AtomicUnion>
   <AtomicUnion>  0 </AtomicUnion>
   
   <!-- Valid "ListUnion" list values -->
   <ListUnion>  2013  </ListUnion>
   <ListUnion>  11 22  </ListUnion>

   <!-- Invalid "ListUnion" values -->
   <ListUnion>  -2013 </ListUnion>
   <ListUnion>  11 22 33 </ListUnion>
</Union_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 
   union_facet_test.xsd union_facet_test.xml
   
Error:
   Line number: 12
   Column number: 38
   Message: cvc-pattern-valid: Value '1000' is not facet-valid with 
   respect to pattern '-?\d\d\d|\d' for type 
   '#AnonType_AtomicUnionUnion_Facet_Test'.

Error:
   Line number: 12
   Column number: 38
   Message: cvc-type.3.1.3: The value '1000' of element 'AtomicUnion' 
   is not valid.

Error:
   Line number: 13
   Column number: 35
   Message: cvc-datatype-valid.1.2.3: '0' is not a valid value of 
   union type '#AnonType_AtomicUnionUnion_Facet_Test'.

Error:
   Line number: 13
   Column number: 35
   Message: cvc-type.3.1.3: The value '0' of element 'AtomicUnion' is
   not valid.

Error:
   Line number: 20
   Column number: 35
   Message: cvc-pattern-valid: Value '-2013' is not facet-valid with 
   respect to pattern '\d\d\d\d|\d\d \d\d' for type 
   '#AnonType_ListUnionUnion_Facet_Test'.

Error:
   Line number: 20
   Column number: 35
   Message: cvc-type.3.1.3: The value '-2013' of element 'ListUnion' 
   is not valid.

Error:
   Line number: 21
   Column number: 38
   Message: cvc-pattern-valid: Value '11 22 33' is not facet-valid 
   with respect to pattern '\d\d\d\d|\d\d \d\d' for type 
   '#AnonType_ListUnionUnion_Facet_Test'.

Error:
   Line number: 21
   Column number: 38
   Message: cvc-type.3.1.3: The value '11 22 33' of element 
   'ListUnion' is not valid.

Failed with errors: 8

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