使用带有lxml前缀的fromstring() [英] using fromstring() with lxml prefixes
本文介绍了使用带有lxml前缀的fromstring()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个可变的ele.我试图将一个子节点附加到其标签中包含名称空间前缀(称为样式)的ele上. ele似乎知道此前缀,如下所示:
I have a variable ele. I'm trying to append a child node onto ele that contains a namespace prefix (called style) in its tag. ele seems to be aware of this prefix, as the line:
print(ele.nsmap['style'])
输出
urn:oasis:names:tc:opendocument:xmlns:style:1.0
但是当我尝试运行时
ele.append(etree.fromstring('<style:style />'))
我得到了错误
lxml.etree.XMLSyntaxError: Namespace prefix style on style is not defined
我在这里想念什么?
推荐答案
etree.fromstring('<style:style />')
会引发错误,因为<style:style />
是不是
etree.fromstring('<style:style />')
throws an error because <style:style />
is a small XML document that is not namespace-well-formed.
如果要提供名称空间作为fromstring()
的参数,则必须在文档中声明名称空间:
You have to declare the namespace in the document if you want to provide it as an argument to fromstring()
:
etree.fromstring('<style:style xmlns:style="urn:oasis:names:tc:opendocument:xmlns:style:1.0" />')
这篇关于使用带有lxml前缀的fromstring()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文