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