Issue 17138: XPath error in xml.etree.ElementTree (original) (raw)

Issue17138

Created on 2013-02-05 19:42 by Antoine2008, last changed 2022-04-11 14:57 by admin. 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("

1

2

") >>> result = xml.find(".//h1[2]") >>> print(result) None ======================================================================== Expected result : "<Element 'h1' at ...>" (

2

)
msg183632 - (view) Author: karl (karlcow) * Date: 2013-03-07 03:53
http://docs.python.org/3/library/xml.etree.elementtree.html#supported-xpath-syntax 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("

1

2

") >>> result = xml.findall(".//h1") >>> for i in result: ... print(i.text) ... 1 2 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

into

, 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']

History
Date User Action Args
2022-04-11 14:57:41 admin set github: 61340
2013-03-09 14:22:18 eli.bendersky set status: open -> closedresolution: not a bugmessages: + stage: resolved
2013-03-07 03:53:41 karlcow set nosy: + karlcowmessages: +
2013-02-05 21:53:51 flox set nosy: + eli.bendersky, flox
2013-02-05 19:42:25 Antoine2008 create