Python3和xml/xslt库 [英] Python3 and xml/xslt libraries

查看:193
本文介绍了Python3和xml/xslt库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在python 2.6中,我这样做是为了实现xsl转换

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)
    ...

Python 3.2中的等效内容是什么?

What would be the equivalent in Python 3.2?

我问是因为lnxml和libxslt似乎在python3.2中不可用.我听说过lxml-这直接相当于libxml2 + libxslt还是它具有不同的调用模式(需要重写代码)?

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)?

推荐答案

使用 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列出了对Python 3.2的支持

lxml lists support for Python 3.2

lxml在幕后使用libxml2/libxslt ,因此结果应该相同.它使用Cython从相同的源 查看全文

登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆