XML/XSLT with Python

K

K. N.

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.
 
C

Colin Fox

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.

Enjoy!
cf

------------------------
#! /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
print html

style.freeStylesheet()
doc.freeDoc()
result.freeDoc()

et = time.time()
totaltime = et-st
print "<!-- Page served in %s seconds. -->" % totaltime
print "<!-- XML conversion took %s seconds. -->" %\
(done_converting-start_converting)
print "<!-- File reading took %s seconds. -->" %\
(readtimeend-readtimestart)
 
C

Colin Fox

On Sun, 21 Sep 2003 11:31:40 +0000, Colin Fox wrote:

Hmm. I think my news client buggered up the linefeeds. Let me try again:

#! /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
print html

style.freeStylesheet()
doc.freeDoc()
result.freeDoc()

et = time.time()
totaltime = et-st
print "<!-- Page served in %s seconds. -->" % totaltime
print "<!-- XML conversion took %s seconds. -->" %\
(done_converting-start_converting)
print "<!-- File reading took %s seconds. -->" %\
(readtimeend-readtimestart)
 
T

Thomas Korb

Is there any good and fast Python module for XSLT
processing ?

I've tested the following 3 'typical' choices

1. 4Suite
http://4suite.org/index.xhtml

2. Pyana (for Apache's Xalan)
http://sourceforge.net/projects/pyana/

3. libxml2/libxslt
http://www.xmlsoft.org/

and decided to use libxml2/libxslt. Easy to install
(on Linux and Windows), easy to use, *very* fast and
no problems at all with the XSLT-processor.
(I cannot say the same about the other two solutions!)

The only problem is the xmlsoft.org site: it's not
really obvious, which versions to install etc.


WINDOWS:

I use the following website for Windows, where you can
download Python bindings which are bundled with a copy
of libxml2/libxslt (very convenient, easy to install):

http://users.skynet.be/sbi/libxml-python/


LINUX:

For Linux, I use the RPMs which come with my distribution,
i.e.: libxml2, libxml2-python, libxslt and libxslt-python.


Hope this helps.
 
U

Uche Ogbuji

K. N. said:
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 ?

4XSLT (http://4suite.org) will do what you seek. In most situatons it
is not as fast as libxslt, which is, after all, written entirely in C,
but it does have its advantages, including a very rich Python API (in
my biased opinion the richest Python API of all the choices). It is
certainly fast enough for most purposes. You can use the cDomlette
mutation API to generate your document in memory and then pass it to a
pocessor instance.

I want to mention that Dr. Korb mentioned having problems with 4Suite
and Pyana, but in private conversation admits this was over a year
ago, i.e. eons in the life of an actively maintained project such as
4Suite. Much has changed since then.

If you decide to give it a try, start with:

http://uche.ogbuji.net/akara/nodes/2003-01-01/basic-xslt
http://uche.ogbuji.net/tech/akara/nodes/2003-01-01/domlettes

See also:

http://www.xml.com/pub/a/2002/10/16/py-xml.html
http://uche.ogbuji.net/tech/akara/nodes/2003-01-01/basic-xpath
http://uche.ogbuji.net/tech/akara/nodes/2003-01-01/xslt-ext-elems
http://uche.ogbuji.net/tech/akara/nodes/2003-01-01/xslt-ext-funcs
http://uche.ogbuji.net/tech/akara/nodes/2003-03-07/xslt-ext-api
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,164
Messages
2,570,898
Members
47,439
Latest member
shasuze

Latest Threads

Top