【问题标题】:Setting 'xml:space' to 'preserve' Python lxml将“xml:space”设置为“保留”Python lxml
【发布时间】:2013-07-02 20:10:28
【问题描述】:

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

【问题讨论】:

    标签: python xml svg lxml


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-21
      • 2012-06-02
      • 1970-01-01
      • 1970-01-01
      • 2015-03-03
      相关资源
      最近更新 更多