Help us improve your experience.

Let us know what you think.

Do you have time for a two-minute survey?

 
 

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: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.

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: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.

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.

Table 1: Examples and Pseudocode for XSLT Programming Instructions

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 host-name statement is included at the [edit system] hierarchy level, change the hostname to M320.

Otherwise, issue the warning message: Missing [edit system host-name] M320.

<xsl:for-each select="interfaces/
interface[starts-with(name, 'ge-')]/unit">

For each Gigabit Ethernet interface configured at the [edit interfaces ge-fpc/pic/port unit logical-unit-number] hierarchy level.

<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 statements of the following form:

apply-macro apply-macro-name {
    parameter-name;
}

And ignore all apply-macro statements of the form:

apply-macro apply-macro-name {
    parameter-name parameter-value;
}
<xsl:if test="not(system/host-name)">

If the host-name statement is not included at the [edit system] hierarchy level.

<xsl:if test="apply-macro[name = 'no-igp']

If the apply-macro statement named no-igp is included at the current hierarchy level.

<xsl:if test="not(../apply-macro[name = 'no-ldp'])

If the apply-macro statement with the name no-ldp is not included two hierarchy levels above the current hierarchy level.