Programming Instructions
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 Functions, Elements, Attributes, and Templates.
The most important XSLT programming instructions for use with commit scripts and op scripts are as follows:
- <xsl:choose>
- <xsl:for-each select="
xpath-expression">- <xsl:if test="
xpath-expression">The syntax and uses for these instructions are described in the following sections. Table 7 provides examples of several instructions and their pseudocode meanings.
<xsl:choose>
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 whosetestattribute evaluates as true. If none of the<xsl:when>elements'testattributes 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 select="xpath-expression">
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 theselectattribute. 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 test="xpath-expression">
An
<xsl:if>programming instruction is a conditional construct that causes instructions to be processed if the XPath expression held in thetestattribute evaluates totrue.<xsl:if test="xpath-expression"></xsl:if>
<xsl:whentest="system/host-name">When the
host-namestatement is included at the[edit system]hierarchy level, change the hostname toM320.Otherwise, issue the warning message: "Missing
[edit system host-name] M320."
<xsl:for-eachselect="interfaces/interface[starts-with(name, 'ge-')]/unit">For each Gigabit Ethernet interface configured at the
[edit interfaces ge-fpc/pic/portunitlogical-unit-number]hierarchy level...
<xsl:for-eachselect="data[not(value)]/name">Select any macro parameter that does not contain a parameter value.
In other words, match all
apply-macrostatements of the form:
apply-macroapply-macro-name{And ignore all
apply-macrostatements of the form:
<xsl:iftest="not(system/host-name)">If the
host-namestatement is not included at the[edit system]hierarchy level...
<xsl:iftest="apply-macro[name = 'no-igp']If the
apply-macrostatement namedno-igpis included at the current hierarchy level...
<xsl:iftest="not(../apply-macro[name = 'no-ldp'])If the
apply-macrostatement with the nameno-ldpis not included two hierarchy levels above the current hierarchy level...