从lxml.objectify.ObjectifiedElement [duplicate]中剥离python名称空间属性

问题描述

||                                                                                                                   这个问题已经在这里有了答案:                                                      

解决方法

还有专门的函数objectify.deannotate(...):
Help on built-in function deannotate in module lxml.objectify:

      deannotate(...)
        deannotate(element_or_tree,pytype=True,xsi=True,xsi_nil=False,cleanup_namespaces=False)

        Recursively de-annotate the elements of an XML tree by removing \'py:pytype\'
        and/or \'xsi:type\' attributes and/or \'xsi:nil\' attributes.

        If the \'pytype\' keyword argument is True (the default),\'py:pytype\'
        attributes will be removed. If the \'xsi\' keyword argument is True (the
        default),\'xsi:type\' attributes will be removed.
        If the \'xsi_nil\' keyword argument is True (default: False),\'xsi:nil\'
        attributes will be removed.

        Note that this does not touch the namespace declarations by
        default.  If you want to remove unused namespace declarations from
        the tree,pass the option ``cleanup_namespaces=True``.
    ,您可以使用
etree.strip_attributes
etree.cleanup_namespaces
完成此操作。
In [8]: etree.strip_attributes(foo,\'{http://codespeak.net/lxml/objectify/pytype}pytype\')
In [9]: print etree.tostring(foo,pretty_print=True)
<foo xmlns:py=\"http://codespeak.net/lxml/objectify/pytype\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">
  <bar>hi</bar>
  <baz>1</baz>
  <fritz xsi:nil=\"true\"/>
</foo>

In [10]: etree.cleanup_namespaces(foo)
In [11]: print etree.tostring(foo,pretty_print=True)
<foo xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">
  <bar>hi</bar>
  <baz>1</baz>
  <fritz xsi:nil=\"true\"/>
</foo>
仍然留下
xsi:nil
参考,您可以类似地剥离它。
In [12]: etree.strip_attributes(foo,\'{http://www.w3.org/2001/XMLSchema-instance}nil\')
In [13]: etree.cleanup_namespaces(foo)
In [14]: print etree.tostring(foo,pretty_print=True)
<foo>
  <bar>hi</bar>
  <baz>1</baz>
  <fritz/>
</foo>
    

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...