Python - XML Element Tree

XML ElementTree Python module usage guide

Python - XML Element Tree
Photo by Shahadat Rahman / Unsplash

Here’s an example that demonstrates some of the XPath capabilities of the module. We’ll be using the countrydata XML document from the Parsing XML section:

import xml.etree.ElementTree as ET

root = ET.fromstring(countrydata)

# Top-level elements
root.findall(".")

# All 'neighbor' grand-children of 'country' children of the top-level
# elements
root.findall("./country/neighbor")

# Nodes with name='Singapore' that have a 'year' child
root.findall(".//year/..[@name='Singapore']")

# 'year' nodes that are children of nodes with name='Singapore'
root.findall(".//*[@name='Singapore']/year")

# All 'neighbor' nodes that are the second child of their parent
root.findall(".//neighbor[2]")

Supported XPath syntax

Syntax

Meaning

tag

Selects all child elements with the given tag. For example, spam selects all child elements named spam, and spam/egg selects all grandchildren named egg in all children named spam. {namespace}* selects all tags in the given namespace, {*}spam selects tags named spam in any (or no) namespace, and {}* only selects tags that are not in a namespace.

Changed in version 3.8: Support for star-wildcards was added.

*

Selects all child elements, including comments and processing instructions. For example, */egg selects all grandchildren named egg.

.

Selects the current node. This is mostly useful at the beginning of the path, to indicate that it’s a relative path.

//

Selects all subelements, on all levels beneath the current element. For example, .//egg selects all egg elements in the entire tree.

..

Selects the parent element. Returns None if the path attempts to reach the ancestors of the start element (the element find was called on).

[@attrib]

Selects all elements that have the given attribute.

[@attrib='value']

Selects all elements for which the given attribute has the given value. The value cannot contain quotes.

[@attrib!='value']

Selects all elements for which the given attribute does not have the given value. The value cannot contain quotes.

New in version 3.10.

[tag]

Selects all elements that have a child named tag. Only immediate children are supported.

[.='text']

Selects all elements whose complete text content, including descendants, equals the given text.

New in version 3.7.

[.!='text']

Selects all elements whose complete text content, including descendants, does not equal the given text.

New in version 3.10.

[tag='text']

Selects all elements that have a child named tag whose complete text content, including descendants, equals the given text.

[tag!='text']

Selects all elements that have a child named tag whose complete text content, including descendants, does not equal the given text.

New in version 3.10.

[position]

Selects all elements that are located at the given position. The position can be either an integer (1 is the first position), the expression last() (for the last position), or a position relative to the last position (e.g. last()-1).

Subscribe to Devtooler

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
[email protected]
Subscribe