A union type is a simple type that represents the combined set of allowable values from other simple types.
A union type defines a list of simple types as its members. A value allowed by any one of its member types will be a valid value for the union type.
The benefit that this provides is reuse. Rather than needing to duplicate values, types can be reused in combination instead.
One kind of use case for a union is for situations when an existing code set provides some, but not all, of the values that are needed. One option is to create a code set with just the missing values, create a union type that combines the original codes with the new codes, and create a new element based on the union type.
This example shows a union type and its two member types.
<xs:simpleType name="GradeSimpleType">
<xs:annotation>
<xs:documentation>A data type for a grade.</xs:documentation>
</xs:annotation>
<xs:union memberTypes="ext:LetterGradeSimpleType ext:NumericGradeSimpleType"/>
</xs:simpleType>
<xs:simpleType name="LetterGradeSimpleType">
<xs:annotation>
<xs:documentation>A data type for a letter grade.</xs:documentation>
</xs:annotation>
<xs:restriction base="xs:token">
<xs:pattern value="[A-F]"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="NumericGradeSimpleType">
<xs:annotation>
<xs:documentation>A data type for a numeric grade.</xs:documentation>
</xs:annotation>
<xs:restriction base="xs:integer">
<xs:minInclusive value="0"/>
<xs:maxInclusive value="100"/>
</xs:restriction>
</xs:simpleType>
This example shows possible values for a “GradeValue” element based on the union type above:
<ext:GradeValue>A</ext:GradeValue>
<ext:GradeValue>C</ext:GradeValue>
<ext:GradeValue>85</ext:GradeValue>
<ext:GradeValue>42</ext:GradeValue>
<ext:GradeValue>B</ext:GradeValue>
The following is a template for a simple union type with two members. Additional member types may be added as needed.
<xs:simpleType name="NAMESimpleType">
<xs:annotation>
<xs:documentation>A data type for a ...</xs:documentation>
</xs:annotation>
<xs:union memberTypes="SIMPLE-TYPE-1 SIMPLE-TYPE-2"/>
</xs:simpleType>
JSON guidance is not yet available for union types.
Rule | Applicability | Title |
---|---|---|
NDR 9-19 | REF, EXT | No union member types of xs:ID |
NDR 9-20 | REF, EXT | No union member types of xs:IDREF |
NDR 9-21 | REF, EXT | No union member types of xs:IDREFS |
NDR 9-22 | REF, EXT | No union member types of xs:anySimpleType |
NDR 9-23 | REF, EXT | No union member types of xs:ENTITY |
NDR 9-24 | REF, EXT | No union member types of xs:ENTITIES |
NDR 11-6 | REF, EXT | Union member types defined by conformant schemas |