Union and Lists
Last time we talked about built-in simple types and how additional types can be defined as restrictions of those types. Now, we will talk about the remaining kinds of simple types: unions and lists.
According to the W3C spec, list types are data types where the legal values are finite-length sequence of values. Each list type can be a list of a specific itemType. Within SQL Server 2005, we do not permit the creation of list of lists, list of union types and list of xs:ID. List types can be derived from the built-in atomic types, user defined restricted atomic types or anonymous types. The lexical representation of instances of list types are space delimited sequences of instances of the itemType. That means itemTypes that require spaces will not validate correctly when used within a list. For example, if I had a simple type that was a enumeration of three possible values "foo bar", "foo baz" and "bar baz", I would not be able to define a list of this type. When the validator runs across the instance "foo bar foo baz bar baz", it will try to validate "foo", "bar", "foo", "baz", "bar" and "baz" as instances of the itemType; all of which will fail. List types are XSD's intrinsic collection type. Although other mechanisms in XSD can be used to form collections of items, the list type may be more suitable for your needs. In additional to being able to create list types from atomic types, new types can derived from list types that restrict the list further by length, enumeration and pattern facets.
Union types are defined as types where the value and lexical space is the union of the value and lexical spaces of one or more simple types. You could create a union type that was the union of xs:duration and xs:time. During validation of an instance of this type, the lexical value will be validated against xs:duration first and if it doesn't pass validation, it will be validated against xs:time. However, if you want to have an instance validate against a specific memberType, you can specify that within the instance element via the xsi:type attribute. XSD unions are similar to C unions but have subtle differences. For one thing, the actual type of the value is not defined by a type descriptor. During validation, the PSVI is annotated with the actual type of the value.
CREATE XML SCHEMA COLLECTION foo AS N'
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tn="urn:test" targetNamespace="urn:test">
<xs:simpleType name="myAtomicType">
<xs:restriction base="xs:string">
<xs:pattern value="(ab)*"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="myList">
<xs:list itemType="tn:myAtomicType"/>
</xs:simpleType>
<xs:simpleType name="myAnonymousList">
<xs:list>
<xs:simpleType>
<xs:restriction base="xs:int">
<xs:totalDigits value="8"/>
</xs:restriction>
</xs:simpleType>
</xs:list>
</xs:simpleType>
<xs:simpleType name="myUnion">
<xs:union memberTypes="tn:myAnonymousList tn:myAtomicType xs:float"/>
</xs:simpleType>
</xs:schema>'
I've created two list types, one that uses a type I've created, another using an anonymous type defined only within its scope. I've also created a union type that is a union of a list type, a restricted type and a builtin type.