MENU
Custom Functions
XPath 3.0 has advanced support for custom functions.error-handling.html:
(1)
let $f := function ($v) {$v * 2} return $f(3)
(2)
let
$process :=function($v as xs:double,
$f as function(xs:double) as xs:double)
as xs:double
{$f($v)},
$mult2 := function ($n as xs:double) as xs:double
{$n*2}
return $process(3,$mult2)
(3)
let
$compose:=function($f as function(xs:double) as xs:double,
$g as function(xs:double) as xs:double)
as function(xs:double) as xs:double
{function ($x as xs:double)
{$g($f($x))}},
$mult2 := function($x as xs:double) {$x *2},
$mult3 := function($x as xs:double) {$x *3}
return $compose($mult2,$mult3)(1)
(4)
let $plus := function($x as xs:integer,
$y as xs:integer)
as xs:integer
{$x+$y}
return $plus(?,?)(2,4)
(5)
let $plus := function ($m as xs:integer)
as (function(xs:integer) as xs:integer)
{function ($n as xs:integer)
{$m + $n}}
return $plus(2)(4)
(6)
let $s := function ($n as xs:integer,
$f as function(xs:integer,
function()) as xs:integer) as xs:integer
{if ($n<1) then 0 else $n+$f($n - 1,$f)},
$sum := function($n as xs:integer)
{$s($n,$s)}
return $sum(3)
(1) a simple function (2) passing a function to a function (3) functions composition (4) partial function application (5) function closure (6) recursive function (not supported by all)