XML technologies / XSLT / XSLT Cookbook / Transform XML into HTML / Generate HTML tables
(Excerpt from “XSLT Kochbuch” by Sal Mangano)
Problem
You want to map XML contents to HTML tables.
Solution
Tables are often generated in two steps: create top-level table markup, then apply templates for rows and cells. This modifies part of the stylesheet from recipe Create linked documents. The modified part is highlighted:
<xsl:stylesheet version="1.1" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html"/>
<xsl:param name="URL"/>
<xsl:template match="/">
<xsl:apply-templates select="*" mode="index"/>
<xsl:apply-templates select="*" mode="content"/>
</xsl:template>
<!-- = = = = = = = = = = = = = = = = = = = = = = = = = -->
<!-- Erzeuge index.html (mode = "index") -->
<!-- = = = = = = = = = = = = = = = = = = = = = = = = = -->
<xsl:template match="salesBySalesperson" mode="index">
<!-- Nicht standardkonformes Saxon-xsl:document! -->
<xsl:document href="index.html">
<html>
<head>
<title>Sales by Salesperson</title>
</head>
<body bgcolor="#FFFFFF" text="#000000">
<h1>Sales By Salesperson</h1>
<xsl:apply-templates mode="index"/>
</body>
</html>
</xsl:document>
</xsl:template>
<xsl:template match="salesperson" mode="index">
<h2>
<a href="{concat($URL,@name,'.html')}">
<xsl:value-of select="@name"/>
</a>
</h2>
</xsl:template>
<!-- = = = = = = = = = = = = = = = = = = = = = = = = = -->
<!-- Erzeuge @name.html (mode = "content") -->
<!-- = = = = = = = = = = = = = = = = = = = = = = = = = -->
<template:xsl match="salesperson" mode="content">
<xsl:document href="{concat(@name,'.html')}">
<html>
<head>
<title><xsl:value select="@name"/></title>
</head>
<body bgcolor="#FFFFFF" text="#000000">
<h1><xsl:value-of select="@name"/> Sales</h1>
<table border="1" cellpadding="3">
<tbody>
<tr>
<th>SKU</th>
<th>Sales (in US $)</th>
</tr>
<xsl:apply-templates mode="content"/>
</tbody>
</table>
<h2><a href="{concat($URL,'index.html')}">Home</a></h2>
</body>
</html>
</xsl:document>
</xsl:template>
<xsl:template match="product" mode="content">
<tr>
<td><xsl:value-of select="@sku"/></td>
<td><xsl:value-of select="@totalSales"/></td>
</tr>
</xsl:template>
</xsl:stylesheet>Discussion
XSLT 1.0
Table generation often requires grouping. The difficulty depends on existing input grouping and whether criteria are closed or open. Suppose sales figures must be grouped by region:
<?xml version="1.0" encoding="UTF-8"?>
<sales>
<product sku="10000" sales="90000.00" region="NE"/>
<product sku="10000" sales="10000.00" region="NW"/>
<product sku="10000" sales="55000.00" region="SE"/>
<product sku="10000" sales="32000.00" region="SW"/>
<product sku="10000" sales="95000.00" region="NC"/>
<product sku="10000" sales="88000.00" region="SC"/>
<product sku="20000" sales="77000.00" region="NE"/>
<product sku="20000" sales="11100.00" region="NW"/>
<product sku="20000" sales="33210.00" region="SE"/>
<product sku="20000" sales="78000.00" region="SW"/>
<product sku="20000" sales="105000.00" region="NC"/>
<product sku="20000" sales="12300.00" region="SC"/>
<product sku="30000" sales="1000.00" region="NE"/>
<product sku="30000" sales="5100.00" region="NW"/>
<product sku="30000" sales="3210.00" region="SE"/>
<product sku="30000" sales="8000.00" region="SW"/>
<product sku="30000" sales="5000.00" region="NC"/>
<product sku="30000" sales="11300.00" region="SC"/>
</sales>With six regions known in advance, explicit selection is possible:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html"/>
<xsl:template match="sales">
<html>
<head>
<title>Sales by Region</title>
</head>
<body>
<h1>Sales by Region</h1>
<table border="1" cellpadding="3">
<tbody>
<tr>
<th>SKU</th>
<th>Sales</th>
</tr>
<xsl:call-template name="group-region">
<xsl:with-param name="region" select=" 'NE' "/>
<xsl:with-param name="title" select="'North East Sales'"/>
</xsl:call-template>
<xsl:call-template name="group-region">
<xsl:with-param name="region" select=" 'NW' "/>
<xsl:with-param name="title" select="'North West Sales'"/>
</xsl:call-template>
<xsl:call-template name="group-region">
<xsl:with-param name="region" select=" 'NC' "/>
<xsl:with-param name="title" select="'North Central Sales'"/>
</xsl:call-template>
<xsl:call-template name="group-region">
<xsl:with-param name="region" select=" 'SE' "/>
<xsl:with-param name="title" select="'South East Sales'"/>
</xsl:call-template>
<xsl:call-template name="group-region">
<xsl:with-param name="region" select=" 'SC' "/>
<xsl:with-param name="title" select="'South Central Sales'"/>
</xsl:call-template>
<xsl:call-template name="group-region">
<xsl:with-param name="region" select=" 'SW' "/>
<xsl:with-param name="title" select="'South West Sales'"/>
</xsl:call-template>
</tbody>
</table>
</body>
</html>
</xsl:template>
<xsl:template name="group-region">
<xsl:param name="region"/>
<xsl:param name="title"/>
<xsl:variable name="products" select="product[@region = $region]" />
<tr>
<th colspan="2"><xsl:value-of select="$title" /></th>
</tr>
<xsl:apply-templates select="$products"/>
<tr style="font-weight:bold">
<td>Total</td>
<td align="right">
<xsl:value-of select="format-number(sum($products/@sales), '#.00')"/>
</td>
</tr>
</xsl:template>
<xsl:template match="product">
<tr>
<td><xsl:value-of select="@sku"/></td>
<td align="right"><xsl:value-of select="@sales"/></td>
</tr>
</xsl:template>
</xsl:stylesheet>To avoid hard-coding names, use a table-driven approach:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:sales="sales">
<sales:region code="NE" name="North East"/>
<sales:region code="NC" name="North Central"/>
<sales:region code="NW" name="North West"/>
<sales:region code="SE" name="South East"/>
<sales:region code="SC" name="South Central"/>
<sales:region code="SW" name="South West"/>
<xsl:variable name="products" select="/sales/product"/>
<xsl:output method="html"/>
<xsl:template match="sales">
<html>
<head>
<title>Sales by Region</title>
</head>
<body>
<h1>Sales by Region</h1>
<table border="1" cellpadding="3">
<tbody>
<tr>
<th>SKU</th>
<th>Sales</th>
</tr>
<xsl:for-each select="document('')/*/sales:region">
<tr>
<th colspan="2"><xsl:value-of select="@name"/> Sales</th>
</tr>
<xsl:call-template name="group-region">
<xsl:with-param name="region" select="@code"/>
</xsl:call-template>
</xsl:for-each>
</tbody>
</table>
</body>
</html>
</xsl:template>
<xsl:template name="group-region">
<xsl:param name="region"/>
<xsl:apply-templates select="$products[@region=$region]"/>
<tr style="font-weight:bold">
<td>Total</td>
<td align="right">
<xsl:value-of select="format-number(sum($products[@region=$region]/@sales),'#.00')"/>
</td>
</tr>
</xsl:template>
<xsl:template match="product">
<tr>
<td><xsl:value-of select="@sku"/></td>
<td align="right"><xsl:value-of select="@sales"/></td>
</tr>
</xsl:template>
</xsl:stylesheet>Many grouping problems are harder. A stylesheet used across companies or departments must handle different regional naming conventions. Among the most efficient solutions is Muenchian grouping, named after its inventor Steve Muench at Oracle, combining keys and IDs:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:sales="sales">
<xsl:output method="html"/>
<xsl:key name="region-key" match="product" use="@region"/>
<xsl:template match="sales">
<html>
<head>
<title>Sales by Region</title>
</head>
<body>
<h1>Sales by Region</h1>
<table border="1" cellpadding="3">
<tbody>
<tr>
<th>SKU</th>
<th>Sales</th>
</tr>
<xsl:variable name="unique-regions" select="/sales/product[generate-id(.) = generate-id(key('region-key',@region))]/@region"/>
<xsl:for-each select="$unique-regions">
<tr >
<th colspan="2"><xsl:value-of select="."/> Sales</th>
</tr>
<xsl:call-template name="group-region">
<xsl:with-param name="region" select="."/>
</xsl:call-template>
</xsl:for-each>
</tbody>
</table>
</body>
</html>
</xsl:template>
<xsl:template name="group-region">
<xsl:param name="region"/>
<xsl:apply-templates select="key('region-key', @region)"/>
<tr style="font-weight:bold">
<td>Total</td>
<td align="right">
<xsl:value-of select="format-number(sum(key('region-key', @region)/@sales),'#.00')"/>
</td>
</tr>
</xsl:template>
<xsl:template match="product">
<tr>
<td><xsl:value-of select="@sku"/></td>
<td align="right"><xsl:value-of select="@sales"/></td>
</tr>
</xsl:template>
</xsl:stylesheet>Structurally this resembles the table-based approach, but unique groups are determined differently. The table approach literally encodes unique regions in sales:region elements. Muenchian grouping defines the group value with a key. Expression key('region-key',@region) returns all products whose region equals @region. For a node set, generate-id returns the first element’s unique ID. Therefore expression [generate-id(.) = generate-id(key('region-key', @region))] is true only for each group’s first node, identifying every unique region. The key also makes other stylesheet parts accessing products more efficient.
XSLT 2.0
Unless you jumped straight here, you know that grouping in XSLT 2.0 is best handled by xsl:for-each-group:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html"/>
<xsl:template match="sales">
<html>
<head>
<title>Sales by Region</title>
</head>
<body>
<h1>Sales by Region</h1>
<table border="1" cellpadding="3">
<tbody>
<tr>
<th>SKU</th>
<th>Sales</th>
</tr>
<xsl:for-each-group select="product" group-by="@region">
<tr >
<th colspan="2"><xsl:value-of select="."/> Sales</th>
</tr>
<xsl:apply-templates select="current-group( )"/>
<tr style="font-weight:bold">
<td>Total</td>
<td align="right">
<xsl:value-of select="format-number(sum(current-group( )/@sales),'#.00')"/>
</td>
</tr>
</xsl:for-each-group>
</tbody>
</table>
</body>
</html>
</xsl:template>
<xsl:template match="product">
<tr>
<td><xsl:value-of select="@sku"/></td>
<td align="right"><xsl:value-of select="@sales"/></td>
</tr>
</xsl:template>
</xsl:stylesheet>
Copyright © 2006 O'Reilly Verlag GmbH & Co. KG
You may print the online version for your private use.
Otherwise, this chapter from “XSLT Kochbuch” is subject to the same conditions as the bound edition. The work, including all its parts, is protected by copyright. All rights reserved, including reproduction, translation, microfilming, and storage and processing in electronic systems.
O'Reilly Verlag GmbH & Co. KG, Balthasarstraße 81, 50670 Köln, kommentar(at)oreilly.de