Creating Hyperlinked Documents

Problem

You want to convert XML into hyperlinked HTML content.

Solution

A typical course of action when converting XML into HTML is to make two or more passes over the XML to create menu or index pages and content pages. The menu pages contain links to the content pages. The following solution generates an index and summary pages for SalesBySalesPerson.xml (see Chapter 2):

<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> <!-- = = = = = = = = = = = = = = = = = = = = = = = = = = --> <!-- Create 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> <!-- = = = = = = = = = = = = = = = = = = = = = = = = = = --> <!-- Create @name.html (mode = "content") --> <!-- = = = = = = = = = = = = = = = = = = = = = = = = = ...

Get XSLT Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.