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. Thenameattribute 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
messagevariable. 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:Devicedevice-namewas changed ondateby 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 6 provides examples of variable declarations and their pseudocode meanings.
<xsl:variablename="mpls" select="protocols/mpls"/>Assigns the
[edit protocols mpls]hierarchy level to the variable named$mpls.
<xxsl:variablename="color" select="data[name = 'color']/value"/>Assigns the value of the
colormacro 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.