XSLT Parameters Overview

Parameters can be passed to either named or unnamed templates using the <xsl:with-param> element. Inside the template, parameters must be declared and can then be referenced by prefixing their name with the dollar sign ($).

The following template matches on /, the root of the XML document. It then generates an element named <outside>, which is added to the output document, and instructs the Junos OS management process (mgd) to recursively apply templates to the configuration/system subtree. A parameter called host is passed to any templates that are processed.

<xsl:template match="/">
     <outside>
        <xsl:apply-templates select="configuration/system">
                <xsl:with-param name="host" select="configuration/system/host-name"/>
        </xsl:apply-templates>
    </outside>
</xsl:template>

The following template matches the <system> element, which is the top of the subtree selected in the previous example. The host parameter is declared with no default value. An <inside> element is generated, which contains the value of the host parameter.

<xsl:template match="system">
     <xsl:param name="host"/>
     <inside>
          <xsl:value-of select="$host"/>
     </inside>
</xsl:template>

To declare a default value for a parameter, include the select attribute and specify the desired default. If the template is invoked without the parameter, the XPath expression is evaluated and the results are assigned to the parameter.

The second template declares two parameters: $dot, which defaults to the current node, and $changed, which defaults to the changed attribute of the node $dot.

<xsl:template name="report-changed">
    <xsl:param name="dot" select="."/>
    <xsl:param name="changed" select="$dot/@changed"/>
    <!-- ... -->
</xsl:template>

The next stanza calls the <report-changed> template and defines a source for the changed attribute other than the default source selected in the <report-changed> template.

<xsl:template match="system">
    <xsl:call-template name="report-changed">
        <xsl:with-param name="changed" select="../@changed"/>
    </xsl:call-template>
</xsl:template>

Likewise, the template call can include the dot parameter and define a source other than the default current node, as shown here:

<xsl:template match="system">
    <xsl:call-template name="report-changed">
        <xsl:with-param name="dot" select="../../>
    </xsl:call-template>
</xsl:template>