[Lxml-checkins] r39359 - lxml/branch/nscleanup/src/lxml

scoder at codespeak.net scoder at codespeak.net
Sat Feb 24 17:02:46 CET 2007


Author: scoder
Date: Sat Feb 24 17:02:41 2007
New Revision: 39359

Modified:
   lxml/branch/nscleanup/src/lxml/proxy.pxi
Log:
let _fakeRootDoc() copy the parent namespaces if not declared in the document

Modified: lxml/branch/nscleanup/src/lxml/proxy.pxi
==============================================================================
--- lxml/branch/nscleanup/src/lxml/proxy.pxi	(original)
+++ lxml/branch/nscleanup/src/lxml/proxy.pxi	Sat Feb 24 17:02:41 2007
@@ -55,6 +55,7 @@
     c_doc  = _copyDoc(c_base_doc, 0)                   # non recursive!
     c_new_root = tree.xmlDocCopyNode(c_node, c_doc, 2) # non recursive!
     tree.xmlDocSetRootElement(c_doc, c_new_root)
+    _copyParentNamespaces(c_node, c_new_root)
 
     c_new_root.children = c_node.children
     c_new_root.last = c_node.last
@@ -91,6 +92,26 @@
         c_root.children = c_root.last = NULL
         tree.xmlFreeDoc(c_doc)
 
+cdef void _copyParentNamespaces(xmlNode* c_from_node, xmlNode* c_to_node):
+    """Copy the namespaces of all ancestors of c_from_node to c_to_node.
+
+    This is used in _fakeRootDoc() to avoid loosing namespace declarations.
+    """
+    cdef xmlNode* c_parent
+    cdef xmlNs* c_ns
+    cdef xmlNs* c_new_ns
+    cdef int prefix_known
+    c_parent = c_from_node.parent
+    while c_parent is not NULL and tree._isElementOrXInclude(c_parent):
+        c_new_ns = c_parent.nsDef
+        while c_new_ns is not NULL:
+            # check if prefix is already defined
+            c_ns = tree.xmlSearchNs(c_to_node.doc, c_to_node, c_new_ns.prefix)
+            if c_ns is NULL:
+                tree.xmlNewNs(c_to_node, c_new_ns.href, c_new_ns.prefix)
+            c_new_ns = c_new_ns.next
+        c_parent = c_parent.parent
+
 ################################################################################
 # support for freeing tree elements when proxy objects are destroyed
 


More information about the lxml-checkins mailing list