xsl:call-template

Contact Us or call 1-877-932-8228
xsl:call-template

xsl:call-template

The xsl:call-template element is used to call a named template. The following example illustrates how it is used.

Code Sample:

TemplatesParametersVariables/Demos/BeatlesCallTemplates.xsl
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" 
				xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
	<xsl:output method="xml" indent="yes"/>
	<xsl:template match="/">
		<xsl:call-template name="StartHTML"/>
	</xsl:template>
	<xsl:template name="StartHTML">
		<html>
			<head>
				<title>Beatles</title>
			</head>
			<body>
				<h1>Beatles</h1>
				<ul>
					<xsl:apply-templates select="beatles/beatle">
						<xsl:sort select="lastname"/>
					</xsl:apply-templates>
				</ul>
			</body>
		</html>
	</xsl:template>
	<xsl:template match="beatle[not(@real='no')]">
		<li style="color:blue;"><xsl:apply-templates/></li>
	</xsl:template>
	<xsl:template match="beatle">
		<li style="color:red; text-decoration:line-through">
			<xsl:apply-templates/>
		</li>
	</xsl:template>
	<xsl:template match="name">
		<xsl:value-of select="lastname"/>,
		<xsl:value-of select="firstname"/>
	</xsl:template>
</xsl:stylesheet>

The template being called is named StartHTML:

<xsl:template name="StartHTML">

The xsl:call-template tag also takes the name attribute, which simply matches the name of the template started above:

<xsl:call-template name="StartHTML"/>

The called template inherits the context from the calling template. In this case the calling template matched the root node. Therefore, the root node will be the context node for all relative XPaths in the called template.

Next