function
Syntax
function function-name (argument-list) { ... result return-value; }
function function-name () { param param-name1; param param-name2; param param-name3 = default-value; ... result return-value; }
Description
Define an extension function that can be used in
XPath expressions. The function
statement
must be defined as a top-level statement in the script using a qualified
name for the function identifier. The argument list is a comma-separated
list of parameter names, which are positionally assigned based on
the function call. Trailing arguments can have default values. Alternatively,
you can define function parameters inside the function block using
the param
statement. The function body
is a set of statements, which should include a result
statement that defines the return value for the function.
If there are fewer arguments in the function invocation than in the definition, the default values are used for any trailing arguments. If there are more arguments in the function invocation than in the definition, the function call generates an error.
Attributes
function-name |
Specifies the name of the function as a qualified name. |
argument-list |
Comma-separated list of parameter names, which are positionally assigned based on the function call. Trailing arguments can have default values. |
return-value |
XML element or XPath expression, scalar value, or a set of instructions providing the return value of the function. |
SLAX Example
The following example defines the function size
, which has three parameters: width
, height
, and scale
. The default value for scale
is 1. If
the function call argument list does not include the scale
argument, the calculation uses the default value
of 1 for that argument. The function’s return value is the product
of the width
, height
, and scale
variables enclosed in a <size>
element.
In the main match template, the function call uses width and
height data selected from each graphic/dimension
element in the source XML file. The script evaluates the function,
and the copy-of
statement emits the return
value to the result tree as the contents of the <out>
element.
version 1.1; ns my = "http://www.example.com/myfunctions"; function my:size ($width, $height, $scale = 1) { result <size> { expr $width * $height * $scale; } } match / { for-each (graphic/dimension) { <out> { copy-of my:size((width/.), (height/.)); } } }
Release Information
Statement introduced in version 1.1 of the SLAX language, which is supported in Junos OS Release 12.2 and later releases.