@staticmethod<EOL><INDENT>def validate_xml(xml, schema, debug=False):<DEDENT>
assert isinstance(xml, basestring) or isinstance(xml, Document) or isinstance(xml, etree._Element)<EOL>assert isinstance(schema, basestring)<EOL>if isinstance(xml, Document):<EOL><INDENT>xml = xml.toxml()<EOL><DEDENT>elif isinstance(xml, etree._Element):<EOL><INDENT>xml = tostring(xml, encoding='<STR_LIT>')<EOL><DEDENT>try:<EOL><INDENT>dom = fromstring(xml.encode('<STR_LIT:utf-8>'), forbid_dtd=True)<EOL><DEDENT>except Exception:<EOL><INDENT>return '<STR_LIT>'<EOL><DEDENT>schema_file = join(dirname(__file__), '<STR_LIT>', schema)<EOL>f_schema = open(schema_file, '<STR_LIT:r>')<EOL>schema_doc = etree.parse(f_schema)<EOL>f_schema.close()<EOL>xmlschema = etree.XMLSchema(schema_doc)<EOL>if not xmlschema.validate(dom):<EOL><INDENT>if debug:<EOL><INDENT>stderr.write('<STR_LIT>')<EOL>stderr.write('<STR_LIT>')<EOL>for error in xmlschema.error_log:<EOL><INDENT>stderr.write('<STR_LIT>' % error.message)<EOL><DEDENT><DEDENT>return '<STR_LIT>'<EOL><DEDENT>return parseString(tostring(dom, encoding='<STR_LIT>').encode('<STR_LIT:utf-8>'), forbid_dtd=True)<EOL>
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