def validate_xml(xml, schema, debug=False):
        """
        Validates a xml against a schema
        :param xml: The xml that will be validated
        :type: string|DomDocument
        :param schema: The schema
        :type: string
        :param debug: If debug is active, the parse-errors will be showed
        :type: bool
        :returns: Error code or the DomDocument of the xml
        :rtype: string
        """
        assert isinstance(xml, basestring) or isinstance(xml, Document) or isinstance(xml, etree._Element)
        assert isinstance(schema, basestring)

        if isinstance(xml, Document):
            xml = xml.toxml()
        elif isinstance(xml, etree._Element):
            xml = tostring(xml, encoding='unicode')

        # Switch to lxml for schema validation
        try:
            dom = fromstring(xml.encode('utf-8'), forbid_dtd=True)
        except Exception:
            return 'unloaded_xml'

        schema_file = join(dirname(__file__), 'schemas', schema)
        f_schema = open(schema_file, 'r')
        schema_doc = etree.parse(f_schema)
        f_schema.close()
        xmlschema = etree.XMLSchema(schema_doc)

        if not xmlschema.validate(dom):
            if debug:
                stderr.write('Errors validating the metadata')
                stderr.write(':\n\n')
                for error in xmlschema.error_log:
                    stderr.write('%s\n' % error.message)

            return 'invalid_xml'

        return parseString(tostring(dom, encoding='unicode').encode('utf-8'), forbid_dtd=True)
def validate_xml ( xml , schema , debug = False ) : assert isinstance ( xml , basestring ) or isinstance ( xml , Document ) or isinstance ( xml , etree . _Element ) assert isinstance ( schema , basestring ) if isinstance ( xml , Document ) : xml = xml . toxml ( ) elif isinstance ( xml , etree . _Element ) : xml = tostring ( xml , encoding = 'unicode' ) try : dom = fromstring ( xml . encode ( 'utf-8' ) , forbid_dtd = True ) except Exception : return 'unloaded_xml' schema_file = join ( dirname ( __file__ ) , 'schemas' , schema ) f_schema = open ( schema_file , 'r' ) schema_doc = etree . parse ( f_schema ) f_schema . close ( ) xmlschema = etree . XMLSchema ( schema_doc ) if not xmlschema . validate ( dom ) : if debug : stderr . write ( 'Errors validating the metadata' ) stderr . write ( ':\n\n' ) for error in xmlschema . error_log : stderr . write ( '%s\n' % error . message ) return 'invalid_xml' return parseString ( tostring ( dom , encoding = 'unicode' ) . encode ( 'utf-8' ) , forbid_dtd = True )
Validates a xml against a schema
        :param xml: The xml that will be validated
        :type: string|DomDocument
        :param schema: The schema
        :type: string
        :param debug: If debug is active, the parse-errors will be showed
        :type: bool
        :returns: Error code or the DomDocument of the xml
        :rtype: string