Simple inline functions

Dot functions

An abbreviated syntax ("dot functions") is available for defining simple inline functions. For example, the expression .{@code} represents a function that takes a single argument (presumably an element node), and returns a selected attribute of that node. A simple inline function takes a single argument with required type item(), and returns any sequence (type item()*). The function body is evaluated with a singleton focus based on the supplied argument value.

Simple inline functions are particularly convenient when providing functions as arguments to higher-order functions, many of which accept a single item as their one argument. For example, to sort employees in order of salary, you can write:

sort(//employee, .{@salary})

Simple inline functions can access externally-defined local variables in the usual way (that is, they have a closure).

The expression .{EXPR} is a syntactic shorthand for:

function($x as item()) as item()* {$x!(EXPR)}

The experimental syntax fn{EXPR} used in earlier releases has been dropped.

Underscore functions

For functions taking more than one argument, or an argument that is not confined to a single item, a second abbreviated syntax ("underscore functions") is provided. For example, the expression _{$1 + $2} is a function that takes two arguments and returns their sum. It is equivalent to the expression:

function($a1 as item()*, $a2 as item()*) as item()* {$a1 + $a2}

The arity of an underscore function is determined by the highest-numbered parameter reference appearing within the function. For example, _{$2} is a function that takes two arguments and returns the value of the second argument.

This syntax can also be used to define a zero-arity function, for example the following is a function that returns tomorrow's date:

_{current-date() + xs:dayTimeDuration('P1D')}