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. For summaries of all XSLT programming instructions used in this guide, see Summary of XPath and XSLT Constructs.

The XSLT programming instructions most commonly used in commit, op, and event scripts 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. The <xsl:choose> instruction contains one or more <xsl:when> elements, each of which tests an XPath expression. If the test evaluates as 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 as true, the XSLT processor ignores all subsequent <xsl:when> elements contained in the <xsl:choose> instruction, even if their XPath expressions evaluate as true. In other words, the XSLT processor processes only the instructions contained in the first <xsl:when> element whose test attribute evaluates as true. If none of the <xsl:when> elements’ test attributes evaluate as true, the content of the <xsl:otherwise> element, if there is one, is processed.

The <xsl:choose> instruction is similar to a switch statement in other programming languages, but the test expression can vary among <xsl:when> elements. The <xsl:when> element is the “case” of the switch statement. Any number of <xsl:when> elements can appear. 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

An <xsl:for-each> programming instruction 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 an <xsl:for-each> instruction is evaluated recursively for each node that matches the XPath expression. The context is moved to the node during each pass.

<xsl:for-each select="xpath-expression">
     ...
</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">
</xsl:if>

Sample XSLT Programming Instructions and Pseudocode

Table 5 presents examples that use several XSLT programming instructions along with pseudocode explanations.

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