Help us improve your experience.

Let us know what you think.

Do you have time for a two-minute survey?

 
 

SLAX Variables Overview

SLAX variables can store any values that you can calculate or statically define. This includes data structures, XML hierarchies, and combinations of text and parameters. For example, you could assign the XML output of an operational mode command to a variable and then access the hierarchy within the variable.

You can define both local and global variables. Variables are global if you define the variable outside of any template. Otherwise, the variables are local. The value of a global variable is accessible anywhere in the script. The scope of a local variable is limited to the template or code block in which you define it.

SLAX version 1.0 supports immutable variables, which you declare with the var statement. SLAX version 1.1 introduces mutable variables, which you declare with the mvar statement. Mutable and immutable variables are discussed in more detail in their respective sections.

Starting with SLAX version 1.3, SLAX scripts accept hexadecimal numbers with the 0x prefix. The numbers are converted to decimal for XSLT. As a result, the script emits any hexadecimal numbers as decimal values.

For example, if a script includes the following code:

The script output is the decimal value.

Immutable Variables

In SLAX version 1.0 and later, you declare immutable variables using the var statement. You can set the value of an immutable variable only when you declare it, after which point the value is fixed.

In the declaration, you prefix a dollar sign ($) to the variable name. The SLAX declaration is unlike the XSLT declaration, where the value of the name attribute of the <xsl:variable> element does not use the dollar sign prefix. Once you declare a variable, you can reference it within an XPath expression using the variable name prefixed with a dollar sign ($). You initialize a variable by following the variable name with an equal sign (=) and an expression.

The following example declares and initializes the variable location, which is then used to initialize the variable message:

The XSLT equivalent is:

Variables declared using the var statement are immutable. As such, you can never change the value of the variable after you initialize it in the declaration. Although you cannot directly update the value of the variable, you can mimic the effect by recursively calling a function and passing in the value of the variable as a parameter. For example:

Executing the op script in the CLI produces the following output in the log file. Although the count variable must remain fixed, myparam is updated with each call to the template.

Mutable Variables

SLAX version 1.1 introduces mutable variables. Unlike variables declared using the var statement, a script can modify the value of a mutable variable. You can set the initial value of a mutable variable at the time you declare it or at any point in the script.

You declare mutable variables using the mvar statement. In the declaration, you prefix a dollar sign ($) to the variable name. You initialize the variable by following the variable name with an equal sign (=) and an expression. Once you declare a mutable variable, you can reference it within an XPath expression using the variable name prefixed with a dollar sign ($).

The following example declares and initializes the mutable variable location, which is then used to initialize the mutable variable message:

Mutable variables can be initialized or updated after they are declared. To initialize or update the value of a mutable variable, use the set statement. The following example declares the variable, block, and initializes it with the element <block>:

For mutable variables that represent a node set, use the append statement to append a new node to the existing node set. The following example creates the mutable variable $mylist, which is initialized with one <item> element. For each grocery item in the $list variable, the script appends an <item> element to the $mylist node set with the name and size of the item.

The output from the script is: