The xsl:call-template Element: Using Named Templates

The <xsl:call-template> Element: Using Named Templates

One of the attributes of the <xsl:template> element is name , which you use to give a name to the template. For example, if I have a literal result element consisting of two <BR> and two <HR> HTML elements that I use to create a vertical separator in HTML documents, as follows :

 <BR/>  <HR/>  <BR/>  <HR/> 

then I can create a template named separator with this literal result element this way:

 <xsl:template name="separator">      <BR/>      <HR/>      <BR/>      <HR/>  </xsl:template> 

This is a named templateall you have to do is assign a name to the name attribute of an <xsl:template> element, and you have a named template.

Note that this template is not set up to match anything in particular. To invoke this template, you must call it explicitly. So how do you do that?

You call named templates with the <xsl:call-template> elementit has just one attribute:

  • name (mandatory). The name of the template to call; set to a QName.

The following example puts the template named separator to work. All I have to do is call that template when its needed, as follows:

 <?xml version="1.0"?>  <xsl:stylesheet version="1.1"  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">      <xsl:template match="/PLANETS">          <HTML>              <HEAD>                  <TITLE>                      The Planets Table                  </TITLE>              </HEAD>              <BODY>                  <H1>                      The Planets Table                  </H1>                  <xsl:call-template name="separator"/>                  <TABLE BORDER="2">                      <TR>                          <TD>Name</TD>                          <TD>Mass</TD>                          <TD>Radius</TD>                          <TD>Day</TD>                      </TR>                      <xsl:apply-templates/>                  </TABLE>                  <xsl:call-template name="separator"/>              </BODY>          </HTML>      </xsl:template>      <xsl:template match="PLANET">         <TR>            <TD><xsl:value-of select="NAME"/></TD>            <TD><xsl:apply-templates select="MASS"/></TD>            <TD><xsl:apply-templates select="RADIUS"/></TD>            <TD><xsl:apply-templates select="DAY"/></TD>         </TR>     </xsl:template>      <xsl:template match="MASS">          <xsl:value-of select="."/>          <xsl:text> </xsl:text>          <xsl:value-of select="@UNITS"/>      </xsl:template>      <xsl:template match="RADIUS">          <xsl:value-of select="."/>          <xsl:text> </xsl:text>          <xsl:value-of select="@UNITS"/>      </xsl:template>      <xsl:template match="DAY">          <xsl:value-of select="."/>          <xsl:text> </xsl:text>          <xsl:value-of select="@UNITS"/>      </xsl:template>      <xsl:template name="separator">          <BR/>          <HR/>          <BR/>          <HR/>      </xsl:template>  </xsl:stylesheet> 

Heres the result. Note that the <BR> and <HR> elements have been inserted as required:

 <HTML>      <HEAD>          <TITLE>              The Planets Table          </TITLE>      </HEAD>      <BODY>          <H1>              The Planets Table          </H1>          <BR>          <HR>          <BR>          <HR>          <TABLE BORDER="2">              <TR>                  <TD>Name</TD>                  <TD>Mass</TD>                  <TD>Radius</TD>                  <TD>Day</TD>              </TR>              <TR>                  <TD>Mercury</TD>                  <TD>.0553 (Earth = 1)</TD>                  <TD>1516 miles</TD>                  <TD>58.65 days</TD>              </TR>              <TR>                  <TD>Venus</TD>                  <TD>.815 (Earth = 1)</TD>                  <TD>3716 miles</TD>                  <TD>116.75 days</TD>              </TR>              <TR>                  <TD>Earth</TD>                  <TD>1 (Earth = 1)</TD>                  <TD>2107 miles</TD>                  <TD>1 days</TD>              </TR>          </TABLE>          <BR>          <HR>          <BR>          <HR>      </BODY>  </HTML> 

You can see this result document in Figure 9.1.

Figure 9.1. Calling a named template.
graphics/09fig01.gif

This answers the question of how you can refer to a literal result element by name for easy placement in the result document. However, this is pretty staticthe literal result element is always the same. As it turns out, though, calling a named template is much like calling a subroutine in a programming language. Just as you can pass data to a subroutine, so you can pass data to named templates using parameters .



Inside XSLT
Inside Xslt
ISBN: B0031W8M4K
EAN: N/A
Year: 2005
Pages: 196

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net