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.
Copyright © Galileo Press, Bonn 2008
You may print the online version for your private use.
Otherwise, this chapter from "XSLT 2.0 & XPath 2.0 ― Das umfassende Handbuch" is subject to the same terms as the printed edition: the work and all its parts are protected by copyright. All rights are reserved, including reproduction, translation, microfilming, and storage and processing in electronic systems.
Galileo Press, Rheinwerkallee 4, 53227 Bonn