[Contents] [Prev] [Next] [Index] [Report an Error]

Variables

You can define both local and global variables in XSLT. Variables are global if they are children of the <xsl:stylesheet> element. Otherwise, they are local. You can set the value of a variable only when you declare the variable by using the <xsl:variable> element. After that point, the value is fixed. The name attribute specifies the name of the variable. After declaring the variable, you can refer to it within an XPath expression using this name, prefixed with the $ character.

The following example declares the message variable. The message variable includes text and parameter values. The script generates a system log message by referring to the value of the message variable. The resulting system log message is as follows:

Device device-name was changed on date by user 'user.'

<xsl:template name="emit-syslog">
    <xsl:param name="user"/>
    <xsl:param name="date"/>
    <xsl:param name="device"/>
    <xsl:variable name="message">
        <xsl:text>Device </xsl:text>
        <xsl:value-of select="$device"/>
        <xsl:text> was changed on </xsl:text>
        <xsl:value-of select="$date"/>
        <xsl:text> by user '</xsl:text>
        <xsl:value-of select="$user"/>
xsl:text>.'</xsl:text>
    </xsl:variable>
    <syslog>
        <message>
            <xsl:value-of select="$message"/>
        </message>
    </syslog>
</xsl:template>

Table 8 provides examples of variable declarations and their pseudocode meanings.

Table 8: Variable Declarations: Examples and Pseudocode

Variable Declaration Examples

Pseudocode Meanings

<xsl:variable name="mpls" select="protocols/mpls"/>

Assigns the [edit protocols mpls] hierarchy level to the variable named $mpls.

xsl:variable name="color" select="data[name = 'color']/value"/>

Assigns the value of the color macro parameter to a variable named $color. The <data> element in the XPath expression is useful in commit script macros. For more information, see Creating a Macro to Read the Custom Syntax and Generate Related Configuration Statements.


[Contents] [Prev] [Next] [Index] [Report an Error]