pyxslt makes it easy to turn Python objects into XML documents. In addition to basic XML serialization, pyxslt can also apply an XSL template to the serialized XML fragments. You could, for example, use pyxslt to convert the results of an SQLObject query to an XHTML file.
All Python objects given to pyxslt are converted into their string representations. pyxslt focuses on serializing objects in such a way as to make the construction of XSL templates as easy as possible. As a result, pyxslt’s XML serialization is usually not reversible.
In other words, pyxslt is not a replacement for pickle, marshal, shelve, or any of the other true serialization modules. pyxslt is designed with one-way XSL transformation in mind.
pyxslt makes use of libxml2 to build its internal XML documents and libxslt to perform XSL transformations. Both packages must be installed in order for pyxslt to do its job.
Python API
The pyxslt API separates the serialization code from the transformation code. This simplifies the integration of pyxslt into your application if you only need to use one module and not the other.
Serialization
pyxslt’s serialize module can be used to turn Python objects into XML documents. The serialization step is lossy in that the objects cannot be rebuilt from the generated XML documents. pyxslt serializes objects with an eye towards XSL transformation.
The following code serializes a few basic Python objects:
# Import the pyxslt serialize module.
import pyxslt.serialize
# Serialize a handful of basic Python objects.
print pyxslt.serialize.toString(prettyPrintXml=True,
foo='bar',
listOfNumbers=(1, 2, 3),
props={
'a': 4,
'b': 5,
'c': (6, 7, 8)
},
)
The above sample will result in the following XML document:
<?xml version="1.0" encoding="ASCII"?>
<pyxslt>
<foo>bar</foo>
<list-of-numbers>
<item>1</item>
<item>2</item>
<item>3</item>
</list-of-numbers>
<props>
<item key="a">4</item>
<item key="b">5</item>
<item key="c">
<item>6</item>
<item>7</item>
<item>8</item>
</item>
</props>
</pyxslt>
pyxslt understands all of the basic Python types and has special code for handling SQLObject instances. The auto-generated documentation for the serialize module provides additional examples and sample code.
Transformation
The transform module provides code for transforming a serialized set of Python objects using an XSL template. The serialization step is automatically performed by the transform module.
Transformation uses a very similar calling sequence to serialization. In addition to the objects to be transformed, the function also requires that the path to an XSL template is specified. The following code transforms a set of Python objects using a stylesheet named numbers.xsl:
# Import the pyxslt transform module.
import pyxslt.transform
# Transform a handful of basic Python objects.
print pyxslt.transform.toString(
'numbers.xsl', prettyPrintXml=True,
foo='bar',
listOfNumbers=(1, 2, 3),
props={
'a': 4,
'b': 5,
'c': (6, 7, 8)
},
)
Here is the source for the numbers.xsl stylesheet:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns="http://www.w3.org/1999/xhtml">
<xsl:template match="/">
<html>
<head>
<title>Number List</title>
</head>
<body>
<h1>Number List</h1>
<xsl:apply-templates select="pyxslt/list-of-numbers" />
</body>
</html>
</xsl:template>
<xsl:template match="pyxslt/list-of-numbers">
<ul>
<xsl:apply-templates />
</ul>
</xsl:template>
<xsl:template match="pyxslt/list-of-numbers/item">
<li>
<xsl:value-of select="." />
</li>
</xsl:template>
</xsl:stylesheet>
The resulting XHTML output is as follows:
<?xml version="1.0" encoding="ASCII"?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Number List</title>
</head>
<body>
<h1>Number List</h1>
<ul>
<li>1</li>
<li>2</li>
<li>3</li>
</ul>
</body>
</html>
Command-Line API
In addition to the Python API, pyxslt also includes a unique command-line interface that allows you to access the serialization and transformation functionality from UNIX shell scripts.
The command-line tool (called pyxslt) expects Python code on stdin (either piped-in, or using a here document). The code is executed in the pyxslt process and the local variables created by the code are serialized and transformed.
This functionality is especially useful when combined with the specialized SQLObject handling in pyxslt.
Serialization
The following shell script could be used to output the list of people found in the sample code in the SQLObject documentation:
#!/bin/bash
pyxslt --pretty-print <<END
from db_example import *
peeps = Person.select(Person.q.firstName=="John")
END
Given the single entry in the SQLObject sample (a Person object named “John Q. Doe”), the following XML fragment would be output by pyxslt:
<?xml version="1.0" encoding="ASCII"?>
<pyxslt>
<peeps>
<item id="1">
<first-name>John</first-name>
<middle-initial>Q</middle-initial>
<last-name>Doe</last-name>
</item>
</peeps>
</pyxslt>
Transformation
With a fairly simple stylesheet, pyxslt can easily output an XHTML file with the results of the SQL query.
Here is a sample stylesheet:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns="http://www.w3.org/1999/xhtml">
<xsl:template match="/">
<html>
<head>
<title>All My Peeps</title>
</head>
<body>
<h1>All My Peeps</h1>
<ul>
<xsl:apply-templates />
</ul>
</body>
</html>
</xsl:template>
<xsl:template match="peeps/item">
<li>
<xsl:value-of select="first-name" />
</li>
</xsl:template>
</xsl:stylesheet>
Here is the output after calling pyxslt (this time giving the path to the stylesheet as the final argument):
<?xml version="1.0" encoding="ASCII"?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>All My Peeps</title>
</head>
<body>
<h1>All My Peeps</h1>
<ul>
<li>John</li>
</ul>
</body>
</html>
SQLObject Handling
pyxslt can tell the difference between a normal Python object and an SQLObject instance. When it finds an SQLObject, pyxslt will serialize the contents of each of the columns in the object. ForeignKey and MultipleJoin columns will be properly traversed as well.
More information on this feature can be found in the SQLObject Examples section of the pyxslt reference documentation.