Importing script code:

External scripts can be loaded using the import statement. For example, 
import "../import/junos.xsl";
will load all the code from junos.xsl file into the present op script prior to execution. Importing allows the use of common code within multiple scripts. junos.xsl script file is included as part of the standard Junos distribution. It contains default templates and parameters.

The main template:

When writing a script, all code and result tree elements must be included within a code structure known as a template. The script engine then begins executing the instructions and writing the included result tree elements. For op scripts, the main template is match /. Note in the Hello World script: the presence of the match / template, the curly braces { } enclosing the code block, and the XML elements to add to the result tree included within the block.

Using Op script boilerplate:

When writing Junos op scripts, work from the standard boilerplate as shown to the right. Boilerplate simplifies script writing by providing the needed namespace URLs along with other components. Various components of the boilerplate are:
  • version: while version 1.0 is currently the only available version of the SLAX language, the version line is required at the beginning of all Junos scripts.
  • ns: a ns statement defines a namespace prefix and its associated namespace URL. The following three namespaces must be included in all Junos scripts:
    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";
  • import: the import statement is used to import code from one script into the current script. As the junos.xsl script contains useful default templates and parameters
  • match: this code block is the main template of the op script. In the standard boilerplate it includes the <op-script-results> result tree element.

version 1.0;

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

import "../import/junos.xsl";

match / {
    <op-script-results> {
        <output> "Hello World!";
        <output> "I'm home!";
    }
}