XML technologies / XML Schema / Creating simple data types / Derivation by union

(Excerpt from “XML Schema” by Eric van der Vlist)

Derivation by union defines data types by combining lexical spaces of several predefined or user-defined types.

As with list derivation, W3C XML Schema defines two notations using element xs:union, allowing either references to existing types or embedded definitions. Here the notations can be mixed. A union type referencing existing types uses attribute memberType, containing a list of data types separated by whitespace:

<xs:simpleType name="integerOrData">
   <xs:union memberTypes="xs:integer xs:date"/>
</xs:simpleType>

A union type can also be defined by embedding one or more <xs:simpleType> elements:

<xs:simpleType name="myIntegerUnion">
  <xs:union>
    <xs:simpleType>
      <xs:restriction base="xs:integer"/>
    </xs:simpleType>
    <xs:simpleType>
      <xs:restriction base="xs:NMTOKEN">
         <xs:enumeration value="undefined"/>
      </xs:restriction>
    </xs:simpleType>
  </xs:union>
</xs:simpleType>



Both styles can be mixed, so the previous example can also be written as:

<xs:simpleType name="myIntegerUnion">
  <xs:union memberTypes="xs:integer">
    <xs:simpleType>
      <xs:restriction base="xs:NMTOKEN">
        <xs:enumeration value="undefined"/>
      </xs:restriction>
    </xs:simpleType>
  </xs:union>
</xs:simpleType>

Taken as a whole, the resulting type is a mixture losing its member types’ semantics and facets. In the previous example, type myIntegerUnion cannot be restricted to values at most 100 or the undefined value except through a pattern. Instead, first restrict a built-in type to values at most 100, then form the union to allow “undefined” too. The only facets applicable to a union type are xs:pattern and xs:enumeration.

These are the only two facets shared by almost all data types. The sole exception is that xs:enumeration is not allowed for xs:boolean. Defining a dummy union over an xs:boolean could work around this to allow an xs:enumeration facet on this type.

   

 

 

 

back to top