Union types

A construct of the form union(type1, type2, type3) can be used to define simple unions of atomic types. For example, union(xs:base64Binary, xs:hexBinary) defines a union type whose members are xs:base64Binary and xs:hexBinary, while union(xs:date, xs:time, xs:dateTime) defines a union type whose members are xs:date, xs:time, and xs:dateTime. These types can conveniently be used in function signatures when writing a function that is designed to take arguments of more than one type.

The names between the parentheses are strictly QNames (not general ItemTypes) and must refer to named atomic types.

The order of member type names is significant, in that it determines the order used for casting or validating strings.

This construct can be used anywhere that a conventional union type name defined in a schema would be recognized, for example in instance of and cast as expressions, in the as attribute of variable and function declarations, or in typeswitch expressions. The semantics are exactly the same as if the union type were declared in an imported schema and referenced by name.

For example, the following code defines an XQuery function that formats a date, time, dateTime, or a string in the form of a date, time, or dateTime using the appropriate variant of the format-date/time function:

declare function local:format($in as union(xs:dateTime, xs:date, xs:time, xs:string), $fmt as xs:string) as xs:string { typeswitch ($in) case xs:dateTime return format-dateTime($in, $fmt) case xs:date return format-date($in, $fmt) case xs:time return format-time($in, $fmt) case xs:string return local:format($in cast as union(xs:dateTime, xs:date, xs:time), $fmt) default return error() }; local:format("2016-10-07", "[D] [Mn] [Y]")]>