ON THIS PAGE
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:
var $x = 0x45 + 0xBEEF; expr jcs:output($x);
The script output is the decimal value.
user@host> op hex-test.slax 48948
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:
var $location = $dot/@location; var $message = "We are in " _ $location _ " now.";
The XSLT equivalent is:
<xsl:variable name="location" select="$dot/@location"/>
<xsl:variable name="message" select="concat('We are in ', $location, ' now.')"/>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:
var $count = 1;
match / {
call update-count($myparam = $count);
}
template update-count($myparam) {
expr $count _ ", " $myparam _"\n";
if ($myparam != 4) {
call update-count($myparam = $myparam + 1)
}
}
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.
1, 1 1, 2 1, 3 1, 4 1, 5
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:
mvar $location = $dot/@location; mvar $message = "We are in " _ $location _ " now.";
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>:
mvar $block; set $block = <block> "start here";
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.
version 1.1;
var $list := {
<list> {
<grocery> {
<name> "milk";
<type> "nonfat";
<brand> "any";
<size> "gallon";
}
<grocery> {
<name> "orange juice";
<type> "no pulp";
<brand> "any";
<size> "half gallon";
}
<drugstore>{
<name> "aspirin";
<brand> "any";
<size> "50 tablets";
}
}
}
match / {
mvar $mylist;
set $mylist = <item> {
<name> "coffee";
<size> "1 lb";
}
for $item ($list/list/grocery) {
append $mylist += <item> {
<name> $item/name;
<size> $item/size;
}
}
<grocery-short-list> {
copy-of $mylist;
}
}The output from the script is:
<?xml version="1.0"?>
<grocery-short-list>
<item>
<name>coffee</name>
<size>1 lb</size>
</item>
<item>
<name>milk</name>
<size>gallon</size>
</item>
<item>
<name>orange juice</name>
<size>half gallon</size>
</item>
</grocery-short-list>