If you want to remove all namespaces from elements and attributes, I suggest the code shown below.
Context: in my application, I get XML representations of SOAP response flows, but I'm not interested in creating objects on the client side; I'm only interested in XML views. Moreover, I am not interested in any namespace thing that only makes things more complicated than they should be for my purposes. That way, I just remove the namespaces from the elements, and I remove all the attributes that contain the namespaces.
def dropns(root): for elem in root.iter(): parts = elem.tag.split(':') if len(parts) > 1: elem.tag = parts[-1] entries = [] for attrib in elem.attrib: if attrib.find(':') > -1: entries.append(attrib) for entry in entries: del elem.attrib[entry]
which prints:
===================================================================== <SOAP-ENV:Envelope SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"> <SOAP-ENV:Body> <ns1:mc_issue_getResponse> <return xsi:type="tns:IssueData"> <id xsi:type="xsd:integer">356</id> <view_state xsi:type="tns:ObjectRef"> <id xsi:type="xsd:integer">10</id> <name xsi:type="xsd:string">public</name> </view_state> </return> </ns1:mc_issue_getResponse> </SOAP-ENV:Body> </SOAP-ENV:Envelope> ===================================================================== <Envelope> <Body> <mc_issue_getResponse> <return> <id>356</id> <view_state> <id>10</id> <name>public</name> </view_state> </return> </mc_issue_getResponse> </Body> </Envelope> =====================================================================
source share