ElementPath
ElementPath is a comprehensive Python library providing XPath 1.0, 2.0, 3.0, and 3.1 parsers and selectors. It works seamlessly with both Python's built-in `ElementTree` and the more performant `lxml` library. Currently at version 5.1.1, it maintains a regular release cadence, typically releasing new versions monthly or bi-monthly, often including bug fixes, performance improvements, and Python version compatibility updates.
Warnings
- breaking Python 3.8 and 3.9 compatibility was dropped in recent major versions. `elementpath` v5.0.0 dropped support for Python 3.8, and v5.1.0 dropped support for Python 3.9.
- breaking The internal XML parser was replaced. `SafeXMLParser` was removed and replaced with `SafeExpatParser` in version 5.0.0. This is an internal change but could affect users who relied on direct access to or configuration of the previous parser.
- breaking Internal node tree structure changed significantly with the introduction of `XPathNodeTree` as the backbone for node representation. This impacts advanced users extending the library or introspecting its internal data structures.
- gotcha XPath expressions often require explicit handling of XML namespaces. If your XML document uses namespaces and your XPath expressions don't account for them, you might get no results or incorrect ones.
Install
-
pip install elementpath
Imports
- select
from elementpath import select
- Selector
from elementpath import Selector
- XPathContext
from elementpath.xpath_context import XPathContext
Quickstart
import xml.etree.ElementTree as ET
from elementpath import select
xml_string = """
<root>
<item id="1">First item</item>
<item id="2">Second item</item>
<item id="3">Third item</item>
</root>
"""
# Parse the XML string using ElementTree
root = ET.fromstring(xml_string)
# Select all 'item' elements under 'root'
items = select(root, '/root/item')
# Extract text from selected items
results = [item.text for item in items]
print(results)
# Expected output: ['First item', 'Second item', 'Third item']