Skip to content

xml.dom.minidom: writexml() treats a setAttribute("xmlns:p", ...) attribute as a declaration of the default namespace #158984

Description

@sricursion

Bug report

Bug description:

Since gh-44376, Node.writexml() (and so toxml() and toprettyxml()) of xml.dom.minidom writes the namespace declarations that are missing from the document. An attribute created with the DOM Level 1 API (setAttribute(), createAttribute(), or a document parsed without namespace processing) has prefix set to None even if its name is xmlns:p. _fixup_namespaces() takes the declared prefix from Attr.prefix, so it records such an attribute as a declaration of the default namespace instead of the prefix p.

from xml.dom import expatbuilder, minidom

NS = "urn:p"
XSD = "http://www.w3.org/2001/XMLSchema"

# 1. The prefix of the element is declared with setAttribute()
doc = minidom.Document()
root = doc.appendChild(doc.createElementNS(NS, "p:root"))
root.setAttribute("xmlns:p", NS)
print(doc.toxml())

# 2. A prefix is declared on an element in no namespace
doc = minidom.Document()
root = doc.appendChild(doc.createElement("root"))
root.setAttribute("xmlns:xsi", "urn:xsi")
print(doc.toxml())

# 3. A prefix is bound to the namespace of the element
doc = minidom.Document()
schema = doc.appendChild(doc.createElementNS(XSD, "schema"))
schema.setAttribute("xmlns:x", XSD)
out = doc.toxml()
print(out)
print(minidom.parseString(out).documentElement.namespaceURI)

# 4. A document parsed without namespace processing
src = '<root xmlns:p="urn:p"><p:child/><other/></root>'
dom = expatbuilder.parseString(src, namespaces=False)
print(dom.documentElement.toxml())

Output on main:

<?xml version="1.0" ?><p:root xmlns:p="urn:p" xmlns:p="urn:p"/>
<?xml version="1.0" ?><root xmlns="" xmlns:xsi="urn:xsi"/>
<?xml version="1.0" ?><schema xmlns:x="http://www.w3.org/2001/XMLSchema"/>
None
<root xmlns="" xmlns:p="urn:p"><p:child/><other/></root>

Expected output:

<?xml version="1.0" ?><p:root xmlns:p="urn:p"/>
<?xml version="1.0" ?><root xmlns:xsi="urn:xsi"/>
<?xml version="1.0" ?><schema xmlns="http://www.w3.org/2001/XMLSchema" xmlns:x="http://www.w3.org/2001/XMLSchema"/>
http://www.w3.org/2001/XMLSchema
<root xmlns:p="urn:p"><p:child/><other/></root>

The first result has a duplicate attribute and is not well-formed. The second and the fourth get a spurious xmlns="". In the third, the element is considered to have its namespace declared as the default one already, so it is written without a declaration and loses its namespace when parsed again.

For cases 1, 2 and 4, 3.13 (before gh-44376) writes the expected output, so these are regressions. Case 3 does not work there either, but it is what gh-44376 is meant to handle.

The prefix should be derived from the attribute name, as is done for the rest of the declarations in that function.

This is related to gh-158208, but has a different cause: that one is about a plain xmlns attribute on an element in no namespace, and the proposed fixes for it do not change the results above.

CPython versions tested on:

CPython main branch

Operating systems tested on:

Windows

Linked PRs

Activity

  1. added
    type-bugAn unexpected behavior, bug, or error
    stdlibStandard Library Python modules in the Lib/ directory
    on Oct 8, 2026
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