【发布时间】:2013-07-02 20:10:28
【问题描述】:
我在使用lxml 生成的 SVG 文件中有一个文本元素。我想在这个元素中保留空格。我创建了文本元素,然后尝试将.set()xml:space 转换为preserve,但我尝试的任何方法似乎都不起作用。我可能在概念上遗漏了一些东西。有什么想法吗?
【问题讨论】:
我在使用lxml 生成的 SVG 文件中有一个文本元素。我想在这个元素中保留空格。我创建了文本元素,然后尝试将.set()xml:space 转换为preserve,但我尝试的任何方法似乎都不起作用。我可能在概念上遗漏了一些东西。有什么想法吗?
【问题讨论】:
您可以通过显式指定与特殊 xml: 前缀关联的命名空间 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"/>
【讨论】: