Title: XPath error in xml.etree.ElementTree
Type: behavior Stage: resolved
Components: XML Versions: Python 3.3
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: Antoine2008, eli.bendersky, flox, karlcow
Priority: normal Keywords:

Created on 2013-02-05 19:42 by Antoine2008, last changed 2013-03-09 14:22 by eli.bendersky. This issue is now closed.

Messages (3)
msg181473 - (view) Author: Gutzwiller (Antoine2008) Date: 2013-02-05 19:42
$ python3
Python 3.3.0 (default, Jan 25 2013, 09:38:18) 
[GCC 4.4.5] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import xml.etree.ElementTree as ET
>>> xml = ET.fromstring("<root><h1>1</h1><p><h1>2</h1></p></root>")
>>> result = xml.find(".//h1[2]")        
>>> print(result)

Expected result : "<Element 'h1' at ...>"  (<h1>2</h1>)
msg183632 - (view) Author: karl (karlcow) * Date: 2013-03-07 03:53

20.5.2. XPath support

This module provides limited support for XPath expressions for locating elements in a tree. The goal is to support a small subset of the abbreviated syntax; a full XPath engine is outside the scope of the module.

What you could do is testing the values with text once you have matched all elements of your choice.

>>> import xml.etree.ElementTree as ET
>>> xml = ET.fromstring("<root><h1>1</h1><p><h1>2</h1></p></root>")
>>> result = xml.findall(".//h1")
>>> for i in result:
...     print(i.text)

Could you close the issue?
msg183822 - (view) Author: Eli Bendersky (eli.bendersky) * (Python committer) Date: 2013-03-09 14:22
Gutzwiller, the [position] syntax means the Nth child *of its parent*. Since you placed the second <h1> into <p>, it's the first child of its parent. So the library's behavior is correct here. Note:

>>> [e.text for e in xml.findall('.//h1[1]')]
['1', '2']
Date User Action Args
2013-03-09 14:22:18eli.benderskysetstatus: open -> closed
resolution: not a bug
messages: + msg183822

stage: resolved
2013-03-07 03:53:41karlcowsetnosy: + karlcow
messages: + msg183632
2013-02-05 21:53:51floxsetnosy: + eli.bendersky, flox
2013-02-05 19:42:25Antoine2008create