Is there a key for the default namespace when creating a dictionary for use with xml.etree.ElementTree.findall () in Python?

I am trying to parse an XML document with a default namespace i.e. the root node has an attribute xmlns

. This is annoying if you want to try to find specific tags in child nodes, because each tag has a default namespace prefix.

xml.etree.ElementTree.findall()

allows you to pass a dictionary namespaces

, but I can't seem to find what the default namespace maps to. I've tried using "default", "No", "xmlns" with no success.

One option that seems to work is to prefix the tag passed in findall()

with "xmlns:" (EDIT: this can be any arbitrary unique name) and the corresponding entry in the namespace dictionary, but I'm wondering if this is even necessary ...

EDIT: I should mention that this is Python 3.3.2. I believe in older versions of Python, findall()

doesn't take an argument namespaces

.

+2


source to share


1 answer


Decided to take a look at the source code of the method. So, as it turns out, the following code in xml.etree.ElementPath does the dirty work:

def xpath_tokenizer(pattern, namespaces=None):
    for token in xpath_tokenizer_re.findall(pattern):
        tag = token[1]
        if tag and tag[0] != "{" and ":" in tag:
            try:
                prefix, uri = tag.split(":", 1)
                if not namespaces:
                    raise KeyError
                yield token[0], "{%s}%s" % (namespaces[prefix], uri)
            except KeyError:
                raise SyntaxError("prefix %r not found in prefix map" % prefix)
        else:
            yield token

      



pattern

is a tag passed to findall()

. If not in the tag :

, the tokenizer simply returns the tag back without any substitutions.

+2


source