This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: ElementTree does not find elements in a default namespace with namespaces
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.9, Python 3.8, Python 3.7
Status: closed Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: myronww
Priority: normal Keywords:

Created on 2020-03-29 19:13 by myronww, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (2)
msg365273 - (view) Author: Myron Walker (myronww) * Date: 2020-03-29 19:13
When you have an xml document like the one with a default namespace below.  When you try to lookup nodes in the document they are not found.


If you add a namespaces map with the '' key and the default namespaces like:

    { '': 'urn:schemas-upnp-org:device-1-0' }

then the nodes are still not found.  The issue is that there is a case
missing from xpath_tokenizer that does not yield a pair with the default namespace when one is specified.  Here is a fix.

from xml.etree.ElementTree import fromstring as parse_xml_fromstring
from xml.etree.ElementTree import ElementTree

SAMPLEXML = """<?xml version="1.0" encoding="utf-8"?>
<root xmlns="urn:schemas-upnp-org:device-1-0">
        <friendlyName>R7400 (Wireless AP)</friendlyName>

rootNode = parse_xml_fromstring(SAMPLEXML)
# Create a namespaces map like { '': 'urn:schemas-upnp-org:device-1-0' }
defaultns = {"": docNode.tag.split("}")[0][1:]}

specVerNode = docNode.find("specVersion", namespaces=defaultns)
Results should look like this

docNode.find("specVersion", namespaces=defaultns)
<Element '{urn:schemas-upnp-org:device-1-0}specVersion' at 0x7f18030e32f0>
msg365276 - (view) Author: Myron Walker (myronww) * Date: 2020-03-29 19:55
Looks like this is fixed in the latest source code.
Date User Action Args
2022-04-11 14:59:28adminsetgithub: 84285
2020-03-29 19:55:45myronwwsetstatus: open -> closed

messages: + msg365276
stage: resolved
2020-03-29 19:13:13myronwwcreate