Show Table of Contents
32.5. Unions
Overview
In XML Schema, a union is a construct that allows you to describe a type whose data can be one of a number of simple types. For example, you can define a type whose value is either the integer
1 or the string first. Unions are mapped to Java Strings.
Defining in XML Schema
XML Schema unions are defined using a
simpleType element. They contain at least one union element that defines the member types of the union. The member types of the union are the valid types of data that can be stored in an instance of the union. They are defined using the union element's memberTypes attribute. The value of the memberTypes attribute contains a list of one or more defined simple type names. Example 32.13, “Simple Union Type” shows the definition of a union that can store either an integer or a string.
Example 32.13. Simple Union Type
<simpleType name="orderNumUnion"> <union memberTypes="xsd:string xsd:int" /> </simpleType>
In addition to specifying named types as a member type of a union, you can also define an anonymous simple type as a member type of a union. This is done by adding the anonymous type definition inside of the
union element. Example 32.14, “Union with an Anonymous Member Type” shows an example of a union containing an anonymous member type that restricts the possible values of a valid integer to the range 1 through 10.
Example 32.14. Union with an Anonymous Member Type
<simpleType name="restrictedOrderNumUnion">
<union memberTypes="xsd:string">
<simpleType>
<restriction base="xsd:int">
<minInclusive value="1" />
<maxInclusive value="10" />
</restriction>
</simpleType>
</union>
</simpleType>Mapping to Java
XML Schema union types are mapped to Java
String objects. By default, Apache CXF does not validate the contents of the generated object. To have Apache CXF validate the contents you will must configure the runtime to use schema validation as described in the section called “Enforcing facets”.

Where did the comment section go?
Red Hat's documentation publication system recently went through an upgrade to enable speedier, more mobile-friendly content. We decided to re-evaluate our commenting platform to ensure that it meets your expectations and serves as an optimal feedback mechanism. During this redesign, we invite your input on providing feedback on Red Hat documentation via the discussion platform.