XML/XSLT with Python

Colin Fox cfox at cfconsulting.ca
Sun Sep 21 13:31:40 CEST 2003

On Sat, 20 Sep 2003 22:32:59 +0200, K. N. wrote:

> Is there any good and fast Python module for XSLT
> processing ? I'm going to use XML and XSLT to generate
> web pages, so I need XSLT processor that will be able
> to transform for example a DOM object in memory - I 
> don't want to create XML file containing data and then
> load it and transform with XSLT, but I want to do 
> this at once - without writing to a temporary file. 
> Actually I've seen alot articles about parsing XML, 
> but nothing about creating XML documents and storing
> it as an object that will be passed to XSLT processor,
> and that is what I'm planning to do.
> I think its a quite good solution, but I have no idea
> which modules will be most suitable for this task.
> Does anyone have some experience in this matter and 
> could point me where should I look ?
> Best regards,
>  K.

Here's a CGI I wrote to do this very thing.
It uses libxml2 & libxslt from the gnome libraries.
I wanted to make sure it was reasonably efficient, so I put a bunch of
timing code into it. It turns out that processing an XML file, and running
it through this XSLT processor is really fast.

I didn't bother to include page.xsl here, as it's actual contents are
irrelevant to the technique.


#! /usr/bin/env python
import time
st = time.time()
import libxml2, libxslt
import cgi, os, sys
query = cgi.FieldStorage()
readtimestart = time.time()
styledoc = libxml2.parseFile("page.xsl")
style = libxslt.parseStylesheetDoc(styledoc)
doc = libxml2.parseFile(query['script'].value)
readtimeend = time.time()
start_converting = time.time()
result = style.applyStylesheet(doc, None)
done_converting = time.time()
html = result.serialize()
print "Content-type: text/html"
print html
et = time.time()
totaltime = et-st
print "<!-- Page served in %s seconds. -->" % totaltime
print "<!-- XML conversion took %s seconds. -->" %\
print "<!-- File reading took %s seconds. -->" %\

More information about the Python-list mailing list