Python Language Searching the XML with XPath


Starting with version 2.7 ElementTree has a better support for XPath queries. XPath is a syntax to enable you to navigate through an xml like SQL is used to search through a database. Both find and findall functions support XPath. The xml below will be used for this example

        <Book id="1" price="7.95">
            <Title>Do Androids Dream of Electric Sheep?</Title>
            <Author>Philip K. Dick</Author>
        <Book id="5" price="5.95">
            <Title>The Colour of Magic</Title>
            <Author>Terry Pratchett</Author>
        <Book id="7" price="6.95">
            <Title>The Eye of The World</Title>
            <Author>Robert Jordan</Author>

Searching for all books:

import xml.etree.cElementTree as ET
tree = ET.parse('sample.xml')

Searching for the book with title = 'The Colour of Magic':

tree.find("Books/Book[Title='The Colour of Magic']") 
# always use '' in the right side of the comparison

Searching for the book with id = 5:

# searches with xml attributes must have '@' before the name

Search for the second book:

# indexes starts at 1, not 0

Search for the last book:

# 'last' is the only xpath function allowed in ElementTree

Search for all authors:

#searches with // must use a relative path