In XML, Namespaces are used to avoid name conflicts by qualifying the names of elements and attributes in an XML document. They are defined using the xmlns attribute in the start tag of an element and usually looks like URLs as shown below.

<root xmlns:ns="">
    <ns:item>Item 1</ns:item>
    <ns:item>Item 2</ns:item>

In the above example, ns is the prefix for a namespace

Using the ElementTree library we can parse, navigate and manipulate XML documents. However, dealing with namespaces needs some additional steps.

Read and parse the XML document

Let’s first read and parse the XML document. We can use the parse() method from the ElementTree library.

import xml.etree.ElementTree as ET

# Parse the XML file
tree = ET.parse('example.xml')
root = tree.getroot()

Handle namespaces

ElementTree expands the namespace into a full URI wrapped in curly braces. Therefore, when searching for elements or attributes, we need to provide the full namespace URI.

Following is the example:

# Namespace map
namespaces = {'ns': ''}

# Accessing elements with namespace
for item in root.findall('ns:item', namespaces):

In the above example, namespaces is a dictionary mapping the namespace prefix to the URI. When using findall, find, or iter, the namespace prefix in the tag name is replaced by the corresponding URI from the namespaces dictionary.

Categorized in:

Tagged in: