def _is_valid_oai_pmh_record(self, xmldict, metadata_prefix=''):
        site_url = config.get('ckan.site_url', '')

        if not metadata_prefix:
            metadata_prefix = 'oai_dc'
        try:
            xml_record = unparse(xmldict)

            oai_pmh_record = XMLRecord(
                MetadataFormats().get_metadata_formats('oai_pmh')[0],
                xml_record)

            # get the format
            metadata_format = MetadataFormats().get_metadata_formats(
                metadata_prefix)[0]
            metadata_schema = metadata_format.get_xsd_url()

            # local xsd for gcmd_dif (nasa hosted is not always available)
            if metadata_prefix == 'gcmd_dif':
                metadata_schema = metadata_schema.replace(
                    'http://gcmd.gsfc.nasa.gov/Aboutus/xml/dif/',
                    site_url + '/package_converter_xsd/')

            # modify xsd due to library bug
            fixed_xsd = '''<xs:schema xmlns="http://www.openarchives.org/OAI/2.0/"
                                  xmlns:xs="http://www.w3.org/2001/XMLSchema"
                                  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                                  xmlns:oai_dc="http://www.openarchives.org/OAI/2.0/oai_dc/" >
                           <xs:import namespace="http://www.openarchives.org/OAI/2.0/" schemaLocation="http://www.openarchives.org/OAI/2.0/OAI-PMH.xsd" />
                           <xs:import namespace="{namespace}" schemaLocation="{schema}" />
                       </xs:schema>'''.format(
                namespace=metadata_format.get_namespace(),
                schema=metadata_schema)

            return oai_pmh_record.validate(custom_xsd=fixed_xsd)

        except Exception as e:
            print(e)
            log.error('Failed to validate OAI-PMH for format {0}'.format(
                metadata_prefix))
        except:
            log.error('Failed to validate OAI-PMH for format {0}'.format(
                metadata_prefix))
            return False
 def test_list_metadata_formats(self):
     request_content = OAIPMHRepository().handle_request('ListMetadataFormats', {}, 'REQUEST_URL')
     oaipmh_record = XMLRecord(MetadataFormats().get_metadata_formats('oai_pmh')[0], request_content)
     # validate the XML
     assert_true(oaipmh_record.validate())
     assert_false(OAIPMHRepository()._is_error_oai_pmh_record(oaipmh_record.get_xml_dict()))