XSLT Programming Instructions Overview
XSLT has a number of traditional programming instructions. Their form tends to be verbose, because their syntax is built from XML elements.
The XSLT programming instructions most commonly used in commit, op, event, and SNMP scripts, which provide flow control within a script, are described in the following sections:
<xsl:choose> Programming Instruction
The <xsl:choose>
instruction is
a conditional construct that causes different instructions to be processed
in different circumstances. It is similar to a switch statement in
traditional programming languages. The <xsl:choose>
instruction contains one or more <xsl:when>
elements, each of which tests an XPath expression. If the test evaluates to true, the XSLT processor executes
the instructions in the <xsl:when>
element.
After the XSLT processor finds an XPath expression in an <xsl:when>
element that evaluates to true, the XSLT
processor ignores all subsequent <xsl:when>
elements contained in the <xsl:choose>
instruction, even if their XPath expressions evaluate to true. In
other words, the XSLT processor processes only the instructions contained
in the first <xsl:when>
element whose test
attribute evaluates to true. If none of the <xsl:when>
elements’ test
attributes evaluate to true, the content of the optional <xsl:otherwise>
element, if one is present, is processed.
The <xsl:choose>
instruction is
similar to a switch statement in other programming languages. The <xsl:when>
element is the “case” of
the switch statement, and you can add any number of <xsl:when>
elements. The <xsl:otherwise>
element
is the “default” of the switch statement.
<xsl:choose> <xsl:when test="xpath-expression"> ... </xsl:when> <xsl:when test="another-xpath-expression"> ... </xsl:when> <xsl:otherwise> ... </xsl:otherwise> </xsl:choose>
<xsl:for-each> Programming Instruction
The <xsl:for-each>
element tells
the XSLT processor to gather together a set of nodes and process them
one by one. The nodes are selected by the XPath expression specified
by the select
attribute. Each of the nodes
is then processed according to the instructions held in the <xsl:for-each>
construct.
<xsl:for-each select="xpath-expression"> ... </xsl:for-each>
Code inside the <xsl: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 <xsl:for-each>
clause,
and processing is relative to that current context.
In the following example, the <xsl:for-each>
construct recursively processes each node in the [system syslog
file]
hierarchy. It updates the current context to each matching
node and prints the value of the name
element,
if one exists, that is a child of the current context.
<xsl:for-each select="system/syslog/file"> <xsl:value-of select=”name”/> </xsl:for-each>
<xsl:if> Programming Instruction
An <xsl:if>
programming instruction
is a conditional construct that causes instructions to be processed
if the XPath expression held in the test
attribute evaluates to true
.
<xsl:if test="xpath-expression"> ...executed if test expression evaluates to true </xsl:if>
There is no corresponding else clause.
Sample XSLT Programming Instructions and Pseudocode
Table 1 presents examples that use several XSLT programming instructions along with pseudocode explanations.
Programming Instruction |
Pseudocode Explanation |
---|---|
<xsl:choose> <xsl:when test="system/host-name"> <change> <system> <host-name>M320</host-name> </system> </change> </xsl:when> <xsl:otherwise> <xnm:error> <message> Missing [edit system host-name] M320. </message> </xnm:error> </xsl:otherwise> </xsl:choose> |
When the Otherwise, issue the warning message: |
<xsl:for-each select="interfaces/ interface[starts-with(name, 'ge-')]/unit"> |
For each Gigabit Ethernet interface configured at the |
<xsl:for-each select="data[not(value)]/name"> |
Select any macro parameter that does not contain a parameter value. In other words, match all apply-macro apply-macro-name { parameter-name; } And ignore all apply-macro apply-macro-name { parameter-name parameter-value; } |
<xsl:if test="not(system/host-name)"> |
If the |
<xsl:if test="apply-macro[name = 'no-igp'] |
If the |
<xsl:if test="not(../apply-macro[name = 'no-ldp']) |
If the |