从 lxml.objectify.ObjectifiedElement 中剥离 python 命名空间属性

发布于 2024-11-10 02:00:52 字数 1025 浏览 4 评论 0原文

可能的重复:
使用 lxml 时,是否可以在不使用 lxml 的情况下渲染 XML命名空间属性?

如何从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

葬花如无物 2024-11-17 02:00:52

您可以使用 etree.strip_attributesetree.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>

You can accomplish this by using etree.strip_attributes and 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>

This still leaves the xsi:nil reference, which you can strip similarly.

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>
谈场末日恋爱 2024-11-17 02:00:52

还有专门的函数 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``.

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``.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文