Skip to content

xml.dom.minidom.Document.getElementById() result for duplicate IDs depends on earlier lookups #161110

Closed

Description

@gpshead

Bug report

Bug description:

When several elements have the same ID, xml.dom.minidom.Document.getElementById() first returns the last of the siblings, and a later lookup of a different ID, which continues the incremental search, overwrites the cached entry. The same call can then return a different element on an unmodified document.

from xml.dom import minidom

doc = minidom.parseString(
    '<!DOCTYPE r [<!ATTLIST e id ID #IMPLIED>]>'
    '<r><e id="a" n="1"/><e id="a" n="2"/></r>')
print(doc.getElementById("a").getAttribute("n"))  # 2
doc.getElementById("b")                            # None
print(doc.getElementById("a").getAttribute("n"))  # 1

Both calls should return the same element, the first one in document order (as the WHATWG DOM and libxml2 do).

CPython versions tested on:

CPython main branch

Operating systems tested on:

Linux

Linked PRs

Activity

  1. self-assigned this
    on Nov 1, 2026
  2. added a commit that references this issue on Nov 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

topic-XMLtype-bugAn unexpected behavior, bug, or error

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions