XSD Tutorials - Herong's Tutorial Examples - Version 5.10, by Dr. Herong Yang

"simpleType" Components with "union" Child Components

This section describes the 'union' child component in 'simpleType' component. 'union' child components are used to create a union of value spaces from multiple member datatypes.

"union" child component in a "simpleType" component offers a different way of defining a new simple datatype by combining value spaces from multiple other simple datatypes.

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

Format 1:
<simpleType ...>
  <union memberTypes="member_type_list">
</simpleType>

Format 2:
<simpleType ...>
  <union>
    <simpleType ...>
      definition of member datatype 1
    </simpleType>
    <simpleType ...>
      definition of member datatype 2
    </simpleType>
    ...
  </union>
</simpleType>

Some notes on using the "union" component:

  • memberTypes="member_type_list" - Optional attribute to specify multiple datatypes to be used as members of the union. If not provided, member datatypes must be defined with child "simpleType" components.
  • Child "simpleType" components are always allowed to add more member datatypes, even memberTypes="member_type_list" is provided.

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

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

    <xs:element name="MixedData" maxOccurs="unbounded">
        <!-- "union" with "memberTypes" attribute -->
        <xs:simpleType>
          <xs:union memberTypes="xs:integer xs:NCName"/>
        </xs:simpleType>
      </xs:element>
    
    <xs:element name="Month" maxOccurs="unbounded">
        <!-- "union" with no "memberTypes" attribute -->
        <xs:simpleType>
          <xs:union>
            <xs:simpleType>
              <xs:restriction base="xs:unsignedByte">
                <xs:minInclusive value="1"/>
                <xs:maxInclusive value="12"/>
              </xs:restriction>
            </xs:simpleType>            
            <xs:simpleType>
              <xs:restriction base="xs:NCName">
<xs:pattern value="Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec"/>
              </xs:restriction>
            </xs:simpleType>
          </xs:union>
        </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_simpleType_test.xml
 - Copyright (c) 2013, HerongYang.com, All Rights Reserved.
-->
<Union_SimpleType_Test>

   <!-- Valid "MixedData" values -->
   <MixedData>  100000  </MixedData>
   <MixedData>  Million  </MixedData>

   <!-- Invalid "MixedData" values -->
   <MixedData>  Unit:Million  </MixedData>
   
   <!-- Valid "Month" values -->
   <Month>  5  </Month>
   <Month>  May  </Month>

   <!-- Invalid "Month" values -->
   <Month>  13  </Month>
   <Month>  may  </Month>
</Union_SimpleType_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_simpleType_test.xsd union_simpleType_test.xml
   
Error:
   Line number: 12
   Column number: 43
   Message: cvc-datatype-valid.1.2.3: '  Unit:Million  ' is not a 
   valid value of union type 
   '#AnonType_MixedDataUnion_SimpleType_Test'.

Error:
   Line number: 12
   Column number: 43
   Message: cvc-type.3.1.3: The value '  Unit:Million  ' of element 
   'MixedData' is not valid.

Error:
   Line number: 19
   Column number: 25
   Message: cvc-datatype-valid.1.2.3: '  13  ' is not a valid value of
   union type '#AnonType_MonthUnion_SimpleType_Test'.

Error:
   Line number: 19
   Column number: 25
   Message: cvc-type.3.1.3: The value '  13  ' of element 'Month' is 
   not valid.

Error:
   Line number: 20
   Column number: 26
   Message: cvc-datatype-valid.1.2.3: '  may  ' is not a valid value 
   of union type '#AnonType_MonthUnion_SimpleType_Test'.

Error:
   Line number: 20
   Column number: 26
   Message: cvc-type.3.1.3: The value '  may  ' of element 'Month' 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

"simpleType" Components with "union" Child Components - Updated in 2014, by Dr. Herong Yang