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

雨燕双飞 提交于 2019-12-07 23:38:31

问题


I'm trying to parse an XML document with a default namespace, i.e. the root node has an xmlns attribute. This is annoying if you want to try find certain tags in the child nodes because each tag is prefixed with the default namespace.

xml.etree.ElementTree.findall() allows for a namespaces dictionary to be passed in but I can't seem to find what the default namespace is mapped to. I have tried using 'default', None, 'xmlns' with no success.

One option that does seem to work is to prefix the tag passed to findall() with 'xmlns:' (EDIT: this can be any arbitrary unique name actually) and a corresponding entry in the namespaces dictionary but I'm wondering if that is even necessary.

EDIT: I should mention this is Python 3.3.2. I believe in older versions of Python, findall() does not accept a namespaces argument.


回答1:


Decided to take a look at the source code for the method. So as it turns out, the following code in xml.etree.ElementPath is doing 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 the tag passed into findall(). If there is no : found in the tag, the tokenizer simply returns the tag back without any substitutions.



来源:https://stackoverflow.com/questions/18171311/is-there-a-key-for-the-default-namespace-when-creating-dictionary-for-use-with-x

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!