Created
July 8, 2010 00:34
-
-
Save Leonidas-from-XIV/467492 to your computer and use it in GitHub Desktop.
Simple stand-alone XML Schema validator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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