makeDOMStandalone(element)
Make a DOM Element Node standalone
The DOM tree starting at element is copied to a new DOM tree where:
So, if the root element to be transformed is defined with a prefix, that prefix is removed from the whole tree:
>>> dom = minidom.parseString('''<?xml version="1.0"?> ... <foo xmlns:bar="http://bar.com"> ... <bar:spam><bar:eggs /></bar:spam> ... </foo>''') >>> element = dom.documentElement.getElementsByTagName('bar:spam')[0] >>> standalone = makeDOMStandalone(element) >>> standalone.toxml() u'<spam><eggs/></spam>'
Prefixes are of course also removed from attributes:
>>> element.setAttributeNS(element.namespaceURI, 'bar:vikings', ... 'singing') >>> standalone = makeDOMStandalone(element) >>> standalone.toxml() u'<spam vikings="singing"><eggs/></spam>'
Any other namespace used will be preserved, with the prefix definitions for these renamed and moved to the root element:
>>> dom = minidom.parseString('''<?xml version="1.0"?> ... <foo xmlns:bar="http://bar.com" xmlns:mp="uri://montypython"> ... <bar:spam> ... <bar:eggs mp:song="vikings" /> ... <mp:holygrail xmlns:c="uri://castle"> ... <c:camelot place="silly" /> ... </mp:holygrail> ... <lancelot xmlns="uri://montypython" /> ... </bar:spam> ... </foo>''') >>> element = dom.documentElement.getElementsByTagName('bar:spam')[0] >>> standalone = makeDOMStandalone(element) >>> print standalone.toxml() <spam xmlns:p0="uri://montypython" xmlns:p1="uri://castle"> <eggs p0:song="vikings"/> <p0:holygrail> <p1:camelot place="silly"/> </p0:holygrail> <p0:lancelot/> </spam>