validate_examples.py 1.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
import glob
import sys
from pathlib import Path
from pprint import pprint

if __name__ == '__main__':

    if sys.version_info <= (3, 5):
        sys.exit('ERROR: You need at least Python 3.5 to run this tool')

    try:
        from lxml import etree
    except ImportError:
        sys.exit('ERROR: You need to install the Python lxml library')

    try:
        import xmlschema
    except ImportError:
        sys.exit('ERROR: You need to install the xml schema library')


    locations = []
    schemaFiles = glob.glob('schema/*.xsd')
    for schemaFile in schemaFiles:
        xs = xmlschema.XMLSchema(schemaFile, validation='skip')
        locations.append((xs.default_namespace, str(Path(schemaFile))[7:]))

28 29 30 31 32 33 34 35
    etsiSchema = str(Path('../103280/TS_103_280.xsd'))
    xs = xmlschema.XMLSchema(etsiSchema, validation='skip')
    locations.append((xs.default_namespace, str(Path("../" + etsiSchema))))

    for location in locations:
        print (location)

    coreSchema = xmlschema.XMLSchema('schema/ts_103120_Core.xsd', locations=locations)
36 37 38 39 40 41 42 43 44 45 46 47 48 49

    for schema in schemaFiles:
        newSchema = xmlschema.XMLSchema(schema, locations=locations)
        coreSchema.import_schema(newSchema.default_namespace, schema)

    examples = glob.glob('examples/*.xml')
    for example in examples:
        try:
            coreSchema.validate(example)
            print ("{0} passed validation".format(example))
        except Exception as ex:
            print ("{0} failed validation: {1}".format(example, ex))

    print ('Done')