XML technologies / XSLT / Introduction to XSLT 2.0 / XSLT and XPath – round two / Numbering, counting and summing / Simple numbering

(Excerpt from "XSLT 2.0 & XPath 2.0" by Frank Bongers, Chapter 2.)

In the simplest case, no special steps are needed to obtain numbering. For example, if you want to output a numbered list in HTML, you can leave the actual numbering to the browser and simply generate HTML <li> elements inside an <ol> container:

<xsl:template match="liste">
  <ol><xsl:apply-templates/></ol>
</xsl:template>
<xsl:templates match="listenelement">
  <li><xsl:apply-templates/></li>
</xsl:template>

This simple approach has the advantage of being completely independent of whether content is processed in document order. Processing out of order can cause problems elsewhere, as will be shown later. For the book example, the following approach is possible:

<xsl:template match="buchhandel">
  <ol><xsl:apply-templates select="buch"/></ol>
</xsl:template>
<xsl:template match="buch">
  <li>
    <xsl:apply-templates/>
    <xsl:choose>
      <xsl:when test="@isbn != ''">
        <br/>(ISBN: <xsl:value-of select="@isbn"/>)
      </xsl:when>
      <xsl:otherwise>
        <br/>(ISBN-Nummer liegt nicht vor.)
      </xsl:otherwise>
    </xsl:choose>
  </li>
</xsl:template>

Code example: kap02/2.03.1/buchhandel-liste.xsl (excerpt).

The drawback, of course, is that this method is tied to HTML or XHTML as the output format. There are also cases requiring more complex, nested numbering, such as 1.A iii) or similar. More powerful tools are then needed.

   

back to top