A More Complex Example
In Chapter 3, we created an XML document that represents the Python classes in the PyXML package (index.py). The pyxml.xml file from Chapter 3 is a lengthy XML document, and makes a good test subject.
In this section, we convert the pyxml.xml file back to HTML, but this time
using XSLT instead of a SAX driver. After using XSLT to perform this,
the SAX and string approach from Chapter
2 will not seem nearly as powerful. However, this type of
conversion work is exactly what XSLT is designed to accomplish. The
basic structure of the pyxml.xml
document 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 above XML represents only a few lines of the 2600 line file.
The stylesheet used to convert this XML to HTML uses a combination of
apply-templates
and value-of
elements to traverse the structure
and generate appropriate output.
The stylesheet starts by creating the HTML opening and closing elements, ...
Get Python & XML 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.