views:

302

answers:

2

In my application I am using the 4Suite.org XSLT library to perform transformations of source XML. The syntax is like this:

from Ft.Xml.Xslt import Transform
transformed_xml = Transform(raw_xml, stylesheet)

where raw_xml and stylesheet have been defined elsewhere in my application. raw_xml will be the xml resulting from reading a filehandle opened with the codecs module so the raw_xml will be unicode.

The problem is that the Transform() function requires the value of the source xml (raw_xml in my example) to be ascii. It says so in the pydoc and my own program fails with an error along those lines if I try to transform unicode.

Is there a different approach or is there another python library which can perform an XSLT transformation against a unicode source? Or, am I misunderstanding something about XSLT transformations?

+2  A: 

You are likely better off using the more modern and actively maintained lxml.

Ned Deily
I would *love* to use lxml but my application is already distributed at over a hundred sites so I don't have a lot of flexibility in swapping out the xml library. It could be done but is something I'm going to try to avoid right now. Now, if I ever get a shot at updating and refactoring this code then, yes, I will probably switch to lxml for ease of use and compatibility with etree.
Mike
+2  A: 

I'm not sure Transform actually needs ascii -- looks to me like it should support any encoded Python str. What happens if you call Transform(raw_xml.encode('utf8'), stylesheet) (and then decode the resulting utf8-encoded string back to Unicode when you're done processing it of course, if you need Unicode) -- doesn't that work?

Alex Martelli
This looks like it did the trick. I'm having it tested now but it seems promising. Thanks!
Mike