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
1or the string
first. Unions are mapped to Java
Defining in XML Schema
XML Schema unions are defined using a
simpleTypeelement. They contain at least one
unionelement 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
memberTypesattribute. The value of the
memberTypesattribute 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
unionelement. 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
Stringobjects. 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”.