我在使用 lxml
生成的 SVG 文件中有一个文本元素。我想在此元素中保留空格。我创建了文本元素,然后尝试将 .set()
xml:space
更改为 preserve
但我尝试的任何操作似乎都不起作用。我可能在概念上遗漏了一些东西。有什么想法吗?
最佳答案
您可以通过显式指定与特殊 xml:
前缀相关联的 namespace URI 来实现(参见 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)
输出:
<root xml:space="preserve"/>
关于python - 将 'xml:space' 设置为 'preserve' Python lxml,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17476749/