Using Named Template Rules in XSLT

MSXML 5.0 SDK

Microsoft XML Core Services (MSXML) 5.0 for Microsoft Office - XSLT Developer's Guide

Using Named Template Rules in XSLT

As the term implies, an XSLT named template contains a template that can be instantiated in the result tree, and this template is given a name for easy reference elsewhere in the style sheet. The named template is actually instantiated in the result tree only at the point of a corresponding <xsl:call-template> element in a regular template rule.

The following is a simple named template.

<xsl:template name="display_price">
   <xsl:value-of select="@price"/>
</xsl:template>

The following is a template rule that instantiates the named template.

<xsl:template match="/">
    <xsl:call-template name="display_price"/>
</xsl:template>

Note that the value of the name attribute of the named template is the same as the value of the name attribute in the <xsl:call-template> element.

The contents of the named template are inserted into the calling template rule, replacing the <xsl:call-template> element.

Named templates have two primary uses:

  • As containers for frequently-used templates.
  • As templates whose contents and structures depend on some variable set of conditions in the invoking template rules, using parameters.

A given named template might fulfill one purpose or the other, or both.

This section begins with the region_qtr example. This example provides an XML file that contains regional sales data for a fictitious company, and an XSLT file to display the data. The subsequent topics in this section add to these initial files, resulting in a finished example at the end of the section.

This section contains the following topics.

See Also

<xsl:template> Element | <xsl:param> Element | <xsl:with-param> Element | Using XSLT Variables and Parameters | XSLT Utilities and Samples You Can Download