Name

<xsl:call-template>

Invokes a particular template by name. If you have output that you need to generate often, you can put the markup that creates that output into a named template and invoke that template whenever you want.

Category

Instruction.

Required Attribute

name

The name of the template you’re invoking.

Optional Attributes

None.

Content

This element can contain any number of optional <xsl:with-param> elements.

Appears in

<xsl:call-template> appears inside a template.

Defined in

[1.0] XSLT section 6, “Named Templates.”

[2.0] XSLT section 10.1, “Named Templates.”

Example

Invoking named templates with <xsl:call-template> is a great way to create modular stylesheets. We’ll use named templates to format our document of chocolate sales:

<?xml version="1.0" encoding="utf-8"?>
<!-- chocolate.xml -->
<report month="8" year="2006">
  <title>Chocolate bar sales</title>
  <brand>
    <name>Lindt</name>
    <units>27408</units>
  </brand>
  <brand>
    <name>Callebaut</name>
    <units>8203</units>
  </brand>
  <brand>
    <name>Valrhona</name>
    <units>22101</units>
  </brand>
  <brand>
    <name>Perugina</name>
    <units>14336</units>
  </brand>
  <brand>
    <name>Ghirardelli</name>
    <units>19268</units>
  </brand>
</report>

Here is our stylesheet with named templates:

<?xml version="1.0"?>
<!-- call-template.xsl -->
<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:output method="html"/>

  <xsl:template match="report">
    <html>
      <head>
        <xsl:call-template name="report-title">
 <xsl:with-param name="in-heading" select="true()"/> ...

Get XSLT, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.