I noticed that it is not possible to use elem.find or elem.findall with an xpath that contains position indices if the method is called with the namespaces argument.
This behavior has also been reported in Bug #1873886.
It appears that during the tokenization of the xpath, the numbers are treated as tags, i.e. they are concatenated with the default namespace (during function calls with namespaces). This results in a wrong path imo.
For example:
>>> from lxml import etree >>> doc = etree.XML(""" <foo xmlns="http://example.com/foo"> <bar>baz</bar> </foo>""") >>> path = "./bar[1]" >>> doc.find(path, namespaces={None:"http://example.com/foo"}) None
The target element is not found here because the path that is used is effectively:
./{http://example.com/foo}bar[{http://example.com/foo}1]
Changes:
- I added a check during the tokenization of the xpath to determine whether the processed
tagis a number to avoid concatenation with the namespace.