1

If I've got an XML file like this:

<root
 xmlns:a="http://example.com/a"
 xmlns:b="http://example.com/b"
 xmlns:c="http://example.com/c"
 xmlns="http://example.com/base">
   ...
</root>

How can I get a list of the namespace definitions (ie, the xmlns:a="…", etc)?

Using:

import xml.etree.ElementTree as ET

tree = ET.parse('foo.xml')
root = tree.getroot()
print root.attrib()

Shows an empty attribute dictionary.

David Wolever
  • 148,955
  • 89
  • 346
  • 502
  • Here is how it can be done with ElementTree: http://stackoverflow.com/a/42372404/407651 – mzjn Mar 27 '17 at 06:28

2 Answers2

3

Via @mzjn, in the comments, here's how to do it with stock ElementTree: https://stackoverflow.com/a/42372404/407651 :

import xml.etree.ElementTree as ET
my_namespaces = dict([
    node for (_, node) in ET.iterparse('file.xml', events=['start-ns'])
])
David Wolever
  • 148,955
  • 89
  • 346
  • 502
2

You might find it easier to use lxml.

from lxml import etree
xml_data = '<root xmlns:a="http://example.com/a" xmlns:b="http://example.com/b" xmlns:c="http://example.com/c" xmlns="http://example.com/base"></root>'

root_node = etree.fromstring(xml_data)
print root_node.nsmap

This outputs

{None: 'http://example.com/base',
'a': 'http://example.com/a',
'b': 'http://example.com/b',
'c': 'http://example.com/c'}
Mr-F
  • 871
  • 7
  • 12