diff -r da9898e7e90d Lib/test/test_xml_etree.py --- a/Lib/test/test_xml_etree.py Wed Jul 27 16:59:22 2016 +0200 +++ b/Lib/test/test_xml_etree.py Wed Jul 27 19:56:56 2016 +0200 @@ -869,6 +869,33 @@ ' \n' '') + def test_strip_namespaces(self): + elem = ET.XML(SAMPLE_XML_NS) + elem.set('nons', 'attribute 1 without ns') + elem.set('{http://effbot.org/ns}withns', 'attribute 2 with ns') + elem[-1].set('{http://effbot.org/ns}yesns', 'attribute 3 with ns') + + self.assertEqual(elem.tag, '{http://effbot.org/ns}body') + self.assertEqual(elem[0].tag, '{http://effbot.org/ns}tag') + self.assertEqual(elem[-1].tag, '{http://effbot.org/ns}section') + self.assertEqual(elem[-1][0].tag, '{http://effbot.org/ns}tag') + + self.assertEqual(elem.get('nons'), 'attribute 1 without ns') + self.assertEqual(elem.get('{http://effbot.org/ns}withns'), 'attribute 2 with ns') + self.assertEqual(elem[-1].get('{http://effbot.org/ns}yesns'), 'attribute 3 with ns') + + ET.strip_namespaces(elem) + self.assertEqual(elem.tag, 'body') + self.assertEqual(elem[0].tag, 'tag') + self.assertEqual(elem[-1].tag, 'section') + self.assertEqual(elem[-1][0].tag, 'tag') + + self.assertEqual(elem.get('nons'), 'attribute 1 without ns') + self.assertEqual(elem.get('withns'), 'attribute 2 with ns') + self.assertEqual(elem[-1].get('yesns'), 'attribute 3 with ns') + self.assertIsNone(elem.get('{http://effbot.org/ns}withns')) + self.assertIsNone(elem[-1].get('{http://effbot.org/ns}yesns')) + def test_qname(self): # Test QName handling. diff -r da9898e7e90d Lib/xml/etree/ElementTree.py --- a/Lib/xml/etree/ElementTree.py Wed Jul 27 16:59:22 2016 +0200 +++ b/Lib/xml/etree/ElementTree.py Wed Jul 27 19:56:56 2016 +0200 @@ -536,6 +536,24 @@ return self.text == other.text return self.text == other + +def strip_namespaces(tree): + """Remove all namespaces from tags and attributes in place. + + Leaves only the local names in the subtree. + """ + for el in tree.iter(): + tag = el.tag + if tag and isinstance(tag, str) and tag[0] == '{': + el.tag = tag.partition('}')[2] + attrib = el.attrib + if attrib: + for name, value in list(attrib.items()): + if name and isinstance(name, str) and name[0] == '{': + del attrib[name] + attrib[name.partition('}')[2]] = value + + # --------------------------------------------------------------------