473,480 Members | 1,823 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 19335
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
1895
by: Doug Farrell | last post by:
Hi all, I'm trying to use the 4Suite xml/xsl tools to transform some XML data into HTML. I'm using some examples from the O'Reilly book "Python and XML" and things are blowing up. Here is the...
1
2634
by: Olivier Hoarau | last post by:
Hello, I would like to create a file from a xml file with a xslt transformation in a python program. Is there someone who can give me some examples to do that ? something like this import...
1
1857
by: øystein | last post by:
Hi! I got an xml file that goes like this. <question id=10 correct_answer=2> <alternative>Answer 1</alternative> <alternative>Answer 2</alternative> <alternative>Answer 3</alternative>...
1
1572
by: Ola Natvig | last post by:
Hi all I'm working with a long running, threaded server which serves HTTP requests with content which are passed through a XSLT processor. The XSLT processor I'm using is the Pyana processor. ...
1
5053
by: Yannick Patois | last post by:
Hi, I would like to merge two XML document, or more exacly enrich a document by inheritiong from another. I read a bit about XSLT and I know a bit of python/sax/dom, and I dont know where I...
1
412
by: K.Simon | last post by:
Hello, I'm looking for a possibility to execute a program like a python-script within a xslt-stylesheet. I read sometime ago that there exists a way to do so. Could somebody explain how? I...
3
3067
by: Ian Roddis | last post by:
Hello, I want to embed SQL type queries within an XML data record. The XML looks something like this: <DISPLAYPAGE> <FIELD NAME="SERVER" TYPE="DROPDOWN"> <OPTION>1<OPTION> <OPTION>2<OPTION>...
2
2123
by: veracon | last post by:
Hello, I'm looking to use XML and XSLT for templates in a system I'm writing, however I'm not really sure which parser is the "best". Basically, which library has the most features, and which is...
21
2504
by: Damian | last post by:
Hi, I'm from an ASP.NET background an am considering making the switch to Python. I decided to develop my next project in tandem to test the waters and everything is working well, loving the...
0
7048
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6911
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7050
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6743
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
4787
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4488
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
2999
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
2988
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
564
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.