summaryrefslogtreecommitdiffstats
path: root/Lib/xml
diff options
context:
space:
mode:
authorMartin v. Löwis <martin@v.loewis.de>2007-02-12 12:21:10 (GMT)
committerMartin v. Löwis <martin@v.loewis.de>2007-02-12 12:21:10 (GMT)
commit2bad58f5a4caef933e3e404ff6a213a0f499614c (patch)
tree5e97a09fdf5b035322469d61b5f266c9c4bf684f /Lib/xml
parenta69aa327aaac8c9156d67b16a81d6b082ba5b012 (diff)
downloadcpython-2bad58f5a4caef933e3e404ff6a213a0f499614c.zip
cpython-2bad58f5a4caef933e3e404ff6a213a0f499614c.tar.gz
cpython-2bad58f5a4caef933e3e404ff6a213a0f499614c.tar.bz2
Patch 1463026: Support default namespace in XMLGenerator.
Fixes #847665. Will backport.
Diffstat (limited to 'Lib/xml')
-rw-r--r--Lib/xml/sax/saxutils.py35
1 files changed, 19 insertions, 16 deletions
diff --git a/Lib/xml/sax/saxutils.py b/Lib/xml/sax/saxutils.py
index a496519..46818f3 100644
--- a/Lib/xml/sax/saxutils.py
+++ b/Lib/xml/sax/saxutils.py
@@ -100,6 +100,17 @@ class XMLGenerator(handler.ContentHandler):
else:
self._out.write(text.encode(self._encoding, _error_handling))
+ def _qname(self, name):
+ """Builds a qualified name from a (ns_url, localname) pair"""
+ if name[0]:
+ # The name is in a non-empty namespace
+ prefix = self._current_context[name[0]]
+ if prefix:
+ # If it is not the default namespace, prepend the prefix
+ return prefix + ":" + name[1]
+ # Return the unqualified name
+ return name[1]
+
# ContentHandler methods
def startDocument(self):
@@ -125,29 +136,21 @@ class XMLGenerator(handler.ContentHandler):
self._write('</%s>' % name)
def startElementNS(self, name, qname, attrs):
- if name[0] is None:
- # if the name was not namespace-scoped, use the unqualified part
- name = name[1]
- else:
- # else try to restore the original prefix from the namespace
- name = self._current_context[name[0]] + ":" + name[1]
- self._write('<' + name)
+ self._write('<' + self._qname(name))
- for pair in self._undeclared_ns_maps:
- self._write(' xmlns:%s="%s"' % pair)
+ for prefix, uri in self._undeclared_ns_maps:
+ if prefix:
+ self._out.write(' xmlns:%s="%s"' % (prefix, uri))
+ else:
+ self._out.write(' xmlns="%s"' % uri)
self._undeclared_ns_maps = []
for (name, value) in attrs.items():
- name = self._current_context[name[0]] + ":" + name[1]
- self._write(' %s=%s' % (name, quoteattr(value)))
+ self._write(' %s=%s' % (self._qname(name), quoteattr(value)))
self._write('>')
def endElementNS(self, name, qname):
- if name[0] is None:
- name = name[1]
- else:
- name = self._current_context[name[0]] + ":" + name[1]
- self._write('</%s>' % name)
+ self._write('</%s>' % self._qname(name))
def characters(self, content):
self._write(escape(content))