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
49 changes: 49 additions & 0 deletions Lib/test/test_minidom.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from test import support
import unittest

import xml.dom.expatbuilder
import xml.dom.minidom

from xml.dom.minidom import parse, Attr, Node, Document, Element, parseString
Expand Down Expand Up @@ -683,6 +684,54 @@ def testWriteXMLExistingNamespaceDeclarations(self):
self.assertEqual(dom.documentElement.toxml(), str)
dom.unlink()

def testWriteXMLDOM1PrefixDeclaration(self):
# setAttribute() does not set the prefix of the attribute,
# but "xmlns:p" still declares the prefix "p".
dom = Document()
root = dom.appendChild(
dom.createElementNS("http://xml.python.org/ns", "p:root"))
root.setAttribute("xmlns:p", "http://xml.python.org/ns")
domstr = dom.documentElement.toxml()
dom.unlink()
self.assertEqual(domstr,
'<p:root xmlns:p="http://xml.python.org/ns"/>')
dom = parseString(domstr)
self.assertEqual(dom.documentElement.namespaceURI,
"http://xml.python.org/ns")
dom.unlink()

def testWriteXMLDOM1PrefixDeclarationNoNamespace(self):
# A prefix declaration is not a declaration of the default namespace.
dom = Document()
root = dom.appendChild(dom.createElement("root"))
root.setAttribute("xmlns:xsi", "http://xml.python.org/ns")
self.assertEqual(dom.documentElement.toxml(),
'<root xmlns:xsi="http://xml.python.org/ns"/>')
dom.unlink()

def testWriteXMLDOM1PrefixDeclarationDefaultNamespace(self):
# The element still needs a declaration of its own default namespace.
dom = Document()
root = dom.appendChild(
dom.createElementNS("http://xml.python.org/ns", "root"))
root.setAttribute("xmlns:x", "http://xml.python.org/ns")
domstr = dom.documentElement.toxml()
dom.unlink()
self.assertEqual(domstr,
'<root xmlns="http://xml.python.org/ns" '
'xmlns:x="http://xml.python.org/ns"/>')
dom = parseString(domstr)
self.assertEqual(dom.documentElement.namespaceURI,
"http://xml.python.org/ns")
dom.unlink()

def testWriteXMLParsedWithoutNamespaces(self):
src = ('<root xmlns:p="http://xml.python.org/ns">'
'<p:child/><other/></root>')
dom = xml.dom.expatbuilder.parseString(src, namespaces=False)
self.assertEqual(dom.documentElement.toxml(), src)
dom.unlink()

def testWriteXMLNotANamespaceDeclaration(self):
# an attribute whose name only starts with "xmlns" is not one
dom = parseString('<root xmlns="http://xml.python.org/ns">'
Expand Down
2 changes: 1 addition & 1 deletion Lib/xml/dom/minidom.py
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,7 @@ def _fixup_namespaces(element, nsmap):
# Declarations already present in the document take precedence.
nsmap = _bind_namespace(
nsmap, inherited,
attr.localName if attr.prefix else None, attr.value)
attr.localName if ":" in name else None, attr.value)
attr_uri = None
elif attr_uri == XML_NAMESPACE:
# The xml prefix is bound by definition.
Expand Down
Loading