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) 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:
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
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