XSLT Templates Overview

An XSLT script consists of one or more sets of rules called templates. Each template 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, and they are described in the following sections.

Unnamed Templates

Unnamed 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..

<xsl:template match="route[starts-with(next-hop, '10.10.')]">
<!-- ... body of the template goes here ... -->
</xsl:template>

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. The select attribute can contain any XPath expression. If the <xsl:apply-templates> element has a select attribute, only nodes matching the XPath expression defined by the attribute are traversed. If the select attribute matches no nodes, nothing is traversed and nothing happens. Without a select attribute, all children of the context node are traversed.

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.

<xsl:template match="route">
<new>
<xsl:apply-templates select="*[@changed]"/>
</new>
</xsl:template>

Using unnamed templates allows the script to ignore where in the XML hierarchy a tag appears. 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 commit scripts, see xsl:template match="/" Template.

Named Templates

Named templates operate like functions in traditional programming languages, although with a verbose syntax. When markup grows complex, and when it appears in several different places in a style sheet, you can turn it into a named template. Named templates resemble variables. However, they enable you to include data from the place where the template is applied, rather than merely inserting fixed text.

Parameters can be passed into named templates, and the parameters can be declared with default values. The following sample template named my-template 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 template is called without values for these parameters, the default values are used. As with unnamed templates, the select attribute can contain any XPath expression. If no select attribute is given for a parameter, it defaults to an empty value.

<xsl:template name="my-template">
    <xsl:param name="a"/>
    <xsl:param name="b" select="'false'"/>
    <xsl:param name="c" select="name"/>
     <!-- ... body of the template goes here ... -->
</xsl:template>

To invoke a named template, you must use the <xsl:call-template> element. It has a required name parameter that names the template it calls. When processed, the <xsl:call-template> element is replaced by the contents of the <xsl:template> element it names. 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.

<xsl:call-template name="my-template">
    <xsl:with-param name="c" select="other-name"/>
</xsl:call-template>

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

Related Topics