Help us improve your experience.

Let us know what you think.

Do you have time for a two-minute survey?

Navigation

Using the SLAX Processor (slaxproc)

The SLAX processor (slaxproc) is a command-line tool that can validate SLAX script syntax, convert between SLAX and XSLT formats, and format or run SLAX scripts. The slaxproc mode options define what function the processor performs. The following sections outline each of the modes:

Validating 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 slaxproc command with the --check or -c mode 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.

Converting Scripts Between XSLT and SLAX Formats

The SLAX processor supports converting scripts between SLAX and XSLT formats. When you convert a script, you have the option to reference the file arguments positionally or use the command-line file options, --input or -i and --output or -o, to specify the original input script and the converted output script, respectively. If you use the command-line file options, the files can be referenced in any order on the command line, and the file options can be interspersed among other command-line options.

If you do not provide an argument specifying an input file or an output file, the standard input or standard output file is used. When using standard input, 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 and the desired output path and filename of the converted XSLT script as the second argument. To reference the files using command-line file options, include the file 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 and the desired output path and filename of the converted SLAX script as the second argument. To reference the files using command-line file options, include the file 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. Acceptable values are 1.0 and 1.1. The default is version 1.1. 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 will include the statement "version 1.0;" as the first line of the script.

$ slaxproc --xslt-to-slax -w 1.0 test/script1.xsl  test/script1.slax

  OR

$ slaxproc -s -w 1.0 -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>;
            }
        }
    }
}

Running SLAX Scripts

The SLAX processor supports executing SLAX scripts from the command line. This is the default slaxproc mode. To explicitly use this mode, issue the slaxproc command with the --run or -r command-line mode option.

When you execute a script, you have the option to reference the file arguments positionally or use the command-line file options, --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 command-line file options, the files can be referenced in any order on the command line, and the file options can be interspersed among other command-line options.

If no input file is required, use the -E option to indicate an empty input document. Additionally, if the input or output argument has the value "‑", the standard input or standard output file is used. When using standard input, 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:

  1. Create a script using your favorite editor.
  2. (Optional) Check the script syntax by invoking the processor with the --check or -c mode option, and fix any indicated errors.
    $ slaxproc -c test/script1.slax
  3. Execute the script and provide the required input and output files as well as any desired slaxproc options.

    You can reference files positionally or use the command-line file options.

    • To execute a script named script1.slax using input.xml as the input document and output.xml as the output document, issue either of the following commands. The two commands are identical in execution.

      $ slaxproc script1.slax input.xml output.xml
      
      $ slaxproc -n script1.slax -i input.xml -o output.xml
    • To execute a script that requires no input file, include the -E option to indicate an empty input document. For example:

      $ slaxproc -E script1.slax output.xml
      
      $ slaxproc -n script1.slax -o output.xml -E
    • To execute a script and use standard input as the input document, issue the slaxproc command with no input file argument. At the prompt, enter the input and press Ctrl+d to signal the end-of-file. For example:

      $ slaxproc -n script1.slax -o output.xml
      <user input>
      [Ctrl+d]

Formatting 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 have the option to reference the file arguments positionally or use the command-line file options, --input or -i and --output or -o, to specify the unformatted input file and the formatted output file, respectively. If you use the command-line file options, the files can be referenced 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 unformatted SLAX script as the first argument and the desired output path and filename of the formatted SLAX script as the second argument. To reference the files using command-line file options, include the file 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.1;

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.1;

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");
}

Published: 2013-07-26