The following DB2XML query is used:
[categories] select Distinct LEFT(ProductName,1) as Letter from products |
[products] select ProductName, CategoryName, QuantityPerUnit, UnitsInStock
from products, categories where products.CategoryID = categories.CategoryID
order by ProductName
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" indent="yes"/>
<!-- Main rule -->
<xsl:template match="/">
<xsl:variable name="First" select="true()"/>
<html>
<head>
<title>
Invoice
</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
</head>
<body bgcolor="#FFFFFF">
<H2 align="left"><font color="black">Alphabetical List of Products</font></H2>
<hr/><p/>
<xsl:apply-templates select="database/categories/categories_rec"/>
</body>
</html>
</xsl:template>
<xsl:template match="categories_rec">
<xsl:variable name="letter" select="categories_rec.Letter"/>
<h4 align="left"><xsl:apply-templates select="categories_rec.Letter"/></h4>
<table border="0" cellpadding="2" width="100%">
<xsl:apply-templates select="/database/products[position()=1]"/>
<xsl:apply-templates select='/database/products/products_rec[starts-with(products_rec.ProductName/text(), $letter)]'/>
</table>
<hr/>
</xsl:template>
<!-- Main Table -->
<xsl:template match="products">
<xsl:for-each select="./*[position()=1]">
<!-- Main Table header -->
<tr bgcolor="white"><td width="3%"></td>
<xsl:for-each select="./*">
<xsl:choose>
<xsl:when test="not(position()=last())">
<td align="left" width="30%">
<font color="black" size="-1"><b>
<xsl:value-of select="@NAME"/>:
</b></font>
</td>
</xsl:when>
<xsl:otherwise>
<td align="right" width="7%">
<font color="black" size="-1"><b>
<xsl:value-of select="@NAME"/>:
</b></font>
</td>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</tr>
</xsl:for-each>
</xsl:template>
<xsl:template match="products_rec">
<tr><td width="3%"></td>
<xsl:for-each select="./*">
<xsl:choose>
<xsl:when test="not(position()=last())">
<td align="left" width="30%">
<font color="black" size="-1">
<xsl:if test="@ISNULL">
</xsl:if>
<xsl:apply-templates/>
</font>
</td>
</xsl:when>
<xsl:otherwise>
<td align="right" width="7%">
<font color="black" size="-1">
<xsl:if test="@ISNULL">
</xsl:if>
<xsl:apply-templates/>
</font>
</td>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</tr>
</xsl:template>
</xsl:stylesheet>