June 2008
Intermediate to advanced
986 pages
27h 8m
English
XSLT provides the <xsl:number> element to number the parts
of a document. (It can also be used to format a numeric value; more on
that later.) In general, <xsl:number> counts something. We’ll
look at a variety of examples here.
To fully illustrate how <xsl:number> works, we’ll need an XML
document with some things to count. We’ll reuse our list of cars from
the previous section:
<?xml version="1.0" encoding="utf-8"?>
<!-- cars.xml -->
<cars>
<manufacturer name="Chevrolet">
<car>Cavalier</car>
<car>Corvette</car>
<car>Impala</car>
<car>Malibu</car>
</manufacturer>
<manufacturer name="Ford">
<car>Pinto</car>
<car>Mustang</car>
<car>Taurus</car>
</manufacturer>
<manufacturer name="Volkswagen">
<car>Beetle</car>
<car>Jetta</car>
<car>Passat</car>
<car>Touraeg</car>
</manufacturer>
</cars>We’ll use <xsl:number> in
several different ways to illustrate the various options we have in
numbering things. We’ll start with something simple:
<?xml version="1.0" encoding="utf-8"?>
<!-- number1.xsl -->
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html"/>
<xsl:template match="/">
<html>
<head>
<title>Automobile manufacturers and their cars</title>
</head>
<body>
<xsl:for-each select="cars/manufacturer">
<p>
<xsl:number format="1. "/>
<xsl:value-of select="@name"/>
</p>
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>We get this HTML document:
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> ...
Read now
Unlock full access