def _parse_xml(self, xml): """Parses the specified XML string and validates it against the geoLink XSD. Args: xml (str or bytes): The XML to be parsed. Returns: lxml.etree._Element: The root element of the parsed geoLink XML. Raises: lxml.etree.XMLSyntaxError: Raised on failed validation. """ if isinstance(xml, bytes): content = fromstring(xml) else: content = fromstring(xml.encode('utf-16be')) if self._xsd_validation: self._schema.assertValid(content) if self._dtd_validation: dtd = content.getroottree().docinfo.internalDTD if isinstance(dtd, DTD): dtd.assertValid(content) else: raise DocumentInvalid('Missing DTD in parsed content') return content
def test_when_documentInvalid_raised_create_validation_objects(self, validate_schematron): exception_obj = DocumentInvalid("error msg") exception_obj.error_log = [ create_mocked_error_log(123, "error msg 1"), create_mocked_error_log(456, "error msg 2"), ] validate_schematron.side_effect = mock.Mock(side_effect=exception_obj) ip = InformationPackage.objects.create() task = ProcessTask.objects.create(information_package=ip) validator = XMLISOSchematronValidator( context="dummy_schema.xsl", options={'rootdir': "dummy_rootdir"}, ip=ip.id, task=task ) with self.assertRaises(DocumentInvalid): validator.validate("some_xml_file_path") validations = Validation.objects.filter(passed=False, validator="XMLISOSchematronValidator") self.assertEqual(validations.count(), 2)