diff --git a/Lib/test/test_minidom.py b/Lib/test/test_minidom.py index 446bbe096bd19dc..8b213f3437bda35 100644 --- a/Lib/test/test_minidom.py +++ b/Lib/test/test_minidom.py @@ -702,6 +702,28 @@ def testWriteXMLNotANamespaceDeclaration(self): '') dom.unlink() + def testWriteXMLOwnDefaultNamespaceDeclaration(self): + # An element in no namespace which declares the default namespace + # itself is written with that declaration only. + dom = Document() + svg = dom.appendChild(dom.createElement("svg")) + svg.setAttribute("xmlns", "http://www.w3.org/2000/svg") + svg.appendChild(dom.createElement("g")) + self.assertEqual(dom.documentElement.toxml(), + '') + dom.unlink() + + dom = Document() + root = dom.appendChild( + dom.createElementNS("http://xml.python.org/ns", "root")) + svg = root.appendChild(dom.createElement("svg")) + svg.setAttribute("xmlns", "http://www.w3.org/2000/svg") + svg.appendChild(dom.createElement("g")) + self.assertEqual(dom.documentElement.toxml(), + '' + '') + dom.unlink() + def testWriteXMLDoesNotModifyDocument(self): dom = Document() root = dom.appendChild( diff --git a/Lib/xml/dom/minidom.py b/Lib/xml/dom/minidom.py index 7cb652a323dcc22..652e0365efcc67e 100644 --- a/Lib/xml/dom/minidom.py +++ b/Lib/xml/dom/minidom.py @@ -449,7 +449,8 @@ def _fixup_namespaces(element, nsmap): elif nsmap.get(None) and ':' not in element.tagName: # The element is in no namespace, undeclare the default one. nsmap = _bind_namespace(nsmap, inherited, None, None) - declarations.append(("xmlns", "")) + if not element.hasAttribute("xmlns"): + declarations.append(("xmlns", "")) items = [] prefixes = None # namespace URI -> prefix, built only when needed diff --git a/Misc/NEWS.d/next/Library/2026-11-03-17-53-55.gh-issue-158208.ZOQnym.rst b/Misc/NEWS.d/next/Library/2026-11-03-17-53-55.gh-issue-158208.ZOQnym.rst new file mode 100644 index 000000000000000..424f2338e2af8ba --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-11-03-17-53-55.gh-issue-158208.ZOQnym.rst @@ -0,0 +1,2 @@ +Fix :mod:`xml.dom.minidom` writing a duplicate ``xmlns`` attribute for an +element in no namespace that has its own ``xmlns`` attribute.