12
def handler(self, msg, data):
14
raise Exception, "Error handler did not receive correct argument"
15
self.errors.append(msg)
17
# Memory debug specific
18
libxml2.debugMemory(1)
20
schema="""<?xml version="1.0" encoding="iso-8859-1"?>
21
<schema xmlns = "http://www.w3.org/2001/XMLSchema">
22
<element name = "Customer">
25
<element name = "FirstName" type = "string" />
26
<element name = "MiddleInitial" type = "string" />
27
<element name = "LastName" type = "string" />
29
<attribute name = "customerID" type = "integer" />
34
valid="""<?xml version="1.0" encoding="iso-8859-1"?>
35
<Customer customerID = "24332">
36
<FirstName>Raymond</FirstName>
37
<MiddleInitial>G</MiddleInitial>
38
<LastName>Bayliss</LastName>
42
invalid="""<?xml version="1.0" encoding="iso-8859-1"?>
43
<Customer customerID = "24332">
44
<MiddleInitial>G</MiddleInitial>
45
<LastName>Bayliss</LastName>
50
ctxt_parser = libxml2.schemaNewMemParserCtxt(schema, len(schema))
51
ctxt_schema = ctxt_parser.schemaParse()
52
ctxt_valid = ctxt_schema.schemaNewValidCtxt()
53
ctxt_valid.setValidityErrorHandler(e.handler, e.handler, ARG)
56
doc = libxml2.parseDoc(valid)
57
ret = doc.schemaValidateDoc(ctxt_valid)
58
if ret != 0 or e.errors:
59
print "error doing schema validation"
63
# Test invalid document
64
doc = libxml2.parseDoc(invalid)
65
ret = doc.schemaValidateDoc(ctxt_valid)
66
if ret == 0 or not e.errors:
67
print "Error: document supposer to be schema invalid"
74
libxml2.schemaCleanupTypes()
76
# Memory debug specific
77
libxml2.cleanupParser()
78
if libxml2.debugMemory(1) == 0:
81
print "Memory leak %d bytes" % (libxml2.debugMemory(1))