Skip to content

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

Open
serhiy-storchaka wants to merge 1 commit into
python:mainfrom
serhiy-storchaka:gh-44376-ns-fixup-a
Open

gh-44376: Write missing namespace declarations in xml.dom.minidom#156674
serhiy-storchaka wants to merge 1 commit 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

Copy link
Copy Markdown

Documentation build overview

📚 cpython-previews | 🛠️ Build #34300776 | 📁 Comparing 6a76a10 against main (852381e)

  🔍 Preview build  

2 files changed
± library/xml.dom.minidom.html
± whatsnew/changelog.html

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant