Python - XML Element Tree
XML ElementTree Python module usage guide
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 |
---|---|
| Selects all child elements with the given tag. For example, Changed in version 3.8: Support for star-wildcards was added. |
| Selects all child elements, including comments and processing instructions. For example, |
| 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, |
| Selects the parent element. Returns |
| Selects all elements that have the given attribute. |
| Selects all elements for which the given attribute has the given value. The value cannot contain quotes. |
| Selects all elements for which the given attribute does not have the given value. The value cannot contain quotes. New in version 3.10. |
| Selects all elements that have a child named |
| Selects all elements whose complete text content, including descendants, equals the given New in version 3.7. |
| Selects all elements whose complete text content, including descendants, does not equal the given New in version 3.10. |
| Selects all elements that have a child named |
| Selects all elements that have a child named New in version 3.10. |
| Selects all elements that are located at the given position. The position can be either an integer (1 is the first position), the expression |