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