Python3 and xml / xslt libraries

In python 2.6 I did this to achieve 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 equivalent in Python 3.2?

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

+5
source share
2 answers

Analogue of your code with 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 Python 3.2 support

+2
source

Since these libraries are just wrappers around C libraries, it shouldn't be very difficult to port them to Python 3.

lxml, ElementTree, "". , .

+1

All Articles