Searching the XML with XPath

suggest change

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

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

Searching for all books:

import xml.etree.cElementTree as ET
tree = ET.parse('sample.xml')
tree.findall('Books/Book')

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:

tree.find("Books/Book[@id='5']")
# searches with xml attributes must have '@' before the name

Search for the second book:

tree.find("Books/Book[2]")
# indexes starts at 1, not 0

Search for the last book:

tree.find("Books/Book[last()]")
# 'last' is the only xpath function allowed in ElementTree

Search for all authors:

tree.findall(".//Author")
#searches with // must use a relative path

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:


Manipulating XML:
* Searching the XML with XPath

Table Of Contents
2 Filter
3 List
7 Loops
22 Reduce
27 Classes
29 Manipulating XML
31 Set
42 Tuple
45 Enum
62 Sockets
89 urllib
92 Idioms
104 Stack
105 Profiling
109 Logging
111 os module
118 Mixins
120 ArcPy
126 Arrays
132 2to3 tool
135 Unicode
138 Neo4j
140 Curses
141 Templates
145 heapq
146 tkinter
154 Audio
155 pyglet
157 ijson
160 Flask
161 Groupby
163 pygame
165 hashlib
166 Gzip
167 ctypes
185 pyaudio
186 shelve