473,545 Members | 721 Online
Bytes | Software Development & Data Engineering Community
+ 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 19346
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.FieldStorag e()

readtimestart = time.time()
styledoc = libxml2.parseFi le("page.xsl")
style = libxslt.parseSt ylesheetDoc(sty ledoc)
doc = libxml2.parseFi le(query['script'].value)
readtimeend = time.time()
start_convertin g = time.time()
result = style.applyStyl esheet(doc, None)
done_converting = time.time()

html = result.serializ e()

print "Content-type: text/html"
print
print html

style.freeStyle sheet()
doc.freeDoc()
result.freeDoc( )

et = time.time()
totaltime = et-st
print "<!-- Page served in %s seconds. -->" % totaltime
print "<!-- XML conversion took %s seconds. -->" %\
(done_convertin g-start_convertin g)
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.FieldStorag e()

readtimestart = time.time()
styledoc = libxml2.parseFi le("page.xsl")
style = libxslt.parseSt ylesheetDoc(sty ledoc)
doc = libxml2.parseFi le(query['script'].value)
readtimeend = time.time()
start_convertin g = time.time()
result = style.applyStyl esheet(doc, None)
done_converting = time.time()

html = result.serializ e()

print "Content-type: text/html"
print
print html

style.freeStyle sheet()
doc.freeDoc()
result.freeDoc( )

et = time.time()
totaltime = et-st
print "<!-- Page served in %s seconds. -->" % totaltime
print "<!-- XML conversion took %s seconds. -->" %\
(done_convertin g-start_convertin g)
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.amb er.eu.org> wrote in message news:<ma******* *************** ************@py thon.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
1898
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 little piece of code I'm working on: from xml.xslt.Processor import Processor proc = Processor()
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 libxslt import libxml2
1
1862
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> <alternative>Answer 4</alternative> </question>
1
1579
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. I have one compiled stylesheet which I uses to process all responses. This way I only need to read and compile the stylesheet once. When serving...
1
5061
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 should go. I tried xslt, without success yet (but I dont know much about it) so I failed back on python+dom wher I know a bit more. I'm asking you...
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 would appreciate any assistance. Kai
3
3073
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> <OPTION>3<OPTION> </FIELD>
2
2129
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 the most supported? A guide I saw mentioned importing xml.xslt, however it appears the xml module/package contains pretty much nothing - xml.xslt...
21
2510
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 language, etc. What I've got is: two websites, one in ASP.NET v2 and one in Python 2.5 (using 4suite for XML/XSLT) both on the same box (Windows...
0
7465
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7398
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7656
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7805
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7416
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
1
5325
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4944
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3449
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1013
muto222
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.