[英]lxml: Converting XML to HTML through XSLT and get HtmlElements
我有作为XML文件的数据。 我还提供了一个XSLT来将XML转换为HTML。 我可以使用lxml来执行转换,但是,我想在转换后更改一些HTML标记。 如何将这个新的etree转换为HtmlElements,以便我可以专门使用某些方法,如.cssselect()等。
>>> import lxml.etree
>>> import lxml.html
>>>
>>> xmlstring = '''\
... <?xml version='1.0' encoding='ASCII'?>
... <root><a class="here">link1</a><a class="there">link2</a></root>
... '''
>>> root = lxml.etree.fromstring(xmlstring)
>>> root.cssselect('a.here')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'lxml.etree._Element' object has no attribute 'cssselect'
lxml.etree.tostring(root)
- > lxml.html.fromstring(..)
>>> root = lxml.html.fromstring(lxml.etree.tostring(root))
>>> root.cssselect('a.here')
[<Element a at 0x2989308>]
获取XML输出:
>>> print lxml.etree.tostring(root, xml_declaration=True)
<?xml version='1.0' encoding='ASCII'?>
<root><a class="here">link1</a><a class="there">link2</a></root>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.