On 05.03.06 21:46:33, Stefan Behnel wrote:
Andreas Pakulat wrote:
On 05.03.06 13:00:49, Stefan Behnel wrote:
Here is a trivial patch that simply calls the function after having copied an element between documents.
If I understand that correctly it should also work if I create a new Element (with a namespace) and insert it as child right?
If that is correct, than this doesn't help. I still get a an extra ns declaration:
print etree.tostring(tree) <elem xmlns="test"><subelem/></elem> [25296 refs] tree.append(etree.Element("{test}sub1")) [25296 refs] print etree.tostring(tree) <elem xmlns="test"><subelem/><ns0:sub1 xmlns:ns0="test"/></elem> [25296 refs] tree.append(etree.Element("{test}sub2")) [25296 refs] print etree.tostring(tree) <elem xmlns="test"><subelem/><ns0:sub1 xmlns:ns0="test"/><ns0:sub2 xmlns:ns0="test"/></elem>
If I'm not mistaken, this is the expected behaviour from my patch. The problem is that it only fixes the tree of the element itself, not the entire tree. If you added the tree itself to a new tree, it should fix the current douplication of namespaces that you saw.
So the following should not happen, if I understand you correctly?
from lxml.etree import * [25180 refs] doc = fromstring("<elem xmlns=\"test\" />") [25226 refs] doc.append("{test}sub") Traceback (most recent call last): File "<stdin>", line 1, in ? File "etree.pyx", line 397, in etree._Element.append TypeError: Argument 'element' has incorrect type (expected etree._Element, got str) [25278 refs] doc.append(Element("{test}sub")) [25278 refs] tostring(doc) '<elem xmlns="test"><ns0:sub xmlns:ns0="test"/></elem>' [25280 refs] doc2 = fromstring("<main />") [25284 refs] doc2.append(doc) [25284 refs] tostring(doc2) '<main><elem xmlns="test"><sub xmlns:ns0="test"/></elem></main>' [25284 refs]
However this works:
doc = fromstring("<ns0:elem xmlns:ns0=\"test\" />") [25285 refs] doc.append(Element("{test}sub")) [25285 refs] tostring(doc) '<ns0:elem xmlns:ns0="test"><ns0:sub/></ns0:elem>' [25285 refs]
So at least something works (my system lxml doesn't show this behaviour). However I think this is the normal ns-cleanup working and it doesn't fix the bug I reported with libxml... Andreas -- You have an unusual equipment for success. Be sure to use it properly.