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