relaxng.py 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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"?>
  7. <element name="foo"
  8. xmlns="http://relaxng.org/ns/structure/1.0"
  9. xmlns:a="http://relaxng.org/ns/annotation/1.0"
  10. xmlns:ex1="http://www.example.com/n1"
  11. xmlns:ex2="http://www.example.com/n2">
  12. <a:documentation>A foo element.</a:documentation>
  13. <element name="ex1:bar1">
  14. <empty/>
  15. </element>
  16. <element name="ex2:bar2">
  17. <empty/>
  18. </element>
  19. </element>
  20. """
  21. instance="""<?xml version="1.0"?>
  22. <foo><pre1:bar1 xmlns:pre1="http://www.example.com/n1"/><pre2:bar2 xmlns:pre2="http://www.example.com/n2"/></foo>"""
  23. rngp = libxml2.relaxNGNewMemParserCtxt(schema, len(schema))
  24. rngs = rngp.relaxNGParse()
  25. ctxt = rngs.relaxNGNewValidCtxt()
  26. doc = libxml2.parseDoc(instance)
  27. ret = doc.relaxNGValidateDoc(ctxt)
  28. if ret != 0:
  29. print "error doing RelaxNG validation"
  30. sys.exit(1)
  31. doc.freeDoc()
  32. del rngp
  33. del rngs
  34. del ctxt
  35. libxml2.relaxNGCleanupTypes()
  36. # Memory debug specific
  37. libxml2.cleanupParser()
  38. if libxml2.debugMemory(1) == 0:
  39. print "OK"
  40. else:
  41. print "Memory leak %d bytes" % (libxml2.debugMemory(1))
  42. libxml2.dumpMemory()