--- tree.c.orig 2006-02-20 14:51:28.428448035 +1300 +++ tree.c 2006-02-20 16:05:42.781217208 +1300 @@ -3872,7 +3872,10 @@ if (node->ns != NULL) { xmlNsPtr ns; - ns = xmlSearchNs(doc, ret, node->ns->prefix); + if (node->ns->prefix == NULL || node->doc != doc) + ns = NULL; + else + ns = xmlSearchNs(doc, ret, node->ns->prefix); if (ns == NULL) { /* * Humm, we are copying an element whose namespace is defined @@ -3883,9 +3886,13 @@ if (ns != NULL) { xmlNodePtr root = ret; - while (root->parent != NULL) root = root->parent; + while (root->parent != NULL && + root->parent->type == XML_ELEMENT_NODE) + root = root->parent; ret->ns = xmlNewNs(root, ns->href, ns->prefix); } + else + ret->ns = xmlNewNs(NULL, node->ns->href, node->ns->prefix); } else { /* * reference the existing namespace definition in our own tree.