Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions Lib/test/test_minidom.py
Original file line number Diff line number Diff line change
Expand Up @@ -702,6 +702,28 @@ def testWriteXMLNotANamespaceDeclaration(self):
'<child xmlns="" xmlnsabc="v"/></root>')
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(),
'<svg xmlns="http://www.w3.org/2000/svg"><g/></svg>')
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(),
'<root xmlns="http://xml.python.org/ns">'
'<svg xmlns="http://www.w3.org/2000/svg"><g/></svg></root>')
dom.unlink()

def testWriteXMLDoesNotModifyDocument(self):
dom = Document()
root = dom.appendChild(
Expand Down
3 changes: 2 additions & 1 deletion Lib/xml/dom/minidom.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Loading