Skip to content

Instantly share code, notes, and snippets.

@gamesbook
Created September 27, 2017 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gamesbook/3c1a289d50eda1c493f4b5d852274219 to your computer and use it in GitHub Desktop.
Save gamesbook/3c1a289d50eda1c493f4b5d852274219 to your computer and use it in GitHub Desktop.
Use lxml to validate an XML file against a Schema (XSD)
# -*- coding: utf-8 -*-
""" Purpose: Use lxml to validate an XML file against a Schema (XSD)
Created: 2017-09-27
Author: dhohls@csir.co.za
Requires::
pip install lxml
Usages::
python xml_validation.py -s myschema.xsd -x myxml.xml
python xml_validation.py y -s myschema.xsd -x myxml.xml -ll=DEBUG
"""
from __future__ import print_function
import argparse
import logging
import sys
# third party
from lxml import etree
log = logging.getLogger(__name__)
def main(args):
filename_xsd = args.xsd
filename_xml = args.xml
logging.basicConfig(
stream=sys.stdout,
format='%(asctime)s %(levelname)-8s [%(filename)s:%(lineno)d %(funcName)s()] %(message)s',
level=getattr(logging, args.loglevel))
# check xml
try:
doc = etree.parse(filename_xml)
log.debug('XML well formed, syntax ok.')
except IOError as err:
log.error(err)
quit()
except etree.XMLSyntaxError as err:
log.error('XML Syntax Error, see error_syntax.log')
with open('error_syntax.log', 'w') as error_log_file:
error_log_file.write(str(err.error_log))
quit()
except Exception as err:
log.error(err)
quit()
# check xsd
try:
xmlschema_doc = etree.parse(filename_xsd)
xmlschema = etree.XMLSchema(xmlschema_doc)
except IOError as err:
log.error(err)
quit()
except Exception as err:
log.error(err)
quit()
# validate xml against schema
try:
xmlschema.assertValid(doc)
log.debug('XML valid, schema validation ok.')
except etree.DocumentInvalid as err:
log.error('Schema validation error, see error_schema.log')
with open('error_schema.log', 'w') as error_log_file:
error_log_file.write(str(err.error_log))
quit()
except Exception as err:
log.error(err)
quit()
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument(
'-x', '--xml',
help="Name of XML data file")
parser.add_argument(
'-s', '--xsd',
help="Name of XSD schema file")
parser.add_argument(
'-ll', '--loglevel', default='WARNING',
choices=['DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL'],
help="Set log level for process (%s)" % 'WARNING')
args = parser.parse_args()
main(args)
@gamesbook
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment