XML technologies / XSLT / XSLT Cookbook / Transforming XML into HTML / Creating a self-contained HTML transformation
(Excerpt from “XSLT Kochbuch” by Sal Mangano)
Problem
You want to combine XML data and a stylesheet that transforms it into HTML in a single file.
Solution
This recipe assumes a browser supporting client-side XSLT transformations (IE 6.0 or later, also IE 5.x + MSXML 3.0, Netscape Navigator 6.1 or later, Mozilla, Firefox 1.0 or later, Apple Macintosh Safari (Tiger), etc.):
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="application/xml" href="selfcontained.xsl"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:pf="http://www.ora.com/XSLTCookbook/namespaces/portfolio">
<portfolio xmlns="http://www.ora.com/XSLTCookbook/namespaces/portfolio">
<investment>
<symbol>IBM</symbol>
<current>72.70</current>
<paid>65.00</paid>
<qty>1000</qty>
</investment>
<investment>
<symbol>JMAR</symbol>
<current>1.90</current>
<paid>5.10</paid>
<qty>5000</qty>
</investment>
<investment>
<symbol>DELL</symbol>
<current>24.50</current>
<paid>18.00</paid>
<qty>100000</qty>
</investment>
<investment>
<symbol>P</symbol>
<current>57.33</current>
<paid>63</paid>
<qty>100</qty>
</investment>
</portfolio>
<xsl:output method="html" />
<xsl:attribute-set name="gain-loss-font">
<xsl:attribute name="color">
<xsl:choose>
<xsl:when test="(pf:current - pf:paid) * pf:qty >= 0">black</xsl:when>
<xsl:otherwise>red</xsl:otherwise>
</xsl:choose>
</xsl:attribute>
</xsl:attribute-set>
<xsl:template match="xsl:stylesheet">
<xsl:apply-templates select="pf:portfolio"/>
</xsl:template>
<xsl:template match="pf:portfolio">
<html>
<head>
<title>My Portfolio</title>
</head>
<body bgcolor="#FFFFFF" text="#000000">
<h1>Portfolio</h1>
<table border="1" cellpadding="2">
<tbody>
<tr>
<th>Symbol</th>
<th>Current</th>
<th>Paid</th>
<th>Qty</th>
<th>Gain/Loss</th>
</tr>
<xsl:apply-templates/>
</tbody>
</table>
</body>
</html>
</xsl:template>
<xsl:template match="pf:investment">
<tr>
<td><xsl:value-of select="pf:symbol"/></td>
<td><xsl:value-of select="pf:current"/></td>
<td><xsl:value-of select="pf:paid"/></td>
<td><xsl:value-of select="pf:qty"/></td>
<td>
<font xsl:use-attribute-sets="gain-loss-font">
<xsl:value-of select="format-number((pf:current - pf:paid) * pf:qty, '#,##0.00')"/>
</font>
</td>
</tr>
</xsl:template>
</xsl:stylesheet>Two components make this stylesheet work.
The first is the processing instruction xml-stylesheet, which tells the browser that the stylesheet is the document it has loaded. You can refer to the document as its own stylesheet using href="" instead of the filename, which is useful if you rename it.
The second is the template matching element xsl:stylesheet and redirecting stylesheet processing to the embedded XML data. Here, the elements are in the namespace "http://www.ora.com/XSLTCookbook/namespaces/portfolio".
Discussion
This recipe is something of a trick to impress your friends. Mixing content and style in some respects violates the idea behind this technology. On the other hand, producing a single file can be convenient. Do not feel guilty about using this recipe if your needs call for it.
The official way to achieve this result is to embed the stylesheet in the document rather than the reverse. See Embedding Stylesheets in the XSLT 1.0 specification or Embedded Stylesheet Modules in the XSLT 2.0 specification for details. However, IE still does not support embedded stylesheets, so this trick at least solves that problem.
You can deliver content in this form without developing it directly that way. The following stylesheet combines a stylesheet and an XML file into the self-contained format. The only requirements are that the XML use a namespace and the stylesheet not start processing at the root node (/):
<!-- generate-selfcontained.xslt -->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xso="dummy">
<!-- Wiederverwendung der Identitätstransformation -->
<xsl:import href="../util/copy.xslt"/>
<!-- Dieses Stylesheet wird den Stylesheet-Inhalt generieren, verwende daher xso als Alias für xsl -->
<xsl:namespace-alias stylesheet-prefix="xso" result-prefix="xsl"/>
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<!-- Es ist keine gute Idee, Leerraum aus Textknoten zu entfernen -->
<xsl:preserve-space elements="xsl:text"/>
<!-- Der Name der Datei, die die XML-Daten enthält -->
<xsl:param name="datafile"/>
<!-- Der Name der resultierenden Ausgabedatei -->
<xsl:param name="outfile"/>
<xsl:template match="/">
<!-- Füge die Verarbeitungsanweisung ein, um dem Browser zu sagen, dass $outfile das Stylesheet ist. -->
<xsl:processing-instruction name="xml-stylesheet">
<xsl:text>type="application/xml" href="</xsl:text><xsl:value-of select="$outfile"/>"<xsl:text/>
</xsl:processing-instruction>
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="xsl:stylesheet">
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:apply-templates/>
<!-- Erzeuge das XSLT -->
<xso:template match="xsl:stylesheet">
<xso:apply-templates select="{name(document($datafile)/*)}"/>
</xso:template>
<!-- Füge die Daten ein -->
<xsl:copy-of select="document($datafile)"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>You can use this stylesheet to transform another stylesheet and its data into a self-contained HTML transformation. The stylesheet is the source, while $datafile is passed as a parameter. Another parameter, $outfile, is required to generate the xml-stylesheet processing instruction correctly.
Using Saxon on the command line, such a call might look like this:
saxon -o self-contained.xsl pf-portfolio.xslt generate-selfcontained.xslt
datafile="pf-portfolio.xml" outfile="self-contained.xsl"Here, self-contained.xsl is the name of the resulting stylesheet, and pf-portfolio.xslt is the stylesheet combined with pf-portfolio.xml.
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