Subsections

10. How to use XML/XSL with CherryPy

10.1 Introduction

With CherryPy, you can use python standard datatypes (lists, dictionaries, ...) to store your data, and then you can use masks (written in CHTL) to convert that data into HTML.

However, this can also be done with XML and XSL. I personally think that using python types directly, and then writing CHTL masks is much easier and straightforward than XML/XSL, but there might be several reasons why you'd want to use XML/XSL:

10.2 Prerequisite

This HowTo uses the 4Suite module developed by Fourthought, Inc. I tested it with version 0.12.0a1 of 4Suite and version 2.1 of python, but other combinations would also probably work.

Also, several other XML/XSL modules are available for Python, and using them is also probably very easy.

Start by downloading 4Suite from http://4suite.org and installing it on your machine.

You can test your installation by firing up the Python interpreter and typing:

>>> from Ft.Xml.Xslt.Processor import Processor
::: Using pDomlette
>>>
If this doesn't work for you, then 4Suite is not correctly installed on your system.

10.3 Using the XML/XSL package from CherryPy

Once you have 4Suite installed, is is very easy to use it from CherryPy. You can use regular masks to write your XSL stylesheets, and you have several ways to generate your XML (using a function, a view or even a mask).

The following example is a simple example that demonstrates a basic XML/XSL transformation:

from Ft.Xml.Xslt.Processor import Processor

CherryClass XslTransform:
function:
    def transform(self, xslStylesheet, xmlInput):
        processor = Processor()
        processor.appendStylesheetString(xslStylesheet)
        return processor.runString(xmlInput, 0, {})

CherryClass Root:
view:
    def index(self):
        return xslTransform.transform(self.xslStylesheet(), self.xmlInput())
mask:
    def xslStylesheet(self):
        <?xml version="1.0" encoding="ISO-8859-1"?>

        <xsl:stylesheet version="1.0"
            xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

        <xsl:template match="/">
            <html><body>
                <h2>My CD Collection</h2>
                <table border="1">
                    <tr bgcolor="#9acd32">
                        <th align="left">Title</th>
                        <th align="left">Artist</th>
                    </tr>
                    <xsl:for-each select="catalog/cd">
                        <tr>
                            <td><xsl:value-of select="title"/></td>
                            <td><xsl:value-of select="artist"/></td>
                        </tr>
                    </xsl:for-each>
                </table>
            </body></html>
        </xsl:template>

        </xsl:stylesheet> 
    def xmlInput(self):
        <?xml version="1.0" encoding="ISO-8859-1"?>
        <catalog>
            <cd>
                <title>Empire Burlesque</title>
                <artist>Bob Dylan</artist>
            </cd>
            <cd>
                <title>Hide your heart</title>
                <artist>Bonnie Tyler</artist>
            </cd>
        </catalog>

How does it work ?

Here is the HTML document generated in this example:

<html>
  <body>
    <h2>My CD Collection</h2>
    <table border='1'>
      <tr bgcolor='#9acd32'>
        <th align='left'>Title</th>
        <th align='left'>Artist</th>
      </tr>
      <tr>
        <td>Empire Burlesque</td>
        <td>Bob Dylan</td>
      </tr>
      <tr>
        <td>Hide your heart</td>
        <td>Bonnie Tyler</td>
      </tr>
    </table>
  </body>
</html>

See About this document... for information on suggesting changes.