问题
I have a text element within an SVG file that I'm generating using lxml
. I want to preserve whitespace in this element. I create the text element and then attempt to .set()
the xml:space
to preserve
but nothing I try seems to work. I'm probably missing something conceptually. Any ideas?
回答1:
You can do it by explicitly specifying the namespace URI associated with the special xml:
prefix (see http://www.w3.org/XML/1998/namespace).
from lxml import etree
root = etree.Element("root")
root.set("{http://www.w3.org/XML/1998/namespace}space", "preserve")
print etree.tostring(root)
Output:
<root xml:space="preserve"/>
来源:https://stackoverflow.com/questions/17476749/setting-xmlspace-to-preserve-python-lxml