How It Works
To invoke a template by name, two things have to happen:
The template you want to invoke has to have a
name.You use the
<xsl:call-template>element to invoke the named template.
Here’s how to do this. Say we have a template named createMasthead that creates the masthead of a web page. Whenever we create an HTML page for our web site, we want to invoke the createMasthead template to create the masthead. Here’s what our stylesheet would look like:
<xsl:template name="createMasthead">
<!-- interesting stuff that generates the masthead goes here -->
</xsl:template>
...
<xsl:template match="/">
<html>
<head>
<title><xsl:value-of select="title"/></title>
</head>
<body>
<xsl:call-template name="createMasthead"/>
...Named templates are extremely useful for defining commonly used
markup. For example, say you’re using an XSLT stylesheet to create web
pages with a particular look and feel. You can write named templates
that create the header, footer, navigation areas, or other items that
define how your web page will look. Every time you need to create a
web page, simply use <xsl:call-template> to invoke those
templates and create the look and feel you want.
Even better, if you put those named templates in a separate
stylesheet and import the stylesheet (with either <xsl:import> or <xsl:include>), you can create a set of stylesheets that generate the look and feel of the web site you want. If you decide to redesign your web site, redesign the stylesheets that define the common graphical ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access