Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ElementTree does not find elements in a default namespace with namespaces #84285

Closed
myronww mannequin opened this issue Mar 29, 2020 · 2 comments
Closed

ElementTree does not find elements in a default namespace with namespaces #84285

myronww mannequin opened this issue Mar 29, 2020 · 2 comments
Labels
3.7 (EOL) end of life 3.8 only security fixes 3.9 only security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@myronww
Copy link
Mannequin

myronww mannequin commented Mar 29, 2020

BPO 40104
Nosy @@myronww

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = <Date 2020-03-29.19:55:45.681>
created_at = <Date 2020-03-29.19:13:13.542>
labels = ['3.7', '3.8', 'type-bug', 'library', '3.9']
title = 'ElementTree does not find elements in a default namespace with namespaces'
updated_at = <Date 2020-03-29.19:55:45.681>
user = 'https://github.com/myronww'

bugs.python.org fields:

activity = <Date 2020-03-29.19:55:45.681>
actor = 'myronww'
assignee = 'none'
closed = True
closed_date = <Date 2020-03-29.19:55:45.681>
closer = 'myronww'
components = ['Library (Lib)']
creation = <Date 2020-03-29.19:13:13.542>
creator = 'myronww'
dependencies = []
files = []
hgrepos = []
issue_num = 40104
keywords = []
message_count = 2.0
messages = ['365273', '365276']
nosy_count = 1.0
nosy_names = ['myronww']
pr_nums = []
priority = 'normal'
resolution = None
stage = 'resolved'
status = 'closed'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue40104'
versions = ['Python 3.7', 'Python 3.8', 'Python 3.9']

@myronww
Copy link
Mannequin Author

myronww mannequin commented Mar 29, 2020

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.

docTree.find("specVersion")
None

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.

myronww@0fc65da

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">
    <specVersion><major>1</major><minor>0</minor></specVersion>
    <device>
        <deviceType>urn:schemas-wifialliance-org:device:WFADevice:1</deviceType>
        <friendlyName>R7400 (Wireless AP)</friendlyName>
    </device>
</root>

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>

@myronww myronww mannequin added 3.7 (EOL) end of life 3.8 only security fixes 3.9 only security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error labels Mar 29, 2020
@myronww
Copy link
Mannequin Author

myronww mannequin commented Mar 29, 2020

Looks like this is fixed in the latest source code.

@myronww myronww mannequin closed this as completed Mar 29, 2020
@myronww myronww mannequin closed this as completed Mar 29, 2020
@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.7 (EOL) end of life 3.8 only security fixes 3.9 only security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

0 participants