How to Use the SLAX Processor (slaxproc)
The SLAX processor (slaxproc) is a command-line tool that can format SLAX scripts, validate SLAX script syntax, run and debug SLAX scripts, and convert between SLAX and XSLT. The slaxproc mode defines the function that the processor performs. The following sections outline commonly used modes. For a complete list of slaxproc modes, see Understanding the SLAX Processor (slaxproc).
You can use the libslax library to develop SLAX scripts on a network management system.
The Junos OS and Junos OS Evolved software also include the libslax tools. To verify the
installed SLAX and libslax versions, issue the slaxproc --version
command. For example, to print the SLAX and libslax versions on a device running Junos
OS, you can issue the following shell command:
% /usr/libexec/ui/slaxproc --version version 1.3; libslax version 3.1.6 Using libxml 20909, libxslt 10133 and libexslt 817 slaxproc was compiled against libxml 20909, libxslt 10133 and libexslt 817 libxslt 10133 was compiled against libxml 20909 libexslt 817 was compiled against libxml 20909
Similarly on a device running Junos OS Evolved:
# /usr/bin/slaxproc --version version 1.3; libslax version 3.1.6-git-main Using libxml 20914, libxslt 10135 and libexslt 820 slaxproc was compiled against libxml 20914, libxslt 10135 and libexslt 820 libxslt 10135 was compiled against libxml 20914 libexslt 820 was compiled against libxml 20914
Validate SLAX Script Syntax
The SLAX processor provides an option to check the syntax of a SLAX script.
To check the syntax of a SLAX script:
-
Issue the
slaxproccommand with the--checkor-cmode option and the script filename.$ slaxproc --check script1.slax OR $ slaxproc -c script1.slax
If the script syntax is correct, the SLAX processor issues a "script check succeeds" message. Otherwise, the processor issues a list of error messages detected during script parsing. Fix any indicated errors, and repeat the check.
Convert Scripts Between XSLT and SLAX Formats
The SLAX processor supports converting scripts between SLAX and XSLT. When you
convert a script, you can reference the file arguments positionally or use the
command-line file options. The options use --input or
-i and --output or -o to
specify the original input script and the converted output script, respectively.
If you use the file options, you can reference the files in any order, and you
can intersperse the file options among other command-line options.
If you do not provide an argument specifying an input file or an output file, the processor uses standard input (stdin) or standard output (stdout). When using stdin, press Ctrl+d to signal the end-of-file.
To convert a SLAX script to XSLT, issue the slaxproc command
with the --slax-to-xslt or -x mode option. To
reference the files positionally, specify the input SLAX file as the first
argument. The second argument is the output path and filename of the converted
XSLT script. To reference the files using the file options, include the options
in any order. For example:
$ slaxproc --slax-to-xslt test/script2.slax test/script2.xsl OR $ slaxproc -x -i test/script2.slax -o test/script2.xsl
To convert an XSLT script to SLAX, issue the slaxproc command
with the --xslt-to-slax or -s mode option. To
reference the files positionally, specify the input XSLT file as the first
argument. The second argument is the output path and filename of the converted
SLAX script. To reference the files using file options, include the options in
any order.
Optionally, when converting a script from XSLT to SLAX, include the
--write-version or -w option to specify
the SLAX version of the converted script. The default version is the latest SLAX
version on the device. Use the -p option for partial input when
you do not require the SLAX script boilerplate in the output.
The following example converts the XSLT script script1.xsl to the SLAX script script1.slax. The SLAX script includes the statement "version 1.3;" as the first line of the script.
$ slaxproc --xslt-to-slax -w 1.3 test/script1.xsl test/script1.slax OR $ slaxproc -s -w 1.3 -i test/script1.xsl -o test/script1.slax
The slaxproc --xslt-to-slax mode with the -p
option is useful for quickly converting Junos OS hierarchies from XML format
into SLAX. The following example provides the Junos OS [edit
policy-options] hierarchy in XML format as input to the SLAX
processor. The -p option indicates partial script input as
opposed to a full script.
$ slaxproc -s -p
<policy-options>
<policy-statement>
<name>export-policy</name>
<term>
<name>term1</name>
<from>
<route-filter>
<address>10.0.4.4/30</address>
<prefix-length-range>/30-/30</prefix-length-range>
</route-filter>
</from>
<then>
<accept/>
</then>
</term>
</policy-statement>
</policy-options>
[Ctrl+d]
The SLAX processor returns the SLAX formatting for the hierarchy.
<policy-options> {
<policy-statement> {
<name> "export-policy";
<term> {
<name> "term1";
<from> {
<route-filter> {
<address> "10.0.4.4/30";
<prefix-length-range> "/30-/30";
}
}
<then> {
<accept>;
}
}
}
}
Run SLAX Scripts
The SLAX processor supports executing SLAX scripts from the command line. This
mode is the default. To explicitly use this mode, issue the
slaxproc command with the --run or
-r mode.
When you execute a script, you can reference the file arguments positionally or
use the command-line file options. The file options use --name
or -n, --input or -i, and
--output or -o to specify the SLAX script
file, and the input and output files, respectively. If you use the file options,
you can reference the files in any order, and you can intersperse the file
options among other command-line options.
If the script does not require an input file, use the -E option
to indicate an empty input document. Additionally, if the input or output
argument has the value "‑", the processor uses stdin or stdout. When using
stdin, press Ctrl+d to signal the end-of-file.
The syntax for executing a script is:
$ slaxproc script input-file output-file OR $ slaxproc (--name | -n) script (--input | -i) input-file (--output | -o) output-file
To execute a script using the slaxproc command-line tool:
Run the Profiler on a Script
The SLAX profiler can report information about the activity and performance of a
script. The profiler tracks script execution until the script terminates.
Starting with SLAX version 1.3, the slaxproc command includes
options to run the profiler on a script and save the output to a file. In
earlier SLAX versions, you must invoke the SLAX debugger to use the profiler.
The slaxproc command options include:
-
--profile <file>—Run the profiler and save the output to the specified file. -
--profile-mode <mode>—Run the profiler using the given mode and save the output to the profile.output file.
The general syntax is:
$ slaxproc --profile file --profile-mode mode [options] [script] [files]
For example, the following command runs the profiler for the script1.slax script and saves the profiler output to the script1-profile file.
$ slaxproc --profile script1-profile -Eg script1.slax
The following command runs the profiler in brief mode for the script1.slax script and saves the profiler output to the default output file profile.output.
$ slaxproc --profile-mode brief -Eg script1.slax
The following command runs the profiler in brief mode for the script1.slax script and saves the profiler output to the script1-profile file.
$ slaxproc --profile script1-profile --profile-mode brief -Eg script1.slax
Format SLAX Scripts
The SLAX processor provides the option to format a script to correct the
indentation and spacing to the preferred style. When you format a script, you
can reference the file arguments positionally or use the command-line file
options. The file options use --input or -i
and --output or -o to specify the original
input file and the formatted output file, respectively. If you use the file
options, you can reference the files in any order on the command line.
To format a SLAX script, issue the slaxproc command with the
--format or -F mode option. To reference
the files positionally, specify the original SLAX script as the first argument.
The second argument is the output path and filename of the formatted SLAX
script. To reference the files using the file options, include the options in
any order. For example:
$ slaxproc --format script1.slax script1-format.slax OR $ slaxproc -F -i script1.slax -o script1-format.slax
Given the following unformatted SLAX script as input:
version 1.3;
decimal-format default-format {
decimal-separator "." ;
digit "#" ;
grouping-separator "," ;
infinity "Infinity" ;
minus-sign "-" ;
nan "NaN";
pattern-separator ";" ;
percent "%";
per-mille "\x2030";
zero-digit "0" ;
}
match / {
var $number = -14560302.5;
expr format-number($number, "###,###.00", "default-format");
}The SLAX processor outputs the following formatted SLAX script:
version 1.3;
decimal-format default-format {
decimal-separator ".";
digit "#";
grouping-separator ",";
infinity "Infinity";
minus-sign "-";
pattern-separator ";";
percent "%";
per-mille " 30";
zero-digit "0";
nan "NaN";
}
match / {
var $number = -14560302.5;
expr format-number($number, "###,###.00", "default-format");
}Starting with SLAX version 1.3, when you format a SLAX script, you can specify
the number of spaces to use for indentation as well as the line width. The
default indentation is four spaces. To define the indentation, use the
--indent-width option. To limit the line width, use the
--width option. The processor attempts to limit each line
to the specified width, but long tokens might exceed the limit.
For example, suppose you use the previous example's original script as input and specify an indentation of two spaces and a line width of 40 characters. The SLAX processor returns the following output:
$ slaxproc -F -i script1.slax --indent-width 2 --width 40
version 1.3;
decimal-format default-format {
decimal-separator ".";
digit "#";
grouping-separator ",";
infinity "Infinity";
minus-sign "-";
pattern-separator ";";
percent "%";
per-mille " 30";
zero-digit "0";
nan "NaN";
}
main {
var $number = -14560302.5;
expr format-number($number,
"###,###.00",
"default-format");
}