XPath is a powerful query language for navigating and extracting specific data nodes from XML documents.
While Python's built-in ElementTree provides basic searching, the high-performance lxml library offers full XPath 1.0 support for complex queries and attribute filtering.
XPath (XML Path Language) uses path expressions to select nodes or node sets in an XML document. This makes it ideal for navigating deeply nested or complex XML structures efficiently.
from lxml import etree
Step 1: Import the etree module from the lxml library, which provides comprehensive support for XPath queries.
# Load the XML data
xml_data = """
<students>
<student>
<id>1</id>
<name>John Deo</name>
<mark>75</mark>
</student>
<student>
<id>2</id>
<name>Jane Smith</name>
<mark>85</mark>
</student>
</students>
"""
root = etree.fromstring(xml_data)
Step 2: Define your XML data as a string and parse it using etree.fromstring() to construct an in-memory XML tree.
# Extract all student names
names = root.xpath("//student/name/text()")
print(names)
Explanation: The expression //student/name/text() navigates directly to the text content of all <name> elements within any <student> node.
# Extract student name with specific ID
student = root.xpath("//student[id='2']/name/text()")
print(student)
Explanation: This conditional predicate filter selects the <name> of the student whose child element <id> equals 2.
# Get names of students with marks greater than 80
high_scorers = root.xpath("//student[mark > 80]/name/text()")
print(high_scorers)
Explanation: Evaluates numeric conditions directly inside predicates to filter records dynamically.
XPath queries enable precise data extraction from XML documents, making it an indispensable tool for data engineers and developers. Python's lxml library simplifies these operations significantly.
Practice XPath queries in our companion Jupyter Notebook:
Explore interactive code cells and test XPath expressions using lxml.
View on GitHubAuthor & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.