cutnpaste.py 1.3 KB

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