Example 17-9: Reusable XSLT templates to format paging display

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <!--
   | UtilPaging.xsl: Transform <paging> structural info into HTML
   |                 presentation for "Page N of M" and Next/Prev Links
   +-->
  <xsl:template match="paging">
      <span class="paging">
        <xsl:call-template name="previousLink"/>
        <xsl:text> </xsl:text>
        <xsl:call-template name="currentPageIndicator"/>
        <xsl:text> </xsl:text>
        <xsl:call-template name="nextLink"/>
        <xsl:text> - </xsl:text>
        <xsl:call-template name="totalRowsIndicator"/>
      </span>
  </xsl:template>
  <!-- Display current page indicator "Page N of M" -->
  <xsl:template name="currentPageIndicator">
    <xsl:text>Page </xsl:text>
    <xsl:value-of select="current-page"/>
    <xsl:text> of </xsl:text>
    <xsl:value-of select="total-pages"/>
  </xsl:template>
  <!-- Display total rows -->
  <xsl:template name="totalRowsIndicator">
    <xsl:text>Total </xsl:text>
    <xsl:value-of select="total-rows"/>
  </xsl:template>
  <!-- Display hyperlink to previous page -->
  <xsl:template name="previousLink">
    <xsl:param name="label">Previous</xsl:param>
    <xsl:call-template name="pagelink">
      <xsl:with-param name="pagenum" select="prev-page"/>
      <xsl:with-param name="label" select="$label"/>
    </xsl:call-template>
  </xsl:template>
  <!-- Display hyperlink to next page -->
  <xsl:template name="nextLink">
    <xsl:param name="label">Next</xsl:param>
    <xsl:call-template name="pagelink">
      <xsl:with-param name="pagenum" select="next-page"/>
      <xsl:with-param name="label" select="$label"/>
    </xsl:call-template>
  </xsl:template>
  <!-- Generate correct hyperlink to page 'pagenum' including URL params -->
  <xsl:template name="pagelink">
    <xsl:param name="pagenum"/>
    <xsl:param name="label"/>
    <xsl:if test="$pagenum">
      <a> <!-- This lonesome-looking <a> is an HTML anchor tag -->
        <xsl:attribute name="href">
          <xsl:value-of select="target-page"/>
          <xsl:text>?p=</xsl:text>
          <xsl:value-of select="$pagenum"/>
          <xsl:if test="target-args">
            <xsl:text disable-output-escaping="yes">&amp;</xsl:text>
            <xsl:value-of select="target-args"/>
          </xsl:if>
        </xsl:attribute>
        <xsl:value-of select="$label"/>
      </a>
    </xsl:if>
  </xsl:template>
</xsl:stylesheet>