Help us improve your experience.

Let us know what you think.

Do you have time for a two-minute survey?

 
 

XSLT Parameters Overview

Parameters can be passed to either named or unnamed templates. Inside the template, parameters must be declared and can then be referenced by prefixing their name with the dollar sign ($).

Declaring Parameters

The scope of a parameter can be global or local. A parameter whose value is set by Junos OS at script initialization must be defined as a global parameter. Global parameter declarations are placed just after the style sheet declarations. A script can assign a default value to the global parameter, which is used in the event that Junos OS does not give a value to the parameter.

Local parameters must be declared at the beginning of a block and their scope is limited to the block in which they are declared. Inside a template, you declare parameters using the <xsl:param> tag and name attribute. Optionally, declare default values for each parameter by including the select attribute, which can contain XPath expressions. If a template is invoked without the parameter, the default expression is evaluated, and the results are assigned to the parameter. If you do not define a default value in the template, the parameter defaults to an empty string.

The following named template print-host-name declares the parameter message and defines a default value:

The template accesses the value of the message parameter by prefixing the parameter name with the dollar sign ($).

Passing Parameters

When you invoke a template, you pass arguments into the template using the <xsl:with-param> element and name attribute. The value of the <xsl:with-param> name attribute must match the name of a parameter defined in the actual template; otherwise the parameter is ignored. Optionally, for each parameter you pass to a template, you can define a value using either the select attribute or the contents of the <xsl:with-param> element.

The parameter value that gets used in a template depends on how the template is called. The following three examples, which call the print-host-name template, illustrate the possible calling environments.

If you call a template but do not include the <xsl:with-param> element for a specific parameter, the default expression defined in the template is evaluated, and the results are assigned to the parameter. If there is no default value for that parameter in the template, the parameter defaults to an empty string. The following example calls the named template print-host-name but does not include any parameters in the call. In this case, the named template will use the default value for the message parameter that was defined in the print-host-name template, or an empty string if no default exists.

If you call a template and include a parameter, but do not define a value for the parameter in the calling environment, the script passes in the current value of the parameter if it was previously initialized, or it generates an error if the parameter was never declared. The following example calls the named template print-host-name and passes in the message parameter, but does not include a value. If message is declared and initialized in the script, and the scope is visible to the block, the current value of message is used. If message is declared in the script but not initialized, the value of message will be an empty string. If message has not been declared, the script produces an error.

If you call a template, include the parameter, and define a value for the parameter, the template uses the provided value. The following example calls the named template print-host-name with the message parameter and a defined value, so the template uses the new value.

Example: Parameters and Match Templates

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. The parameter host is used in the processing of any matching nodes.The value of the host parameter is the value of the host-name statement at the [edit system] level of the configuration hierarchy.

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 that was defined in the <xsl:with-param> tag in the previous example.

Example: Parameters and Named Templates

The following named template report-changed declares two parameters: dot, which defaults to the current node, and changed, which defaults to the changed attribute of the node dot.

The next stanza calls the report-changed template and defines a source for the changed attribute different from the default source defined in the report-changed template. When the report-changed template is invoked, it will use the newly defined source for the changed attribute in place of the default source.

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