gpt4 book ai didi

python - 将 'xml:space' 设置为 'preserve' Python lxml

转载 作者:太空狗 更新时间:2023-10-29 21:43:38 24 4
gpt4 key购买 nike

我在使用 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com