21

Is there an XSLT library that is pure Python?

Installing libxml2+libxslt or any similar C libraries is a problem on some of the platforms I need to support.

I really only need basic XSLT support, and speed is not a major issue.

Andy Balaam
  • 6,423
  • 6
  • 34
  • 37

5 Answers5

10

Unfortunately there are no pure-python XSLT processors at the moment. If you need something that is more platform independent, you may want to use a Java-based XSLT processor like Saxon. 4Suite is working on a pure-python XPath parser, but it doesn't look like a pure XSLT processor will be out for some time. Perhaps it would be best to use some of Python's functional capabilities to try and approximate the existing stylesheet or look into the feasibility of using Java instead.

Jon W
  • 15,480
  • 6
  • 37
  • 47
7

I don't think you can do it in cpython: there are no pure python XSLT implementations.

But you can trivially do it in jython, using the inbuilt XSLT APIs of the JVM. I wrote a blog post for the specific case of doing it on Google AppEngine, but the code given should work under jython in anyn circumstances.

Transforming with XSLT on Google AppEngine and jython

http://jython.xhaus.com/transforming-with-xslt-on-google-appengine-and-jython/

HTH,

Alan.

Alan Kennedy
  • 71
  • 1
  • 1
1

Have you looked at 4suite?

JeniT
  • 3,660
  • 20
  • 11
1

If you only need basic support, and your XML isn't too crazy, consider removing the XSLT element from the equation and just using a DOM/SAX parser.

Here's some info from the PythonInfo Wiki:

[DOM] sucks up an entire XML file, holds it in memory, and lets you work with it. Sax, on the other hand, emits events as it goes step by step through the file.

What do you think?

Pete Karl II
  • 4,060
  • 3
  • 21
  • 27
  • Nice idea, but in this case I have existing XSLTs that I want to use on platforms where I can't compile any code or install libraries. – Andy Balaam Nov 13 '08 at 13:19
1

There's also http://lxml.de/

"lxml is the most feature-rich and easy-to-use library for processing XML and HTML in the Python language."

mbear
  • 121
  • 1
  • 3
  • 1
    Hmm. Siting marketing claims of "the most feature-rich and easy-to-use" seems a little much (unless you've done exhaustive research on the options and have come to this conclusion on your own). – Rob Feb 01 '13 at 16:16
  • 1
    lxml is, I'm pretty sure, just another Python binding around the same C libraries we're trying to get around using here: "[The lxml XML toolkit is a Pythonic binding for the C libraries libxml2 and libxslt.](http://lxml.de/index.html)." – Steven D. Jul 27 '15 at 15:26
  • Also, to quote lxml's [installation page](http://lxml.de/installation.html): "Unless you are using a static binary distribution (e.g. from a Windows binary installer), lxml requires libxml2 and libxslt to be installed..." – Steven D. Jul 27 '15 at 15:27