From add14fcde12ebf1ce7fc18396492b2312b125c5d Mon Sep 17 00:00:00 2001 From: Tony123-tech Date: Sat, 26 Sep 2026 11:47:03 +0800 Subject: [PATCH 1/3] gh-158208: Don't write a duplicate xmlns attribute in xml.dom.minidom Element.writexml() added an empty xmlns="" declaration to elements in no namespace even when the element had an explicit xmlns attribute, producing output that is not well-formed. Skip the empty declaration if the element itself declares a default namespace via setAttribute("xmlns", ...). --- Lib/test/test_minidom.py | 15 +++++++++++++++ Lib/xml/dom/minidom.py | 7 ++++++- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/Lib/test/test_minidom.py b/Lib/test/test_minidom.py index 446bbe096bd19d..d0310bf41b6a3b 100644 --- a/Lib/test/test_minidom.py +++ b/Lib/test/test_minidom.py @@ -612,6 +612,21 @@ def testWriteXMLDefaultNamespace(self): '') dom.unlink() + def testWriteXMLNoDuplicateXmlns(self): + # gh-158208: setting an explicit xmlns attribute must not + # result in a duplicate xmlns declaration in the output. + dom = Document() + svg = dom.appendChild(dom.createElement("svg")) + svg.setAttribute("xmlns", "https://www.w3.org/2000/svg") + xml = dom.toxml() + self.assertEqual( + xml, + '' + ) + # The result must be well-formed XML. + parseString(xml) + dom.unlink() + def testWriteXMLAttributeNamespacePrefix(self): dom = Document() root = dom.appendChild(dom.createElement("root")) diff --git a/Lib/xml/dom/minidom.py b/Lib/xml/dom/minidom.py index 7cb652a323dcc2..afddc72eb24ab9 100644 --- a/Lib/xml/dom/minidom.py +++ b/Lib/xml/dom/minidom.py @@ -424,6 +424,7 @@ def _fixup_namespaces(element, nsmap): declarations = [] # (name, value, namespace URI, attribute) of the attributes to write. entries = [] + has_own_xmlns = False if attrs: for attr in attrs.values(): name = attr.name @@ -435,6 +436,8 @@ def _fixup_namespaces(element, nsmap): nsmap, inherited, attr.localName if attr.prefix else None, attr.value) attr_uri = None + if name == "xmlns": + has_own_xmlns = True elif attr_uri == XML_NAMESPACE: # The xml prefix is bound by definition. attr_uri = None @@ -446,8 +449,10 @@ def _fixup_namespaces(element, nsmap): if nsmap.get(prefix) != uri: nsmap = _bind_namespace(nsmap, inherited, prefix, uri) declarations.append(("xmlns:" + prefix if prefix else "xmlns", uri)) - elif nsmap.get(None) and ':' not in element.tagName: + elif (nsmap.get(None) and ':' not in element.tagName + and not has_own_xmlns): # The element is in no namespace, undeclare the default one. + # Don't undeclare if the element itself declared xmlns. nsmap = _bind_namespace(nsmap, inherited, None, None) declarations.append(("xmlns", "")) From 0e26f8293de7da47f0ed3331a5f0d3d9fda77d30 Mon Sep 17 00:00:00 2001 From: Tony123-tech Date: Sat, 26 Sep 2026 11:54:09 +0800 Subject: [PATCH 2/3] gh-158208: Add NEWS entry --- .../Library/2026-09-26-11-53-32.gh-issue-158208.AbCdEf.rst | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2026-09-26-11-53-32.gh-issue-158208.AbCdEf.rst diff --git a/Misc/NEWS.d/next/Library/2026-09-26-11-53-32.gh-issue-158208.AbCdEf.rst b/Misc/NEWS.d/next/Library/2026-09-26-11-53-32.gh-issue-158208.AbCdEf.rst new file mode 100644 index 00000000000000..b397ade17414f3 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-09-26-11-53-32.gh-issue-158208.AbCdEf.rst @@ -0,0 +1,3 @@ +Fix :mod:`xml.dom.minidom` to not write a duplicate ``xmlns`` attribute +when an element has an explicit ``xmlns`` attribute set with +:meth:`~xml.dom.Element.setAttribute`. \ No newline at end of file From 2ac603b18cc102627609f68e412a89d14f01d8e9 Mon Sep 17 00:00:00 2001 From: Tony123-tech Date: Sat, 26 Sep 2026 11:55:39 +0800 Subject: [PATCH 3/3] gh-158208: Fix missing final newline in NEWS entry --- .../next/Library/2026-09-26-11-53-32.gh-issue-158208.AbCdEf.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Library/2026-09-26-11-53-32.gh-issue-158208.AbCdEf.rst b/Misc/NEWS.d/next/Library/2026-09-26-11-53-32.gh-issue-158208.AbCdEf.rst index b397ade17414f3..d9413356c37fcd 100644 --- a/Misc/NEWS.d/next/Library/2026-09-26-11-53-32.gh-issue-158208.AbCdEf.rst +++ b/Misc/NEWS.d/next/Library/2026-09-26-11-53-32.gh-issue-158208.AbCdEf.rst @@ -1,3 +1,3 @@ Fix :mod:`xml.dom.minidom` to not write a duplicate ``xmlns`` attribute when an element has an explicit ``xmlns`` attribute set with -:meth:`~xml.dom.Element.setAttribute`. \ No newline at end of file +:meth:`~xml.dom.Element.setAttribute`.