469,353 Members | 2,066 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,353 developers. It's quick & easy.

XML/XSLT with Python


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.

Jul 18 '05 #1
5 19094
Have you looked at http://pyana.sourceforge.net/? I'm quite happy with it.

Achim
Jul 18 '05 #2
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.

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)

Jul 18 '05 #3
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)
Jul 18 '05 #4
On Sat, 20 Sep 2003 22:32:59 +0200, K. N. wrote:

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.

--
Dr. Thomas Korb

Jul 18 '05 #5
"K. N." <va**@valis.amber.eu.org> wrote in message news:<ma**********************************@python. org>...
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/2...-01/basic-xslt
http://uche.ogbuji.net/tech/akara/no...1-01/domlettes

See also:

http://www.xml.com/pub/a/2002/10/16/py-xml.html
http://uche.ogbuji.net/tech/akara/no...01/basic-xpath
http://uche.ogbuji.net/tech/akara/no...xslt-ext-elems
http://uche.ogbuji.net/tech/akara/no...xslt-ext-funcs
http://uche.ogbuji.net/tech/akara/no...7/xslt-ext-api
--
Uche Ogbuji
http://uche.ogbuji.net
Jul 18 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Doug Farrell | last post: by
1 post views Thread by Olivier Hoarau | last post: by
1 post views Thread by °ystein | last post: by
1 post views Thread by Yannick Patois | last post: by
1 post views Thread by K.Simon | last post: by
3 posts views Thread by Ian Roddis | last post: by
2 posts views Thread by veracon | last post: by
21 posts views Thread by Damian | last post: by
1 post views Thread by CARIGAR | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.