本文介绍了将'xml:space'设置为'preserve'Python lxml的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用lxml生成的SVG文件中有一个text元素.我想在此元素中保留空格.我创建了text元素,然后尝试将.set()xml:space转换为preserve,但是我尝试的任何方法似乎都无效.我可能在概念上缺少一些东西.有什么想法吗?

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?

推荐答案

您可以通过显式指定与特殊xml:前缀关联的名称空间URI来实现(请参阅).

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)

输出:

<root xml:space="preserve"/>    

这篇关于将'xml:space'设置为'preserve'Python lxml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 21:11