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