Skip to Main Content
XSLT
book

XSLT

by Doug Tidwell
August 2001
Intermediate to advanced content levelIntermediate to advanced
480 pages
11h 16m
English
O'Reilly Media, Inc.
Content preview from XSLT

Invoking Templates by Name

Up to this point, we’ve always used XSLT’s <xsl:apply-templates> element to invoke other templates. You can think of this as a limited form of polymorphism; a single instruction is invoked a number of times, and the XSLT processor uses each node in the node-set to determine which <xsl:template> to invoke. Most of the time, this is what we want. However, sometimes we want to invoke a particular template. XSLT allows us to do this with the <xsl:call-template> element.

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

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.
Start your free trial

You might also like

Learning XSLT

Learning XSLT

Michael Fitzgerald
Inside XSLT

Inside XSLT

Steven Holzner
XSLT Cookbook

XSLT Cookbook

Sal Mangano
XSLT and XPATH: A Guide to XML Transformations

XSLT and XPATH: A Guide to XML Transformations

John Robert Gardner, Zarella L. Rendon

Publisher Resources

ISBN: 0596000537Supplemental ContentCatalog PageErrata