changeset 28:21cef3864c33

Add namespace list to XML serializer.
author Bastian Blank <bblank@thinkmo.de>
date Mon, 23 Jun 2008 14:53:58 +0200
parents 4dc29d2eac0e
children 3a9710b7a785
files emeraldtree/ElementTree.py
diffstat 1 files changed, 12 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/emeraldtree/ElementTree.py	Thu Jun 12 20:57:22 2008 +0200
+++ b/emeraldtree/ElementTree.py	Mon Jun 23 14:53:58 2008 +0200
@@ -658,7 +658,8 @@
               encoding="us-ascii",
               xml_declaration=None,
               default_namespace=None,
-              method=None):
+              method=None,
+              namespaces={}):
         assert self._root is not None
         if not hasattr(file, "write"):
             file = open(file, "wb")
@@ -674,7 +675,7 @@
             _serialize_text(write, self._root, encoding)
         else:
             qnames, namespaces = _namespaces(
-                self._root, encoding, default_namespace
+                self._root, encoding, default_namespace, namespaces
                 )
             if method == "xml":
                 _serialize_xml(
@@ -690,16 +691,18 @@
 # --------------------------------------------------------------------
 # serialization support
 
-def _namespaces(elem, encoding, default_namespace = None):
+def _namespaces(elem, encoding, default_namespace, namespaces):
     # identify namespaces used in this tree
 
     # maps qnames to *encoded* prefix:local names
     qnames = {None: None}
 
     # maps uri:s to prefixes
-    namespaces = {}
+    candidate_namespaces = _namespace_map.copy()
+    candidate_namespaces.update(namespaces)
     if default_namespace:
-        namespaces[default_namespace] = ""
+        candidate_namespaces[default_namespace] = ""
+    used_namespaces = {}
 
     def encode(text):
         return text.encode(encoding)
@@ -712,13 +715,13 @@
         try:
             if qname.uri is not None:
                 uri = qname.uri
-                prefix = namespaces.get(uri, None)
+                prefix = used_namespaces.get(uri, None)
                 if prefix is None:
-                    prefix = _namespace_map.get(uri)
+                    prefix = candidate_namespaces.get(uri, None)
                     if prefix is None:
                         prefix = "ns%d" % len(namespaces)
                     if prefix != "xml":
-                        namespaces[uri] = prefix
+                        used_namespaces[uri] = prefix
                 if prefix:
                     qnames[qname] = encode("%s:%s" % (prefix, qname.name))
                 else:
@@ -754,7 +757,7 @@
                     elif key is not None:
                         _raise_serialization_error(key)
 
-    return qnames, namespaces
+    return qnames, used_namespaces
 
 def _serialize_xml(write, elem, encoding, qnames, namespaces):
     if isinstance(elem, Comment):