ElementPath

5.1.1 · active · verified Thu Apr 09

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

Install

Imports

Quickstart

This example demonstrates how to parse an XML string using `ElementTree` and then use `elementpath.select` to query elements with an XPath expression. It prints the text content of all 'item' elements found.

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

view raw JSON →