diff --git a/owslib/catalogue/csw2.py b/owslib/catalogue/csw2.py index 37f5854f..2a56b541 100644 --- a/owslib/catalogue/csw2.py +++ b/owslib/catalogue/csw2.py @@ -580,11 +580,9 @@ def _parserecords(self, outputschema, esn): val = i.find(util.nspath_eval('gm03:fileIdentifier', namespaces)) identifier = self._setidentifierkey(util.testXMLValue(val)) self.records[identifier] = GM03(i) - elif outputschema == namespaces['mdb']: # ISO 19115 Part 3 XML - for i in self._exml.findall('.//' + util.nspath_eval('mdb:MD_Metadata', namespaces)): - val = i.find(util.nspath_eval('mdb:metadataIdentifier/mcc:MD_Identifier/mcc:code/gco:CharacterString', namespaces)) - identifier = self._setidentifierkey(util.testXMLValue(val)) - self.records[identifier] = MD_Metadata_3(i) + elif MD_Metadata_3.handles(outputschema): # ISO 19115 Part 3 XML + for elem, id in MD_Metadata_3.find_ids(self._exml): + self.records[self._setidentifierkey(id)] = MD_Metadata_3(elem) for i in self._exml.findall('.//' + util.nspath_eval('gfc:FC_FeatureCatalogue', namespaces)): identifier = self._setidentifierkey(util.testXMLValue(i.attrib['uuid'], attrib=True)) self.records[identifier] = FC_FeatureCatalogue_3(i) diff --git a/owslib/catalogue/csw3.py b/owslib/catalogue/csw3.py index bb522bce..312551ec 100644 --- a/owslib/catalogue/csw3.py +++ b/owslib/catalogue/csw3.py @@ -479,11 +479,9 @@ def _parserecords(self, outputschema, esn): val = i.find(util.nspath_eval('gm03:fileIdentifier', namespaces)) identifier = self._setidentifierkey(util.testXMLValue(val)) self.records[identifier] = GM03(i) - elif outputschema == namespaces['mdb']: # ISO 19115 Part 3 XML - for i in self._exml.findall('.//' + util.nspath_eval('mdb:MD_Metadata', namespaces)): - val = i.find(util.nspath_eval('mdb:metadataIdentifier/mcc:MD_Identifier/mcc:code/gco:CharacterString', namespaces)) - identifier = self._setidentifierkey(util.testXMLValue(val)) - self.records[identifier] = MD_Metadata_3(i) + elif MD_Metadata_3.handles(outputschema): # ISO 19115 Part 3 XML + for elem, id in MD_Metadata_3.find_ids(self._exml): + self.records[self._setidentifierkey(id)] = MD_Metadata_3(elem) for i in self._exml.findall('.//' + util.nspath_eval('gfc:FC_FeatureCatalogue', namespaces)): identifier = self._setidentifierkey(util.testXMLValue(i.attrib['uuid'], attrib=True)) self.records[identifier] = FC_FeatureCatalogue_3(i) diff --git a/owslib/iso_3.py b/owslib/iso_3.py index 9c03bac5..43770d46 100755 --- a/owslib/iso_3.py +++ b/owslib/iso_3.py @@ -270,6 +270,33 @@ def find_start(doc): return mtags[0] return None + @staticmethod + def handles(outputschema): + """ Returns True iff the outputschema is handled by this class + + :param outputschema: outputschema parameter string + :returns: True iff the outputschema is handled by this class + """ + return outputschema == NAMESPACES_V1['mdb'] or \ + outputschema == NAMESPACES_V2['mdb'] + + @staticmethod + def find_ids(elemtree): + """ Finds identifer strings and outer 'mdb:MD_Metadata' Elements + + :param elemtree: lxml.ElementTree to search in + :returns: a list of tuples (id string, 'mdb:MD_Metadata' lxml.Element) + """ + for ns in [NAMESPACES_V2, NAMESPACES_V1]: + elems = elemtree.findall('.//' + util.nspath_eval('mdb:MD_Metadata', ns)) + if len(elems) > 0: + ret_list = [] + for i in elems: + val = i.find(util.nspath_eval('mdb:metadataIdentifier/mcc:MD_Identifier/mcc:code/gco:CharacterString', NAMESPACES_V2)) + ret_list.append((i, util.testXMLValue(val))) + return ret_list + return [] + def get_all_contacts(self): """ Get all contacts in identification part of document diff --git a/tests/test_csw_geonetwork.py b/tests/test_csw_geonetwork.py index 9016d2d0..c5f66617 100644 --- a/tests/test_csw_geonetwork.py +++ b/tests/test_csw_geonetwork.py @@ -17,3 +17,24 @@ def test_csw_geonetwork(): assert c.results.get('returned') > 0 assert c.results.get('nextrecord') > 0 assert c.results.get('matches') > 0 + +SERVICE_URL3 = 'https://metawal.wallonie.be/geonetwork/srv/eng/csw' + +@pytest.mark.skipif(not service_ok(SERVICE_URL3), + reason='service is unreachable') +@pytest.mark.parametrize("esn_in", ['full', 'summary']) +def test_csw_geonetwork_iso_3(esn_in): + """ Test retrieving records from Belgian geonetwork, + specifically requesting ISO 19115 Part 3 XML records. + """ + c = CatalogueServiceWeb(SERVICE_URL3) + c.getrecords2(outputschema='http://standards.iso.org/iso/19115/-3/mdb/2.0', + typenames='mdb:MD_Metadata', + esn=esn_in) + + # Valid results were returned + assert c.results.get('returned') > 0 + assert c.results.get('nextrecord') > 0 + assert c.results.get('matches') > 0 + for id in c.records.keys(): + assert c.records[id].identifier == id \ No newline at end of file diff --git a/tests/test_remote_metadata.py b/tests/test_remote_metadata.py index 2cecc94b..98099426 100644 --- a/tests/test_remote_metadata.py +++ b/tests/test_remote_metadata.py @@ -206,14 +206,12 @@ def read(*args, **kwargs): def openURL(*args, **kwargs): """ Used to patch the 'openURL' call, returning ISO 19139 XML """ - print("@patch openURL") return open('tests/resources/csw_dov_getrecordbyid.xml', 'rb') def openURL_3(*args, **kwargs): """ Used to patch the 'openURL' call, returning ISO 19115 Part 3 XML """ - print("@patch openURL_3") return open('tests/resources/iso3_examples/auscope-3d-model.xml', 'rb')