just a quick status update.
I committed a patch to the trunk (24115) that basically refactors the above functions and the _addNamespaces function. The two are the main API functions (and the oldest grown ones around, I guess), so as I was running a few benchmarks anyway, I wanted to check if I could come up with some tweaks to make them faster. I took a closer look at them and found a couple of potential bugs and inefficient indirections. The result is that (I think) they should be more straight forward to read now and about 10-30% faster according to my benchmark.
One of the problems was that name and namespace of the new element were set twice. That was actually good, as they were set incorrectly the first time, so that bug was masked. However, that was neither very readable nor efficient. The new implementation moves the namespace handling and prefix setting into the _addNamespaces function (which I renamed to _setNamespaces). It is therefore combined with the namespace setup from the nsmap dictionary, so that the prefixes and the C namespace structure only have to be generated once.
The test cases pass just as before. I also added a few new ones whenever I found sequences of code that made me wonder. So I can somewhat hope I didn't replace the old bugs by new ones...
Have fun, Stefan