SLAX Parameters Overview
Parameters may be passed to named or unnamed templates or to functions. After declaring a parameter, you can reference it by prefixing the parameter name with the dollar sign ($).
Declaring Parameters
In SLAX scripts, you declare parameters using the param
statement. Optionally, you can define an initial
value for each parameter in the declaration. For example:
param $dot = .;
The scope of a parameter can be local or global. Local parameters must be declared at the beginning of a block, and their scope is limited to the block in which they are declared. A parameter whose value is set by Junos OS at script initialization must be defined as a global parameter. Global parameter declarations are placed just after the style sheet declarations. A script can assign a default value to the global parameter, which is used in the event that Junos OS does not give a value to the parameter.
version 1.2; ns junos = "http://xml.juniper.net/junos/*/junos"; ns xnm = "http://xml.juniper.net/xnm/1.1/xnm"; ns jcs = "http://xml.juniper.net/junos/commit-scripts/1.0"; ns ext = "http://xmlsoft.org/XSLT/namespace"; /* global parameter */ param $interface1 = "fxp0";
In a template, you declare parameters either in a parameter list or by using the
param
statement in the template block. Optionally, you can
declare default values for each template parameter. If a template is invoked without
the parameter, the parameter uses the default value. If you do not define a default
value in the template, the parameter defaults to an empty string.
The following named template print-host-name
declares the parameter message
and defines
a default value:
template print-host-name ($message = "host name: " _ system/host-name) { <xnm:warning> { <message> $message; } }
An alternative, but equivalent, declaration is:
template print-host-name () { param $message = "host name: " _ system/host-name; <xnm:warning> { <message> $message; } }
In the SLAX template, you prefix the parameter with the dollar sign ($) when you declare the parameter and when you access its value. In XSLT, you prefix the parameter name with the dollar sign when you access it but not when you declare it.
In a function, you declare parameters either in a parameter list or by using the
param
statement in the function block. Optionally, you can
declare default values for trailing parameters. If you invoke a function without
that trailing parameter, the parameter uses the default value. If you do not define
a default value, the parameter value defaults to an empty string.
The following example defines a function named size
, which has three parameters:
width
, height
, and scale
. The
default value for scale
is 1. If the function call's argument list
does not include the scale
argument, the calculation uses the
default value of 1 for that argument. The return value for the function is the
product of the width
, height
, and
scale
variables enclosed in a <size>
element.
function my:size ($width, $height, $scale = 1) { result <size> { expr $width * $height * $scale; } }
An alternative, but equivalent declaration, which uses the param
statement, is:
function my:size () { param $width; param $height; param $scale = 1; result <size> { expr $width * $height * $scale; } }
Passing Parameters to Templates
When you invoke a template, you pass arguments into the template
either in an argument list or by using the with
statement. The name of the parameter supplied in the calling environment
must match the name of a parameter defined in the actual template.
Otherwise, the parameter is ignored. Optionally, for each parameter
you pass to a template, you can define a value using an equal sign
(=) and a value expression. In the following example, the two calls
to the named template print-host-name
are
identical:
match configuration { call print-host-name($message = "passing in host name: " _ system/host-name); } match configuration { call print-host-name() { with $message = "passing in host name: " _ system/host-name; } }
The parameter value that gets used in a template depends on
how the template is called. The following three examples, which call
the print-host-name
template, illustrate
the possible calling environments.
If you call a template but do not include a specific parameter, the parameter uses the default
value defined in the template for that parameter. If there is no default value for
that parameter in the template, the parameter value defaults to an empty string. The
following example calls the named template print-host-name
but does
not include any parameters in the call. In this case, the named template will use
the default value for the message
parameter that was defined in the
print-host-name
template, or an empty string if no default
exists.
match configuration { call print-host-name(); }
If you call a template and include a parameter, but you do not define a value for the parameter in the calling environment, the script passes in the current value of the parameter, if it was previously initialized. If the parameter was never declared, the script generates an error.
The following example calls the named template print-host-name
and
passes in the message
parameter but does not include a value. If
the script declares and initializes message
, and the scope is
visible to the block, the template uses the current value of
message
. If the script declares message
but
does not initialize the parameter, the value of message
is an empty
string. If the script does not declare message
, the call produces
an error.
match configuration { call print-host-name($message); /* If $message was initialized previously, the current value is used; * If $message was declared but not initialized, an empty string is used; * If $message was never declared, the call generates an error. */ }
If you call a template, include the parameter, and define a
value for the parameter, the template uses the provided value. The
following example calls the named template print-host-name
with the message
parameter and a defined
value, so the template uses the new value:
match configuration { call print-host-name($message = "passing in host name: " _ system/host-name); }
Example: Parameters and Match Templates
The following example matches the top level configuration
hierarchy element and then instructs the Junos OS management process
(mgd) to recursively apply templates to the system/host-name
subtree. The parameters message
and domain
are used in the processing of any matching nodes.
match configuration { var $domain = domain-name; apply-templates system/host-name { with $message = "Invalid host-name"; with $domain; } } match host-name { param $message = "Error"; param $domain; <hello> $message _ ":: " _ . _ " (" _ $domain _ ")"; }
The XSLT equivalent is:
<xsl:template match="configuration"> <xsl:apply-templates select="system/host-name"> <xsl:with-param name="message" select="'Invalid host-name'"/> <xsl:with-param name="domain" select="$domain"/> </xsl:apply-templates> </xsl:template> <xsl:template match="host-name"> <xsl:param name="message" select="'Error'"/> <xsl:param name="domain"/> <hello> <xsl:value-of select="concat($message, ':: ', ., ' (', $domain, ')')"/> </hello> </xsl:template>
Passing Parameters to Functions
SLAX supports functions starting in SLAX version 1.1. Although you can use the
param
statement to define function parameters, you cannot use
the with
statement to pass parameter values into the function from
the calling environment. When you call a function, you pass arguments into the
function in a comma-separated list. Function arguments are passed to the function by
position rather than by name as in a template.
A function declaration can define default values for trailing arguments. 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.
In the following 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. The function call includes arguments for width
and height
, but not for scale
. The default value of 1 is used for scale
within the function block.
version 1.2; ns my = "http://www.example.com/myfunctions"; function my:size () { param $width; param $height; param $scale = 1; result <size> { expr $width * $height * $scale; } } match / { for-each (graphic/dimension) { <out> { copy-of my:size((width/.), (height/.)); } } }