7

In python 2.6 I did this to achieve an xsl tranform

    import libxml2
    import libxslt
    ...
    styledoc = libxml2.parseFile(my_xslt_file)
    style = libxslt.parseStylesheetDoc(styledoc)
    doc = libxml2.parseDoc(siri_response_data)
    result = style.applyStylesheet(doc, None)
    ...

What would be the equivalent in Python 3.2?

I ask because it seems that lnxml and libxslt are not available in python3.2. I have heard of lxml - is this a direct equivalent of libxml2 + libxslt or does it have different calling patterns (needing rewriting of the code)?

barking.pete
  • 191
  • 1
  • 4
  • I think libxml2/xslt libraries now offer Python3 bindings via the `./configure` script argument `--with-python=${PATH_TO_PYTHON3_BINARY}`, e.g., `/usr/bin/python3`. – kevinarpe Nov 11 '15 at 12:26

1 Answers1

4

The analog of your code using lxml:

from lxml import etree

# ...    
styledoc = etree.parse(my_xslt_file)
transform = etree.XSLT(styledoc)
doc = etree.fromstring(siri_response_data)
result = transform(doc)
# ...

lxml lists support for Python 3.2

lxml uses libxml2/libxslt under the hood so results should be the same. It uses Cython to generate C extensions that work both on Python 2.x and 3.x from the same source, example.

Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
jfs
  • 399,953
  • 195
  • 994
  • 1,670