1 #!/usr/bin/python -u 2 import sys 3 import libxml2 4 5 # Memory debug specific 6 libxml2.debugMemory(1) 7 8 # 9 # Testing XML document serialization 10 # 11 source = libxml2.parseDoc("""<?xml version="1.0"?> 12 <root xmlns:foo="http://example.org/foo" 13 xmlns:bar="http://example.org/bar"> 14 <include xmlns="http://example.org/include"> 15 <fragment><foo:elem bar="tricky"/></fragment> 16 </include> 17 </root> 18 """) 19 20 target = libxml2.parseDoc("""<?xml version="1.0"?> 21 <root xmlns:foobar="http://example.org/bar"/>""") 22 23 fragment = source.xpathEval("//*[name()='fragment']")[0] 24 dest = target.getRootElement() 25 26 # do a cut and paste operation 27 fragment.unlinkNode() 28 dest.addChild(fragment) 29 # do the namespace fixup 30 dest.reconciliateNs(target) 31 32 # The source tree can be freed at that point 33 source.freeDoc() 34 35 # check the resulting tree 36 str = dest.serialize() 37 if str != """<root xmlns:foobar="http://example.org/bar" xmlns:default="http://example.org/include" xmlns:foo="http://example.org/foo"><default:fragment><foo:elem bar="tricky"/></default:fragment></root>""": 38 print("reconciliateNs() failed") 39 sys.exit(1) 40 target.freeDoc() 41 42 # Memory debug specific 43 libxml2.cleanupParser() 44 if libxml2.debugMemory(1) == 0: 45 print("OK") 46 else: 47 print("Memory leak %d bytes" % (libxml2.debugMemory(1))) 48 libxml2.dumpMemory() 49