Context-specific formatting (modes)

The problem with the examples above is that the formatting of the copied material is not affected by its move to a new location. In many examples, including the building of a table of contents, there is a need to apply different formatting to the material at its new location. For example, the chapter titles should be larger than the table of contents entries (as they are in this book).

XSLT includes the concept of 'modes'. Sets of templates can be assigned to specific modes, and the whole stylesheet can be switched between these modes to enable and disable templates that match the same element. The Mode attribute is added to the Template element to assign the template to a specific mode. In the following example, there are two templates that match the Title element, but one belongs to the 'contents' mode:

<template match="title">...</template>


<template match="title" mode="contents">...</template>

The title may be formatted in different ways, depending on which mode (in this case the 'default' mode or the 'contents' mode) is in effect at the time. Normally, the current mode is the default mode and all templates that have a Mode attribute are disabled. In the example above, the first template for the Title element is active and the second is inactive. To enable a specific mode, the Mode attribute is added to the Apply Templates element. The children (or other) elements processed as a result of this instruction are processed using the applicable templates:

<apply-templates mode="contents" />

This feature can be used to great effect when the need is to format copied material in a different way. Taking the table of contents example, the default mode can be used for normal rendering of the title at the top of each chapter, and another mode can be used to render the titles in a smaller point size within the table of contents. In the following example, H1 elements are used to format the Title element within the chapter, and H4 elements are used to format the Title element in the table of contents:

<xsl:template match="book">
  <HTML><HEAD><TITLE>A book</TITLE></HEAD>
  <BODY>
    <DIV>
      <xsl:apply-templates select="chapter/title"
                           mode="contents" />
    </DIV>
    <xsl:apply-templates/>
  </BODY>
</xsl:template>


<xsl:template match="chapter">
  <xsl:apply-templates/>
</xsl:template>


<xsl:template match="title">
  <H1><xsl:apply-templates/></H1>
</xsl:template>


<xsl:template match="title" mode="contents">
  <H4><xsl:apply-templates/></H4>
</xsl:template>

Note that there are built-in, mode-specific versions of the template that match elements with no explicit template definition. It is not necessary to create such templates as the following:

<!-- NOT NECESSARY (default behaviour) -->
<xsl:template match="*|/" mode="contents">
  <xsl:apply-templates/>
</xsl:template>

But it can be done, if the intention is to override the default behaviour (such as avoiding processing the descendants of unspecified elements):

<xsl:template match="*|/" mode="special">
  <!-- DO NOT PROCESS CHILDREN OF UNSPECIFIED ELEMENTS -->
</xsl:template>

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

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