Named templates

When the same output structures are required in a number of circumstances, it is possible to create a template to hold them. This template can be named, and referenced from other templates.

Simple referenced templates

The template is given a name using the Name attribute. For example, a dividing line may need to be output above a number of different output structures:

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

This template can be referenced from within other templates using the Call Template element. This element also has a Name attribute, which is used to name the template to use:

<xsl:template name="chapter">
  <xsl:call-template name="divideLine"/>
  <H1>NEW CHAPTER</H1><xsl:apply-templates/>
</xsl:template>

<xsl:template name="section">
  <xsl:call-template name="divideLine"/>
  <H2>NEW SECTION</H2><xsl:apply-templates/>
</xsl:template>

The named template does not change the current element, and if it contains Match, Priority or Mode attributes, they have no effect. It is simply a convenient wrapper for material that is needed in several places.

Parameters

As described above, named templates appear to be of limited value. It is not very common for exactly the same material to be needed in a number of different circumstances. For example, it is unlikely that a dividing line between chapters will be the same as between sections (it may be thicker or wider).

The following templates show how the Note, Warning and Danger elements have very similar formatting (only the background colour and prefix text differ):

<xsl:template name="note">
  <HR/>
  <DIV STYLE='background-color:blue'>
    <P><xsl:text>Note: </xsl:text>
      <xsl:apply-templates/>
    </P>
  </DIV>
  <HR/>
</xsl:template>

<xsl:template name="warning">
  <HR/>
  <DIV STYLE='background-color:yellow'>
    <P><xsl:text>Warning: </xsl:text>
      <xsl:apply-templates/>
    </P>
  </DIV>
  <HR/>
</xsl:template>
<xsl:template name="danger">
  <HR/>
  <DIV STYLE='background-color:red'>
    <P><xsl:text>Danger: </xsl:text>
      <xsl:apply-templates/>
    </P>
  </DIV>
  <HR/>
</xsl:template>

It would be useful to be able to use a named template, but to modify just part of its contents, depending on where it is applied. An approach familiar to software developers allows 'parameters' to be passed to a function, procedure or method; the With Parameter element provides this kind of feature in XSLT. This element can be used within the Call Template element. It has a Name attribute to name the parameter. The examples above can be reformulated in the following way. In the following example, the Note template passes two parameters to the 'NoteWarningDanger' template, specifying the colour and prefix text to be used in this context:

<template name="note">
  <call-template name="NoteWarningDanger">
    <with-param name="colour">blue</with-param>
    <with-param name="prefix">Note</with-param>
  </call-template>
</template>

The named template needs to accept and use the two parameters. The parameters are defined within the template using the Parameter element. This element works in the same way as the Variable element. It has the same attributes and is referenced in the same way (by name, with a '$' prefix). The Name attribute is used to define the parameter, and the value is defined by the Select attribute or by the content of the element. The one big difference is that the value specified is only the default value, to be employed when a specific value is not passed to the template. In the following example, the color parameter is given a default value of 'white', and the prefix parameter is given a default value of 'Note':

<xsl:template name="NoteWarningDanger">
  <xsl:param
						name="colour">white</xsl:param>
  <xsl:param
						name="prefix">Note</xsl:param>
  <HR/>
  <DIV STYLE='background-color:{$colour}'>
    <P><xsl:value-of select='$prefix'>
      <xsl:text>: </xsl:text>
      <xsl:apply-templates/>
    </P>
  </DIV>
  <HR/>
</xsl:template>

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset