Thanks to visit codestin.com
Credit goes to github.com

Skip to content

gh-44376: Write missing namespace declarations in xml.dom.minidom - #156674

Merged
serhiy-storchaka merged 3 commits into
python:mainfrom
serhiy-storchaka:gh-44376-ns-fixup-a
Sep 13, 2026
Merged

gh-44376: Write missing namespace declarations in xml.dom.minidom#156674
serhiy-storchaka merged 3 commits into
python:mainfrom
serhiy-storchaka:gh-44376-ns-fixup-a

Conversation

@serhiy-storchaka

@serhiy-storchaka serhiy-storchaka commented Aug 30, 2026

Copy link
Copy Markdown
Member

Element.writexml() now writes the xmlns declarations needed to serialize the namespaces of the element and its attributes, if they are not already declared for an ancestor. A prefix is invented for a namespaced attribute without a prefix, because attributes cannot use the default namespace. The document is not modified by the serialization.

>>> doc = parseString("<doc/>")
>>> elem = doc.createElementNS("http://xml.python.org/ns", "p:elem")
>>> doc.documentElement.appendChild(elem)
>>> doc.documentElement.toxml()
'<doc><p:elem xmlns:p="http://xml.python.org/ns"/></doc>'

The namespaces in scope are passed down the recursion as a keyword-only argument. They are passed only to the standard implementation of writexml(); an overridden method is called with the documented signature, and computes them by walking the ancestors of the element.

Element.writexml() now emits the xmlns declarations needed to serialize the
namespaces of the element and its attributes, if they are not already declared
for an ancestor.  A prefix is invented for a namespaced attribute without a
prefix, because attributes cannot use the default namespace.  The document is
not modified by the serialization.

The namespaces in scope are passed down the recursion as a keyword-only
argument.  They are passed only to the standard implementation of writexml();
an overridden method is called with the documented signature and computes them
by walking the ancestors of the element.
@read-the-docs-community

read-the-docs-community Bot commented Aug 30, 2026

Copy link
Copy Markdown

A prefix is only invented if no prefix in scope is bound to the namespace
of the attribute.  The reverse mapping is built only when such attribute
is encountered, so there is no cost in the common case.  The counter of
invented prefixes is not reset for every attribute.
@serhiy-storchaka
serhiy-storchaka merged commit da5a155 into python:main Sep 13, 2026
54 checks passed
@serhiy-storchaka
serhiy-storchaka deleted the gh-44376-ns-fixup-a branch September 13, 2026 17:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant