Visitar URL original
`xml.dom.minidom`: cloneNode() and importNode() raise NamespaceErr for DOM Level 1 nodes · Issue #158978 · python/cpython · GitHub
Skip to content

xml.dom.minidom: cloneNode() and importNode() raise NamespaceErr for DOM Level 1 nodes #158978

Description

@sricursion

Bug report

Bug description:

Since gh-156665, Node.cloneNode(), Document.importNode() and Attr.cloneNode() raise xml.dom.NamespaceErr for DOM Level 1 nodes that have a colon in the name. Such nodes have no namespace and are created by createElement("svg:rect"), createAttribute("a:b"), setAttribute("xmlns:xsi", ...), or by parsing with namespace processing disabled.

from xml.dom import minidom

doc = minidom.Document()
root = doc.appendChild(doc.createElement("root"))
root.setAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance")
root.appendChild(doc.createElement("svg:rect"))
for label, func in [
    ("cloneNode", lambda: root.cloneNode(True)),
    ("importNode", lambda: minidom.Document().importNode(root, True)),
    ("Attr.cloneNode", lambda: doc.createAttribute("a:b").cloneNode(True)),
]:
    try:
        node = func()
    except Exception as exc:
        print(f"{label}: {type(exc).__name__}: {exc}")
    else:
        print(f"{label}: {node.nodeName}")

Output on main:

cloneNode: NamespaceErr: cannot use the prefix 'xmlns' with an empty namespace
importNode: NamespaceErr: cannot use the prefix 'xmlns' with an empty namespace
Attr.cloneNode: NamespaceErr: cannot use the prefix 'a' with an empty namespace

Expected, as in 3.15 and earlier:

cloneNode: root
importNode: root
Attr.cloneNode: a:b

_clone_node() creates the clones with createElementNS(), setAttributeNS() and createAttributeNS(), and these now reject a prefixed name when the namespace is empty. A plain setAttribute("xmlns", ...) fails the same way, because the xmlns attribute is only allowed in the XMLNS namespace.

CPython versions tested on:

CPython main branch (3.13 and 3.12 are not affected)

Operating systems tested on:

Windows

Linked PRs

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibStandard Library Python modules in the Lib/ directorytopic-XMLtype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions