XML technologies / XML / Python & XML / Transforming XML with XSLT / A more complex example
(Excerpt from "Python & XML" by Christopher A. Jones & Fred L. Drake, Jr.)
In The Simple API for XML we created an XML document representing the Python classes in the PyXML package (index.py). The file pyxml.xml is a long XML document well suited to testing.
In this section, we convert the file pyxml.xml back to HTML, this time using XSLT rather than a SAX driver. Once we do this with XSLT, the SAX and string approach from XML basics will seem much less powerful by comparison. However, this type of transformation is precisely what XSLT was designed for. The basic structure of the document pyxml.xml consists of a file element followed by one or more class elements, followed by one or more method-definition elements:
<file name="../xml/dom/ext/reader/HtmlLib.py">
<class name="class HtmlToDomParser(SGMLParser):">
<method name="def __init__(self):"/>
<method name="def unknown_starttag(self,tag,attrs):"/>
<method name="def unknown_endtag(self, tag):"/>
<method name="def handle_data(self, data):"/>
<method name="def handle_comment(self, comment):"/>
<method name="def handle_generic_node(self, node):"/>
<method name="def report_unbalanced(self, tag):"/>
<method name="def toDom(self, st, ownerDoc=None):"/>
</class>
</file>The XML above shows only a few lines of the 2,600-line file. The stylesheet converting it to HTML uses a combination of apply-templates and value-of elements to traverse the structure and generate appropriate output.
The stylesheet first generates the opening and closing HTML tags and calls apply-templates between them to fill in the content:
<xsl:template match="pyxml">
<html>
<body bgcolor="#FFFFFF" text="#3333FF">
<xsl:apply-templates/>
</body>
</html>
</xsl:template>The next section defines three separate templates, one for each element type generated by index.py, to capture content and generate the corresponding HTML output.
File template
To match file elements in pyxml.xml, create a template matching the element name. On a match, HTML is formatted to display the file name in red in a new table row:
<xsl:template match="file">
<p>
<table cellpadding="0" cellspacing="0" border="1" bordercolor="#000000" width="540">
<tr>
<td align="center">Quelldatei: <b class="filename"><xsl:value-of select="./@name"/></b></td>
</tr>
<xsl:apply-templates/>
</table>
</p>
</xsl:template>Within the template, a value-of element uses a path expression targeting the name attribute. Once the table row is complete, apply-templates is used to insert content below this element, which may consist of several class and method elements.
Class template
The class template creates a new table row containing the class name and simply outputs that name:
<xsl:template match="class">
<tr>
<td>Klasse: <b class="classname"><xsl:value-of select="./@name"/></b></td>
</tr>
<xsl:apply-templates/>
</xsl:template>As shown earlier, the apply-templates statement inserts further content below this element.
Method template
The method template follows suit, generating its own HTML to display method names, this time in black:
<xsl:template match="method">
<tr>
<td align="left"><span class="methodname"><xsl:value-of select="./@name"/></span></td>
</tr>
</xsl:template>Here, apply-templates is not used because there are no child elements of a method element in the document pyxml.xml.
The following example lists all of pyxml.xsl.
Example: pyxml.xsl
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="html"/>
<xsl:template match="pyxml">
<html>
<body bgcolor="#FFFFFF" text="#3333FF">
<xsl:apply-templates/>
</body>
</html>
</xsl:template>
<xsl:template match="file">
<p>
<table cellpadding="0" cellspacing="0" border="1" bordercolor="#000000" width="540">
<tr>
<td align="center">Quelldatei: <b><font color="#FF0000"><xsl:value-of select="./@name"/></font></b></td>
</tr>
<xsl:apply-templates/>
</table>
</p>
</xsl:template>
<xsl:template match="class">
<tr>
<td>Klasse: <b><xsl:value-of select="./@name"/></b></td>
</tr>
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="method">
<tr>
<td align="left"><font color="#000000"><xsl:value-of select="./@name"/></font></td>
</tr>
</xsl:template>
</xsl:stylesheet>Running the transformation in the preceding example pyxml.xsl produces the document pyxml.html, displaying all classes in the PyXML package.
C:\mein-dir> 4xslt pyxml.xml pyxml.xsl > pyxml.html
Copyright © 2002 O'Reilly Verlag GmbH & Co. KG
You may print the online version for your private use.
Otherwise, this chapter from the book "Python & XML" is subject to the same conditions as the printed edition: the work and all its parts are 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