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'])
outputs
urn:oasis:names:tc:opendocument:xmlns:style:1.0
But when I try to run
ele.append(etree.fromstring('<style:style />'))
I get the error
lxml.etree.XMLSyntaxError: Namespace prefix style on style is not defined
What am I missing here?
etree.fromstring('<style:style />')
throws an error because <style:style />
is a small XML document that is not namespace-well-formed.
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" />')
来源:https://stackoverflow.com/questions/24876855/using-fromstring-with-lxml-prefixes