1 | #!/usr/bin/python -u
|
---|
2 | import libxml2
|
---|
3 | import sys
|
---|
4 |
|
---|
5 | # Memory debug specific
|
---|
6 | libxml2.debugMemory(1)
|
---|
7 |
|
---|
8 | schema="""<?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 |
|
---|
22 | instance="""<?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 |
|
---|
30 | ctxt_parser = libxml2.schemaNewMemParserCtxt(schema, len(schema))
|
---|
31 | ctxt_schema = ctxt_parser.schemaParse()
|
---|
32 | ctxt_valid = ctxt_schema.schemaNewValidCtxt()
|
---|
33 | doc = libxml2.parseDoc(instance)
|
---|
34 | ret = doc.schemaValidateDoc(ctxt_valid)
|
---|
35 | if ret != 0:
|
---|
36 | print("error doing schema validation")
|
---|
37 | sys.exit(1)
|
---|
38 |
|
---|
39 | doc.freeDoc()
|
---|
40 | del ctxt_parser
|
---|
41 | del ctxt_schema
|
---|
42 | del ctxt_valid
|
---|
43 | libxml2.schemaCleanupTypes()
|
---|
44 |
|
---|
45 | # Memory debug specific
|
---|
46 | libxml2.cleanupParser()
|
---|
47 | if libxml2.debugMemory(1) == 0:
|
---|
48 | print("OK")
|
---|
49 | else:
|
---|
50 | print("Memory leak %d bytes" % (libxml2.debugMemory(1)))
|
---|
51 | libxml2.dumpMemory()
|
---|
52 |
|
---|