XSLT » Elements » xsl:call-template


The xsl:call-template element is used to invoke a template by name. By invoke, we mean that the named template is called and applied to the source document. If a template does not have a name, it cannot be called by this element.

The xsl:template element is used to create a template. You can name a template by using the name attribute of the xsl:template element. Further, the name called by the mandatory name attribute of the xsl:call-template element must match the name specified by the name attribute of the xsl:template element. Complicating matters is the fact that a template is not required to have a name. A template is only required to have either a name or match attribute. (It can have both.) Ideally, each template will have a unique name. However, if a name is repeated, then the two templates must have a different import precedence (refer to the xsl:import element), otherwise it is an error.
An xsl:call-template element cannot directly return a result. You need to enclose the xsl:call-template element inside an xsl:variable element which serves as the current output destination (see code example).
The xsl:call-template element can contain zero or more xsl:with-param elements. It cannot contain any other XSLT elements. These xsl:with-param elements will only be evaluated if there is a matching xsl:param element in the template being called. If there is no such match, then the xsl:with-param element is simply ignored.
This is not a self-closing element. The separate closing element is mandatory.


<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

<xsl:variable name="hoo">
<xsl:call-template name="boo">
<xsl:with-param name="name" />
<xsl:with-param name="dob" />

<xsl:template name="boo" match="/">
<xsl:param name="name" />
<xsl:param name="dob" />
<xsl:for-each select="devguru_staff/programmer">
NAME: <xsl:value-of select="name" />
<br />
DOB: <xsl:value-of select="dob" />
<hr />

NAME: Bugs Bunny
DOB: 03/21/1970
NAME: Daisy Duck
DOB: 08/09/1949
NAME: Minnie Mouse
DOB: 04/13/1977
NAME: Pluto
DOB: 07/04/1979
NAME: Porky Pig
DOB: 11/30/1956
NAME: Road Runner
DOB: 01/19/1953

This is the code for xslt_example_calltemplate.xsl. We use the DevGuru Staff List XML file for our example with the following header:
<xml-stylesheet type="text/xsl" href="xslt_example_calltemplate.xsl">

Language(s): XSLT

See Also: