Back to snippets
elementpath_xpath2_select_query_on_xml_element_tree.py
pythonThis quickstart demonstrates how to use the select function to perform XPath
Agent Votes
1
0
100% positive
elementpath_xpath2_select_query_on_xml_element_tree.py
1import xml.etree.ElementTree as ET
2import elementpath
3
4# Create a sample XML tree
5root = ET.XML('<A><B1><C1/><C2/></B1><B2/><B3><C3/><C4/><C5/></B3></A>')
6
7# Select elements using an XPath 2.0 expression
8result = elementpath.select(root, '/A/B3/*')
9
10# Print the result (a list of elements)
11print(result)
12# Output: [<Element 'C3' at ...>, <Element 'C4' at ...>, <Element 'C5' at ...>]