XML technologies / XSLT / XSLT Cookbook / Transform XML into HTML / Generate frames
(Excerpt from “XSLT Kochbuch” by Sal Mangano)
Problem
You want to generate HTML structured with frames.
Solution
As in recipe Create linked documents, use modes to make multiple passes over the XML. First create a frameset container with two frames. The smaller left frame lists salesperson names as links activating content in the main frame, which displays the selected salesperson’s figures. A default main frame appears on the first visit:
<xsl:stylesheet version="1.0" 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="frameset"/>
<xsl:apply-templates select="*" mode="salespeople_frame"/>
<xsl:apply-templates select="*" mode="sales_frames"/>
</xsl:template>
<!-- = = = = = = = = = = = = = = = = = = = = = = = = = -->
<!-- Erzeuge Frameset-Container (mode ="frameset") -->
<!-- = = = = = = = = = = = = = = = = = = = = = = = = = -->
<xsl:template match="salesBySalesperson" mode="frameset">
<!-- Nicht standardkonformes Saxon-xsl:document! -->
<xsl:document href="index.html">
<html>
<head>
<title>Salesperson Frameset</title>
</head>
<frameset rows="100%" cols="25%, 75%" border="0">
<frame name="salespeople" src="salespeople_frame.html" noresize=""/>
<frame name="mainFrame" src="default_sales.html" noresize=""/>
</frameset>
<body bgcolor="#FFFFFF" text="#000000"></body>
</html>
</xsl:document>
</xsl:template>
<!-- = = = = = = = = = = = = = = = = = = = = = = = = = -->
<!-- Erzeuge salespeople_frame.html (mode = "salespeople_frame") -->
<!-- = = = = = = = = = = = = = = = = = = = = = = = = = -->
<xsl:template match="salesBySalesperson" mode="salespeople_frame">
<!-- Nicht standardkonformes Saxon-xsl:document! -->
<xsl:document href="salespeople_frame.html">
<html>
<head>
<title>Salespeople</title>
</head>
<body bgcolor="#FFFFFF" text="#000000">
<table>
<tbody>
<xsl:apply-templates mode="index"/>
</tbody>
</table>
</body>
</html>
</xsl:document>
</xsl:template>
<xsl:template match="salesperson" mode="index">
<tr>
<td>
<a href="{concat(@name,'.html')}" target="mainFrame">
<xsl:value-of select="@name"/>
</a>
</td>
</tr>
</xsl:template>
<!-- = = = = = = = = = = = = = = = = = = = = = = = = = -->
<!-- Erzeuge @name.html (mode = "content") -->
<!-- = = = = = = = = = = = = = = = = = = = = = = = = = -->
<xsl:template match="salesperson" mode="sales_frames">
<xsl:document href="default_sales.html">
<html>
<head>
<title>Default</title>
</head>
<body bgcolor="#FFFFFF" text="#000000">
<h1><center>Sales By Salesperson</center></h1>
<br/>
Click on a salesperson on the left to load his or her sales figures.
</body>
</html>
</xsl:document>
<xsl:document href="{concat(@name,'.html')}">
<html>
<head>
<title><xsl:value-of select="@name"/></title>
</head>
<body bgcolor="#FFFFFF" text="#000000">
<h1><center>Sales By Salesperson</center></h1>
<h2><xsl:value-of select="@name"/></h2>
<table border="1" cellpadding="3">
<tbody>
<tr>
<th>SKU</th>
<th>Sales (in US $)</th>
</tr>
<xsl:apply-templates mode="content"/>
</tbody>
</table>
</body>
</html>
</xsl:document>
</xsl:template>
<xsl:template match="product" mode="content">
<tr>
<td><xsl:value-of select="@sku"/></td>
<td align="right"><xsl:value-of select="@totalSales"/></td>
</tr>
</xsl:template>
</xsl:stylesheet>Discussion
Frames divide a page into logical sections, though visible or resizable borders look dated. This stylesheet cannot be used for client-side transformation because it produces separate HTML files for the frameset, the left salesperson list and each salesperson.
This solution is dynamic and open-ended in the number of pages produced from one XML file. Client-side XSLT with frames can be simpler: produce a frameset whose frames contain separate XML documents with their own transformations:
<html>
<head>
<title>Frameset</title>
</head>
<frameset rows="100%" cols="25%, 75%" border="0">
<frame name="leftFrame" src="left.xml" noresize="">
<frame name="mainFrame" src="main.xml" noresize="">
</frameset>
<body bgcolor="#FFFFFF" text="#000000"></body>
</html>Separate XML documents are needed; the mechanism is xml-stylesheet: one processing instruction per file. The contents can still share one stylesheet if a xinclude link is processed with an XSLT template.
left.xml looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="application/xml" href="left.xsl"?>
<xi:include href="salesBySalesperson.xml" xmlns:xi="http://www.w3.org/2001/XInclude"/>main.xml is similar:
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="application/xml" href="main.xsl"?>
<xi:include href="salesBySalesperson.xml" xmlns:xi="http://www.w3.org/2001/XInclude"/>left.xsl and main.xsl contain a template processing element xinclude using function document:
<!-- left.xsl -->
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html"/>
<xsl:template match="xi:include" xmlns:xi="http://www.w3.org/2001/XInclude">
<xsl:for-each select="document(@href,.)">
<xsl:apply-templates/>
</xsl:for-each>
</xsl:template>
<xsl:template match="salesBySalesperson">
<html>
<head>
<title>Salespeople</title>
</head>
<body bgcolor="#FFFFFF" text="#000000">
<table>
<tbody>
<xsl:apply-templates/>
</tbody>
</table>
</body>
</html>
</xsl:template>
<xsl:template match="salesperson">
<tr>
<td>
<a href="{concat('main.xml#',@name)}" target="mainFrame">
<xsl:value-of select="@name"/></a>
</td>
</tr>
</xsl:template>
</xsl:stylesheet>
<!-- main.xsl -->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html"/>
<xsl:template match="xi:include" xmlns:xi="http://www.w3.org/2001/XInclude">
<xsl:for-each select="document(@href)">
<xsl:apply-templates/>
</xsl:for-each>
</xsl:template>
<xsl:template match="salesBySalesperson">
<html>
<head>
<title>Sales By Salesperson</title>
</head>
<body bgcolor="#FFFFFF" text="#000000">
<xsl:apply-templates/>
</body>
</html>
</xsl:template>
<xsl:template match="salesperson">
<h1><a name="{@name}"><center>Sales By Salesperson</center></a></h1>
<h2><xsl:value-of select="@name"/></h2>
<table border="1" cellpadding="3">
<tbody>
<tr>
<th>SKU</th>
<th>Sales (in US $)</th>
</tr>
<xsl:apply-templates />
</tbody>
</table>
<div style="padding-top:1000"/>
</xsl:template>
<xsl:template match="product">
<tr>
<td><xsl:value-of select="@sku"/></td>
<td align="right"><xsl:value-of select="@totalSales"/></td>
</tr>
</xsl:template>
</xsl:stylesheet>In main.xsl, the main stylesheet, generate named anchors for every salesperson, separated by generous whitespace using <div style="padding-top: 1000"/>. In left.xsl, generate links to those anchors. This roughly simulates the multipage example.
See also
Jeni Tennison’s book XSLT and XPath on the Edge (M&T, 2001) discusses client-side XSLT with frames in detail, showing how JavaScript can produce more sophisticated results.
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