XML technologies / XSLT / XSLT Cookbook / Transform XML into HTML / Create linked documents
(Excerpt from “XSLT Kochbuch” by Sal Mangano)
Problem
You want to convert XML into HTML containing hyperlinks.
Solution
A typical XML-to-HTML approach makes two or more passes over the XML to create menu or index pages and content pages. The menu pages link to the content. XSLT 1.0 is sufficient. This solution generates index and summary pages for SalesBySalesPerson.xml:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:saxon="http://icl.com/saxon" extension-element-prefixes="saxon">
<xsl:output method="html"/>
<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">
<saxon:output href="index.html">
<html>
<head>
<title>Sales By Salesperson Index</title>
</head>
<body bgcolor="#FFFFFF" text="#000000">
<h1>Sales By Salesperson</h1>
<xsl:apply-templates mode="index"/>
</body>
</html>
</saxon:output>
</xsl:template>
<xsl:template match="salesperson" mode="index">
<h2>
<a href="{concat(@name,'.html')}">
<xsl:value-of select="@name"/>
</a>
</h2>
</xsl:template>
<!-- = = = = = = = = = = = = = = = = = = = = = = = = = -->
<!-- Erzeuge @name.html (mode = "content") -->
<!-- = = = = = = = = = = = = = = = = = = = = = = = = = -->
<xsl:template match="salesperson" mode="content">
<saxon:output href="{@name}.html">
<html>
<head>
<title><xsl:value-of select="@name"/> Sales</title>
</head>
<body bgcolor="#FFFFFF" text="#000000">
<h1><xsl:value-of select="@name"/> Sales</h1>
<ol>
<xsl:apply-templates mode="content"/>
</ol>
</body>
</html>
</saxon:output>
</xsl:template>
<xsl:template match="product" mode="content">
<li><xsl:value-of select="@sku"/>      $<xsl:value-of select="@totalSales"/></li>
</xsl:template>
</xsl:stylesheet>Notice how modes separate transformation into index and information content for each salesperson’s HTML page. HTML transformations often use modes to present data in several ways within one stylesheet.
This stylesheet is intended for batch use. A parameter controls which document is produced. It also removes the need for nonstandard extension saxon:output, which is also unnecessary when using XSLT 2.0:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html"/>
<!-- Gibt an, welches Dokument ausgegeben wird -->
<!-- INDEX : erzeugt das Indexdokument -->
<!-- Verkäufername : erzeugt die Seite für diesen Verkäufer -->
<xsl:param name="which" select="'INDEX'"/>
<xsl:template match="/">
<xsl:choose>
<xsl:when test="$which='INDEX'">
<xsl:apply-templates select="*" mode="index"/>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates select="*/salesperson[@name = $which]" mode="content"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<!-- = = = = = = = = = = = = = = = = = = = = = = = = = -->
<!-- Erzeuge index.html (mode = "index") -->
<!-- = = = = = = = = = = = = = = = = = = = = = = = = = -->
<xsl:template match="salesBySalesperson" mode="index">
<!-- saxon:output entfernt. Der Rest ist gleich. -->
</xsl:template>
<!-- ... -->
<xsl:template match="salesperson" mode="content">
<!-- saxon:output entfernt. Der Rest ist gleich. -->
</xsl:template>
<!-- ... -->
</xsl:stylesheet>This technique would be more robust if every salesperson used an ID rather than a name as the parameter.
Discussion
The output is not stylish, but illustrates the mechanics of linked HTML content. Producing all pages with one stylesheet required a nonstandard XSLT 1.0 element, saxon:output. Most processors have similar extensions, and XSLT 2.0 provides xsl:result-document.
Relative links are normally what you want. If absolute links are needed, introduce a top-level URL parameter rather than hard-coding the URL:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html"/>
<xsl:param name="URL" select="http://www.mycompany.com/"/>
<!-- ausgelassen ... -->
<xsl:template match="salesperson" mode="index">
<h2>
<a href="{$URL}{@name}.html'">
<xsl:value-of select="@name"/>
</a>
</h2>
</xsl:template>
</xsl:stylesheet>See also
See recipe Emulate object-oriented reuse and design patterns for more on generating multiple output documents.
This output is not particularly user-friendly. Recipes Generate HTML tables and Generate frames show how to improve its appearance.
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