Randy Syring Randy Syring - 1 month ago 5
Python Question

When using lxml, can the XML be rendered without namespace attributes?

I am generating some XML with lxml and getting nodes generated like this:

<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 are killing Quickbooks' parser. Can I get LXML to render without the custom stuff?

Answer

Looks like the following take care of it:

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

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

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