def isSchemaValidateXML(schemaFile, xmlFile): # is the schema is a file if not os.path.isfile(schemaFile): logging.warn( "The specified schema file ({0}) is not valid: its not a file." .format(str(schemaFile))) return False # is it readable if not os.access(schemaFile, os.R_OK): logging.warn( "The specified schema file ({0}) is not readable.".format( str(schemaFile))) return False schemaF = open(schemaFile, "r") schemaContent = schemaF.read() schemaF.close() if schemaContent is None or len(schemaContent) == 0: logging.warn( "Impossible to read the schema file (no content found in it)") return False # Extended version of an XSD validator # Create an xmlParser for the schema schemaParser = etree.XMLParser() # Register a resolver (to locate the other XSDs according to the path of static resources) xsdResolver = XSDResolver() xsdResolver.addMapping( "common.xsd", os.path.join(os.path.dirname(schemaFile), "common.xsd")) schemaParser.resolvers.add(xsdResolver) schemaParsed = etree.parse(schemaContent, parser=schemaParser) schema = etree.XMLSchema(schemaParsed) # We parse the given XML file try: xmlRoot = etree.parse(xmlFile) try: schema.assertValid(xmlRoot) return True except DocumentInvalid, err: log = schema.error_log error = log.last_error logging.error(error) logging.error("XML Document in invalid: {0}".format(err)) return False except etree.XMLSyntaxError, e: log = e.error_log.filter_from_level(etree.ErrorLevels.FATAL) logging.error(log)
def isSchemaValidateXML(schemaFile, xmlFile): # is the schema is a file if not os.path.isfile(schemaFile): logging.warn("The specified schema file (" + str(schemaFile) + ") is not valid : its not a file.") return False # is it readable if not os.access(schemaFile, os.R_OK): logging.warn("The specified schema file (" + str(schemaFile) + ") is not readable.") return False schemaF = open(schemaFile, "r") schemaContent = schemaF.read() schemaF.close() if schemaContent is None or len(schemaContent) == 0: logging.warn( "Impossible to read the schema file (no content found in it)") return False # Extended version of an XSD validator # Create an xmlParser for the schema schemaParser = etree.XMLParser() # Register a resolver (to locate the other XSDs according to the path of static resources) xsdResolver = XSDResolver() xsdResolver.addMapping( "common.xsd", os.path.join(os.path.dirname(schemaFile), "common.xsd")) schemaParser.resolvers.add(xsdResolver) schemaParsed = etree.parse(schemaContent, parser=schemaParser) schema = etree.XMLSchema(schemaParsed) try: xmlRoot = etree.parse(xmlFile) schema.assertValid(xmlRoot) return True except Exception as e: logging.warn(e) log = schema.error_log error = log.last_error logging.warn(error) return False return False
def isSchemaValidateXML(schemaFile, xmlFile): # is the schema is a file if not os.path.isfile(schemaFile): logging.warn("The specified schema file ({0}) is not valid: its not a file.".format(str(schemaFile))) return False # is it readable if not os.access(schemaFile, os.R_OK): logging.warn("The specified schema file ({0}) is not readable.".format(str(schemaFile))) return False schemaF = open(schemaFile, "r") schemaContent = schemaF.read() schemaF.close() if schemaContent is None or len(schemaContent) == 0: logging.warn("Impossible to read the schema file (no content found in it)") return False # Extended version of an XSD validator # Create an xmlParser for the schema schemaParser = etree.XMLParser() # Register a resolver (to locate the other XSDs according to the path of static resources) xsdResolver = XSDResolver() xsdResolver.addMapping("common.xsd", os.path.join(os.path.dirname(schemaFile), "common.xsd")) schemaParser.resolvers.add(xsdResolver) schemaParsed = etree.parse(schemaContent, parser=schemaParser) schema = etree.XMLSchema(schemaParsed) # We parse the given XML file try: xmlRoot = etree.parse(xmlFile) try: schema.assertValid(xmlRoot) return True except DocumentInvalid, err: log = schema.error_log error = log.last_error logging.error(error) logging.error("XML Document in invalid: {0}".format(err)) return False except etree.XMLSyntaxError, e: log = e.error_log.filter_from_level(etree.ErrorLevels.FATAL) logging.error(log)
def isSchemaValidateXML(schemaFile, xmlFile): # is the schema is a file if not os.path.isfile(schemaFile): logging.warn("The specified schema file (" + str(schemaFile) + ") is not valid : its not a file.") return False # is it readable if not os.access(schemaFile, os.R_OK): logging.warn("The specified schema file (" + str(schemaFile) + ") is not readable.") return False schemaF = open(schemaFile, "r") schemaContent = schemaF.read() schemaF.close() if schemaContent is None or len(schemaContent) == 0: logging.warn("Impossible to read the schema file (no content found in it)") return False # Extended version of an XSD validator # Create an xmlParser for the schema schemaParser = etree.XMLParser() # Register a resolver (to locate the other XSDs according to the path of static resources) xsdResolver = XSDResolver() xsdResolver.addMapping("common.xsd", os.path.join(os.path.dirname(schemaFile), "common.xsd")) schemaParser.resolvers.add(xsdResolver) schemaParsed = etree.parse(schemaContent, parser=schemaParser) schema = etree.XMLSchema(schemaParsed) try: xmlRoot = etree.parse(xmlFile) schema.assertValid(xmlRoot) return True except Exception as e: logging.warn(e) log = schema.error_log error = log.last_error logging.warn(error) return False return False