Skip to content

Instantly share code, notes, and snippets.

@Leonidas-from-XIV
Created July 8, 2010 00:34
Show Gist options
  • Save Leonidas-from-XIV/467492 to your computer and use it in GitHub Desktop.
Save Leonidas-from-XIV/467492 to your computer and use it in GitHub Desktop.
Simple stand-alone XML Schema validator
#!/usr/bin/env python
# -*- coding: utf8 -*-
import sys
from lxml import etree
def main():
if len(sys.argv) != 3:
print >>sys.stderr, 'Usage: %s XML-file XSD-file' % sys.argv[0]
sys.exit(3)
schema_root = etree.parse(sys.argv[2])
try:
schema = etree.XMLSchema(schema_root)
except etree.XMLSchemaParseError, e:
print 'Parsing XML Schema failed'
print e
sys.exit(2)
parser = etree.XMLParser(schema=schema)
try:
etree.parse(sys.argv[1], parser=parser)
except etree.XMLSyntaxError, e:
print 'Validation failed'
print e
sys.exit(1)
else:
#print 'Validation succeeded'
sys.exit(0)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment