XSD Tutorials - Herong's Tutorial Examples - v5.23, by 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:
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) 2002-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) 2002-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:
herong> 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
Table of Contents
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
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
"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
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