从 lxml.objectify.ObjectifiedElement 中剥离 python 命名空间属性
如何从 lxml.objectify.ObjectifiedElement
中剥离 python 属性?
示例:
In [1]: from lxml import etree, objectify
In [2]: foo = objectify.Element("foo")
In [3]: foo.bar = "hi"
In [4]: foo.baz = 1
In [5]: foo.fritz = None
In [6]: 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" py:pytype="TREE">
<bar py:pytype="str">hi</bar>
<baz py:pytype="int">1</baz>
<fritz xsi:nil="true"/>
</foo>
我希望输出如下所示:
<foo>
<bar>hi</bar>
<baz>1</baz>
<fritz/>
</foo>
Possible Duplicate:
When using lxml, can the XML be rendered without namespace attributes?
How can I strip the python attributes from an lxml.objectify.ObjectifiedElement
?
Example:
In [1]: from lxml import etree, objectify
In [2]: foo = objectify.Element("foo")
In [3]: foo.bar = "hi"
In [4]: foo.baz = 1
In [5]: foo.fritz = None
In [6]: 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" py:pytype="TREE">
<bar py:pytype="str">hi</bar>
<baz py:pytype="int">1</baz>
<fritz xsi:nil="true"/>
</foo>
I'd instead like the output to look like:
<foo>
<bar>hi</bar>
<baz>1</baz>
<fritz/>
</foo>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用
etree.strip_attributes
和etree.cleanup_namespaces
。这仍然留下
xsi:nil
引用,您可以类似地删除它。You can accomplish this by using
etree.strip_attributes
andetree.cleanup_namespaces
.This still leaves the
xsi:nil
reference, which you can strip similarly.还有专门的函数 objectify.deannotate(...):
There's also the specialized function objectify.deannotate(...):