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 ~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 ~cx:complex is more meaningful than tuple(r: xs:double, i: 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 saxon:type-alias:

<saxon:type-alias name="cx:complex" type="tuple(r: xs:double, i: xs:double)"/> <xsl:function name="cx:complex" as="~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="~cx:complex"> <xsl:param name="x" as="~cx:complex"/> <xsl:param name="y" as="~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 Patterns). For example:

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

The construct ~T at the start of a pattern can be regarded as an abbreviation for .[. instance of ~T].

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