且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-02-15 21:18:48

您可以通过显式指定与特殊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"/>