schema.py 1.3 KB

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