[mod_python] Speaking of Output Filters....

Lee Brown administrator at leebrown.org
Mon Apr 10 08:08:54 EDT 2006


Greetings!

I am probably remiss for not having shared this before now.  Here are two
output filters for mod_python that I have developed.

The first filter, xsltfilter.py, transforms XML into XHTML (or whatever else
you may desire) via an XSLT template.  The second filter, xincludefilter.py,
processes xinclude elements imbedded in XHTML.  Both are meant as a simple
means of providing "quasi-dynamic" web content.  Benchmarks on my hardware
show throughputs of 100 to 500+ pages per second using these filters.

Dependencies:
lxml:  an ElemenTree-like binding for the libxml2/libxslt libraries (see
http://www.codespeak.net/lxml)

In the "main" Apache config:

AddHandler mod_python .py
PythonPath " ...(as needed)... "
PythonOutputFilter xsltfilter XSLTFILTER
PythonOutputFilter xincludefilter XINCLUDEFILTER

In the appropriate Apache VHOST section:

AddType text/html .xml .xhtml

<Directory " .(as needed). ">
	AddOutputFilter XSLTFILTER .xml
	AddOutputFilter XINCLUDEFILTER .xhtml
	..
</Directory>

File xsltfilter.py:  (Note - Could probably stand some refactoring)

from mod_python import apache
import lxml.etree
from cStringIO import StringIO

xsltfile = open('/path/to/my/template.xslt', 'rU')
xsltstring = xsltfile.read()
xsltfile.close()

styledoc = lxml.etree.parse(StringIO(xsltstring))
transformer = lxml.etree.XSLT(styledoc)

def outputfilter (filter):
    xmlstring = filter.read()
    doc = lxml.etree.parse(StringIO(xmlstring))
    doc.xinclude()
    result = str(transformer(doc))
    filter.write(result)
    filter.close()

File xincludefilter.py:  (Note - Could probabaly stand some refactoring)

from mod_python import apache
import lxml.etree
from cStringIO import StringIO

xsltstring = """<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" 
  xmlns="http://www.w3.org/1999/xhtml" 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  
  <xsl:output method="xml" version="1.0" encoding="ISO-8859-1" 
    indent="yes" omit-xml-declaration="yes" 
    doctype-public="-//W3C//DTD xhtml 1.0 Strict//EN" 
    doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"/>
  
  <xsl:template match="/">
    <xsl:apply-templates select="@*|node()"/>
  </xsl:template>

  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>

</xsl:stylesheet>
"""

styledoc = lxml.etree.parse(StringIO(xsltstring))
transformer = lxml.etree.XSLT(styledoc)

def outputfilter (filter):
    xmlstring = filter.read()
    doc = lxml.etree.parse(StringIO(xmlstring))
    doc.xinclude()
    result = str(transformer(doc))
    filter.write(result)
    filter.close()

Best Regards,
Lee E. Brown
(leebrown at leebrown.org)

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mm_cfg_has_not_been_edited_to_set_host_domains/pipermail/mod_python/attachments/20060410/330343b8/attachment-0001.html


More information about the Mod_python mailing list