Creating the CGI Script
The xslt.cgi script pulls the stylesheets together and coordinates the processing and updating of the XML on disk. While this application lets you edit and display XML in your browser, it only consists of a single CGI script and two XSL sheets. The source data that may constantly change is also stored on disk as XML.
XSLT transformations can be done programmatically using the xml.xslt.processor.Processor class (provided 4XSLT is installed, as shown earlier). When the CGI script launches, it imports and instantiates the XSLT processor:
#!/usr/local/bin/python # xlst.cgi import cgi import os import sys from xml.xslt.Processor import Processor
# parse query string & instantiate xlst proc query = cgi.FieldStorage( ) xsltproc = Processor( )
Using the XSLT processor in the CGI is simple. Two methods are exposed to establish a stylesheet and perform a transformation returning the result as a string:
xsltproc.appendStylesheetUri("story.xsl") html = xsltproc.runUri("story.xml")
The appendStylesheetUri method is used to establish which stylesheet is used during a transformation. The runUri method performs the transformation against a source XML document and returns the result as a string.
The CGI script does not get around to transformations until it figures out what you're trying to do. Your choices are communicated to the script using a query string passed to the server as part of the request.
Post a comment