SLAX Statements Overview
This section lists some commonly used SLAX statements, with brief examples and XSLT equivalents.
for-each Statement
The SLAX for-each
statement functions
like the <xsl:for-each>
element. The
statement consists of the for-each
keyword,
a parentheses-delimited expression, and a curly braces-delimited block.
The for-each
statement tells the processor
to gather together a set of nodes and process them one by one. The
nodes are selected by the specified XPath expression. Each of the nodes is then processed according to the
instructions held in the for-each
code
block.
for-each (xpath-expression) { ... }
Code inside the for-each
instruction
is evaluated recursively for each node that matches the XPath expression.
That is, the current context is moved to each node selected by the for-each
clause, and processing is relative to that
current context.
In the following example, the inventory
variable stores the inventory hierarchy. The for-each
statement recursively processes each chassis-sub-module
node that is a child of chassis-module
that is a child of the chassis
node.
For each chassis-sub-module
element that
contains a part-number
with a value equal
to the specified part number, a message
element is created that includes the name of the chassis module
and the name and description of the chassis sub module.
for-each ($inventory/chassis/chassis-module/ chassis-sub-module[part-number == '750-000610']) { <message> "Down rev PIC in " _ ../name _ ", " _ name _ ": " _ description; }
The XSLT equivalent is:
<xsl:for-each select="$inventory/chassis/chassis-module/ chassis-sub-module[part-number = '750-000610']"> <message> <xsl:value-of select="concat('Down rev PIC in ', ../name, ', ', name, ': ', description)"/> </message> </xsl:for-each>
if, else if, and else Statements
SLAX supports if
, else
if
, and else
statements.
The if
statement is a conditional construct
that causes instructions to be processed if the specified XPath expression
evaluates to true. The if
construct may
have one or more associated else if
clauses,
each of which tests an XPath expression. If the expression in the if
statement evaluates to false, the processor checks
each else if
expression. If a statement
evaluates to true, the script executes the instructions in the associated
block and ignores all subsequent else if
and else
statements. The optional else
clause is the default code that is executed in
the event that all associated if
and else-if
expressions evaluate to false. If all of the if
and else if
statements
evaluate to false, and the else
statement
is not present, no action is taken.
The expressions that appear in parentheses are extended XPath
expressions, which support the double equal sign (==
) in place of XPath’s single equal sign (=
).
if (expression) { /* If block Statement */ } else if (expression) { /* else if block statement */ } else { /* else block statement */ }
During script processing, an if
statement
that does not have an associated else if
or else
statement is transformed into
an <xsl:if>
element. If either the else if
or else
clauses
are present, the if
statement and associated else if
and else
blocks
are transformed into an <xsl:choose>
element.
if (starts-with(name, "fe-")) { if (mtu < 1500) { /* Select Fast Ethernet interfaces with low MTUs */ } } else { if (mtu > 8096) { /* Select non-Fast Ethernet interfaces with high MTUs */ } }
The XSLT equivalent is:
<xsl:choose> <xsl:when test="starts-with(name, 'fe-')"> <xsl:if test="mtu < 1500"> <!-- Select Fast Ethernet interfaces with low MTUs --> </xsl:if> </xsl:when> <xsl:otherwise> <xsl:if test="mtu > 8096"> <!-- Select non-Fast Ethernet interfaces with high MTUs --> </xsl:if> </xsl:otherwise> </xsl:choose>
match Statement
You specify basic match templates using the match
statement, followed by an expression specifying when the template
should be allowed and a block of statements enclosed in a set of braces.
match configuration { <xnm:error> { <message> "..."; } }
The XSLT equivalent is:
<xsl:template match="configuration"> <xnm:error> <message> ...</message> </xnm:error> </xsl:template>
For more information about constructing match templates, see SLAX Templates Overview.
ns Statement
You specify namespace definitions using the SLAX ns
statement. This consists of the ns
keyword, a prefix string, an equal sign, and a namespace Uniform Resource Identifier (URI). To define the
default namespace, use only the ns
keyword
and a namespace URI.
ns junos = "https://www.juniper.net/junos/";
The ns
statement can appear after
the version
statement at the beginning
of the style sheet or at the beginning of any block.
ns a = "http://example.com/1"; ns "http://example.com/global"; ns b = "http://example.com/2"; match / { ns c = "http://example.com/3"; <top> { ns a = "http://example.com/4"; apply-templates commit-script-input/configuration; } }
When it appears at the beginning of the style sheet, the ns
statement can include either the exclude
or extension
keyword. The keyword instructs
the parser to add the namespace prefix to the exclude-result-prefixes
or extension-element-prefixes
attribute.
ns exclude foo = "http://example.com/foo"; ns extension jcs = "http://xml.juniper.net/jcs";
The XSLT equivalent is:
<xsl:stylesheet xmlns:foo="http://example.com/foo" xmlns:jcs="http://xml.juniper.net/jcs" exclude-result-prefixes="foo" extension-element-prefixes="jcs"> <!-- ... --> </xsl:stylesheet>
version Statement
All SLAX style sheets must begin with a version
statement, which
specifies the version number for the SLAX language. Supported versions include 1.0, 1.1,
and 1.2.
version 1.2;