Hello Mr Veillard,
Instead of parsing a separate document, I would rather create new elements directly.
You have a token to search in libxml2.py, a snippet ? It's not critic because it works, but if it can be better...
But it's hard to guess if it makes the code simpler ornot.
I can't figure how complex it could be implemented. Msxml have a node.xml property <http://msdn2.microsoft.com/en-us/library/ms755989.aspx> (read only), for HTML there's the very handy node.innerHTML (read and write).
You need to unlink the nodes from the origin document otherwise when you do the freeDoc() those will be freed and you end up returning pointers to freed elements, so this looks right.
From a user point of view, this was quite tricky to understand. I haven't experienced memory leak without freeDoc(), but has different very strange outputs without unlink after freeDoc().
-- Frédéric Glorieux <http://fictif.org>