When you run the transformation in Example 6-5
, you produce a pyxml.html
document that shows all of the classes in the PyXML package.
C:\my-dir> 4xslt pyxml.xml pyxml.xsl > pyxml.html
6.7 Embedding XSLT Transformations in Python
XML is frequently used to store the "core" version of a document while
transformations are used to integrate the data into other systems. For example, you
may receive a purchase order as XML over the Web and dispatch it in several
different directions (and in different formats) to your other data systems. You may
parse the XML inserting the data into Oracle tables, transform it to HTML, add it to
an internal web site, transform the purchase order into another flavor of XML, and
pass it on to your suppliers.
Regardless of where you're sending your XML, the ability to perform XSLT
transformations at runtime is critical. The 4XSLT package works nicely from inside
your Python programs. In this section, we create a Python CGI executable for use
within Linux and Apache, or in any web server that is configured to run external CGI
The process involves two stylesheets, one XML document, and one CGI executable.
The first stylesheet converts the XML document into HTML for your browser. The
second stylesheet converts the XML document into HTML for your browser, but adds
additional HTML allowing you to edit the text of the XML document and update it on
the server. The Python CGI script exists to run the XML through the appropriate
stylesheet based on your actions. The script also takes care of updating the source
XML on disk. In order for the script to run correctly, it must be placed in a directory
where the web user (user nobody on Apache and Unix) has permission to write a
new XML file.
6.7.1 Creating the Source XML
For starters, we need to create an XML document. Further updates to the XML can be
accomplished through the web browser once you've created the CGI script. For now,
you can get by with the following code saved to disk as story.xml:
<title>Web Sites Use XML</title>
It is no surprise, web sites are using XML these days.