Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stripping python namespace attributes from an lxml.objectify.ObjectifiedElement [duplicate]

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>
like image 675
Daenyth Avatar asked May 26 '11 15:05

Daenyth


People also ask

What is objectify in LXML?

objectify through a custom Element implementation. The main idea is to hide the usage of XML behind normal Python objects, sometimes referred to as data-binding. It allows you to use XML as if you were dealing with a normal Python object hierarchy.

What is LXML Etree?

lxml. etree supports parsing XML in a number of ways and from all important sources, namely strings, files, URLs (http/ftp) and file-like objects. The main parse functions are fromstring() and parse(), both called with the source as first argument.


2 Answers

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>
like image 93
Daenyth Avatar answered Sep 20 '22 06:09

Daenyth


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``.
like image 27
Holger Avatar answered Sep 22 '22 06:09

Holger