Nested Atomic and List Union Datatypes

This section provides a tutorial example on how atomic union and list union datatypes can be used as member datatypes to construct nested union datatypes.

Comparing to "list" construction component, "union" is more open. It can be nested on top of other atomic, list, atomic union and list union datatypes to build nested union datatypes.

For example, the following XSD document uses "union" components nested with a "union" or "list" sub component define 2 new simple datatypes:

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

<xs:simpleType name="mixedUnion">
  <xs:union memberTypes="xs:time xs:NCName"/>
</xs:simpleType>

<xs:simpleType name="integerList">
  <xs:list itemType="xs:integer"/>
</xs:simpleType>

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

      <xs:element name="UnionOfList" maxOccurs="unbounded">
        <!-- "union" of list and atomic datatypes -->
        <xs:simpleType>
          <xs:union memberTypes="integerList xs:NCName"/>
        </xs:simpleType>
      </xs:element>

      <xs:element name="UnionOfUnion" maxOccurs="unbounded">
        <!-- "union" of union and list datatypes -->
        <xs:simpleType>
          <xs:union memberTypes="mixedUnion integerList"/>
        </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"?>
<!-- nested_union_test.xml
 - Copyright (c) 2002-2013 HerongYang.com. All Rights Reserved.
-->
<Nested_Union_Test>

   <!-- Valid "UnionOfList" list of 3 items -->
   <UnionOfList>  01   02   03 </UnionOfList>
   <UnionOfList>  xsd  </UnionOfList>

   <!-- Invalid "UnionOfList" values -->
   <UnionOfList>  xsd   schema   </UnionOfList>

   <!-- Valid "UnionOfUnion" list values -->
   <UnionOfUnion>  01  02  03  </UnionOfUnion>
   <UnionOfUnion>  01:02:03  </UnionOfUnion>
   <UnionOfUnion>  xsd  </UnionOfUnion>

   <!-- Invalid "UnionOfUnion" values -->
   <UnionOfUnion>  xsd:schema </UnionOfUnion>
   <UnionOfUnion>  xsd   schema   </UnionOfUnion>
</Nested_Union_Test>

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

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

Error:
   Line number: 12
   Column number: 48
   Message: cvc-datatype-valid.1.2.3: '  xsd   schema   ' is not a
   valid value of union type '#AnonType_UnionOfListNested_Union_Test'.

Error:
   Line number: 12
   Column number: 48
   Message: cvc-type.3.1.3: The value '  xsd   schema   ' of element
   'UnionOfList' is not valid.

Error:
   Line number: 20
   Column number: 46
   Message: cvc-datatype-valid.1.2.3: '  xsd:schema ' is not a valid
   value of union type '#AnonType_UnionOfUnionNested_Union_Test'.

Error:
   Line number: 20
   Column number: 46
   Message: cvc-type.3.1.3: The value '  xsd:schema ' of element
   'UnionOfUnion' is not valid.

Error:
   Line number: 21
   Column number: 50
   Message: cvc-datatype-valid.1.2.3: '  xsd   schema   ' is not a
   valid value of union type
   '#AnonType_UnionOfUnionNested_Union_Test'.

Error:
   Line number: 21
   Column number: 50
   Message: cvc-type.3.1.3: The value '  xsd   schema   ' of element
   'UnionOfUnion' is not valid.

Failed with errors: 6

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