• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/python -u
2import libxml2
3import sys
4
5# Memory debug specific
6libxml2.debugMemory(1)
7
8schema="""<?xml version="1.0" encoding="iso-8859-1"?>
9<schema xmlns = "http://www.w3.org/2001/XMLSchema">
10	<element name = "Customer">
11		<complexType>
12			<sequence>
13				<element name = "FirstName" type = "string" />
14				<element name = "MiddleInitial" type = "string" />
15				<element name = "LastName" type = "string" />
16			</sequence>
17			<attribute name = "customerID" type = "integer" />
18		</complexType>
19	</element>
20</schema>"""
21
22instance="""<?xml version="1.0" encoding="iso-8859-1"?>
23<Customer customerID = "24332">
24	<FirstName>Raymond</FirstName>
25	<MiddleInitial>G</MiddleInitial>
26	<LastName>Bayliss</LastName>
27</Customer>
28"""
29
30ctxt_parser = libxml2.schemaNewMemParserCtxt(schema, len(schema))
31ctxt_schema = ctxt_parser.schemaParse()
32ctxt_valid  = ctxt_schema.schemaNewValidCtxt()
33doc = libxml2.parseDoc(instance)
34ret = doc.schemaValidateDoc(ctxt_valid)
35if ret != 0:
36    print("error doing schema validation")
37    sys.exit(1)
38
39doc.freeDoc()
40del ctxt_parser
41del ctxt_schema
42del ctxt_valid
43libxml2.schemaCleanupTypes()
44
45# Memory debug specific
46libxml2.cleanupParser()
47if libxml2.debugMemory(1) == 0:
48    print("OK")
49else:
50    print("Memory leak %d bytes" % (libxml2.debugMemory(1)))
51    libxml2.dumpMemory()
52
53