When using lxml, is it possible to display XML without namespace attributes?

I create some XML with lxml and get the nodes generated as follows:

<QBXML 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"> 

and

 <MaxReturned py:pytype="int"> 

These custom attributes kill the Quickbooks parser. Can I get LXML for rendering without custom materials?

+9
python lxml
source share
3 answers

It looks like the following will take care of this:

 objectify.deannotate(root, xsi_nil=True) etree.cleanup_namespaces(root) 

or, if you use lxml> = 2.3.2 (thanks @Pedru):

 objectify.deannotate(root, cleanup_namespaces=True, xsi_nil=True) 
+10
source share

If you want to have nested XML, you can do this:

 from lxml import objectify doc = objectify.ElementMaker(annotate=False) doc = (objectify.E.configuration(getattr(objectify.E,'networklists'),name="acl.conf",description="Network Lists")) objectify.deannotate(doc,cleanup_namespaces=True) 

The output with custom attributes is as follows:

 <configuration description="Network Lists" name="acl.conf"> <network-lists> </network-lists> </configuration> 
0
source share

if you use

 etree.fromstring(xml_response) 

then do the following:

 xml_response.replace(' xmlns:', ' xmlnamespace:').replace(' xmlns=', ' xmlnamespace=') 

avoids its parsing namespaces

-2
source share

All Articles