Help us improve your experience.

Let us know what you think.

Do you have time for a two-minute survey?


XSLT Templates Overview

An XSLT script consists of one or more sets of rules called templates. Each template is a segment of code that contains rules to apply when a specified node is matched. You use the <xsl:template> element to build templates.

There are two types of templates, named and unnamed (or match), and they are described in the following sections.

Unnamed (Match) Templates

Unnamed templates, also known as match templates, include a match attribute that contains an XPath expression to specify the criteria for nodes upon which the template should be invoked. In the following example, the template applies to the element named route that is a child of the current context and that has a child element named next-hop whose value starts with the string 10.10..

By default, when XSLT processes a document, it recursively traverses the entire document hierarchy, inspecting each node, looking for a template that matches the current node. When a matching template is found, the contents of that template are evaluated.

The <xsl:apply-templates> element can be used inside an unnamed template to limit and control XSLT’s default, hierarchical traversal of nodes. If the <xsl:apply-templates> element has a select attribute, only nodes matching the XPath expression defined by the attribute are traversed. Otherwise all children of the context node are traversed. If the select attribute is included, but does not match any nodes, nothing is traversed and nothing happens.

In the following example, the template rule matches the <route> element in the XML hierarchy. All the nodes containing a changed attribute are processed. All <route> elements containing a changed attribute are replaced with a <new> element.

Using unnamed templates allows the script to ignore the location of a tag in the XML hierarchy. For example, if you want to convert all <author> tags into <div class="author"> tags, using templates enables you to write a single rule that converts all <author> tags, regardless of their location in the input XML document.

For more information about how unnamed templates are used in scripts, see xsl:template match="/" Template.

Named Templates

Named templates operate like functions in traditional programming languages, although with a verbose syntax. When the complexity of a script increases or a code segment appears in multiple places, you can modularize the code and create named templates. Like functions, named templates accept arguments and run only when explicitly called.

You create a named template by using the <xsl:template> element and defining the name attribute, which is similar to a function name in traditional programming languages. Use the <xsl:param> tag and its name attribute to define parameters for the named template, and optionally include the select attribute to declare default values for each parameter. The select attribute can contain XPath expressions. If the select attribute is not defined, the parameter defaults to an empty string.

The following example creates a template named my-template and defines three parameters, one of which defaults to the string false, and one of which defaults to the contents of the element node named name that is a child of the current context node. If the script calls the template and does not pass in a parameter, the default value is used.

To invoke a named template in a script, use the <xsl:call-template> element. The name attribute is required and defines the name of the template being called. When processed, the <xsl:call-template> element is replaced by the contents of the <xsl:template> element it names.

When you invoke a named template, you can pass arguments into the template by including the <xsl:with-param> child element and specifying the name attribute. The value of the <xsl:with-param> name attribute must match a parameter defined in the actual template; otherwise the parameter is ignored. Optionally, you can set a value for each parameter with either the select attribute or the content of the <xsl:with-param> element. If you 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. For more information about passing parameters, see XSLT Parameters Overview.

In the following example, the template my-template is called with the parameter c containing the contents of the element node named other-name that is a child of the current context node.

For an example showing how to use named templates in a commit script, see Example: Require and Restrict Configuration Statements.