从lxml.objectify.ObjectifiedElement剥离python命名空间属性 [英] Stripping python namespace attributes from an lxml.objectify.ObjectifiedElement

查看:380
本文介绍了从lxml.objectify.ObjectifiedElement剥离python命名空间属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:



如何从一个 lxml.objectify.ObjectifiedElement



示例:

  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/pytypexmlns:xsd =http://www.w3.org/2001/XMLSchemaxmlns: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>


解决方案

还有专门的函数objectify.deannotate(.. 。):

 模块lxml.objectify中的内置函数deannotate的帮助:

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

通过删除py来递归地去注释XML树的元素:pytype'
和/或'xsi:type'属性和/或'xsi:nil'属性。

如果'pytype'关键字参数是True(默认值),'py:pytype'
属性将被删除。如果'xsi'关键字参数为True(
默认值),则'xsi:type'属性将被删除。
如果'xsi_nil'关键字参数是True(默认值:False),那么'xsi:nil'
属性将被删除。

请注意,这不会触及
default的命名空间声明。如果要从
树中删除未使用的名称空间声明,请传递选项``cleanup_namespaces = True``。


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>

解决方案

There's also the specialized function 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``.

这篇关于从lxml.objectify.ObjectifiedElement剥离python命名空间属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆