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.

1

2 Answers 2

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'])
])
Sign up to request clarification or add additional context in comments.

Comments

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'}

1 Comment

This is not working. Give me an empty field.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.