I have a GPX file in which the prefix for most elements is simply the empty string, i.e. ''
.
To search for elements I use the findall
function and pass the appropriate namespace.
However, I cannot search for elements with an empty prefix as demonstrated below.
import xml.etree.ElementTree as ET
xml = '''<?xml version="1.0" encoding="UTF-8"?>
<foo xmlns="some-url" xmlns:ns1="other-url">
<ns1:bar/>
<baz/>
</foo>'''
tree = ET.fromstring(xml)
ns = {'': 'some-url', 'ns1': 'other-url'}
To search for elements I use
tree.findall('.//ns1:bar', ns)
which works correctly and gives [<Element '{other-url}bar' at 0x12128f230>]
. But
tree.findall('.//baz', ns)
only returns []
.
Am I doing something wrong? There is a hint in the documentation that says “Pass ''
as prefix to move all unprefixed tag names in the expression into the given namespace.” But I don’t know what that is supposed to tell me. Using tree.findall('.//'':baz', ns)
just throws an error.
PS.: I cannot really imagine this being a bug. But if it is one, I am happy to try to fix it and send a PR.