Type aliases

Wherever an ItemType may appear (for example in a function signature or variable declaration, or in instance of or cast as expressions), the syntax type(alias) is permitted, where alias is a name for an item type declared elsewhere. Type aliases are QNames, and prefixes are resolved in the usual way (using the default namespace for elements and types).

Type aliases are useful because they abstract away from the detail of how a type is implemented: for example type(cx:complex) is more meaningful than record(r as xs:double, i as xs:double), and it creates more scope for changing the way types are represented as the software evolves. However, type aliases do not provide any real encapsulation or information hiding. Type aliases cannot be used within their own definition, and the definition of a type alias cannot be circular.

The mapping of type aliases to item types is an extension to the static context of an expression.

In XSLT, type aliases can be defined using the top-level declaration xsl:item-type:

<xsl:item-type name="cx:complex" as="record(r as xs:double, i as xs:double)"/> <xsl:function name="cx:complex" as="type(cx:complex)"> <xsl:param name="r" as="xs:double"/> <xsl:param name="i" as="xs:double"/> <xsl:sequence select="map{'r':$r, 'i':$i}"/> </xsl:function> <xsl:function name="cx:add" as="type(cx:complex)"> <xsl:param name="x" as="type(cx:complex)"/> <xsl:param name="y" as="type(cx:complex)"/> <xsl:sequence select="cx:complex($x?r + $y?r, $x?i + $y?i)"/> </xsl:function>

XSLT-defined type aliases are scoped to a package. The usual rules for import precedence apply.

Type aliases can be used in XSLT match patterns (see XSLT 4.0 extensions: Match patterns). For example:

<xsl:template match="type(cx:complex)[?i=0]">{?r}</xsl:template><xsl:template match="type(cx:complex)">{?r}{if (?i ge 0) then '+' else ''}{?i}i</xsl:template>

The construct type(T) at the start of a pattern can be regarded as an abbreviation for .[. instance of T]. The experimental syntax ~T used in earlier releases has been dropped.

In XQuery, type aliases can be declared in the query prolog using the syntax declare type QName = ItemType;, for example: declare type cx:complex = record(r as xs:double, i as xs:double);.

For further details see xsl:item-type.