Setting 'xml:space' to 'preserve' Python lxml

痴心易碎 提交于 2019-12-08 17:24:52

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!