2

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?

Fred the Fantastic
  • 1,295
  • 1
  • 9
  • 11
  • I'm so glad I finally found this question with the terms "lxml from string with prefix" - I would have thought this was a bigger pain/problem and there would be lots of info (or at least a mention in http://lxml.de/tutorial.html)! – Pat Mar 11 '15 at 22:15

1 Answers1

1

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" />')
mzjn
  • 48,958
  • 13
  • 128
  • 248