Changeset 2228 for TI07-MOLES/trunk


Ignore:
Timestamp:
02/03/07 17:20:20 (12 years ago)
Author:
ko23
Message:

XQuery update for library use. StubB still needs scope problem sorting. Note additional $targetCollection parameter

File:
1 edited

Legend:

Unmodified
Added
Removed
  • TI07-MOLES/trunk/PythonCode/wsgi/ndg_xqueries.py

    r2056 r2228  
    1212ObjectTypeQuery = '''declare default element namespace 'http://ndg.nerc.ac.uk/moles';for $Obj in collection('/db/ndg_B_metadata')/dgMetadata/dgMetadataRecord[dgMetadataID/schemeIdentifier='NDG-B0' and dgMetadataID/repositoryIdentifier='RepositoryID' and dgMetadataID/localIdentifier='LocalID']  return <objectType>{if (exists($Obj/dgDataEntity)) then 4 else (if (exists($Obj/dgObservationStation)) then 3 else (if (exists($Obj/dgDataProductionTool)) then 2 else (if (exists($Obj/dgActivity)) then 1 else (0))))}</objectType>''' 
    1313 
    14 StubBDEQuery = '''declare default element namespace 'http://ndg.nerc.ac.uk/moles/dataentity';declare namespace moles='http://ndg.nerc.ac.uk/moles';declare namespace f='http://ndg.nerc.ac.uk/moles/localfunctions';declare function f:strip-namespace($e as element()) as element(){element {QName('', local-name($e))}{let $parent :=$e let $childcount := count($parent/*) return if ($childcount>0) then for $child in ($e/*) return f:strip-namespace($child) else (data($e))}};declare function f:return-stub-activity($out-element-name as xs:string, $repid as xs:string, $locid as xs:string) as element(){let $found := count(collection('/db/ndg_B_metadata')/moles:dgMetadata/moles:dgMetadataRecord[moles:dgMetadataID/moles:repositoryIdentifier=$repid and moles:dgMetadataID/moles:localIdentifier=$locid]) return if ($found = 0) then element {$out-element-name} {element error {'not found'},element repositoryIdentifier {$repid},element localIdentifier {$locid}} else (for $SRAct in collection('/db/ndg_B_metadata')/moles:dgMetadata/moles:dgMetadataRecord[moles:dgMetadataID/moles:repositoryIdentifier=$repid and moles:dgMetadataID/moles:localIdentifier=$locid] return element {$out-element-name} {($SRAct/moles:dgMetadataID),($SRAct/moles:dgMetadataDescription),($SRAct/moles:name),($SRAct/moles:abbreviation),element dgActivity {if (exists($SRAct/*/moles:dgActivityDataCollection)) then ($SRAct/*/moles:dgActivityDataCollection) else if (exists($SRAct/*/moles:dgActivityDataProject)) then ($SRAct/*/moles:dgActivityDataProject) else if (exists($SRAct/*/dgActivityDataCampaign)) then ($SRAct/moles:dgActivity/moles:dgActivityDataCampaign) else ($SRAct/moles:dgActivity/moles:dgActivityDataInvestigation),($SRAct/moles:dgActivity/moles:dgActivityCoverage),($SRAct/moles:dgActivity/moles:dgActivityDuration)}})};declare function f:return-stub-dataentity($out-element-name as xs:string, $repid as xs:string, $locid as xs:string) as element(){let $found := count(collection('/db/ndg_B_metadata')/moles:dgMetadata/moles:dgMetadataRecord[moles:dgMetadataID/moles:repositoryIdentifier=$repid and moles:dgMetadataID/moles:localIdentifier=$locid]) return if ($found = 0) then element {$out-element-name} {element error {'not found'},element repositoryIdentifier {$repid},element localIdentifier {$locid}} else (for $SRDE in collection('/db/ndg_B_metadata')/moles:dgMetadata/moles:dgMetadataRecord[moles:dgMetadataID/moles:repositoryIdentifier=$repid and moles:dgMetadataID/moles:localIdentifier=$locid] return element {$out-element-name} {($SRDE/moles:dgMetadataID),($SRDE/moles:dgMetadataDescription),($SRDE/moles:name),($SRDE/moles:abbreviation),element dgDataEntity {($SRDE/moles:dgDataEntity/moles:dgDataSetType),($SRDE/moles:dgDataEntity/moles:dgDataGranule),($SRDE/moles:dgDataEntity/moles:dgDataSummary)}})};declare function f:return-stub-dpt($out-element-name as xs:string, $repid as xs:string, $locid as xs:string) as element(){let $found := count(collection('/db/ndg_B_metadata')/moles:dgMetadata/moles:dgMetadataRecord[ moles:dgMetadataID/moles:repositoryIdentifier=$repid and moles:dgMetadataID/moles:localIdentifier=$locid]) return if ($found = 0) then element {$out-element-name} {element error {'not found'},element repositoryIdentifier {$repid},element localIdentifier {$locid}} else (for $SRDPT in collection('/db/ndg_B_metadata')/moles:dgMetadata/moles:dgMetadataRecord[ moles:dgMetadataID/moles:repositoryIdentifier=$repid and moles:dgMetadataID/moles:localIdentifier=$locid] return element {$out-element-name} {$SRDPT/moles:dgMetadataID,$SRDPT/moles:dgMetadataDescription,$SRDPT/moles:name,$SRDPT/moles:abbreviation,$SRDPT/moles:logos,element dgDataProductionTool {$SRDPT/moles:dgDataProductionTool/moles:contactDetails,if (exists($SRDPT/moles:dgDataProductionTool/moles:dgModel)) then $SRDPT/moles:dgDataProductionTool/moles:dgModel else (),if (exists($SRDPT/moles:dgDataProductionTool/moles:dgInstrument)) then $SRDPT/moles:dgDataProductionTool/moles:dgInstrument else ()}})};declare function f:return-stub-obsstn($out-element-name as xs:string, $repid as xs:string, $locid as xs:string) as element(){let $found := count(collection('/db/ndg_B_metadata')/moles:dgMetadata/moles:dgMetadataRecord[moles:dgMetadataID/moles:repositoryIdentifier=$repid and moles:dgMetadataID/moles:localIdentifier=$locid]) return if ($found = 0) then element {$out-element-name} {element error {'not found'},element repositoryIdentifier {$repid},element localIdentifier {$locid}} else (for $SRObsStn in collection('/db/ndg_B_metadata')/moles:dgMetadata/moles:dgMetadataRecord[moles:dgMetadataID/moles:repositoryIdentifier=$repid and moles:dgMetadataID/moles:localIdentifier=$locid] return element {$out-element-name} {($SRObsStn/moles:dgMetadataID),($SRObsStn/moles:dgMetadataDescription),($SRObsStn/moles:name),($SRObsStn/moles:abbreviation),element dgObservationStation {($SRObsStn/moles:dgObservationStation/moles:contactDetails),if (exists($SRObsStn/moles:dgObservationStation/moles:dgStationaryPlatform)) then element dgStationaryPlatform {$SRObsStn/moles:dgObservationStation/moles:dgStationaryPlatform/moles:position,if (exists($SRObsStn/moles:dgObservationStation/moles:dgStationaryPlatform/dgLandStation)) then element dgLandStation {''} else if (exists($SRObsStn/moles:dgObservationStation/moles:dgStationaryPlatform/dgMooring)) then element dgMooring {''} else if (exists($SRObsStn/moles:dgObservationStation/moles:dgStationaryPlatform/dgStationGroup)) then element dgStationGroup {''} else ()} else if (exists($SRObsStn/moles:dgObservationStation/moles:dgMovingPlatform)) then $SRObsStn/moles:dgObservationStation/moles:dgMovingPlatform else ()}})};declare function f:return-stub-person($out-element-name as xs:string, $repid as xs:string, $locid as xs:string) as element(){let $found := count(collection('/db/ndg_B_metadata')/moles:dgMetadata/moles:dgPerson[moles:dgMetadataID/moles:repositoryIdentifier=$repid and moles:dgMetadataID/moles:localIdentifier=$locid]) return if ($found = 0) then element {$out-element-name} {element error {'not found'},element repositoryIdentifier {$repid},element localIdentifier {$locid}} else (for $person in collection('/db/ndg_B_metadata')/moles:dgMetadata/moles:dgPerson[moles:dgMetadataID/moles:repositoryIdentifier=$repid and moles:dgMetadataID/moles:localIdentifier=$locid] return element {$out-element-name} {($person/moles:name),($person/moles:contactDetails),($person/moles:dgMetadataID)})};declare function f:return-stub-organisation($out-element-name as xs:string, $repid as xs:string, $locid as xs:string) as element(){let $found := count(collection('/db/ndg_B_metadata')/moles:dgMetadata/moles:dgOrganisation[moles:dgMetadataID/moles:repositoryIdentifier=$repid and moles:dgMetadataID/moles:localIdentifier=$locid])return if ($found = 0) then element {$out-element-name} {element error {'not found'},element repositoryIdentifier {$repid},element localIdentifier {$locid}} else (for $org in collection('/db/ndg_B_metadata')/moles:dgMetadata/moles:dgOrganisation[moles:dgMetadataID/moles:repositoryIdentifier=$repid and moles:dgMetadataID/moles:localIdentifier=$locid] return element {$out-element-name} {$org/moles:dgMetadataID,$org/moles:name,$org/moles:abbreviation,$org/moles:contactDetails,$org/moles:logos})};for $DE in collection('/db/ndg_B_metadata')/moles:dgMetadata/moles:dgMetadataRecord[exists(moles:dgDataEntity)and moles:dgMetadataID[moles:schemeIdentifier='NDG-B0' and moles:repositoryIdentifier='RepositoryID' and moles:localIdentifier='LocalID']] return f:strip-namespace(element dgMetadataRecord {element dgMetadataID {element schemeIdentifier {'NDG-B1'},$DE/moles:dgMetadataID/moles:repositoryIdentifier,$DE/moles:dgMetadataID/moles:localIdentifier},$DE/moles:dgMetadataDescription,$DE/moles:name,$DE/moles:abbreviation,element dgDataEntity {$DE/moles:dgDataEntity/moles:dgDataSetType,$DE/moles:dgDataEntity/moles:dgDataObjectType,$DE/moles:dgDataEntity/moles:dgDataGranule,$DE/moles:dgDataEntity/moles:dgDataSummary,element dgDataRoles {if (exists($DE/moles:dgDataEntity/moles:dgDataRoles/moles:dgDataCreator/moles:dgMetadataID)) then element dgDataCreator {$DE/moles:dgDataEntity/moles:dgDataRoles/moles:dgDataCreator/moles:dgMetadataID,$DE/moles:dgDataEntity/moles:dgDataRoles/moles:dgDataCreator/moles:roleName,$DE/moles:dgDataEntity/moles:dgDataRoles/moles:dgDataCreator/moles:abbreviation,$DE/moles:dgDataEntity/moles:dgDataRoles/moles:dgDataCreator/moles:contactDetails,for $DataCreatorRole in $DE/moles:dgDataEntity/moles:dgDataRoles/moles:dgDataCreator/moles:dgRoleHolder order by $DataCreatorRole/moles:startDate empty least, $DataCreatorRole/moles:endDate empty least return element dgRoleHolder {if (exists($DataCreatorRole/moles:dgOrganisationID)) then f:return-stub-organisation('organisation', data($DataCreatorRole/moles:dgOrganisationID/moles:repositoryIdentifier), data($DataCreatorRole/moles:dgOrganisationID/moles:localIdentifier)) else if (exists($DataCreatorRole/moles:dgPersonID)) then f:return-stub-person('person', string($DataCreatorRole/moles:dgPersonID/moles:repositoryIdentifier), string($DataCreatorRole/moles:dgPersonID/moles:localIdentifier)) else (),$DataCreatorRole/moles:startDate,$DataCreatorRole/moles:endDate,$DataCreatorRole/moles:localName}} else (),if (exists($DE/moles:dgDataEntity/moles:dgDataRoles/moles:dgDataCurator/moles:dgMetadataID)) then element dgDataCurator {$DE/moles:dgDataEntity/moles:dgDataRoles/moles:dgDataCurator/moles:dgMetadataID,$DE/moles:dgDataEntity/moles:dgDataRoles/moles:dgDataCurator/moles:roleName,$DE/moles:dgDataEntity/moles:dgDataRoles/moles:dgDataCurator/moles:abbreviation,$DE/moles:dgDataEntity/moles:dgDataRoles/moles:dgDataCurator/moles:contactDetails,for $DataCuratorRole in $DE/moles:dgDataEntity/moles:dgDataRoles/moles:dgDataCurator/moles:dgRoleHolder order by $DataCuratorRole/moles:startDate empty least, $DataCuratorRole/moles:endDate empty least return element dgRoleHolder {if (exists($DataCuratorRole/moles:dgOrganisationID)) then f:return-stub-organisation('organisation', data($DataCuratorRole/moles:dgOrganisationID/moles:repositoryIdentifier), data($DataCuratorRole/moles:dgOrganisationID/moles:localIdentifier)) else if (exists($DataCuratorRole/moles:dgPersonID)) then f:return-stub-person('person', string($DataCuratorRole/moles:dgPersonID/moles:repositoryIdentifier), string($DataCuratorRole/moles:dgPersonID/moles:localIdentifier)) else (),$DataCuratorRole/moles:startDate,$DataCuratorRole/moles:endDate,$DataCuratorRole/moles:localName}} else (),if (exists($DE/moles:dgDataEntity/moles:dgDataRoles/moles:dgDataOtherRoles/moles:dgMetadataID)) then element dgDataOtherRoles {$DE/moles:dgDataEntity/moles:dgDataRoles/moles:dgDataOtherRoles/moles:dgMetadataID,$DE/moles:dgDataEntity/moles:dgDataRoles/moles:dgDataOtherRoles/moles:roleName,$DE/moles:dgDataEntity/moles:dgDataRoles/moles:dgDataOtherRoles/moles:abbreviation,$DE/moles:dgDataEntity/moles:dgDataRoles/moles:dgDataOtherRoles/moles:contactDetails,for $DataOtherRolesRole in $DE/moles:dgDataEntity/moles:dgDataRoles/moles:dgDataOtherRoles/moles:dgRoleHolder order by $DataOtherRolesRole/moles:startDate empty least, $DataOtherRolesRole/moles:endDate empty least return element dgRoleHolder {if (exists($DataOtherRolesRole/moles:dgOrganisationID)) then f:return-stub-organisation('organisation', data($DataOtherRolesRole/moles:dgOrganisationID/moles:repositoryIdentifier), data($DataOtherRolesRole/moles:dgOrganisationID/moles:localIdentifier)) else if (exists($DataOtherRolesRole/moles:dgPersonID)) then f:return-stub-person('person', string($DataOtherRolesRole/moles:dgPersonID/moles:repositoryIdentifier), string($DataOtherRolesRole/moles:dgPersonID/moles:localIdentifier)) else (),$DataOtherRolesRole/moles:startDate,$DataOtherRolesRole/moles:endDate,$DataOtherRolesRole/moles:localName}} else ()},for $RelDep in $DE/moles:dgDataEntity/moles:RelatedDeployment return element RelatedDeployment {$RelDep/moles:DateStart,$RelDep/moles:DateEnd,$RelDep/moles:dgMetadataID,f:return-stub-activity('activity', data($RelDep/moles:ActivityID/moles:repositoryIdentifier), data($RelDep/moles:ActivityID/moles:localIdentifier)),f:return-stub-dpt('dataproductiontool', data($RelDep/moles:DataProductionToolID/moles:repositoryIdentifier), data($RelDep/moles:DataProductionToolID/moles:localIdentifier)),f:return-stub-obsstn('observationstation', data($RelDep/moles:ObservationStationID/moles:repositoryIdentifier), data($RelDep/moles:ObservationStationID/moles:localIdentifier)),$RelDep/moles:Coverage},for $RelatedDataEntity in $DE/moles:dgDataEntity/moles:dgRelatedDataEntity return element relateddataentity {element relation {$RelatedDataEntity/moles:RelationID/moles:dgValidTerm,$RelatedDataEntity/moles:RelationID/moles:dgValidTermID},f:return-stub-dataentity('dataentity', data($RelatedDataEntity/(moles:RelatedDataEntityID | moles:RelatedDataGranuleID)/moles:repositoryIdentifier), data($RelatedDataEntity/(moles:RelatedDataEntityID | moles:RelatedDataGranuleID)/moles:localIdentifier))}},for $strkwrd in $DE/moles:dgStructuredKeyword return $strkwrd,if (exists($DE/moles:dgMetadataProvenance)) then $DE/moles:dgMetadataProvenance else (),if (exists($DE/moles:dgMetadataSecurity)) then $DE/moles:dgMetadataSecurity else ()})''' 
    15  
    16 StubBActQuery = '''declare default element namespace 'http://ndg.nerc.ac.uk/moles/activity'; declare namespace moles='http://ndg.nerc.ac.uk/moles'; declare namespace f='http://ndg.nerc.ac.uk/moles/localfunctions';  declare function f:strip-namespace($e as element()) as element() {element {QName('', local-name($e))} {let $parent :=$e let $childcount := count($parent/*) return if ($childcount>0) then for $child in ($e/*) return f:strip-namespace($child) else (data($e))}} ;  declare function f:return-stub-activity($out-element-name as xs:string, $repid as xs:string, $locid as xs:string) as element() {let $found := count(collection('/db/ndg_B_metadata')/moles:dgMetadata/moles:dgMetadataRecord[ moles:dgMetadataID/moles:repositoryIdentifier=$repid and moles:dgMetadataID/moles:localIdentifier=$locid]) return if ($found = 0) then element {$out-element-name} {element error {'not found'}, element repositoryIdentifier {$repid}, element localIdentifier {$locid}} else (for $SRAct in collection('/db/ndg_B_metadata')/moles:dgMetadata/moles:dgMetadataRecord[ moles:dgMetadataID/moles:repositoryIdentifier=$repid and moles:dgMetadataID/moles:localIdentifier=$locid] return element {$out-element-name} {($SRAct/moles:dgMetadataID), ($SRAct/moles:dgMetadataDescription), ($SRAct/moles:name), ($SRAct/moles:abbreviation), element dgActivity {if (exists($SRAct/*/moles:dgActivityDataCollection)) then ($SRAct/*/moles:dgActivityDataCollection) else if (exists($SRAct/*/moles:dgActivityDataProject)) then ($SRAct/*/moles:dgActivityDataProject) else if (exists($SRAct/*/dgActivityDataCampaign)) then ($SRAct/moles:dgActivity/moles:dgActivityDataCampaign) else ($SRAct/moles:dgActivity/moles:dgActivityDataInvestigation), ($SRAct/moles:dgActivity/moles:dgActivityCoverage), ($SRAct/moles:dgActivity/moles:dgActivityDuration)}})};declare function f:return-stub-dataentity($out-element-name as xs:string, $repid as xs:string, $locid as xs:string) as element() {let $found := count(collection('/db/ndg_B_metadata')/moles:dgMetadata/moles:dgMetadataRecord[ moles:dgMetadataID/moles:repositoryIdentifier=$repid and moles:dgMetadataID/moles:localIdentifier=$locid]) return if ($found = 0) then element {$out-element-name} {element error {'not found'}, element repositoryIdentifier {$repid}, element localIdentifier {$locid}} else (for $SRDE in collection('/db/ndg_B_metadata')/moles:dgMetadata/moles:dgMetadataRecord[ moles:dgMetadataID/moles:repositoryIdentifier=$repid and moles:dgMetadataID/moles:localIdentifier=$locid] return element {$out-element-name} {($SRDE/moles:dgMetadataID), ($SRDE/moles:dgMetadataDescription), ($SRDE/moles:name), ($SRDE/moles:abbreviation), element dgDataEntity {($SRDE/moles:dgDataEntity/moles:dgDataSetType), ($SRDE/moles:dgDataEntity/moles:dgDataGranule), ($SRDE/moles:dgDataEntity/moles:dgDataSummary)}})};declare function f:return-stub-dpt($out-element-name as xs:string, $repid as xs:string, $locid as xs:string) as element() {let $found := count(collection('/db/ndg_B_metadata')/moles:dgMetadata/moles:dgMetadataRecord[ moles:dgMetadataID/moles:repositoryIdentifier=$repid and moles:dgMetadataID/moles:localIdentifier=$locid]) return if ($found = 0) then element {$out-element-name} {element error {'not found'}, element repositoryIdentifier {$repid}, element localIdentifier {$locid}} else (for $SRDPT in collection('/db/ndg_B_metadata')/moles:dgMetadata/moles:dgMetadataRecord[ moles:dgMetadataID/moles:repositoryIdentifier=$repid and moles:dgMetadataID/moles:localIdentifier=$locid] return element {$out-element-name} {$SRDPT/moles:dgMetadataID, $SRDPT/moles:dgMetadataDescription, $SRDPT/moles:name, $SRDPT/moles:abbreviation, $SRDPT/moles:logos, element dgDataProductionTool {$SRDPT/moles:dgDataProductionTool/moles:contactDetails, if (exists($SRDPT/moles:dgDataProductionTool/moles:dgModel)) then $SRDPT/moles:dgDataProductionTool/moles:dgModel else (), if (exists($SRDPT/moles:dgDataProductionTool/moles:dgInstrument)) then $SRDPT/moles:dgDataProductionTool/moles:dgInstrument else ()}})}; declare function f:return-stub-obsstn($out-element-name as xs:string, $repid as xs:string, $locid as xs:string) as element() {let $found := count(collection('/db/ndg_B_metadata')/moles:dgMetadata/moles:dgMetadataRecord[ moles:dgMetadataID/moles:repositoryIdentifier=$repid and moles:dgMetadataID/moles:localIdentifier=$locid]) return if ($found = 0) then element {$out-element-name} {element error {'not found'}, element repositoryIdentifier {$repid}, element localIdentifier {$locid}} else (for $SRObsStn in collection('/db/ndg_B_metadata')/moles:dgMetadata/moles:dgMetadataRecord[ moles:dgMetadataID/moles:repositoryIdentifier=$repid and moles:dgMetadataID/moles:localIdentifier=$locid] return element {$out-element-name} {($SRObsStn/moles:dgMetadataID), ($SRObsStn/moles:dgMetadataDescription), ($SRObsStn/moles:name), ($SRObsStn/moles:abbreviation), element dgObservationStation {($SRObsStn/moles:dgObservationStation/moles:contactDetails), if (exists($SRObsStn/moles:dgObservationStation/moles:dgStationaryPlatform)) then element dgStationaryPlatform {$SRObsStn/moles:dgObservationStation/moles:dgStationaryPlatform/moles:position, if (exists($SRObsStn/moles:dgObservationStation/moles:dgStationaryPlatform/dgLandStation)) then element dgLandStation {''} else if (exists($SRObsStn/moles:dgObservationStation/moles:dgStationaryPlatform/dgMooring)) then element dgMooring {''} else if (exists($SRObsStn/moles:dgObservationStation/moles:dgStationaryPlatform/dgStationGroup)) then element dgStationGroup {''} else ()} else if (exists($SRObsStn/moles:dgObservationStation/moles:dgMovingPlatform)) then $SRObsStn/moles:dgObservationStation/moles:dgMovingPlatform else ()}})};declare function f:return-stub-person($out-element-name as xs:string, $repid as xs:string, $locid as xs:string) as element() {let $found := count(collection('/db/ndg_B_metadata')/moles:dgMetadata/moles:dgPerson[ moles:dgMetadataID/moles:repositoryIdentifier=$repid and moles:dgMetadataID/moles:localIdentifier=$locid]) return if ($found = 0) then element {$out-element-name} {element error {'not found'}, element repositoryIdentifier {$repid}, element localIdentifier {$locid}} else (for $person in collection('/db/ndg_B_metadata')/moles:dgMetadata/moles:dgPerson[ moles:dgMetadataID/moles:repositoryIdentifier=$repid and moles:dgMetadataID/moles:localIdentifier=$locid] return element {$out-element-name} {($person/moles:name), ($person/moles:contactDetails), ($person/moles:dgMetadataID)})}; declare function f:return-stub-organisation($out-element-name as xs:string, $repid as xs:string, $locid as xs:string) as element() {let $found := count(collection('/db/ndg_B_metadata')/moles:dgMetadata/moles:dgOrganisation[ moles:dgMetadataID/moles:repositoryIdentifier=$repid and moles:dgMetadataID/moles:localIdentifier=$locid]) return if ($found = 0) then element {$out-element-name} {element error {'not found'}, element repositoryIdentifier {$repid}, element localIdentifier {$locid}} else (for $org in collection('/db/ndg_B_metadata')/moles:dgMetadata/moles:dgOrganisation[ moles:dgMetadataID/moles:repositoryIdentifier=$repid and moles:dgMetadataID/moles:localIdentifier=$locid] return element {$out-element-name} {$org/moles:dgMetadataID, $org/moles:name, $org/moles:abbreviation, $org/moles:contactDetails, $org/moles:logos})};for $Act in collection('/db/ndg_B_metadata')/moles:dgMetadata/moles:dgMetadataRecord[ moles:dgActivity!='' and moles:dgMetadataID/moles:schemeIdentifier='NDG-B0' and moles:dgMetadataID/moles:repositoryIdentifier='RepositoryID' and moles:dgMetadataID/moles:localIdentifier='LocalID'] return f:strip-namespace(element dgMetadataRecord {element dgMetadataID {element schemeIdentifier {'NDG-B1'}, (element repositoryIdentifier {data($Act/moles:dgMetadataID/moles:repositoryIdentifier)}), element localIdentifier {data($Act/moles:dgMetadataID/moles:localIdentifier)}}, $Act/moles:dgMetadataDescription, $Act/moles:name, $Act/moles:abbreviation, element dgActivity {for $RelatedActivities in $Act/dgActivity/moles:relatedActivity return element relatedActivity {$RelatedActivities/moles:activityRelation, f:return-stub-activity('activity', data($RelatedActivities/moles:relatedActivityID/moles:repositoryIdentifier), data($RelatedActivities/moles:relatedActivityID/moles:localIdentifier))},if (exists($Act/moles:dgActivity/moles:dgActivityDataCollection)) then ($Act/moles:dgActivity/moles:dgActivityDataCollection) else if (exists($Act/moles:dgActivity/moles:dgActivityDataProject)) then ($Act/moles:dgActivity/moles:dgActivityDataProject) else if (exists($Act/moles:dgActivity/moles:dgActivityDataInvestigation)) then ($Act/moles:dgActivity/moles:dgActivityDataInvestigation) else if (exists($Act/moles:dgActivity/moles:dgActivityDataCampaign)) then ($Act/moles:dgActivity/moles:dgActivityDataCampaign) else (), element dgActivityRole {element dgInvestigator {element dgPrincipalInvestigator {($Act/moles:dgActivity/moles:dgActivityRole/moles:dgInvestigator/moles:dgPrincipalInvestigator/moles:dgMetadataID), ($Act/moles:dgActivity/moles:dgActivityRole/moles:dgInvestigator/moles:dgPrincipalInvestigator/moles:roleName), ($Act/moles:dgActivity/moles:dgActivityRole/moles:dgInvestigator/moles:dgPrincipalInvestigator/moles:abbreviation), ($Act/moles:dgActivity/moles:dgActivityRole/moles:dgInvestigator/moles:dgPrincipalInvestigator/moles:contactDetails), for $PIRole in $Act/moles:dgActivity/moles:dgActivityRole/moles:dgInvestigator/moles:dgPrincipalInvestigator/moles:dgRoleHolder order by $PIRole/moles:startDate empty least, $PIRole/moles:endDate empty least return element dgRoleHolder {if (exists($PIRole/moles:dgOrganisationID)) then f:return-stub-organisation('organisation', data($PIRole/moles:dgOrganisationID/moles:repositoryIdentifier), data($PIRole/moles:dgOrganisationID/moles:localIdentifier)) else if (exists($PIRole/moles:dgPersonID)) then f:return-stub-person('person', string($PIRole/moles:dgPersonID/moles:repositoryIdentifier), string($PIRole/moles:dgPersonID/moles:localIdentifier)) else (), ($PIRole/moles:startDate), if (exists($PIRole/moles:endDate)) then ($PIRole/moles:endDate) else (), if (exists($PIRole/moles:localName)) then ($PIRole/moles:localName) else ()}},for $CI in $Act/moles:dgActivity/moles:dgActivityRole/moles:dgInvestigator/moles:dgCoInvestigator return element dgCoInvestigator {($CI/moles:dgMetadataID), ($CI/moles:roleName), ($CI/moles:abbreviation), ($CI/moles:contactDetails), for $CIRole in $CI/moles:dgRoleHolder order by $CIRole/moles:startDate empty least, $CIRole/moles:endDate empty least return element dgRoleHolder {if (exists(CIRole/moles:dgOrganisationID)) then f:return-stub-organisation('organisation', data($CIRole/moles:dgOrganisationID/moles:repositoryIdentifier), data($CIRole/moles:dgOrganisationID/moles:localIdentifier)) else if (exists($CIRole/moles:dgPersonID)) then f:return-stub-person('person', string($CIRole/moles:dgPersonID/moles:repositoryIdentifier), string($CIRole/moles:dgPersonID/moles:localIdentifier)) else (), ($CIRole/moles:startDate), if (exists($CIRole/moles:endDate)) then ($CIRole/moles:endDate) else (), if (exists($CIRole/moles:localName)) then ($CIRole/moles:localName) else ()}}},if (exists($Act/moles:dgActivity/moles:dgActivityRole/moles:dgTechnicalContact)) then (element dgTechnicalContact {($Act/moles:dgActivity/moles:dgActivityRole/moles:dgTechnicalContact/moles:dgMetadataID), ($Act/moles:dgActivity/moles:dgActivityRole/moles:dgTechnicalContact/moles:roleName), ($Act/moles:dgActivity/moles:dgActivityRole/moles:dgTechnicalContact/moles:abbreviation), ($Act/moles:dgActivity/moles:dgActivityRole/moles:dgTechnicalContact/moles:contactDetails), for $TechConRole in $Act/moles:dgActivity/moles:dgActivityRole/moles:dgTechnicalContact/moles:dgRoleHolder order by $TechConRole/moles:startDate empty least, $TechConRole/moles:endDate empty least return element dgRoleHolder {if (exists($TechConRole/moles:dgOrganisationID)) then f:return-stub-organisation('organisation', data($TechConRole/moles:dgOrganisationID/moles:repositoryIdentifier), data($TechConRole/moles:dgOrganisationID/moles:localIdentifier)) else if (exists($TechConRole/moles:dgPersonID)) then f:return-stub-person('person', string($TechConRole/moles:dgPersonID/moles:repositoryIdentifier), string($TechConRole/moles:dgPersonID/moles:localIdentifier)) else (), ($TechConRole/moles:startDate), if (exists($TechConRole/moles:endDate)) then ($TechConRole/moles:endDate) else (), if (exists($TechConRole/moles:localName)) then ($TechConRole/moles:localName) else ()}}) else (),if (exists($Act/dgActivity/dgActivityRole/dgProjectManager)) then (element dgProjectManager {($Act/dgActivity/dgActivityRole/dgProjectManager/dgMetadataID), ($Act/dgActivity/dgActivityRole/dgProjectManager/roleName), ($Act/dgActivity/dgActivityRole/dgProjectManager/abbreviation), ($Act/dgActivity/dgActivityRole/dgProjectManager/contactDetails), for $PMRole in $Act/dgActivity/dgActivityRole/dgProjectManager/dgRoleHolder order by $PMRole/startDate empty least, $PMRole/endDate empty least return element dgRoleHolder {if (exists($PMRole/moles:dgOrganisationID)) then f:return-stub-organisation('organisation', data($PMRole/moles:dgOrganisationID/moles:repositoryIdentifier), data($PMRole/moles:dgOrganisationID/moles:localIdentifier)) else if (exists($PMRole/moles:dgPersonID)) then f:return-stub-person('person', string($PMRole/moles:dgPersonID/moles:repositoryIdentifier), string($PMRole/moles:dgPersonID/moles:localIdentifier)) else (), ($PMRole/startDate), if (exists($PMRole/moles:endDate)) then ($PMRole/moles:endDate) else (), if (exists($PMRole/moles:localName)) then ($PMRole/moles:localName) else ()}}) else ()},for $RelDep in $Act/moles:dgActivity/moles:ActivityDeployment return element ActivityDeployment {($RelDep/moles:DateStart), if (exists($RelDep/moles:DateEnd)) then ($RelDep/moles:DateEnd) else (), ($RelDep/moles:dgMetadataID), f:return-stub-dpt('dataproductiontool', data($RelDep/moles:DataProductionToolID/moles:repositoryIdentifier), data($RelDep/moles:DataProductionToolID/moles:localIdentifier)), f:return-stub-obsstn('observationstation', data($RelDep/moles:ObservationStationID/moles:repositoryIdentifier), data($RelDep/moles:ObservationStationID/moles:localIdentifier)), for $DE in collection('/db/ndg_B_metadata')/moles:dgMetadata/moles:dgMetadataRecord where $DE/moles:dgDataEntity/moles:RelatedDeployment/moles:dgMetadataID/moles:repositoryIdentifier=$RelDep/moles:dgMetadataID/moles:repositoryIdentifier and $DE/moles:dgDataEntity/moles:RelatedDeployment/moles:dgMetadataID/moles:localIdentifier=$RelDep/moles:dgMetadataID/moles:localIdentifier return f:return-stub-dataentity('dataentity', data($DE/moles:dgMetadataID/moles:repositoryIdentifier), data($DE/moles:dgMetadataID/moles:localIdentifier))}, (: </ActivityDeployment> :) if (exists($Act/dgActivityCoverage)) then ($Act/dgActivityCoverage) else (), if (exists($Act/dgActivityDuration)) then ($Act/dgActivityDuration) else ()} ,for $strkwrd in $Act/moles:dgStructuredKeyword return $strkwrd, if (exists($Act/moles:dgMetadataProvenance)) then $Act/moles:dgMetadataProvenance else (), if (exists($Act/moles:dgMetadataSecurity)) then $Act/moles:dgMetadataSecurity else ()})''' 
    17  
    18 StubBDPTQuery = '''declare default element namespace 'http://ndg.nerc.ac.uk/moles/dataproductiontool';declare namespace moles='http://ndg.nerc.ac.uk/moles';declare namespace f='http://ndg.nerc.ac.uk/moles/localfunctions';declare function f:strip-namespace($e as element()) as element(){element {QName('', local-name($e))}{let $parent :=$e let $childcount := count($parent/*) return if ($childcount>0) then for $child in ($e/*) return f:strip-namespace($child) else (data($e))}};declare function f:return-stub-activity($out-element-name as xs:string, $repid as xs:string, $locid as xs:string) as element(){let $found := count(collection('/db/ndg_B_metadata')/moles:dgMetadata/moles:dgMetadataRecord[moles:dgMetadataID/moles:repositoryIdentifier=$repid and moles:dgMetadataID/moles:localIdentifier=$locid]) return if ($found = 0) then element {$out-element-name} {element error {'not found'},element repositoryIdentifier {$repid},element localIdentifier {$locid}} else (for $SRAct in collection('/db/ndg_B_metadata')/moles:dgMetadata/moles:dgMetadataRecord[moles:dgMetadataID/moles:repositoryIdentifier=$repid and moles:dgMetadataID/moles:localIdentifier=$locid] return element {$out-element-name} {($SRAct/moles:dgMetadataID),($SRAct/moles:dgMetadataDescription),($SRAct/moles:name),($SRAct/moles:abbreviation),element dgActivity {if (exists($SRAct/*/moles:dgActivityDataCollection)) then ($SRAct/*/moles:dgActivityDataCollection) else if (exists($SRAct/*/moles:dgActivityDataProject)) then ($SRAct/*/moles:dgActivityDataProject) else if (exists($SRAct/*/dgActivityDataCampaign)) then ($SRAct/moles:dgActivity/moles:dgActivityDataCampaign) else ($SRAct/moles:dgActivity/moles:dgActivityDataInvestigation),($SRAct/moles:dgActivity/moles:dgActivityCoverage),($SRAct/moles:dgActivity/moles:dgActivityDuration)}})};declare function f:return-stub-dataentity($out-element-name as xs:string, $repid as xs:string, $locid as xs:string) as element(){let $found := count(collection('/db/ndg_B_metadata')/moles:dgMetadata/moles:dgMetadataRecord[moles:dgMetadataID/moles:repositoryIdentifier=$repid and moles:dgMetadataID/moles:localIdentifier=$locid]) return if ($found = 0) then element {$out-element-name} {element error {'not found'},element repositoryIdentifier {$repid},element localIdentifier {$locid}} else (for $SRDE in collection('/db/ndg_B_metadata')/moles:dgMetadata/moles:dgMetadataRecord[moles:dgMetadataID/moles:repositoryIdentifier=$repid and moles:dgMetadataID/moles:localIdentifier=$locid] return element {$out-element-name} {($SRDE/moles:dgMetadataID),($SRDE/moles:dgMetadataDescription),($SRDE/moles:name),($SRDE/moles:abbreviation),element dgDataEntity {($SRDE/moles:dgDataEntity/moles:dgDataSetType),($SRDE/moles:dgDataEntity/moles:dgDataGranule),($SRDE/moles:dgDataEntity/moles:dgDataSummary)}})};declare function f:return-stub-dpt($out-element-name as xs:string, $repid as xs:string, $locid as xs:string) as element(){let $found := count(collection('/db/ndg_B_metadata')/moles:dgMetadata/moles:dgMetadataRecord[moles:dgMetadataID/moles:repositoryIdentifier=$repid and moles:dgMetadataID/moles:localIdentifier=$locid]) return if ($found = 0) then element {$out-element-name} {element error {'not found'},element repositoryIdentifier {$repid},element localIdentifier {$locid}}else (for $SRDPT in collection('/db/ndg_B_metadata')/moles:dgMetadata/moles:dgMetadataRecord[moles:dgMetadataID/moles:repositoryIdentifier=$repid and moles:dgMetadataID/moles:localIdentifier=$locid] return element {$out-element-name} {$SRDPT/moles:dgMetadataID,$SRDPT/moles:dgMetadataDescription,$SRDPT/moles:name,$SRDPT/moles:abbreviation,$SRDPT/moles:logos,element dgDataProductionTool {$SRDPT/moles:dgDataProductionTool/moles:contactDetails,if (exists($SRDPT/moles:dgDataProductionTool/moles:dgModel)) then $SRDPT/moles:dgDataProductionTool/moles:dgModel else (),if (exists($SRDPT/moles:dgDataProductionTool/moles:dgInstrument)) then $SRDPT/moles:dgDataProductionTool/moles:dgInstrument else ()}})};declare function f:return-stub-obsstn($out-element-name as xs:string, $repid as xs:string, $locid as xs:string) as element(){let $found := count(collection('/db/ndg_B_metadata')/moles:dgMetadata/moles:dgMetadataRecord[moles:dgMetadataID/moles:repositoryIdentifier=$repid and moles:dgMetadataID/moles:localIdentifier=$locid]) return if ($found = 0) then element {$out-element-name} {element error {'not found'},element repositoryIdentifier {$repid},element localIdentifier {$locid}} else (for $SRObsStn in collection('/db/ndg_B_metadata')/moles:dgMetadata/moles:dgMetadataRecord[moles:dgMetadataID/moles:repositoryIdentifier=$repid and moles:dgMetadataID/moles:localIdentifier=$locid] return element {$out-element-name} {($SRObsStn/moles:dgMetadataID),($SRObsStn/moles:dgMetadataDescription),($SRObsStn/moles:name),($SRObsStn/moles:abbreviation),element dgObservationStation {($SRObsStn/moles:dgObservationStation/moles:contactDetails),if (exists($SRObsStn/moles:dgObservationStation/moles:dgStationaryPlatform)) then element dgStationaryPlatform {$SRObsStn/moles:dgObservationStation/moles:dgStationaryPlatform/moles:position,if (exists($SRObsStn/moles:dgObservationStation/moles:dgStationaryPlatform/dgLandStation)) then element dgLandStation {''} else if (exists($SRObsStn/moles:dgObservationStation/moles:dgStationaryPlatform/dgMooring)) then element dgMooring {''} else if (exists($SRObsStn/moles:dgObservationStation/moles:dgStationaryPlatform/dgStationGroup)) then element dgStationGroup {''} else ()} else if (exists($SRObsStn/moles:dgObservationStation/moles:dgMovingPlatform)) then $SRObsStn/moles:dgObservationStation/moles:dgMovingPlatform else ()}})};declare function f:return-stub-person($out-element-name as xs:string, $repid as xs:string, $locid as xs:string) as element(){let $found := count(collection('/db/ndg_B_metadata')/moles:dgMetadata/moles:dgPerson[moles:dgMetadataID/moles:repositoryIdentifier=$repid and moles:dgMetadataID/moles:localIdentifier=$locid]) return if ($found = 0) then element {$out-element-name} {element error {'not found'},element repositoryIdentifier {$repid},element localIdentifier {$locid}} else (for $person in collection('/db/ndg_B_metadata')/moles:dgMetadata/moles:dgPerson[moles:dgMetadataID/moles:repositoryIdentifier=$repid and moles:dgMetadataID/moles:localIdentifier=$locid] return element {$out-element-name}{($person/moles:name),($person/moles:contactDetails),($person/moles:dgMetadataID)})};declare function f:return-stub-organisation($out-element-name as xs:string, $repid as xs:string, $locid as xs:string) as element(){let $found := count(collection('/db/ndg_B_metadata')/moles:dgMetadata/moles:dgOrganisation[moles:dgMetadataID/moles:repositoryIdentifier=$repid and moles:dgMetadataID/moles:localIdentifier=$locid]) return if ($found = 0) then element {$out-element-name} {element error {'not found'},element repositoryIdentifier {$repid},element localIdentifier {$locid}} else (for $org in collection('/db/ndg_B_metadata')/moles:dgMetadata/moles:dgOrganisation[moles:dgMetadataID/moles:repositoryIdentifier=$repid and moles:dgMetadataID/moles:localIdentifier=$locid] return element {$out-element-name} {$org/moles:dgMetadataID,$org/moles:name,$org/moles:abbreviation,$org/moles:contactDetails,$org/moles:logos})};for $DPT in collection('/db/ndg_B_metadata')/moles:dgMetadata/moles:dgMetadataRecord[moles:dgDataProductionTool!='' and moles:dgMetadataID/moles:schemeIdentifier='NDG-B0' and moles:dgMetadataID/moles:repositoryIdentifier='RepositoryID' and moles:dgMetadataID/moles:localIdentifier='LocalID'] return f:strip-namespace(element dgMetadataRecord {$DPT/dgMetadataID,element dgMetadataID {element schemeIdentifier {'NDG-B1'},$DPT/moles:dgMetadataID/moles:repositoryIdentifier,$DPT/moles:dgMetadataID/moles:localIdentifier},$DPT/moles:dgMetadataDescription,$DPT/moles:name,$DPT/moles:abbreviation,element dgDataProductionTool {$DPT/moles:contactDetails,if (exists($DPT/moles:dgModel)) then $DPT/moles:dgModel else $DPT/moles:dgInstrument,if (exists($DPT/moles:dgDPTRoles)) then element dgDPTRoles {'Roles are available, and will be added soon'} else(),for $RelDep in $DPT/moles:dgDataProductionTool/moles:DPTDeployment return element DPTDeployment {$RelDep/moles:DateStart,$RelDep/moles:DateEnd,$RelDep/moles:dgMetadataID,f:return-stub-activity('activity', data($RelDep/moles:ActivityID/moles:repositoryIdentifier), data($RelDep/moles:ActivityID/moles:localIdentifier)),f:return-stub-obsstn('observationstation', data($RelDep/moles:ObservationStationID/moles:repositoryIdentifier), data($RelDep/moles:ObservationStationID/moles:localIdentifier)),for $DE in collection('/db/ndg_B_metadata')/moles:dgMetadata/moles:dgMetadataRecord where $DE/moles:dgDataEntity/moles:RelatedDeployment/moles:dgMetadataID/moles:repositoryIdentifier=$RelDep/moles:dgMetadataID/moles:repositoryIdentifier and $DE/moles:dgDataEntity/moles:RelatedDeployment/moles:dgMetadataID/moles:localIdentifier=$RelDep/moles:dgMetadataID/moles:localIdentifier return f:return-stub-dataentity('dataentity', data($DE/moles:dgMetadataID/moles:repositoryIdentifier), data($DE/moles:dgMetadataID/moles:localIdentifier)),$RelDep/moles:Coverage}},for $strkwrd in $DPT/moles:dgStructuredKeyword return $strkwrd,if (exists($DPT/moles:dgMetadataProvenance)) then $DPT/moles:dgMetadataProvenance else (),if (exists($DPT/moles:dgMetadataSecurity)) then $DPT/moles:dgMetadataSecurity else ()})''' 
    19  
    20 StubBObsStnQuery = '''declare default element namespace 'http://ndg.nerc.ac.uk/moles/observationstation';declare namespace moles='http://ndg.nerc.ac.uk/moles';declare namespace f='http://ndg.nerc.ac.uk/moles/localfunctions';declare function f:strip-namespace($e as element()) as element(){element {QName('', local-name($e))}{let $parent :=$e let $childcount := count($parent/*) return if ($childcount>0) then for $child in ($e/*) return f:strip-namespace($child) else (data($e))}};declare function f:return-stub-activity($out-element-name as xs:string, $repid as xs:string, $locid as xs:string) as element(){let $found := count(collection('/db/ndg_B_metadata')/moles:dgMetadata/moles:dgMetadataRecord[ moles:dgMetadataID/moles:repositoryIdentifier=$repid and moles:dgMetadataID/moles:localIdentifier=$locid]) return if ($found = 0) then element {$out-element-name} {element error {'not found'},element repositoryIdentifier {$repid},element localIdentifier {$locid}}else (for $SRAct in collection('/db/ndg_B_metadata')/moles:dgMetadata/moles:dgMetadataRecord[ moles:dgMetadataID/moles:repositoryIdentifier=$repid and moles:dgMetadataID/moles:localIdentifier=$locid] return element {$out-element-name} {($SRAct/moles:dgMetadataID),($SRAct/moles:dgMetadataDescription),($SRAct/moles:name),($SRAct/moles:abbreviation),element dgActivity {if (exists($SRAct/*/moles:dgActivityDataCollection)) then ($SRAct/*/moles:dgActivityDataCollection) else if (exists($SRAct/*/moles:dgActivityDataProject)) then ($SRAct/*/moles:dgActivityDataProject) else if (exists($SRAct/*/dgActivityDataCampaign)) then ($SRAct/moles:dgActivity/moles:dgActivityDataCampaign) else ($SRAct/moles:dgActivity/moles:dgActivityDataInvestigation),($SRAct/moles:dgActivity/moles:dgActivityCoverage),($SRAct/moles:dgActivity/moles:dgActivityDuration)}})};declare function f:return-stub-dataentity($out-element-name as xs:string, $repid as xs:string, $locid as xs:string) as element(){let $found := count(collection('/db/ndg_B_metadata')/moles:dgMetadata/moles:dgMetadataRecord[ moles:dgMetadataID/moles:repositoryIdentifier=$repid and moles:dgMetadataID/moles:localIdentifier=$locid]) return if ($found = 0) then element {$out-element-name} {element error {'not found'},element repositoryIdentifier {$repid},element localIdentifier {$locid}} else (for $SRDE in collection('/db/ndg_B_metadata')/moles:dgMetadata/moles:dgMetadataRecord[moles:dgMetadataID/moles:repositoryIdentifier=$repid and moles:dgMetadataID/moles:localIdentifier=$locid] return element {$out-element-name} {($SRDE/moles:dgMetadataID),($SRDE/moles:dgMetadataDescription),($SRDE/moles:name),($SRDE/moles:abbreviation),element dgDataEntity {($SRDE/moles:dgDataEntity/moles:dgDataSetType),($SRDE/moles:dgDataEntity/moles:dgDataGranule),($SRDE/moles:dgDataEntity/moles:dgDataSummary)}})};declare function f:return-stub-dpt($out-element-name as xs:string, $repid as xs:string, $locid as xs:string) as element(){let $found := count(collection('/db/ndg_B_metadata')/moles:dgMetadata/moles:dgMetadataRecord[moles:dgMetadataID/moles:repositoryIdentifier=$repid and moles:dgMetadataID/moles:localIdentifier=$locid]) return if ($found = 0) then element {$out-element-name} {element error {'not found'},element repositoryIdentifier {$repid},element localIdentifier {$locid}} else (for $SRDPT in collection('/db/ndg_B_metadata')/moles:dgMetadata/moles:dgMetadataRecord[moles:dgMetadataID/moles:repositoryIdentifier=$repid and moles:dgMetadataID/moles:localIdentifier=$locid] return element {$out-element-name} {$SRDPT/moles:dgMetadataID,$SRDPT/moles:dgMetadataDescription,$SRDPT/moles:name,$SRDPT/moles:abbreviation,$SRDPT/moles:logos,element dgDataProductionTool {$SRDPT/moles:dgDataProductionTool/moles:contactDetails,if (exists($SRDPT/moles:dgDataProductionTool/moles:dgModel)) then $SRDPT/moles:dgDataProductionTool/moles:dgModel else (),if (exists($SRDPT/moles:dgDataProductionTool/moles:dgInstrument)) then $SRDPT/moles:dgDataProductionTool/moles:dgInstrument else ()}})};declare function f:return-stub-obsstn($out-element-name as xs:string, $repid as xs:string, $locid as xs:string) as element(){let $found := count(collection('/db/ndg_B_metadata')/moles:dgMetadata/moles:dgMetadataRecord[moles:dgMetadataID/moles:repositoryIdentifier=$repid and moles:dgMetadataID/moles:localIdentifier=$locid]) return if ($found = 0) then element {$out-element-name} {element error {'not found'},element repositoryIdentifier {$repid},element localIdentifier {$locid}} else (for $SRObsStn in collection('/db/ndg_B_metadata')/moles:dgMetadata/moles:dgMetadataRecord[moles:dgMetadataID/moles:repositoryIdentifier=$repid and moles:dgMetadataID/moles:localIdentifier=$locid] return element {$out-element-name} {($SRObsStn/moles:dgMetadataID),($SRObsStn/moles:dgMetadataDescription),($SRObsStn/moles:name),($SRObsStn/moles:abbreviation),element dgObservationStation {($SRObsStn/moles:dgObservationStation/moles:contactDetails),if (exists($SRObsStn/moles:dgObservationStation/moles:dgStationaryPlatform)) then element dgStationaryPlatform {$SRObsStn/moles:dgObservationStation/moles:dgStationaryPlatform/moles:position,if (exists($SRObsStn/moles:dgObservationStation/moles:dgStationaryPlatform/dgLandStation)) then element dgLandStation {''} else if (exists($SRObsStn/moles:dgObservationStation/moles:dgStationaryPlatform/dgMooring)) then element dgMooring {''} else if (exists($SRObsStn/moles:dgObservationStation/moles:dgStationaryPlatform/dgStationGroup)) then element dgStationGroup {''} else ()} else if (exists($SRObsStn/moles:dgObservationStation/moles:dgMovingPlatform)) then $SRObsStn/moles:dgObservationStation/moles:dgMovingPlatform else ()}})};declare function f:return-stub-person($out-element-name as xs:string, $repid as xs:string, $locid as xs:string) as element(){let $found := count(collection('/db/ndg_B_metadata')/moles:dgMetadata/moles:dgPerson[moles:dgMetadataID/moles:repositoryIdentifier=$repid and moles:dgMetadataID/moles:localIdentifier=$locid]) return if ($found = 0) then element {$out-element-name} {element error {'not found'},element repositoryIdentifier {$repid},element localIdentifier {$locid}} else (for $person in collection('/db/ndg_B_metadata')/moles:dgMetadata/moles:dgPerson[moles:dgMetadataID/moles:repositoryIdentifier=$repid and moles:dgMetadataID/moles:localIdentifier=$locid] return element {$out-element-name}{($person/moles:name),($person/moles:contactDetails),($person/moles:dgMetadataID)})};declare function f:return-stub-organisation($out-element-name as xs:string, $repid as xs:string, $locid as xs:string) as element(){let $found := count(collection('/db/ndg_B_metadata')/moles:dgMetadata/moles:dgOrganisation[moles:dgMetadataID/moles:repositoryIdentifier=$repid and moles:dgMetadataID/moles:localIdentifier=$locid]) return if ($found = 0) then element {$out-element-name} {element error {'not found'},element repositoryIdentifier {$repid},element localIdentifier {$locid}} else (for $org in collection('/db/ndg_B_metadata')/moles:dgMetadata/moles:dgOrganisation[moles:dgMetadataID/moles:repositoryIdentifier=$repid and moles:dgMetadataID/moles:localIdentifier=$locid] return element {$out-element-name} {$org/moles:dgMetadataID,$org/moles:name,$org/moles:abbreviation,$org/moles:contactDetails,$org/moles:logos})};for $ObsStn in collection('/db/ndg_B_metadata')/moles:dgMetadata/moles:dgMetadataRecord[moles:dgObservationStation!='' and moles:dgMetadataID/moles:schemeIdentifier='NDG-B0' and moles:dgMetadataID/moles:repositoryIdentifier='RepositoryID' and moles:dgMetadataID/moles:localIdentifier='LocalID'] return f:strip-namespace(element dgMetadataRecord {element dgMetadataID {element schemeIdentifier {'NDG-B1',$ObsStn/moles:dgMetadataID/moles:repositoryIdentifier,$ObsStn/moles:dgMetadataID/moles:localIdentifier},$ObsStn/moles:dgMetadataDescription,$ObsStn/moles:name,$ObsStn/moles:abbreviation,element dgObservationStation {$ObsStn/moles:dgObservationStation/moles:contactDetails,if (exists($ObsStn/moles:dgObservationStation/moles:dgStationaryPlatform)) then element dgStationaryPlatform {$ObsStn/moles:dgObservationStation/moles:dgStationaryPlatform/moles:position,if (exists($ObsStn/moles:dgObservationStation/moles:dgStationaryPlatform/moles:dgMooring)) then element dgMooring {$ObsStn/moles:dgObservationStation/moles:dgStationaryPlatform/moles:dgMooring/moles:dateStart,$ObsStn/moles:dgObservationStation/moles:dgStationaryPlatform/moles:dgMooring/moles:dateEnd,for $DepositingCruise in $ObsStn/moles:dgObservationStation/moles:dgStationaryPlatform/moles:dgMooring/moles:DepositingCruise return f:return-stub-activity('depositingcruise', data($DepositingCruise/moles:dgMetadataID/moles:repositoryIdentifier), data($DepositingCruise/moles:dgMetadataID/moles:localIdentifier)),if (exists($ObsStn/moles:dgObservationStation/moles:dgStationaryPlatform/moles:dgMooring/moles:dgStationGrouping)) then for $StationGrouping in $ObsStn/moles:dgObservationStation/moles:dgStationaryPlatform/moles:dgMooring/dgStationGrouping return f:return-stub-obsstn('stationgrouping', data($StationGrouping/moles:dgMetadataID/moles:repositoryIdentifier), data($StationGrouping/moles:dgMetadataID/moles:localIdentifier)) else ()} else if (exists($ObsStn/dgObservationStation/dgStationaryPlatform/dgStationGroup)) then element dgStationGroup {element dgGroupedStations {if (exists($ObsStn/dgObservationStation/dgStationaryPlatform/dgStationGroup/dgGroupedStations)) then for $GroupedStation in $ObsStn/dgObservationStation/dgStationaryPlatform/dgStationGroup/dgGroupedStation/dgGroupedStations return f:return-stub-obsstn('dgGroupedStation', data($GroupedStation/moles:dgMetadataID/moles:repositoryIdentifier), data($GroupedStation/moles:dgMetadataID/moles:localIdentifier)) else ()}} else $ObsStn/moles:dgObservationStation/moles:dgLandStation} else $ObsStn/moles:dgObservationStation/moles:dgMovingPlatform},for $RelDep in $ObsStn/moles:dgObservationStation/moles:ObsStationDeployment return element ObsStationDeployment {$RelDep/moles:DateStart,$RelDep/moles:DateEnd,$RelDep/moles:dgMetadataID,f:return-stub-activity('activity', data($RelDep/moles:ActivityID/moles:repositoryIdentifier), data($RelDep/moles:ActivityID/moles:localIdentifier)),f:return-stub-dpt('dataproductiontool', data($RelDep/moles:DataProductionToolID/moles:repositoryIdentifier), data($RelDep/moles:DataProductionToolID/moles:localIdentifier)),for $DE in collection('/db/ndg_B_metadata')/moles:dgMetadata/moles:dgMetadataRecord where $DE/moles:dgDataEntity/moles:RelatedDeployment/moles:dgMetadataID/moles:repositoryIdentifier=$RelDep/moles:dgMetadataID/moles:repositoryIdentifier and $DE/moles:dgDataEntity/moles:RelatedDeployment/moles:dgMetadataID/moles:localIdentifier=$RelDep/moles:dgMetadataID/moles:localIdentifier return f:return-stub-dataentity('dataentity', data($DE/moles:dgMetadataID/moles:repositoryIdentifier), data($DE/moles:dgMetadataID/moles:localIdentifier)),$RelDep/moles:Coverage}},for $strkwrd in $ObsStn/moles:dgStructuredKeyword return $strkwrd,if (exists($ObsStn/moles:dgMetadataProvenance)) then $ObsStn/moles:dgMetadataProvenance else (),if (exists($ObsStn/moles:dgMetadataSecurity)) then $ObsStn/moles:dgMetadataSecurity else ()})''' 
    21  
    22 MOLESQuery = '''declare default element namespace 'http://ndg.nerc.ac.uk/moles';for $Moles in collection('/db/ndg_B_metadata')/dgMetadata/dgMetadataRecord[dgMetadataID/schemeIdentifier='NDG-B0' and dgMetadataID/repositoryIdentifier='RepositoryID' and dgMetadataID/localIdentifier='LocalID'] return $Moles''' 
    23  
    24 DublinCoreDEQuery = '''declare default element namespace 'http://ndg.nerc.ac.uk/moles'; 
    25 declare namespace xsi='http://www.w3.org/2001/XMLSchema-instance';  
    26 declare namespace dc='http://purl.org/dc/elements/1.1/'; 
    27 declare namespace oai_dc='http://www.openarchives.org/OAI/2.0/oai_dc'; 
    28 declare variable $targetCollection as xs:string {'/db/ndg_B_metadata'}; 
    29 declare variable $gcmd_science_valids as xs:string {'http://gcmd.gsfc.nasa.gov/Resources/valids/gcmd_parameters.html'}; 
    30 declare variable $gcmd_project_valids as xs:string {'http://gcmd.gsfc.nasa.gov/Resources/valids/projects.html'}; 
    31 declare variable $cf_standard_names as xs:string {'http://www.cgd.ucar.edu/cms/eaton/cf-metadata/standard_name.html'}; 
    32 declare variable $iso_topic_list as xs:string {'http://www.isotc211.org/2005/resources/Codelist/gmxCodelists.xml#MD_TopicCategoryCode'}; 
    33  
    34 for $DE in collection($targetCollection)/dgMetadata/dgMetadataRecord[dgDataEntity!='' and dgMetadataID/schemeIdentifier='NDG-B0' and dgMetadataID/repositoryIdentifier='RepositoryID' and dgMetadataID/localIdentifier='LocalID'] 
     14StubBDEQuery = '''import module namespace voclib='http://ndg.nerc.ac.uk/xquery/lib/vocab' at 'xmldb:exist:///db/xqueryLib/Vocabs/vocab_xquery_lib.xquery'; 
     15import module namespace utillib='http://ndg.nerc.ac.uk/xquery/lib/utilities' at 'xmldb:exist:///db/xqueryLib/Utilities/utility_xquery_lib.xquery'; 
     16(:  
     17Will return when scope problem sussed... ;( 
     18import module namespace stubblib='http://ndg.nerc.ac.uk/xquery/lib/stubb' at 'xmldb:exist:///db/xqueryLib/StubB/stubb_xquery_lib.xquery'; 
     19:) 
     20declare namespace f='http://ndg.nerc.ac.uk/moles/localfunctions'; 
     21declare default element namespace 'http://ndg.nerc.ac.uk/moles/dataentity';  
     22declare namespace moles='http://ndg.nerc.ac.uk/moles'; 
     23 
     24declare variable $targetCollection as xs:string {'TargetCollection'}; 
     25declare variable $repositoryIdentifier as xs:string {'RepositoryID'}; 
     26declare variable $localIdentifier as xs:string {'LocalID'}; 
     27 
     28declare function f:return-stub-activity($out-element-name as xs:string, $repid as xs:string, $locid as xs:string) as element()  
     29(: Returns a 'stubB' style activity record to expand an activity record :) 
     30(: Security not implemented here yet :) 
     31{ 
     32let $found := count(collection($targetCollection)/moles:dgMetadata/moles:dgMetadataRecord[ 
     33            moles:dgMetadataID/moles:repositoryIdentifier=$repid  
     34            and moles:dgMetadataID/moles:localIdentifier=$locid]) 
     35return   
     36    if ($found = 0) then  
     37        element {$out-element-name} { 
     38            element error {'not found'}, 
     39            element repositoryIdentifier {$repid}, 
     40            element localIdentifier {$locid} 
     41        }  
     42    else ( 
     43        for $SRAct in collection($targetCollection)/moles:dgMetadata/moles:dgMetadataRecord[ 
     44            moles:dgMetadataID/moles:repositoryIdentifier=$repid  
     45            and moles:dgMetadataID/moles:localIdentifier=$locid]  
     46        return  
     47            element  {$out-element-name} {  
     48                ($SRAct/moles:dgMetadataID), 
     49                ($SRAct/moles:dgMetadataDescription), 
     50                ($SRAct/moles:name), 
     51                ($SRAct/moles:abbreviation), 
     52                element dgActivity { 
     53                    if (exists($SRAct/*/moles:dgActivityDataCollection)) then  
     54                        ($SRAct/*/moles:dgActivityDataCollection) 
     55                    else if (exists($SRAct/*/moles:dgActivityDataProject)) then  
     56                        ($SRAct/*/moles:dgActivityDataProject) 
     57                    else if (exists($SRAct/*/dgActivityDataCampaign)) then 
     58                        ($SRAct/moles:dgActivity/moles:dgActivityDataCampaign) 
     59                    else  
     60                        ($SRAct/moles:dgActivity/moles:dgActivityDataInvestigation),  
     61                    ($SRAct/moles:dgActivity/moles:dgActivityCoverage), 
     62                    ($SRAct/moles:dgActivity/moles:dgActivityDuration) 
     63                } (: </dgActivity> :)  
     64            } (: </activity> :)  
     65    ) 
     66} ; 
     67 
     68declare function f:return-stub-dataentity($out-element-name as xs:string, $repid as xs:string, $locid as xs:string) as element()  
     69(: Returns a 'stubB' style observation station record to expand an data entity record :) 
     70(: Security not implemented yet :) 
     71{ 
     72let $found := count(collection($targetCollection)/moles:dgMetadata/moles:dgMetadataRecord[ 
     73        moles:dgMetadataID/moles:repositoryIdentifier=$repid  
     74        and moles:dgMetadataID/moles:localIdentifier=$locid]) 
     75return   
     76    if ($found = 0) then  
     77        element {$out-element-name} { 
     78            element error {'not found'}, 
     79            element repositoryIdentifier {$repid}, 
     80            element localIdentifier {$locid} 
     81        }  
     82    else ( 
     83        for $SRDE in collection($targetCollection)/moles:dgMetadata/moles:dgMetadataRecord[ 
     84            moles:dgMetadataID/moles:repositoryIdentifier=$repid  
     85            and moles:dgMetadataID/moles:localIdentifier=$locid]  
     86        return   
     87            element {$out-element-name} { 
     88                ($SRDE/moles:dgMetadataID), 
     89                ($SRDE/moles:dgMetadataDescription), 
     90                ($SRDE/moles:name), 
     91                ($SRDE/moles:abbreviation), 
     92                element dgDataEntity { 
     93                    ($SRDE/moles:dgDataEntity/moles:dgDataSetType), 
     94                    ($SRDE/moles:dgDataEntity/moles:dgDataGranule), 
     95                    ($SRDE/moles:dgDataEntity/moles:dgDataSummary) 
     96                } (: </dgDataEntity> :)         
     97            } 
     98    ) 
     99} ; 
     100 
     101declare function f:return-stub-dpt($out-element-name as xs:string, $repid as xs:string, $locid as xs:string) as element()  
     102(: Returns a 'stubB' style dpt record to expand a dpt record :) 
     103(: Security not implemented yet :) 
     104{ 
     105let $found := count(collection($targetCollection)/moles:dgMetadata/moles:dgMetadataRecord[ 
     106        moles:dgMetadataID/moles:repositoryIdentifier=$repid  
     107        and moles:dgMetadataID/moles:localIdentifier=$locid]) 
     108return   
     109    if ($found = 0) then  
     110        element {$out-element-name} { 
     111            element error {'not found'}, 
     112            element repositoryIdentifier {$repid}, 
     113            element localIdentifier {$locid} 
     114        }  
     115    else ( 
     116        for $SRDPT in collection($targetCollection)/moles:dgMetadata/moles:dgMetadataRecord[ 
     117                moles:dgMetadataID/moles:repositoryIdentifier=$repid  
     118                and moles:dgMetadataID/moles:localIdentifier=$locid] 
     119        return   
     120            element {$out-element-name} { 
     121                $SRDPT/moles:dgMetadataID, 
     122                $SRDPT/moles:dgMetadataDescription, 
     123                $SRDPT/moles:name, 
     124                $SRDPT/moles:abbreviation, 
     125                $SRDPT/moles:logos, 
     126                element dgDataProductionTool { 
     127                    $SRDPT/moles:dgDataProductionTool/moles:contactDetails, 
     128                    if (exists($SRDPT/moles:dgDataProductionTool/moles:dgModel)) then 
     129                        $SRDPT/moles:dgDataProductionTool/moles:dgModel 
     130                    else (), 
     131                    if (exists($SRDPT/moles:dgDataProductionTool/moles:dgInstrument)) then 
     132                        $SRDPT/moles:dgDataProductionTool/moles:dgInstrument 
     133                    else () 
     134                    } (: </dgDataProductionTool> :) 
     135                } (: </dataproductiontool> :) 
     136    ) 
     137} ; 
     138 
     139declare function f:return-stub-obsstn($out-element-name as xs:string, $repid as xs:string, $locid as xs:string) as element()  
     140(: Returns a 'stubB' style observation station record to expand an observation station record :) 
     141(: Security not implemented here yet;  :) 
     142{ 
     143let $found := count(collection($targetCollection)/moles:dgMetadata/moles:dgMetadataRecord[ 
     144            moles:dgMetadataID/moles:repositoryIdentifier=$repid  
     145            and moles:dgMetadataID/moles:localIdentifier=$locid]) 
     146return   
     147    if ($found = 0) then  
     148        element {$out-element-name} { 
     149            element error {'not found'}, 
     150            element repositoryIdentifier {$repid}, 
     151            element localIdentifier {$locid} 
     152        }  
     153    else ( 
     154        for $SRObsStn in collection($targetCollection)/moles:dgMetadata/moles:dgMetadataRecord[ 
     155            moles:dgMetadataID/moles:repositoryIdentifier=$repid  
     156            and moles:dgMetadataID/moles:localIdentifier=$locid]  
     157        return 
     158            element {$out-element-name} { 
     159                ($SRObsStn/moles:dgMetadataID), 
     160                ($SRObsStn/moles:dgMetadataDescription), 
     161                ($SRObsStn/moles:name), 
     162                ($SRObsStn/moles:abbreviation), 
     163                element dgObservationStation { 
     164                    ($SRObsStn/moles:dgObservationStation/moles:contactDetails), 
     165                    if (exists($SRObsStn/moles:dgObservationStation/moles:dgStationaryPlatform)) then 
     166                        element dgStationaryPlatform { 
     167                            $SRObsStn/moles:dgObservationStation/moles:dgStationaryPlatform/moles:position, 
     168                            if (exists($SRObsStn/moles:dgObservationStation/moles:dgStationaryPlatform/dgLandStation)) then element dgLandStation {''} 
     169                            else if (exists($SRObsStn/moles:dgObservationStation/moles:dgStationaryPlatform/dgMooring)) then element dgMooring {''} 
     170                            else if (exists($SRObsStn/moles:dgObservationStation/moles:dgStationaryPlatform/dgStationGroup)) then element dgStationGroup {''} 
     171                            else () 
     172                            } 
     173                    else if (exists($SRObsStn/moles:dgObservationStation/moles:dgMovingPlatform)) then  
     174                        $SRObsStn/moles:dgObservationStation/moles:dgMovingPlatform 
     175                    else () 
     176                } (: </dgObservationStation> :) 
     177            } 
     178    ) 
     179} ; 
     180 
     181declare function f:return-stub-person($out-element-name as xs:string, $repid as xs:string, $locid as xs:string) as element()  
     182(: Returns a 'stubB' style observation station record to expand a person record :) 
     183(: Security not implemented yet :) 
     184{ 
     185let $found := count(collection($targetCollection)/moles:dgMetadata/moles:dgPerson[ 
     186            moles:dgMetadataID/moles:repositoryIdentifier=$repid  
     187            and moles:dgMetadataID/moles:localIdentifier=$locid]) 
     188return   
     189    if ($found = 0) then  
     190        element {$out-element-name} { 
     191            element error {'not found'}, 
     192            element repositoryIdentifier {$repid}, 
     193            element localIdentifier {$locid} 
     194        }  
     195    else ( 
     196    for $person in collection($targetCollection)/moles:dgMetadata/moles:dgPerson[ 
     197        moles:dgMetadataID/moles:repositoryIdentifier=$repid  
     198        and moles:dgMetadataID/moles:localIdentifier=$locid]  
     199    return  
     200        element  {$out-element-name}  
     201        { 
     202            ($person/moles:name), 
     203            ($person/moles:contactDetails), 
     204            ($person/moles:dgMetadataID) 
     205        } 
     206    ) 
     207} ; 
     208 
     209declare function f:return-stub-organisation($out-element-name as xs:string, $repid as xs:string, $locid as xs:string) as element()  
     210(: Returns a 'stubB' style observation station record to expand an organisation record :) 
     211(: Security not implemented yet :) 
     212{ 
     213    let $found := count(collection($targetCollection)/moles:dgMetadata/moles:dgOrganisation[ 
     214                moles:dgMetadataID/moles:repositoryIdentifier=$repid  
     215                and moles:dgMetadataID/moles:localIdentifier=$locid]) 
     216    return   
     217        if ($found = 0) then  
     218            element {$out-element-name} { 
     219                element error {'not found'}, 
     220                element repositoryIdentifier {$repid}, 
     221                element localIdentifier {$locid} 
     222            }  
     223        else ( 
     224            for $org in collection($targetCollection)/moles:dgMetadata/moles:dgOrganisation[ 
     225                moles:dgMetadataID/moles:repositoryIdentifier=$repid  
     226                and moles:dgMetadataID/moles:localIdentifier=$locid]  
     227            return  
     228                element  {$out-element-name} { 
     229                    $org/moles:dgMetadataID, 
     230                    $org/moles:name, 
     231                    $org/moles:abbreviation, 
     232                    $org/moles:contactDetails, 
     233                    $org/moles:logos 
     234                } 
     235        ) 
     236} ; 
     237 
     238for $DE in collection($targetCollection)/moles:dgMetadata/moles:dgMetadataRecord[ 
     239exists(moles:dgDataEntity)  
     240and moles:dgMetadataID[ 
     241moles:schemeIdentifier='NDG-B0'  
     242and moles:repositoryIdentifier='badc.nerc.ac.uk'  
     243and moles:localIdentifier='dataent1']] 
     244return 
     245utillib:strip-namespace(element dgMetadataRecord { 
     246    element dgMetadataID { 
     247        element schemeIdentifier {'NDG-B1'}, 
     248        $DE/moles:dgMetadataID/moles:repositoryIdentifier, 
     249        $DE/moles:dgMetadataID/moles:localIdentifier 
     250    }, (: </dgMetadataID> :) 
     251    $DE/moles:dgMetadataDescription, 
     252    $DE/moles:name, 
     253    $DE/moles:abbreviation, 
     254    element dgDataEntity { 
     255        $DE/moles:dgDataEntity/moles:dgDataSetType, 
     256        $DE/moles:dgDataEntity/moles:dgDataObjectType, 
     257        $DE/moles:dgDataEntity/moles:dgDataGranule, 
     258        $DE/moles:dgDataEntity/moles:dgDataSummary, 
     259        element dgDataRoles { 
     260            if (exists($DE/moles:dgDataEntity/moles:dgDataRoles/moles:dgDataCreator/moles:dgMetadataID)) then  
     261                element dgDataCreator { 
     262                $DE/moles:dgDataEntity/moles:dgDataRoles/moles:dgDataCreator/moles:dgMetadataID, 
     263                $DE/moles:dgDataEntity/moles:dgDataRoles/moles:dgDataCreator/moles:roleName, 
     264                $DE/moles:dgDataEntity/moles:dgDataRoles/moles:dgDataCreator/moles:abbreviation, 
     265                $DE/moles:dgDataEntity/moles:dgDataRoles/moles:dgDataCreator/moles:contactDetails, 
     266                for $DataCreatorRole in $DE/moles:dgDataEntity/moles:dgDataRoles/moles:dgDataCreator/moles:dgRoleHolder  
     267                    order by $DataCreatorRole/moles:startDate empty least, $DataCreatorRole/moles:endDate empty least 
     268                    return  
     269                        element dgRoleHolder { 
     270                            if (exists($DataCreatorRole/moles:dgOrganisationID)) then  
     271                                f:return-stub-organisation('organisation', data($DataCreatorRole/moles:dgOrganisationID/moles:repositoryIdentifier), data($DataCreatorRole/moles:dgOrganisationID/moles:localIdentifier))  
     272                             else if (exists($DataCreatorRole/moles:dgPersonID)) then  
     273                                f:return-stub-person('person', string($DataCreatorRole/moles:dgPersonID/moles:repositoryIdentifier), string($DataCreatorRole/moles:dgPersonID/moles:localIdentifier))  
     274                             else (), 
     275                            $DataCreatorRole/moles:startDate, 
     276                            $DataCreatorRole/moles:endDate, 
     277                            $DataCreatorRole/moles:localName 
     278                        } (: </dgRoleHolder> :) 
     279                    } (: </dgDataCreator>) :) 
     280            else (), 
     281            if (exists($DE/moles:dgDataEntity/moles:dgDataRoles/moles:dgDataCurator/moles:dgMetadataID)) then  
     282                element dgDataCurator { 
     283                $DE/moles:dgDataEntity/moles:dgDataRoles/moles:dgDataCurator/moles:dgMetadataID, 
     284                $DE/moles:dgDataEntity/moles:dgDataRoles/moles:dgDataCurator/moles:roleName, 
     285                $DE/moles:dgDataEntity/moles:dgDataRoles/moles:dgDataCurator/moles:abbreviation, 
     286                $DE/moles:dgDataEntity/moles:dgDataRoles/moles:dgDataCurator/moles:contactDetails, 
     287                for $DataCuratorRole in $DE/moles:dgDataEntity/moles:dgDataRoles/moles:dgDataCurator/moles:dgRoleHolder  
     288                    order by $DataCuratorRole/moles:startDate empty least, $DataCuratorRole/moles:endDate empty least 
     289                    return  
     290                        element dgRoleHolder { 
     291                            if (exists($DataCuratorRole/moles:dgOrganisationID)) then  
     292                                f:return-stub-organisation('organisation', data($DataCuratorRole/moles:dgOrganisationID/moles:repositoryIdentifier), data($DataCuratorRole/moles:dgOrganisationID/moles:localIdentifier))  
     293                             else if (exists($DataCuratorRole/moles:dgPersonID)) then  
     294                                f:return-stub-person('person', string($DataCuratorRole/moles:dgPersonID/moles:repositoryIdentifier), string($DataCuratorRole/moles:dgPersonID/moles:localIdentifier))  
     295                             else (), 
     296                            $DataCuratorRole/moles:startDate, 
     297                            $DataCuratorRole/moles:endDate, 
     298                            $DataCuratorRole/moles:localName 
     299                        } (: </dgRoleHolder> :) 
     300            } (: </dgDataCurator> :)  
     301            else (), 
     302            if (exists($DE/moles:dgDataEntity/moles:dgDataRoles/moles:dgDataOtherRoles/moles:dgMetadataID)) then  
     303                element dgDataOtherRoles { 
     304                $DE/moles:dgDataEntity/moles:dgDataRoles/moles:dgDataOtherRoles/moles:dgMetadataID, 
     305                $DE/moles:dgDataEntity/moles:dgDataRoles/moles:dgDataOtherRoles/moles:roleName, 
     306                $DE/moles:dgDataEntity/moles:dgDataRoles/moles:dgDataOtherRoles/moles:abbreviation, 
     307                $DE/moles:dgDataEntity/moles:dgDataRoles/moles:dgDataOtherRoles/moles:contactDetails, 
     308                for $DataOtherRolesRole in $DE/moles:dgDataEntity/moles:dgDataRoles/moles:dgDataOtherRoles/moles:dgRoleHolder  
     309                    order by $DataOtherRolesRole/moles:startDate empty least, $DataOtherRolesRole/moles:endDate empty least 
     310                    return 
     311                        element dgRoleHolder { 
     312                            if (exists($DataOtherRolesRole/moles:dgOrganisationID)) then  
     313                                f:return-stub-organisation('organisation', data($DataOtherRolesRole/moles:dgOrganisationID/moles:repositoryIdentifier), data($DataOtherRolesRole/moles:dgOrganisationID/moles:localIdentifier))  
     314                             else if (exists($DataOtherRolesRole/moles:dgPersonID)) then  
     315                                f:return-stub-person('person', string($DataOtherRolesRole/moles:dgPersonID/moles:repositoryIdentifier), string($DataOtherRolesRole/moles:dgPersonID/moles:localIdentifier))  
     316                             else (), 
     317                            $DataOtherRolesRole/moles:startDate, 
     318                            $DataOtherRolesRole/moles:endDate, 
     319                            $DataOtherRolesRole/moles:localName 
     320                        } (: </dgRoleHolder> :) 
     321            } (: </dgDataOtherRoles> :) 
     322            else () 
     323        } , (: </dgDataRoles> :) 
     324        for $RelDep in $DE/moles:dgDataEntity/moles:RelatedDeployment 
     325            return 
     326            element RelatedDeployment { 
     327            $RelDep/moles:DateStart, 
     328            $RelDep/moles:DateEnd, 
     329            $RelDep/moles:dgMetadataID, 
     330            f:return-stub-activity('activity', data($RelDep/moles:ActivityID/moles:repositoryIdentifier), data($RelDep/moles:ActivityID/moles:localIdentifier)), 
     331            f:return-stub-dpt('dataproductiontool', data($RelDep/moles:DataProductionToolID/moles:repositoryIdentifier), data($RelDep/moles:DataProductionToolID/moles:localIdentifier)), 
     332            f:return-stub-obsstn('observationstation', data($RelDep/moles:ObservationStationID/moles:repositoryIdentifier), data($RelDep/moles:ObservationStationID/moles:localIdentifier)), 
     333            $RelDep/moles:Coverage 
     334        } (: </RelatedDeployment> :)  
     335        , 
     336        for $RelatedDataEntity in $DE/moles:dgDataEntity/moles:dgRelatedDataEntity 
     337            return 
     338                element relateddataentity { 
     339                element relation { 
     340                    $RelatedDataEntity/moles:RelationID/moles:dgValidTerm, 
     341                    $RelatedDataEntity/moles:RelationID/moles:dgValidTermID 
     342                }, (: </relation> :) 
     343                f:return-stub-dataentity('dataentity', data($RelatedDataEntity/(moles:RelatedDataEntityID | moles:RelatedDataGranuleID)/moles:repositoryIdentifier), data($RelatedDataEntity/(moles:RelatedDataEntityID | moles:RelatedDataGranuleID)/moles:localIdentifier)) 
     344                } (: </relateddataentity> :) 
     345    } (: </dgDataEntity> :), 
     346    for $strkwrd in $DE/moles:dgStructuredKeyword return $strkwrd, 
     347    if (exists($DE/moles:dgMetadataProvenance)) then $DE/moles:dgMetadataProvenance else (), 
     348    if (exists($DE/moles:dgMetadataSecurity)) then $DE/moles:dgMetadataSecurity else () 
     349} (: </dgMetadataRecord> :) 
     350) (: end of namespace strip :)''' 
     351 
     352StubBActQuery = '''import module namespace voclib='http://ndg.nerc.ac.uk/xquery/lib/vocab' at 'xmldb:exist:///db/xqueryLib/Vocabs/vocab_xquery_lib.xquery'; 
     353import module namespace utillib='http://ndg.nerc.ac.uk/xquery/lib/utilities' at 'xmldb:exist:///db/xqueryLib/Utilities/utility_xquery_lib.xquery'; 
     354(:  
     355Will return when scope problem sussed... ;( 
     356import module namespace stubblib='http://ndg.nerc.ac.uk/xquery/lib/stubb' at 'xmldb:exist:///db/xqueryLib/StubB/stubb_xquery_lib.xquery'; 
     357:) 
     358declare namespace f='http://ndg.nerc.ac.uk/moles/localfunctions'; 
     359declare default element namespace 'http://ndg.nerc.ac.uk/moles/activity';  
     360declare namespace moles='http://ndg.nerc.ac.uk/moles'; 
     361 
     362declare variable $targetCollection as xs:string {'TargetCollection'}; 
     363declare variable $repositoryIdentifier as xs:string {'RepositoryID'}; 
     364declare variable $localIdentifier as xs:string {'LocalID'}; 
     365 
     366declare function f:return-stub-activity($out-element-name as xs:string, $repid as xs:string, $locid as xs:string) as element()  
     367(: Returns a 'stubB' style activity record to expand an activity record :) 
     368(: Security not implemented here yet :) 
     369{ 
     370let $found := count(collection($targetCollection)/moles:dgMetadata/moles:dgMetadataRecord[ 
     371            moles:dgMetadataID/moles:repositoryIdentifier=$repid  
     372            and moles:dgMetadataID/moles:localIdentifier=$locid]) 
     373return   
     374    if ($found = 0) then  
     375        element {$out-element-name} { 
     376            element error {'not found'}, 
     377            element repositoryIdentifier {$repid}, 
     378            element localIdentifier {$locid} 
     379        }  
     380    else ( 
     381        for $SRAct in collection($targetCollection)/moles:dgMetadata/moles:dgMetadataRecord[ 
     382            moles:dgMetadataID/moles:repositoryIdentifier=$repid  
     383            and moles:dgMetadataID/moles:localIdentifier=$locid]  
     384        return  
     385            element  {$out-element-name} {  
     386                ($SRAct/moles:dgMetadataID), 
     387                ($SRAct/moles:dgMetadataDescription), 
     388                ($SRAct/moles:name), 
     389                ($SRAct/moles:abbreviation), 
     390                element dgActivity { 
     391                    if (exists($SRAct/*/moles:dgActivityDataCollection)) then  
     392                        ($SRAct/*/moles:dgActivityDataCollection) 
     393                    else if (exists($SRAct/*/moles:dgActivityDataProject)) then  
     394                        ($SRAct/*/moles:dgActivityDataProject) 
     395                    else if (exists($SRAct/*/dgActivityDataCampaign)) then 
     396                        ($SRAct/moles:dgActivity/moles:dgActivityDataCampaign) 
     397                    else  
     398                        ($SRAct/moles:dgActivity/moles:dgActivityDataInvestigation),  
     399                    ($SRAct/moles:dgActivity/moles:dgActivityCoverage), 
     400                    ($SRAct/moles:dgActivity/moles:dgActivityDuration) 
     401                } (: </dgActivity> :)  
     402            } (: </activity> :)  
     403    ) 
     404} ; 
     405 
     406declare function f:return-stub-dataentity($out-element-name as xs:string, $repid as xs:string, $locid as xs:string) as element()  
     407(: Returns a 'stubB' style observation station record to expand an data entity record :) 
     408(: Security not implemented yet :) 
     409{ 
     410let $found := count(collection($targetCollection)/moles:dgMetadata/moles:dgMetadataRecord[ 
     411        moles:dgMetadataID/moles:repositoryIdentifier=$repid  
     412        and moles:dgMetadataID/moles:localIdentifier=$locid]) 
     413return   
     414    if ($found = 0) then  
     415        element {$out-element-name} { 
     416            element error {'not found'}, 
     417            element repositoryIdentifier {$repid}, 
     418            element localIdentifier {$locid} 
     419        }  
     420    else ( 
     421        for $SRDE in collection($targetCollection)/moles:dgMetadata/moles:dgMetadataRecord[ 
     422            moles:dgMetadataID/moles:repositoryIdentifier=$repid  
     423            and moles:dgMetadataID/moles:localIdentifier=$locid]  
     424        return   
     425            element {$out-element-name} { 
     426                ($SRDE/moles:dgMetadataID), 
     427                ($SRDE/moles:dgMetadataDescription), 
     428                ($SRDE/moles:name), 
     429                ($SRDE/moles:abbreviation), 
     430                element dgDataEntity { 
     431                    ($SRDE/moles:dgDataEntity/moles:dgDataSetType), 
     432                    ($SRDE/moles:dgDataEntity/moles:dgDataGranule), 
     433                    ($SRDE/moles:dgDataEntity/moles:dgDataSummary) 
     434                } (: </dgDataEntity> :)         
     435            } 
     436    ) 
     437} ; 
     438 
     439declare function f:return-stub-dpt($out-element-name as xs:string, $repid as xs:string, $locid as xs:string) as element()  
     440(: Returns a 'stubB' style dpt record to expand a dpt record :) 
     441(: Security not implemented yet :) 
     442{ 
     443let $found := count(collection($targetCollection)/moles:dgMetadata/moles:dgMetadataRecord[ 
     444        moles:dgMetadataID/moles:repositoryIdentifier=$repid  
     445        and moles:dgMetadataID/moles:localIdentifier=$locid]) 
     446return   
     447    if ($found = 0) then  
     448        element {$out-element-name} { 
     449            element error {'not found'}, 
     450            element repositoryIdentifier {$repid}, 
     451            element localIdentifier {$locid} 
     452        }  
     453    else ( 
     454        for $SRDPT in collection($targetCollection)/moles:dgMetadata/moles:dgMetadataRecord[ 
     455                moles:dgMetadataID/moles:repositoryIdentifier=$repid  
     456                and moles:dgMetadataID/moles:localIdentifier=$locid] 
     457        return   
     458            element {$out-element-name} { 
     459                $SRDPT/moles:dgMetadataID, 
     460                $SRDPT/moles:dgMetadataDescription, 
     461                $SRDPT/moles:name, 
     462                $SRDPT/moles:abbreviation, 
     463                $SRDPT/moles:logos, 
     464                element dgDataProductionTool { 
     465                    $SRDPT/moles:dgDataProductionTool/moles:contactDetails, 
     466                    if (exists($SRDPT/moles:dgDataProductionTool/moles:dgModel)) then 
     467                        $SRDPT/moles:dgDataProductionTool/moles:dgModel 
     468                    else (), 
     469                    if (exists($SRDPT/moles:dgDataProductionTool/moles:dgInstrument)) then 
     470                        $SRDPT/moles:dgDataProductionTool/moles:dgInstrument 
     471                    else () 
     472                    } (: </dgDataProductionTool> :) 
     473                } (: </dataproductiontool> :) 
     474    ) 
     475} ; 
     476 
     477declare function f:return-stub-obsstn($out-element-name as xs:string, $repid as xs:string, $locid as xs:string) as element()  
     478(: Returns a 'stubB' style observation station record to expand an observation station record :) 
     479(: Security not implemented here yet;  :) 
     480{ 
     481let $found := count(collection($targetCollection)/moles:dgMetadata/moles:dgMetadataRecord[ 
     482            moles:dgMetadataID/moles:repositoryIdentifier=$repid  
     483            and moles:dgMetadataID/moles:localIdentifier=$locid]) 
     484return   
     485    if ($found = 0) then  
     486        element {$out-element-name} { 
     487            element error {'not found'}, 
     488            element repositoryIdentifier {$repid}, 
     489            element localIdentifier {$locid} 
     490        }  
     491    else ( 
     492        for $SRObsStn in collection($targetCollection)/moles:dgMetadata/moles:dgMetadataRecord[ 
     493            moles:dgMetadataID/moles:repositoryIdentifier=$repid  
     494            and moles:dgMetadataID/moles:localIdentifier=$locid]  
     495        return 
     496            element {$out-element-name} { 
     497                ($SRObsStn/moles:dgMetadataID), 
     498                ($SRObsStn/moles:dgMetadataDescription), 
     499                ($SRObsStn/moles:name), 
     500                ($SRObsStn/moles:abbreviation), 
     501                element dgObservationStation { 
     502                    ($SRObsStn/moles:dgObservationStation/moles:contactDetails), 
     503                    if (exists($SRObsStn/moles:dgObservationStation/moles:dgStationaryPlatform)) then 
     504                        element dgStationaryPlatform { 
     505                            $SRObsStn/moles:dgObservationStation/moles:dgStationaryPlatform/moles:position, 
     506                            if (exists($SRObsStn/moles:dgObservationStation/moles:dgStationaryPlatform/dgLandStation)) then element dgLandStation {''} 
     507                            else if (exists($SRObsStn/moles:dgObservationStation/moles:dgStationaryPlatform/dgMooring)) then element dgMooring {''} 
     508                            else if (exists($SRObsStn/moles:dgObservationStation/moles:dgStationaryPlatform/dgStationGroup)) then element dgStationGroup {''} 
     509                            else () 
     510                            } 
     511                    else if (exists($SRObsStn/moles:dgObservationStation/moles:dgMovingPlatform)) then  
     512                        $SRObsStn/moles:dgObservationStation/moles:dgMovingPlatform 
     513                    else () 
     514                } (: </dgObservationStation> :) 
     515            } 
     516    ) 
     517} ; 
     518 
     519declare function f:return-stub-person($out-element-name as xs:string, $repid as xs:string, $locid as xs:string) as element()  
     520(: Returns a 'stubB' style observation station record to expand a person record :) 
     521(: Security not implemented yet :) 
     522{ 
     523let $found := count(collection($targetCollection)/moles:dgMetadata/moles:dgPerson[ 
     524            moles:dgMetadataID/moles:repositoryIdentifier=$repid  
     525            and moles:dgMetadataID/moles:localIdentifier=$locid]) 
     526return   
     527    if ($found = 0) then  
     528        element {$out-element-name} { 
     529            element error {'not found'}, 
     530            element repositoryIdentifier {$repid}, 
     531            element localIdentifier {$locid} 
     532        }  
     533    else ( 
     534    for $person in collection($targetCollection)/moles:dgMetadata/moles:dgPerson[ 
     535        moles:dgMetadataID/moles:repositoryIdentifier=$repid  
     536        and moles:dgMetadataID/moles:localIdentifier=$locid]  
     537    return  
     538        element  {$out-element-name}  
     539        { 
     540            ($person/moles:name), 
     541            ($person/moles:contactDetails), 
     542            ($person/moles:dgMetadataID) 
     543        } 
     544    ) 
     545} ; 
     546 
     547declare function f:return-stub-organisation($out-element-name as xs:string, $repid as xs:string, $locid as xs:string) as element()  
     548(: Returns a 'stubB' style observation station record to expand an organisation record :) 
     549(: Security not implemented yet :) 
     550{ 
     551    let $found := count(collection($targetCollection)/moles:dgMetadata/moles:dgOrganisation[ 
     552                moles:dgMetadataID/moles:repositoryIdentifier=$repid  
     553                and moles:dgMetadataID/moles:localIdentifier=$locid]) 
     554    return   
     555        if ($found = 0) then  
     556            element {$out-element-name} { 
     557                element error {'not found'}, 
     558                element repositoryIdentifier {$repid}, 
     559                element localIdentifier {$locid} 
     560            }  
     561        else ( 
     562            for $org in collection($targetCollection)/moles:dgMetadata/moles:dgOrganisation[ 
     563                moles:dgMetadataID/moles:repositoryIdentifier=$repid  
     564                and moles:dgMetadataID/moles:localIdentifier=$locid]  
     565            return  
     566                element  {$out-element-name} { 
     567                    $org/moles:dgMetadataID, 
     568                    $org/moles:name, 
     569                    $org/moles:abbreviation, 
     570                    $org/moles:contactDetails, 
     571                    $org/moles:logos 
     572                } 
     573        ) 
     574} ; 
     575 
     576for $Act in collection($targetCollection)/moles:dgMetadata/moles:dgMetadataRecord[ 
     577    moles:dgActivity!=''  
     578    and moles:dgMetadataID/moles:schemeIdentifier='NDG-B0'  
     579    and moles:dgMetadataID/moles:repositoryIdentifier='badc.nerc.ac.uk'  
     580    and moles:dgMetadataID/moles:localIdentifier='activity3'] 
     581return 
     582utillib:strip-namespace(element dgMetadataRecord { 
     583    element dgMetadataID { 
     584        element schemeIdentifier {'NDG-B1'}, 
     585        (element repositoryIdentifier {data($Act/moles:dgMetadataID/moles:repositoryIdentifier)}), 
     586        element localIdentifier {data($Act/moles:dgMetadataID/moles:localIdentifier)} 
     587    }, 
     588    $Act/moles:dgMetadataDescription, 
     589    $Act/moles:name, 
     590    $Act/moles:abbreviation, 
     591    element dgActivity { 
     592        for $RelatedActivities in $Act/dgActivity/moles:relatedActivity 
     593            return  
     594                element relatedActivity { 
     595                    $RelatedActivities/moles:activityRelation, 
     596                    f:return-stub-activity('activity', data($RelatedActivities/moles:relatedActivityID/moles:repositoryIdentifier), data($RelatedActivities/moles:relatedActivityID/moles:localIdentifier)) 
     597            },    (: </relatedActivity> :) 
     598        if (exists($Act/moles:dgActivity/moles:dgActivityDataCollection)) then ($Act/moles:dgActivity/moles:dgActivityDataCollection) 
     599        else if (exists($Act/moles:dgActivity/moles:dgActivityDataProject)) then ($Act/moles:dgActivity/moles:dgActivityDataProject) 
     600        else if (exists($Act/moles:dgActivity/moles:dgActivityDataInvestigation)) then ($Act/moles:dgActivity/moles:dgActivityDataInvestigation) 
     601        else if (exists($Act/moles:dgActivity/moles:dgActivityDataCampaign)) then ($Act/moles:dgActivity/moles:dgActivityDataCampaign) 
     602        else (), 
     603        element dgActivityRole { 
     604            element dgInvestigator { 
     605                element dgPrincipalInvestigator { 
     606                    ($Act/moles:dgActivity/moles:dgActivityRole/moles:dgInvestigator/moles:dgPrincipalInvestigator/moles:dgMetadataID), 
     607                    ($Act/moles:dgActivity/moles:dgActivityRole/moles:dgInvestigator/moles:dgPrincipalInvestigator/moles:roleName), 
     608                    ($Act/moles:dgActivity/moles:dgActivityRole/moles:dgInvestigator/moles:dgPrincipalInvestigator/moles:abbreviation), 
     609                    ($Act/moles:dgActivity/moles:dgActivityRole/moles:dgInvestigator/moles:dgPrincipalInvestigator/moles:contactDetails), 
     610                    for $PIRole in $Act/moles:dgActivity/moles:dgActivityRole/moles:dgInvestigator/moles:dgPrincipalInvestigator/moles:dgRoleHolder  
     611                        order by $PIRole/moles:startDate empty least, $PIRole/moles:endDate empty least 
     612                        return  
     613                        element dgRoleHolder { 
     614                            if (exists($PIRole/moles:dgOrganisationID)) then  
     615                                f:return-stub-organisation('organisation', data($PIRole/moles:dgOrganisationID/moles:repositoryIdentifier), data($PIRole/moles:dgOrganisationID/moles:localIdentifier))  
     616                             else if (exists($PIRole/moles:dgPersonID)) then  
     617                                f:return-stub-person('person', string($PIRole/moles:dgPersonID/moles:repositoryIdentifier), string($PIRole/moles:dgPersonID/moles:localIdentifier))  
     618                             else (), 
     619                            ($PIRole/moles:startDate), 
     620                            if (exists($PIRole/moles:endDate)) then  
     621                                ($PIRole/moles:endDate) 
     622                            else (), 
     623                            if (exists($PIRole/moles:localName)) then  
     624                                ($PIRole/moles:localName) 
     625                            else () 
     626                        } (: </dgRoleHolder> :) 
     627                }, (: </dgPrincipalInvestigator> :) 
     628                for $CI in $Act/moles:dgActivity/moles:dgActivityRole/moles:dgInvestigator/moles:dgCoInvestigator  
     629                return   
     630                    element dgCoInvestigator { 
     631                        ($CI/moles:dgMetadataID), 
     632                        ($CI/moles:roleName), 
     633                        ($CI/moles:abbreviation), 
     634                        ($CI/moles:contactDetails), 
     635                        for $CIRole in $CI/moles:dgRoleHolder  
     636                            order by $CIRole/moles:startDate empty least, $CIRole/moles:endDate empty least 
     637                        return  
     638                        element dgRoleHolder { 
     639                            if (exists(CIRole/moles:dgOrganisationID)) then  
     640                                f:return-stub-organisation('organisation', data($CIRole/moles:dgOrganisationID/moles:repositoryIdentifier), data($CIRole/moles:dgOrganisationID/moles:localIdentifier))  
     641                             else if (exists($CIRole/moles:dgPersonID)) then  
     642                                f:return-stub-person('person', string($CIRole/moles:dgPersonID/moles:repositoryIdentifier), string($CIRole/moles:dgPersonID/moles:localIdentifier))  
     643                             else (), 
     644                            ($CIRole/moles:startDate), 
     645                            if (exists($CIRole/moles:endDate)) then  
     646                                ($CIRole/moles:endDate) 
     647                            else (), 
     648                            if (exists($CIRole/moles:localName)) then  
     649                                ($CIRole/moles:localName) 
     650                            else () 
     651                        } (: </dgRoleHolder> :) 
     652                } (: </dgCoInvestigator> :) 
     653        }, (: </dgInvestigator> :) 
     654        if (exists($Act/moles:dgActivity/moles:dgActivityRole/moles:dgTechnicalContact)) then ( 
     655            element dgTechnicalContact { 
     656                ($Act/moles:dgActivity/moles:dgActivityRole/moles:dgTechnicalContact/moles:dgMetadataID), 
     657                ($Act/moles:dgActivity/moles:dgActivityRole/moles:dgTechnicalContact/moles:roleName), 
     658                ($Act/moles:dgActivity/moles:dgActivityRole/moles:dgTechnicalContact/moles:abbreviation), 
     659                ($Act/moles:dgActivity/moles:dgActivityRole/moles:dgTechnicalContact/moles:contactDetails), 
     660                for $TechConRole in $Act/moles:dgActivity/moles:dgActivityRole/moles:dgTechnicalContact/moles:dgRoleHolder  
     661                    order by $TechConRole/moles:startDate empty least, $TechConRole/moles:endDate empty least 
     662                    return  
     663                    element dgRoleHolder { 
     664                        if (exists($TechConRole/moles:dgOrganisationID)) then  
     665                            f:return-stub-organisation('organisation', data($TechConRole/moles:dgOrganisationID/moles:repositoryIdentifier), data($TechConRole/moles:dgOrganisationID/moles:localIdentifier))  
     666                         else if (exists($TechConRole/moles:dgPersonID)) then  
     667                            f:return-stub-person('person', string($TechConRole/moles:dgPersonID/moles:repositoryIdentifier), string($TechConRole/moles:dgPersonID/moles:localIdentifier))  
     668                         else (), 
     669                        ($TechConRole/moles:startDate), 
     670                        if (exists($TechConRole/moles:endDate)) then  
     671                            ($TechConRole/moles:endDate) 
     672                        else (), 
     673                        if (exists($TechConRole/moles:localName)) then  
     674                                ($TechConRole/moles:localName) 
     675                           else () 
     676                    } (: </dgRoleHolder> :) 
     677            } (: </dgTechnicalContact> :) 
     678        ) 
     679        else (), 
     680        if (exists($Act/dgActivity/dgActivityRole/dgProjectManager)) then ( 
     681            element dgProjectManager { 
     682                ($Act/dgActivity/dgActivityRole/dgProjectManager/dgMetadataID), 
     683                ($Act/dgActivity/dgActivityRole/dgProjectManager/roleName), 
     684                ($Act/dgActivity/dgActivityRole/dgProjectManager/abbreviation), 
     685                ($Act/dgActivity/dgActivityRole/dgProjectManager/contactDetails), 
     686                for $PMRole in $Act/dgActivity/dgActivityRole/dgProjectManager/dgRoleHolder  
     687                    order by $PMRole/startDate empty least, $PMRole/endDate empty least 
     688                    return  
     689                    element dgRoleHolder { 
     690                        if (exists($PMRole/moles:dgOrganisationID)) then  
     691                            f:return-stub-organisation('organisation', data($PMRole/moles:dgOrganisationID/moles:repositoryIdentifier), data($PMRole/moles:dgOrganisationID/moles:localIdentifier))  
     692                         else if (exists($PMRole/moles:dgPersonID)) then  
     693                            f:return-stub-person('person', string($PMRole/moles:dgPersonID/moles:repositoryIdentifier), string($PMRole/moles:dgPersonID/moles:localIdentifier))  
     694                         else (), 
     695                        ($PMRole/startDate), 
     696                        if (exists($PMRole/moles:endDate)) then  
     697                            ($PMRole/moles:endDate) 
     698                        else (), 
     699                        if (exists($PMRole/moles:localName)) then  
     700                            ($PMRole/moles:localName) 
     701                        else () 
     702                    } (: </dgRoleHolder> :) 
     703            } (: </dgProjectManager> :) 
     704        ) 
     705        else () 
     706    }, (: </dgActivityRole> :) 
     707    for $RelDep in $Act/moles:dgActivity/moles:ActivityDeployment 
     708        return 
     709        element ActivityDeployment { 
     710            ($RelDep/moles:DateStart), 
     711            if (exists($RelDep/moles:DateEnd)) then  
     712                ($RelDep/moles:DateEnd) 
     713            else (), 
     714            ($RelDep/moles:dgMetadataID), 
     715            f:return-stub-dpt('dataproductiontool', data($RelDep/moles:DataProductionToolID/moles:repositoryIdentifier), data($RelDep/moles:DataProductionToolID/moles:localIdentifier)), 
     716            f:return-stub-obsstn('observationstation', data($RelDep/moles:ObservationStationID/moles:repositoryIdentifier), data($RelDep/moles:ObservationStationID/moles:localIdentifier)), 
     717            for $DE in collection($targetCollection)/moles:dgMetadata/moles:dgMetadataRecord 
     718                where $DE/moles:dgDataEntity/moles:RelatedDeployment/moles:dgMetadataID/moles:repositoryIdentifier=$RelDep/moles:dgMetadataID/moles:repositoryIdentifier 
     719                        and $DE/moles:dgDataEntity/moles:RelatedDeployment/moles:dgMetadataID/moles:localIdentifier=$RelDep/moles:dgMetadataID/moles:localIdentifier 
     720            return 
     721                f:return-stub-dataentity('dataentity', data($DE/moles:dgMetadataID/moles:repositoryIdentifier), data($DE/moles:dgMetadataID/moles:localIdentifier)) 
     722        }, (: </ActivityDeployment> :) 
     723        if (exists($Act/dgActivityCoverage)) then 
     724            ($Act/dgActivityCoverage) 
     725            else (), 
     726        if (exists($Act/dgActivityDuration)) then 
     727            ($Act/dgActivityDuration) 
     728            else () 
     729    } , (: </dgActivity> :) 
     730    for $strkwrd in $Act/moles:dgStructuredKeyword return $strkwrd, 
     731    if (exists($Act/moles:dgMetadataProvenance)) then $Act/moles:dgMetadataProvenance else (), 
     732    if (exists($Act/moles:dgMetadataSecurity)) then $Act/moles:dgMetadataSecurity else () 
     733} (:</dgMRecord>:) 
     734) (: End namespace strip :) 
     735''' 
     736 
     737StubBDPTQuery = '''declare default element namespace 'http://ndg.nerc.ac.uk/moles/dataproductiontool';  
     738import module namespace voclib='http://ndg.nerc.ac.uk/xquery/lib/vocab' at 'xmldb:exist:///db/xqueryLib/Vocabs/vocab_xquery_lib.xquery'; 
     739import module namespace utillib='http://ndg.nerc.ac.uk/xquery/lib/utilities' at 'xmldb:exist:///db/xqueryLib/Utilities/utility_xquery_lib.xquery'; 
     740(:  
     741Will return when scope problem sussed... ;( 
     742import module namespace stubblib='http://ndg.nerc.ac.uk/xquery/lib/stubb' at 'xmldb:exist:///db/xqueryLib/StubB/stubb_xquery_lib.xquery'; 
     743:) 
     744declare namespace f='http://ndg.nerc.ac.uk/moles/localfunctions'; 
     745declare namespace moles='http://ndg.nerc.ac.uk/moles'; 
     746 
     747declare variable $targetCollection as xs:string {'TargetCollection'}; 
     748declare variable $repositoryIdentifier as xs:string {'RepositoryID'}; 
     749declare variable $localIdentifier as xs:string {'LocalID'}; 
     750 
     751declare function f:return-stub-activity($out-element-name as xs:string, $repid as xs:string, $locid as xs:string) as element()  
     752(: Returns a 'stubB' style activity record to expand an activity record :) 
     753(: Security not implemented here yet :) 
     754{ 
     755let $found := count(collection($targetCollection)/moles:dgMetadata/moles:dgMetadataRecord[ 
     756            moles:dgMetadataID/moles:repositoryIdentifier=$repid  
     757            and moles:dgMetadataID/moles:localIdentifier=$locid]) 
     758return   
     759    if ($found = 0) then  
     760        element {$out-element-name} { 
     761            element error {'not found'}, 
     762            element repositoryIdentifier {$repid}, 
     763            element localIdentifier {$locid} 
     764        }  
     765    else ( 
     766        for $SRAct in collection($targetCollection)/moles:dgMetadata/moles:dgMetadataRecord[ 
     767            moles:dgMetadataID/moles:repositoryIdentifier=$repid  
     768            and moles:dgMetadataID/moles:localIdentifier=$locid]  
     769        return  
     770            element  {$out-element-name} {  
     771                ($SRAct/moles:dgMetadataID), 
     772                ($SRAct/moles:dgMetadataDescription), 
     773                ($SRAct/moles:name), 
     774                ($SRAct/moles:abbreviation), 
     775                element dgActivity { 
     776                    if (exists($SRAct/*/moles:dgActivityDataCollection)) then  
     777                        ($SRAct/*/moles:dgActivityDataCollection) 
     778                    else if (exists($SRAct/*/moles:dgActivityDataProject)) then  
     779                        ($SRAct/*/moles:dgActivityDataProject) 
     780                    else if (exists($SRAct/*/dgActivityDataCampaign)) then 
     781                        ($SRAct/moles:dgActivity/moles:dgActivityDataCampaign) 
     782                    else  
     783                        ($SRAct/moles:dgActivity/moles:dgActivityDataInvestigation),  
     784                    ($SRAct/moles:dgActivity/moles:dgActivityCoverage), 
     785                    ($SRAct/moles:dgActivity/moles:dgActivityDuration) 
     786                } (: </dgActivity> :)  
     787            } (: </activity> :)  
     788    ) 
     789} ; 
     790 
     791declare function f:return-stub-dataentity($out-element-name as xs:string, $repid as xs:string, $locid as xs:string) as element()  
     792(: Returns a 'stubB' style observation station record to expand an data entity record :) 
     793(: Security not implemented yet :) 
     794{ 
     795let $found := count(collection($targetCollection)/moles:dgMetadata/moles:dgMetadataRecord[ 
     796        moles:dgMetadataID/moles:repositoryIdentifier=$repid  
     797        and moles:dgMetadataID/moles:localIdentifier=$locid]) 
     798return   
     799    if ($found = 0) then  
     800        element {$out-element-name} { 
     801            element error {'not found'}, 
     802            element repositoryIdentifier {$repid}, 
     803            element localIdentifier {$locid} 
     804        }  
     805    else ( 
     806        for $SRDE in collection($targetCollection)/moles:dgMetadata/moles:dgMetadataRecord[ 
     807            moles:dgMetadataID/moles:repositoryIdentifier=$repid  
     808            and moles:dgMetadataID/moles:localIdentifier=$locid]  
     809        return   
     810            element {$out-element-name} { 
     811                ($SRDE/moles:dgMetadataID), 
     812                ($SRDE/moles:dgMetadataDescription), 
     813                ($SRDE/moles:name), 
     814                ($SRDE/moles:abbreviation), 
     815                element dgDataEntity { 
     816                    ($SRDE/moles:dgDataEntity/moles:dgDataSetType), 
     817                    ($SRDE/moles:dgDataEntity/moles:dgDataGranule), 
     818                    ($SRDE/moles:dgDataEntity/moles:dgDataSummary) 
     819                } (: </dgDataEntity> :)         
     820            } 
     821    ) 
     822} ; 
     823 
     824declare function f:return-stub-dpt($out-element-name as xs:string, $repid as xs:string, $locid as xs:string) as element()  
     825(: Returns a 'stubB' style dpt record to expand a dpt record :) 
     826(: Security not implemented yet :) 
     827{ 
     828let $found := count(collection($targetCollection)/moles:dgMetadata/moles:dgMetadataRecord[ 
     829        moles:dgMetadataID/moles:repositoryIdentifier=$repid  
     830        and moles:dgMetadataID/moles:localIdentifier=$locid]) 
     831return   
     832    if ($found = 0) then  
     833        element {$out-element-name} { 
     834            element error {'not found'}, 
     835            element repositoryIdentifier {$repid}, 
     836            element localIdentifier {$locid} 
     837        }  
     838    else ( 
     839        for $SRDPT in collection($targetCollection)/moles:dgMetadata/moles:dgMetadataRecord[ 
     840                moles:dgMetadataID/moles:repositoryIdentifier=$repid  
     841                and moles:dgMetadataID/moles:localIdentifier=$locid] 
     842        return   
     843            element {$out-element-name} { 
     844                $SRDPT/moles:dgMetadataID, 
     845                $SRDPT/moles:dgMetadataDescription, 
     846                $SRDPT/moles:name, 
     847                $SRDPT/moles:abbreviation, 
     848                $SRDPT/moles:logos, 
     849                element dgDataProductionTool { 
     850                    $SRDPT/moles:dgDataProductionTool/moles:contactDetails, 
     851                    if (exists($SRDPT/moles:dgDataProductionTool/moles:dgModel)) then 
     852                        $SRDPT/moles:dgDataProductionTool/moles:dgModel 
     853                    else (), 
     854                    if (exists($SRDPT/moles:dgDataProductionTool/moles:dgInstrument)) then 
     855                        $SRDPT/moles:dgDataProductionTool/moles:dgInstrument 
     856                    else () 
     857                    } (: </dgDataProductionTool> :) 
     858                } (: </dataproductiontool> :) 
     859    ) 
     860} ; 
     861 
     862declare function f:return-stub-obsstn($out-element-name as xs:string, $repid as xs:string, $locid as xs:string) as element()  
     863(: Returns a 'stubB' style observation station record to expand an observation station record :) 
     864(: Security not implemented here yet;  :) 
     865{ 
     866let $found := count(collection($targetCollection)/moles:dgMetadata/moles:dgMetadataRecord[ 
     867            moles:dgMetadataID/moles:repositoryIdentifier=$repid  
     868            and moles:dgMetadataID/moles:localIdentifier=$locid]) 
     869return   
     870    if ($found = 0) then  
     871        element {$out-element-name} { 
     872            element error {'not found'}, 
     873            element repositoryIdentifier {$repid}, 
     874            element localIdentifier {$locid} 
     875        }  
     876    else ( 
     877        for $SRObsStn in collection($targetCollection)/moles:dgMetadata/moles:dgMetadataRecord[ 
     878            moles:dgMetadataID/moles:repositoryIdentifier=$repid  
     879            and moles:dgMetadataID/moles:localIdentifier=$locid]  
     880        return 
     881            element {$out-element-name} { 
     882                ($SRObsStn/moles:dgMetadataID), 
     883                ($SRObsStn/moles:dgMetadataDescription), 
     884                ($SRObsStn/moles:name), 
     885                ($SRObsStn/moles:abbreviation), 
     886                element dgObservationStation { 
     887                    ($SRObsStn/moles:dgObservationStation/moles:contactDetails), 
     888                    if (exists($SRObsStn/moles:dgObservationStation/moles:dgStationaryPlatform)) then 
     889                        element dgStationaryPlatform { 
     890                            $SRObsStn/moles:dgObservationStation/moles:dgStationaryPlatform/moles:position, 
     891                            if (exists($SRObsStn/moles:dgObservationStation/moles:dgStationaryPlatform/dgLandStation)) then element dgLandStation {''} 
     892                            else if (exists($SRObsStn/moles:dgObservationStation/moles:dgStationaryPlatform/dgMooring)) then element dgMooring {''} 
     893                            else if (exists($SRObsStn/moles:dgObservationStation/moles:dgStationaryPlatform/dgStationGroup)) then element dgStationGroup {''} 
     894                            else () 
     895                            } 
     896                    else if (exists($SRObsStn/moles:dgObservationStation/moles:dgMovingPlatform)) then  
     897                        $SRObsStn/moles:dgObservationStation/moles:dgMovingPlatform 
     898                    else () 
     899                } (: </dgObservationStation> :) 
     900            } 
     901    ) 
     902} ; 
     903 
     904declare function f:return-stub-person($out-element-name as xs:string, $repid as xs:string, $locid as xs:string) as element()  
     905(: Returns a 'stubB' style observation station record to expand a person record :) 
     906(: Security not implemented yet :) 
     907{ 
     908let $found := count(collection($targetCollection)/moles:dgMetadata/moles:dgPerson[ 
     909            moles:dgMetadataID/moles:repositoryIdentifier=$repid  
     910            and moles:dgMetadataID/moles:localIdentifier=$locid]) 
     911return   
     912    if ($found = 0) then  
     913        element {$out-element-name} { 
     914            element error {'not found'}, 
     915            element repositoryIdentifier {$repid}, 
     916            element localIdentifier {$locid} 
     917        }  
     918    else ( 
     919    for $person in collection($targetCollection)/moles:dgMetadata/moles:dgPerson[ 
     920        moles:dgMetadataID/moles:repositoryIdentifier=$repid  
     921        and moles:dgMetadataID/moles:localIdentifier=$locid]  
     922    return  
     923        element  {$out-element-name}  
     924        { 
     925            ($person/moles:name), 
     926            ($person/moles:contactDetails), 
     927            ($person/moles:dgMetadataID) 
     928        } 
     929    ) 
     930} ; 
     931 
     932declare function f:return-stub-organisation($out-element-name as xs:string, $repid as xs:string, $locid as xs:string) as element()  
     933(: Returns a 'stubB' style observation station record to expand an organisation record :) 
     934(: Security not implemented yet :) 
     935{ 
     936    let $found := count(collection($targetCollection)/moles:dgMetadata/moles:dgOrganisation[ 
     937                moles:dgMetadataID/moles:repositoryIdentifier=$repid  
     938                and moles:dgMetadataID/moles:localIdentifier=$locid]) 
     939    return   
     940        if ($found = 0) then  
     941            element {$out-element-name} { 
     942                element error {'not found'}, 
     943                element repositoryIdentifier {$repid}, 
     944                element localIdentifier {$locid} 
     945            }  
     946        else ( 
     947            for $org in collection($targetCollection)/moles:dgMetadata/moles:dgOrganisation[ 
     948                moles:dgMetadataID/moles:repositoryIdentifier=$repid  
     949                and moles:dgMetadataID/moles:localIdentifier=$locid]  
     950            return  
     951                element  {$out-element-name} { 
     952                    $org/moles:dgMetadataID, 
     953                    $org/moles:name, 
     954                    $org/moles:abbreviation, 
     955                    $org/moles:contactDetails, 
     956                    $org/moles:logos 
     957                } 
     958        ) 
     959} ; 
     960 
     961for $DPT in collection($targetCollection)/moles:dgMetadata/moles:dgMetadataRecord[ 
     962    moles:dgDataProductionTool!=''  
     963    and moles:dgMetadataID/moles:schemeIdentifier='NDG-B0'  
     964    and moles:dgMetadataID/moles:repositoryIdentifier='badc.nerc.ac.uk'  
     965    and moles:dgMetadataID/moles:localIdentifier='dpt1']  
    35966return  
    36 element oai_dc:dc { 
    37 attribute xsi:schemaLocation {'http://www.openarchives.org/OAI/2.0/oai_dc/ http://www.openarchives.org/OAI/2.0/oai_dc.xsd'}, 
    38 element dc:title {string($DE/name)}, 
    39 element dc:type {'Dataset'}, 
    40 element dc:identifier {concat($DE/dgMetadataID/repositoryIdentifier, ':DC:', $DE/dgMetadataID/localIdentifier)}, 
    41 element dc:description {string($DE/dgMetadataDescription/abstract/abstractText)}, 
    42 element dc:date 
    43 { 
    44 if (exists($DE/(dgMetadataProvenance | DataProvenance)/RecordUpdate/UpdateDate)) then 
    45 for $updatedate in ($DE/(dgMetadataProvenance | DataProvenance)/RecordUpdate/UpdateDate) 
    46 order by xs:dateTime($updatedate) 
    47 return data($updatedate[1]) 
    48 else 
    49 string($DE/(dgMetadataProvenance | DataProvenance)/RecordCreation/CreatedDate)  
    50 }, 
    51 for $StructuredKeyword in distinct-values($DE/dgStructuredKeyword [dgValidTermID/ParentListID=$cf_standard_names or dgValidTermID/ParentListID=$gcmd_science_valids or dgValidTermID/ParentListID=$gcmd_project_valids or dgValidTermID/ParentListID=$iso_topic_list ]//dgValidTerm)  
    52 order by $StructuredKeyword 
     967utillib:strip-namespace(element dgMetadataRecord {  
     968    $DPT/dgMetadataID, 
     969        element dgMetadataID { 
     970            element schemeIdentifier {'NDG-B1'}, 
     971            $DPT/moles:dgMetadataID/moles:repositoryIdentifier, 
     972            $DPT/moles:dgMetadataID/moles:localIdentifier 
     973        } (: End </dgMetadataID> :),  
     974    $DPT/moles:dgMetadataDescription, 
     975    $DPT/moles:name, 
     976    $DPT/moles:abbreviation, 
     977    element dgDataProductionTool {  
     978        $DPT/moles:contactDetails, 
     979        if (exists($DPT/moles:dgModel)) then $DPT/moles:dgModel 
     980        else $DPT/moles:dgInstrument  
     981    , 
     982    if (exists($DPT/moles:dgDPTRoles)) then  
     983        element dgDPTRoles {'Roles are available, and will be added soon'}  
     984     else(), 
     985    for $RelDep in $DPT/moles:dgDataProductionTool/moles:DPTDeployment  
     986    return  
     987        element DPTDeployment {  
     988            $RelDep/moles:DateStart, 
     989            $RelDep/moles:DateEnd, 
     990            $RelDep/moles:dgMetadataID, 
     991            f:return-stub-activity('activity', data($RelDep/moles:ActivityID/moles:repositoryIdentifier), data($RelDep/moles:ActivityID/moles:localIdentifier)), 
     992            f:return-stub-obsstn('observationstation', data($RelDep/moles:ObservationStationID/moles:repositoryIdentifier), data($RelDep/moles:ObservationStationID/moles:localIdentifier)), 
     993            for $DE in collection($targetCollection)/moles:dgMetadata/moles:dgMetadataRecord 
     994                where $DE/moles:dgDataEntity/moles:RelatedDeployment/moles:dgMetadataID/moles:repositoryIdentifier=$RelDep/moles:dgMetadataID/moles:repositoryIdentifier 
     995                        and $DE/moles:dgDataEntity/moles:RelatedDeployment/moles:dgMetadataID/moles:localIdentifier=$RelDep/moles:dgMetadataID/moles:localIdentifier 
     996            return 
     997                f:return-stub-dataentity('dataentity', data($DE/moles:dgMetadataID/moles:repositoryIdentifier), data($DE/moles:dgMetadataID/moles:localIdentifier)), 
     998            $RelDep/moles:Coverage 
     999        } (: </DPTDeployment> :)  
     1000    } (: </dgDataProductionTool> :),  
     1001    for $strkwrd in $DPT/moles:dgStructuredKeyword return $strkwrd, 
     1002    if (exists($DPT/moles:dgMetadataProvenance)) then $DPT/moles:dgMetadataProvenance else (), 
     1003    if (exists($DPT/moles:dgMetadataSecurity)) then $DPT/moles:dgMetadataSecurity else () 
     1004} (:End </dgMetadataRecord> :) 
     1005) (: End namespace strip :)''' 
     1006 
     1007StubBObsStnQuery = '''declare default element namespace 'http://ndg.nerc.ac.uk/moles/observationstation';  
     1008import module namespace voclib='http://ndg.nerc.ac.uk/xquery/lib/vocab' at 'xmldb:exist:///db/xqueryLib/Vocabs/vocab_xquery_lib.xquery'; 
     1009import module namespace utillib='http://ndg.nerc.ac.uk/xquery/lib/utilities' at 'xmldb:exist:///db/xqueryLib/Utilities/utility_xquery_lib.xquery'; 
     1010(:  
     1011Will return when scope problem sussed... ;( 
     1012import module namespace stubblib='http://ndg.nerc.ac.uk/xquery/lib/stubb' at 'xmldb:exist:///db/xqueryLib/StubB/stubb_xquery_lib.xquery'; 
     1013:) 
     1014declare namespace f='http://ndg.nerc.ac.uk/moles/localfunctions'; 
     1015declare namespace moles='http://ndg.nerc.ac.uk/moles'; 
     1016 
     1017declare variable $targetCollection as xs:string {'TargetCollection'}; 
     1018declare variable $repositoryIdentifier as xs:string {'RepositoryID'}; 
     1019declare variable $localIdentifier as xs:string {'LocalID'}; 
     1020 
     1021declare function f:return-stub-activity($out-element-name as xs:string, $repid as xs:string, $locid as xs:string) as element()  
     1022(: Returns a 'stubB' style activity record to expand an activity record :) 
     1023(: Security not implemented here yet :) 
     1024{ 
     1025let $found := count(collection($targetCollection)/moles:dgMetadata/moles:dgMetadataRecord[ 
     1026            moles:dgMetadataID/moles:repositoryIdentifier=$repid  
     1027            and moles:dgMetadataID/moles:localIdentifier=$locid]) 
     1028return   
     1029    if ($found = 0) then  
     1030        element {$out-element-name} { 
     1031            element error {'not found'}, 
     1032            element repositoryIdentifier {$repid}, 
     1033            element localIdentifier {$locid} 
     1034        }  
     1035    else ( 
     1036        for $SRAct in collection($targetCollection)/moles:dgMetadata/moles:dgMetadataRecord[ 
     1037            moles:dgMetadataID/moles:repositoryIdentifier=$repid  
     1038            and moles:dgMetadataID/moles:localIdentifier=$locid]  
     1039        return  
     1040            element  {$out-element-name} {  
     1041                ($SRAct/moles:dgMetadataID), 
     1042                ($SRAct/moles:dgMetadataDescription), 
     1043                ($SRAct/moles:name), 
     1044                ($SRAct/moles:abbreviation), 
     1045                element dgActivity { 
     1046                    if (exists($SRAct/*/moles:dgActivityDataCollection)) then  
     1047                        ($SRAct/*/moles:dgActivityDataCollection) 
     1048                    else if (exists($SRAct/*/moles:dgActivityDataProject)) then  
     1049                        ($SRAct/*/moles:dgActivityDataProject) 
     1050                    else if (exists($SRAct/*/dgActivityDataCampaign)) then 
     1051                        ($SRAct/moles:dgActivity/moles:dgActivityDataCampaign) 
     1052                    else  
     1053                        ($SRAct/moles:dgActivity/moles:dgActivityDataInvestigation),  
     1054                    ($SRAct/moles:dgActivity/moles:dgActivityCoverage), 
     1055                    ($SRAct/moles:dgActivity/moles:dgActivityDuration) 
     1056                } (: </dgActivity> :)  
     1057            } (: </activity> :)  
     1058    ) 
     1059} ; 
     1060 
     1061declare function f:return-stub-dataentity($out-element-name as xs:string, $repid as xs:string, $locid as xs:string) as element()  
     1062(: Returns a 'stubB' style observation station record to expand an data entity record :) 
     1063(: Security not implemented yet :) 
     1064{ 
     1065let $found := count(collection($targetCollection)/moles:dgMetadata/moles:dgMetadataRecord[ 
     1066        moles:dgMetadataID/moles:repositoryIdentifier=$repid  
     1067        and moles:dgMetadataID/moles:localIdentifier=$locid]) 
     1068return   
     1069    if ($found = 0) then  
     1070        element {$out-element-name} { 
     1071            element error {'not found'}, 
     1072            element repositoryIdentifier {$repid}, 
     1073            element localIdentifier {$locid} 
     1074        }  
     1075    else ( 
     1076        for $SRDE in collection($targetCollection)/moles:dgMetadata/moles:dgMetadataRecord[ 
     1077            moles:dgMetadataID/moles:repositoryIdentifier=$repid  
     1078            and moles:dgMetadataID/moles:localIdentifier=$locid]  
     1079        return   
     1080            element {$out-element-name} { 
     1081                ($SRDE/moles:dgMetadataID), 
     1082                ($SRDE/moles:dgMetadataDescription), 
     1083                ($SRDE/moles:name), 
     1084                ($SRDE/moles:abbreviation), 
     1085                element dgDataEntity { 
     1086                    ($SRDE/moles:dgDataEntity/moles:dgDataSetType), 
     1087                    ($SRDE/moles:dgDataEntity/moles:dgDataGranule), 
     1088                    ($SRDE/moles:dgDataEntity/moles:dgDataSummary) 
     1089                } (: </dgDataEntity> :)         
     1090            } 
     1091    ) 
     1092} ; 
     1093 
     1094declare function f:return-stub-dpt($out-element-name as xs:string, $repid as xs:string, $locid as xs:string) as element()  
     1095(: Returns a 'stubB' style dpt record to expand a dpt record :) 
     1096(: Security not implemented yet :) 
     1097{ 
     1098let $found := count(collection($targetCollection)/moles:dgMetadata/moles:dgMetadataRecord[ 
     1099        moles:dgMetadataID/moles:repositoryIdentifier=$repid  
     1100        and moles:dgMetadataID/moles:localIdentifier=$locid]) 
     1101return   
     1102    if ($found = 0) then  
     1103        element {$out-element-name} { 
     1104            element error {'not found'}, 
     1105            element repositoryIdentifier {$repid}, 
     1106            element localIdentifier {$locid} 
     1107        }  
     1108    else ( 
     1109        for $SRDPT in collection($targetCollection)/moles:dgMetadata/moles:dgMetadataRecord[ 
     1110                moles:dgMetadataID/moles:repositoryIdentifier=$repid  
     1111                and moles:dgMetadataID/moles:localIdentifier=$locid] 
     1112        return   
     1113            element {$out-element-name} { 
     1114                $SRDPT/moles:dgMetadataID, 
     1115                $SRDPT/moles:dgMetadataDescription, 
     1116                $SRDPT/moles:name, 
     1117                $SRDPT/moles:abbreviation, 
     1118                $SRDPT/moles:logos, 
     1119                element dgDataProductionTool { 
     1120                    $SRDPT/moles:dgDataProductionTool/moles:contactDetails, 
     1121                    if (exists($SRDPT/moles:dgDataProductionTool/moles:dgModel)) then 
     1122                        $SRDPT/moles:dgDataProductionTool/moles:dgModel 
     1123                    else (), 
     1124                    if (exists($SRDPT/moles:dgDataProductionTool/moles:dgInstrument)) then 
     1125                        $SRDPT/moles:dgDataProductionTool/moles:dgInstrument 
     1126                    else () 
     1127                    } (: </dgDataProductionTool> :) 
     1128                } (: </dataproductiontool> :) 
     1129    ) 
     1130} ; 
     1131 
     1132declare function f:return-stub-obsstn($out-element-name as xs:string, $repid as xs:string, $locid as xs:string) as element()  
     1133(: Returns a 'stubB' style observation station record to expand an observation station record :) 
     1134(: Security not implemented here yet;  :) 
     1135{ 
     1136let $found := count(collection($targetCollection)/moles:dgMetadata/moles:dgMetadataRecord[ 
     1137            moles:dgMetadataID/moles:repositoryIdentifier=$repid  
     1138            and moles:dgMetadataID/moles:localIdentifier=$locid]) 
     1139return   
     1140    if ($found = 0) then  
     1141        element {$out-element-name} { 
     1142            element error {'not found'}, 
     1143            element repositoryIdentifier {$repid}, 
     1144            element localIdentifier {$locid} 
     1145        }  
     1146    else ( 
     1147        for $SRObsStn in collection($targetCollection)/moles:dgMetadata/moles:dgMetadataRecord[ 
     1148            moles:dgMetadataID/moles:repositoryIdentifier=$repid  
     1149            and moles:dgMetadataID/moles:localIdentifier=$locid]  
     1150        return 
     1151            element {$out-element-name} { 
     1152                ($SRObsStn/moles:dgMetadataID), 
     1153                ($SRObsStn/moles:dgMetadataDescription), 
     1154                ($SRObsStn/moles:name), 
     1155                ($SRObsStn/moles:abbreviation), 
     1156                element dgObservationStation { 
     1157                    ($SRObsStn/moles:dgObservationStation/moles:contactDetails), 
     1158                    if (exists($SRObsStn/moles:dgObservationStation/moles:dgStationaryPlatform)) then 
     1159                        element dgStationaryPlatform { 
     1160                            $SRObsStn/moles:dgObservationStation/moles:dgStationaryPlatform/moles:position, 
     1161                            if (exists($SRObsStn/moles:dgObservationStation/moles:dgStationaryPlatform/dgLandStation)) then element dgLandStation {''} 
     1162                            else if (exists($SRObsStn/moles:dgObservationStation/moles:dgStationaryPlatform/dgMooring)) then element dgMooring {''} 
     1163                            else if (exists($SRObsStn/moles:dgObservationStation/moles:dgStationaryPlatform/dgStationGroup)) then element dgStationGroup {''} 
     1164                            else () 
     1165                            } 
     1166                    else if (exists($SRObsStn/moles:dgObservationStation/moles:dgMovingPlatform)) then  
     1167                        $SRObsStn/moles:dgObservationStation/moles:dgMovingPlatform 
     1168                    else () 
     1169                } (: </dgObservationStation> :) 
     1170            } 
     1171    ) 
     1172} ; 
     1173 
     1174declare function f:return-stub-person($out-element-name as xs:string, $repid as xs:string, $locid as xs:string) as element()  
     1175(: Returns a 'stubB' style observation station record to expand a person record :) 
     1176(: Security not implemented yet :) 
     1177{ 
     1178let $found := count(collection($targetCollection)/moles:dgMetadata/moles:dgPerson[ 
     1179            moles:dgMetadataID/moles:repositoryIdentifier=$repid  
     1180            and moles:dgMetadataID/moles:localIdentifier=$locid]) 
     1181return   
     1182    if ($found = 0) then  
     1183        element {$out-element-name} { 
     1184            element error {'not found'}, 
     1185            element repositoryIdentifier {$repid}, 
     1186            element localIdentifier {$locid} 
     1187        }  
     1188    else ( 
     1189    for $person in collection($targetCollection)/moles:dgMetadata/moles:dgPerson[ 
     1190        moles:dgMetadataID/moles:repositoryIdentifier=$repid  
     1191        and moles:dgMetadataID/moles:localIdentifier=$locid]  
     1192    return  
     1193        element  {$out-element-name}  
     1194        { 
     1195            ($person/moles:name), 
     1196            ($person/moles:contactDetails), 
     1197            ($person/moles:dgMetadataID) 
     1198        } 
     1199    ) 
     1200} ; 
     1201 
     1202declare function f:return-stub-organisation($out-element-name as xs:string, $repid as xs:string, $locid as xs:string) as element()  
     1203(: Returns a 'stubB' style observation station record to expand an organisation record :) 
     1204(: Security not implemented yet :) 
     1205{ 
     1206    let $found := count(collection($targetCollection)/moles:dgMetadata/moles:dgOrganisation[ 
     1207                moles:dgMetadataID/moles:repositoryIdentifier=$repid  
     1208                and moles:dgMetadataID/moles:localIdentifier=$locid]) 
     1209    return   
     1210        if ($found = 0) then  
     1211            element {$out-element-name} { 
     1212                element error {'not found'}, 
     1213                element repositoryIdentifier {$repid}, 
     1214                element localIdentifier {$locid} 
     1215            }  
     1216        else ( 
     1217            for $org in collection($targetCollection)/moles:dgMetadata/moles:dgOrganisation[ 
     1218                moles:dgMetadataID/moles:repositoryIdentifier=$repid  
     1219                and moles:dgMetadataID/moles:localIdentifier=$locid]  
     1220            return  
     1221                element  {$out-element-name} { 
     1222                    $org/moles:dgMetadataID, 
     1223                    $org/moles:name, 
     1224                    $org/moles:abbreviation, 
     1225                    $org/moles:contactDetails, 
     1226                    $org/moles:logos 
     1227                } 
     1228        ) 
     1229} ; 
     1230 
     1231for $ObsStn in collection($targetCollection)/moles:dgMetadata/moles:dgMetadataRecord[ 
     1232    moles:dgObservationStation!='' and moles:dgMetadataID/moles:schemeIdentifier='NDG-B0'  
     1233    and moles:dgMetadataID/moles:repositoryIdentifier='badc.nerc.ac.uk'  
     1234    and moles:dgMetadataID/moles:localIdentifier='obs1']  
    531235return  
    54 element dc:subject {string($StructuredKeyword)}, 
    55  
    56 for $StructuredKeyword1 in distinct-values($DE/dgDataEntity/dgDataSummary/dgParameterSummary/dgStdParameterMeasured[dgValidTermID/ParentListID=$cf_standard_names or dgValidTermID/ParentListID=$gcmd_science_valids or dgValidTermID/ParentListID=$gcmd_project_valids]//dgValidTerm)  
    57 order by $StructuredKeyword1 
    58 return  
    59 element dc:subject {string($StructuredKeyword1)}, 
    60 for $DataCreatorRole in $DE/dgDataEntity/dgDataRoles/dgDataCreator/dgRoleHolder  
    61 order by $DataCreatorRole/startDate empty least 
    62 return  
    63 for $DataCreatorRoleHolder in collection($targetCollection)/dgMetadata/(dgOrganisation | dgPerson) 
    64 [(dgMetadataID/schemeIdentifier='NDG-B0' 
    65 and dgMetadataID/repositoryIdentifier=$DataCreatorRole/(dgOrganisationID | dgPersonID)/repositoryIdentifier 
    66 and dgMetadataID/localIdentifier=$DataCreatorRole/(dgOrganisationID | dgPersonID)/localIdentifier)] 
    67 return  
    68 element dc:creator { 
    69 if (string(local-name($DataCreatorRoleHolder))='dgOrganisation') then 
    70 (string($DataCreatorRoleHolder/name)) 
    71 else if (string(local-name($DataCreatorRoleHolder))='dgPerson') then 
    72 (string(concat(string($DataCreatorRoleHolder/name/initials), ' ', string($DataCreatorRoleHolder/name/familyName)))) 
    73 else ('empty content') 
    74 }, 
    75 for $DataCuratorRole in $DE/dgDataEntity/dgDataRoles/dgDataCurator/dgRoleHolder[not(exists(endDate)) or endDate=''] 
    76 order by $DataCuratorRole/startDate empty least 
    77 return  
    78 for $DataCuratorRoleHolder in collection($targetCollection)/dgMetadata/(dgOrganisation | dgPerson) 
    79 [(dgMetadataID/schemeIdentifier='NDG-B0' 
    80 and dgMetadataID/repositoryIdentifier=$DataCuratorRole/(dgOrganisationID | dgPersonID)/repositoryIdentifier 
    81 and dgMetadataID/localIdentifier=$DataCuratorRole/(dgOrganisationID | dgPersonID)/localIdentifier)] 
    82 return  
    83 element dc:publisher { 
    84 if (string(local-name($DataCuratorRoleHolder))='dgOrganisation') then 
    85 (string($DataCuratorRoleHolder/name)) 
    86 else if (string(local-name($DataCuratorRoleHolder))='dgPerson') then 
    87 (string(concat(string($DataCuratorRoleHolder/name/initials), ' ', string($DataCuratorRoleHolder/name/familyName)))) 
    88 else ('empty content') 
    89 } 
    90 }''' 
    91  
    92 DIFQuery = '''declare default element namespace 'http://gcmd.gsfc.nasa.gov/Aboutus/xml/dif/'; 
    93 declare namespace xsi='http://www.w3.org/2001/XMLSchema-instance';  
    94 declare namespace moles='http://ndg.nerc.ac.uk/moles'; 
    95 declare namespace f='http://ndg.nerc.ac.uk/moles/localfunctions'; 
    96  
    97 for $DE in collection('/db/ndg_B_metadata')/moles:dgMetadata/moles:dgMetadataRecord[ 
    98 exists('moles:dgDataEntity')  
    99 and moles:dgMetadataID/moles:schemeIdentifier='NDG-B0'  
    100 and moles:dgMetadataID/moles:repositoryIdentifier='RepositoryID'  
    101 and moles:dgMetadataID/moles:localIdentifier='LocalID'] 
    102 return 
    103 element DIF { 
    104 attribute xsi:schemaLocation {'http://gcmd.gsfc.nasa.gov/Aboutus/xml/dif/ http://gcmd.gsfc.nasa.gov/Aboutus/xml/dif/dif_v9.4.xsd'}, 
    105 element Entry_ID {concat($DE/moles:dgMetadataID/moles:repositoryIdentifier, ':DIF:', $DE/moles:dgMetadataID/moles:localIdentifier)}, 
    106 element Entry_Title {string($DE/moles:name)}, 
    107 element Data_Set_Citation { 
    108 for $DataCreatorRole in $DE/moles:dgDataEntity/moles:dgDataRoles/moles:dgDataCreator/moles:dgRoleHolder[not(exists(moles:endDate)) or moles:endDate=''][1] 
    109 return  
    110 for $DataCreatorRoleHolder in collection('/db/ndg_B_metadata')/moles:dgMetadata/*[(moles:dgMetadataID/moles:schemeIdentifier='NDG-B0' 
    111 and moles:dgMetadataID/moles:repositoryIdentifier=$DataCreatorRole/*/moles:repositoryIdentifier 
    112 and moles:dgMetadataID/moles:localIdentifier=$DataCreatorRole/*/moles:localIdentifier)] 
    113 return  
    114 element Data_Creator { 
    115 if (exists($DataCreatorRoleHolder/moles:name/moles:initials)) then  
    116 string(concat(string($DataCreatorRoleHolder/moles:name/moles:initials), ' ', string($DataCreatorRoleHolder/moles:name/moles:familyName))) 
    117 else  
    118 string($DataCreatorRoleHolder/moles:abbreviation) 
    119 }, 
    120 element Dataset_Title {string($DE/moles:name)} 
    121 }, 
    122 for $StructuredKeyword in ($DE//(moles:dgStructuredKeyword | moles:dgStdParameterMeasured)[moles:dgValidTermID/moles:ParentListID='http://gcmd.gsfc.nasa.gov/Resources/valids/gcmd_parameters.html' and moles:ListLevel = 0]) 
    123 return if (exists($StructuredKeyword/*/moles:dgValidTerm)) then  
    124 element Parameters { 
    125 element Category {string($StructuredKeyword/*/moles:dgValidTerm)}, 
    126 if (exists($StructuredKeyword//moles:dgValidSubterm)  
    127 and $StructuredKeyword//moles:dgValidSubterm != ''  
    128 and $StructuredKeyword//moles:dgValidSubterm != ' ') then  
    129 for $Subterm in $StructuredKeyword//moles:dgValidSubterm[exists(moles:dgValidTerm)] 
    130 where exists($Subterm/moles:dgValidSubterm) 
    131 order by $Subterm/moles:ListLevel 
    132 return  
    133 if ($Subterm/moles:ListLevel=1) then element Topic {string($Subterm/moles:dgValidSubterm/moles:dgValidTerm)}  
    134 else if ($Subterm/moles:ListLevel=2) then element Term {string($Subterm/moles:dgValidSubterm/moles:dgValidTerm)}  
    135 else if ($Subterm/moles:ListLevel=3) then element Variable {string($Subterm/moles:dgValidSubterm/moles:dgValidTerm)}  
    136 else if ($Subterm/moles:ListLevel=4) then element Detailed_Variable {string($Subterm/moles:dgValidSubterm/moles:dgValidTerm)}  
    137 else element GCMD_Science_Valid {string($Subterm/moles:dgValidSubterm/moles:dgValidTerm)} 
    138 else() 
    139 } 
    140 else(), 
    141 for $ISOTopicCategory in ($DE/moles:dgStructuredKeyword[moles:dgValidTermID/moles:ParentListID='http://www.isotc211.org/2005/resources/Codelist/gmxCodelists.xml#MD_TopicCategoryCode']) 
    142 return  
    143 element ISO_Topic_Category {string($ISOTopicCategory/moles:dgValidTerm)}, 
    144 for $Keyword in distinct-values($DE//moles:dgStructuredKeyword[ 
    145 moles:dgValidTermID/moles:ParentListID!='http://gcmd.gsfc.nasa.gov/Resources/valids/gcmd_parameters.html'  
    146 and  
    147 moles:dgValidTermID/moles:ParentListID!='http://www.isotc211.org/2005/resources/Codelist/gmxCodelists.xml#MD_TopicCategoryCode'  
    148 ]/moles:dgValidTerm) 
    149 return element Keyword {string($Keyword)},  
    150 for $DepDPT in ($DE/moles:dgDataEntity/moles:RelatedDeployment/moles:DataProductionToolID)  
    151 return  
    152 element Sensor_Name { 
    153 for $DepDPTAbbrev in distinct-values((collection('/db/ndg_B_metadata')/moles:dgMetadata/moles:dgMetadataRecord[moles:dgMetadataID[moles:schemeIdentifier='NDG-B0' and moles:repositoryIdentifier=$DepDPT/moles:repositoryIdentifier and moles:localIdentifier=$DepDPT/moles:localIdentifier]]/moles:abbreviation)) 
    154 return element Short_Name {data($DepDPTAbbrev)}, 
    155 for $DepDPTName in distinct-values((collection('/db/ndg_B_metadata')/moles:dgMetadata/moles:dgMetadataRecord[moles:dgMetadataID[moles:schemeIdentifier='NDG-B0' and moles:repositoryIdentifier=$DepDPT/moles:repositoryIdentifier and moles:localIdentifier=$DepDPT/moles:localIdentifier]]/moles:name)) 
    156 return element Long_Name {data($DepDPTName)} 
    157 }, 
    158 for $DepObsStn in ($DE/moles:dgDataEntity/moles:RelatedDeployment/moles:ObservationStationID) 
    159 return  
    160 element Source_Name { 
    161 for $DepObsStnAbbrev in distinct-values((collection('/db/ndg_B_metadata')/moles:dgMetadata/moles:dgMetadataRecord[moles:dgMetadataID[moles:schemeIdentifier='NDG-B0' and moles:repositoryIdentifier=$DepObsStn/moles:repositoryIdentifier and moles:localIdentifier=$DepObsStn/moles:localIdentifier]]/moles:abbreviation)) 
    162 return element Short_Name {data($DepObsStnAbbrev)}, 
    163 for $DepObsStnName in distinct-values((collection('/db/ndg_B_metadata')/moles:dgMetadata/moles:dgMetadataRecord[moles:dgMetadataID[moles:schemeIdentifier='NDG-B0' and moles:repositoryIdentifier=$DepObsStn/moles:repositoryIdentifier and moles:localIdentifier=$DepObsStn/moles:localIdentifier]]/moles:name)) 
    164 return element Long_Name {data($DepObsStnName)} 
    165 }, 
    166 for $TemporalRange in ($DE/moles:dgDataEntity/moles:dgDataSummary/moles:dgDataCoverage//moles:dgDateRange) 
    167 return  
    168 element Temporal_Coverage 
    169 { 
    170 element Start_Date {data($TemporalRange/moles:DateRangeStart)}, 
    171 element End_Date {data($TemporalRange/moles:DateRangeEnd)} 
    172 }, 
    173 for $TemporalTerm in ($DE/moles:dgDataEntity/moles:dgDataSummary/moles:dgDataCoverage//moles:dgArea/moles:dgValidTerm) 
    174 return element Paleo_Temporal_Coverage {element Chronostratigraphic_Unit {data($TemporalTerm)}},  
    175 for $Data_Set_Progress in ($DE/moles:dgDataEntity/moles:dgDataSummary/moles:dgDataStatus/moles:dgDatasetClosure) 
    176 return element Data_Set_Progress {data($Data_Set_Progress)}, 
    177 for $BoundingBox in ($DE/moles:dgDataEntity/moles:dgDataSummary/moles:dgDataCoverage//moles:BoundingBox) 
    178 return  
    179 element Spatial_Coverage { 
    180 element Southernmost_Latitude {data($BoundingBox/moles:LimitSouth)}, 
    181 element Northernmost_Latitude {data($BoundingBox/moles:LimitNorth)}, 
    182 element Westernmost_Longitude {data($BoundingBox/moles:LimitWest)}, 
    183 element Easternmost_Longitude {data($BoundingBox/moles:LimitEast)} 
    184 }, 
    185 for $Location in ($DE/moles:dgDataEntity/moles:dgDataSummary/moles:dgDataCoverage//moles:dgArea[moles:dgValidTermID/moles:ParentListID='http://gcmd.gsfc.nasa.gov/Resources/valids/gcmd_locations.html']) 
    186 return element Location {$Location},  
    187 for $DepAct in ($DE/moles:dgDataEntity/moles:RelatedDeployment/moles:ActivityID) 
    188 return  
    189 element Project { 
    190 for $DepActAbbrev in distinct-values(collection('/db/ndg_B_metadata')/moles:dgMetadata/moles:dgMetadataRecord[moles:dgMetadataID[moles:schemeIdentifier='NDG-B0' and moles:repositoryIdentifier=$DepAct/moles:repositoryIdentifier and moles:localIdentifier=$DepAct/moles:localIdentifier]]/moles:abbreviation) 
    191 return  
    192 element Short_Name {data($DepActAbbrev)}, 
    193 for $DepActName in distinct-values(collection('/db/ndg_B_metadata')/moles:dgMetadata/moles:dgMetadataRecord[moles:dgMetadataID[moles:schemeIdentifier='NDG-B0' and moles:repositoryIdentifier=$DepAct/moles:repositoryIdentifier and moles:localIdentifier=$DepAct/moles:localIdentifier]]/moles:name) 
    194 return  
    195 element Long_Name {data($DepActName)} 
    196 }, 
    197 if (exists($DE/moles:dgDataEntity/moles:dgDataGranule/moles:accessControlPolicy)) then  
    198 for $DG in $DE/moles:dgDataEntity/moles:dgDataGranule 
    199 return  
    200 element Access_Constraints { 
    201 if (exists($DG/moles:dgGranuleSummary/moles:dgGranuleName)) then  
    202 concat('For data granule ', $DG/moles:dgGranuleSummary/moles:dgGranuleName, ': ') 
    203 else (), 
    204 if (exists($DG/moles:accessControlPolicy/moles:accessControlPolicyURL)) then 
    205 concat('See access control policy at ', escape-uri($DG/moles:accessControlPolicy/moles:accessControlPolicyURL, true())) 
    206 else if (exists($DG/moles:accessControlPolicy/moles:accessControlPolicyText)) then 
    207 data($DG/moles:accessControlPolicy/moles:accessControlPolicyText) 
    208 else  
    209 for $securityCondition in $DG/moles:accessControlPolicy/moles:dgSecurityCondition 
    210 return 
    211 if (exists($securityCondition/moles:conditionExplanationText)) then 
    212 concat('Effect: ', data($securityCondition/moles:effect), ' - ', data($securityCondition/moles:conditionExplanationText)) 
    213 else concat('Effect: ', data($securityCondition/moles:effect), 'needs ', data($securityCondition/moles:attauthRole), ' from ', data($securityCondition/moles:dgAttributeAuthority)) 
    214 } 
    215 else (), 
    216 if (exists($DE/moles:dgDataEntity/moles:dgDataRoles/moles:dgDataCreator/moles:dgRoleHolder)) then 
    217 for $DataCreatorRole in $DE/moles:dgDataEntity/moles:dgDataRoles/moles:dgDataCreator/moles:dgRoleHolder  
    218 order by $DataCreatorRole/moles:startDate empty least 
    219 return  
    220 for $DataCreatorRoleHolder in collection('/db/ndg_B_metadata')/moles:dgMetadata/(moles:dgOrganisation | moles:dgPerson) 
    221 [(moles:dgMetadataID/moles:schemeIdentifier='NDG-B0' 
    222 and moles:dgMetadataID/moles:repositoryIdentifier=$DataCreatorRole/(moles:dgOrganisationID | moles:dgPersonID)/moles:repositoryIdentifier 
    223 and moles:dgMetadataID/moles:localIdentifier=$DataCreatorRole/(moles:dgOrganisationID | moles:dgPersonID)/moles:localIdentifier)] 
    224 return  
    225 element Originating_Center { 
    226 if (string(local-name($DataCreatorRoleHolder))='dgOrganisation') then 
    227 (string($DataCreatorRoleHolder/moles:name)) 
    228 else if (string(local-name($DataCreatorRoleHolder))='dgPerson') then 
    229 (string(concat(string($DataCreatorRoleHolder/moles:name/moles:initials), ' ', string($DataCreatorRoleHolder/moles:name/moles:familyName)))) 
    230 else ('empty content') 
    231 } 
    232 else (), 
    233 for $DataCuratorRole in $DE/moles:dgDataEntity/moles:dgDataRoles/moles:dgDataCurator/moles:dgRoleHolder[not(exists(moles:endDate)) or moles:endDate=''] 
    234 return  
    235 for $DataCuratorRoleHolder in collection('/db/ndg_B_metadata')/moles:dgMetadata/*[(moles:dgMetadataID/moles:schemeIdentifier='NDG-B0' 
    236 and moles:dgMetadataID/moles:repositoryIdentifier=$DataCuratorRole/*/moles:repositoryIdentifier 
    237 and moles:dgMetadataID/moles:localIdentifier=$DataCuratorRole/*/moles:localIdentifier)] 
    238 return  
    239 element Data_Center { 
    240 element Data_Center_Name { 
    241 element Short_Name { 
    242 if (exists($DataCuratorRoleHolder/moles:name/moles:initials)) then  
    243 string(concat(string($DataCuratorRoleHolder/moles:name/moles:initials), ' ', string($DataCuratorRoleHolder/moles:name/moles:familyName))) 
    244 else  
    245 string($DataCuratorRoleHolder/moles:abbreviation) 
    246 }, 
    247 element Long_Name { 
    248 if (exists($DataCuratorRoleHolder/moles:name/moles:initials)) then  
    249 string(concat(string($DataCuratorRoleHolder/moles:name/moles:initials), ' ', string($DataCuratorRoleHolder/moles:name/moles:familyName))) 
    250 else string($DataCuratorRoleHolder/moles:name) 
    251 } 
    252 }, 
    253 if (exists($DataCuratorRoleHolder/moles:contactDetails/moles:URI)) then  
    254 element Data_Center_URL {data($DataCuratorRoleHolder/moles:contactDetails/moles:URI)} 
    255 else (), 
    256 for $DGID in $DE/moles:dgDataEntity/moles:dgDataGranule/moles:dataModelID 
    257 return  
    258 element Data_Set_ID {concat($DGID/moles:repositoryIdentifier, ':', $DGID/moles:schemeIdentifier, ':', $DGID/moles:localIdentifier)}, 
    259 element Personnel { 
    260 element Role {'Data Center Contact'}, 
    261 if (exists($DataCuratorRoleHolder/name/knownAs)) then  
    262 element First_Name {string($DataCuratorRoleHolder/moles:name/moles:knownAs)} 
    263 else if (exists($DataCuratorRoleHolder/moles:name/moles:initials)) then  
    264 element First_Name {string($DataCuratorRoleHolder/moles:name/moles:initials)} 
    265 else (), 
    266 element Last_Name { 
    267 if (exists($DataCuratorRoleHolder/moles:name/moles:familyName)) then  
    268 string($DataCuratorRoleHolder/moles:name/moles:familyName) 
    269 else string($DataCuratorRoleHolder/moles:name) 
    270 }, 
    271 if (exists($DataCuratorRole/moles:contactDetails/moles:eMail)) then  
    272 element Email {string($DataCuratorRole/moles:contactDetails/moles:eMail)} 
    273 else  
    274 if (exists($DataCuratorRoleHolder/contactDetails/eMail)) then  
    275 (element Email {string($DataCuratorRoleHolder/moles:contactDetails/moles:eMail)}) 
    276 else (), 
    277 if (exists($DataCuratorRole/moles:contactDetails/moles:telephone)) then  
    278 element Phone {string($DataCuratorRole/moles:contactDetails/moles:telephone)} 
    279 else if (exists($DataCuratorRoleHolder/moles:contactDetails/moles:telephone)) then  
    280 element Phone {string($DataCuratorRoleHolder/moles:contactDetails/moles:telephone)} 
    281 else (), 
    282 if (exists($DataCuratorRole/moles:contactDetails/moles:fax)) then  
    283 element Fax {string($DataCuratorRole/moles:contactDetails/moles:fax)} 
    284 else if (exists($DataCuratorRoleHolder/moles:contactDetails/moles:fax)) then  
    285 element Fax {string($DataCuratorRoleHolder/moles:contactDetails/moles:fax)} 
    286 else (), 
    287 if (exists($DataCuratorRole/moles:contactDetails/moles:address)) then  
    288 element Contact_Address { 
    289 for $addressline in $DataCuratorRole/moles:contactDetails/moles:address/moles:addressline 
    290 return element Address {data($addressline)}, 
    291 if (exists($DataCuratorRole/moles:contactDetails/moles:address/moles:city)) then  
    292 element City {string($DataCuratorRole/moles:contactDetails/moles:address/moles:city)} 
    293 else (), 
    294 if (exists($DataCuratorRole/moles:contactDetails/moles:address/moles:postcode)) then  
    295 element Postal_Code {string($DataCuratorRole/moles:contactDetails/moles:address/moles:postcode)} 
    296 else (), 
    297 if (exists($DataCuratorRole/moles:contactDetails/moles:address/moles:country)) then  
    298 element Country {string($DataCuratorRole/moles:contactDetails/moles:address/moles:country)} 
    299 else () 
    300 } 
    301 else if (exists($DataCuratorRoleHolder/moles:contactDetails/moles:address)) then  
    302 element Contact_Address { 
    303 for $addressline in $DataCuratorRoleHolder/moles:contactDetails/moles:address/moles:addressline 
    304 return element Address {data($addressline)}, 
    305 if (exists($DataCuratorRoleHolder/moles:contactDetails/moles:address/moles:city)) then  
    306 element City {string($DataCuratorRoleHolder/moles:contactDetails/moles:address/moles:city)} 
    307 else (), 
    308 if (exists($DataCuratorRoleHolder/moles:contactDetails/moles:address/moles:postcode)) then  
    309 element Postal_Code {string($DataCuratorRole/moles:contactDetails/moles:postcode)} 
    310 else (), 
    311 if (exists($DataCuratorRoleHolder/moles:contactDetails/moles:address/moles:country)) then  
    312 element Country {string($DataCuratorRoleHolder/moles:contactDetails/moles:address/moles:country)} 
    313 else () 
    314 } 
    315 else () 
    316 } 
    317 }, 
    318 element Summary {string($DE/moles:dgMetadataDescription/moles:abstract/moles:abstractText)}, 
    319 element Related_URL { 
    320 element URL_Content_Type {'NDG_B_SERVICE'}, 
    321 element URL {data($DE/moles:dgMetadataID/moles:repositoryIdentifier)}, 
    322 element Description {'The NDG service for browsing metadata.'} 
    323 }, 
    324 for $DG in $DE/moles:dgDataEntity/moles:dgDataGranule 
    325 return 
    326 if ($DG/moles:dataModelID/moles:schemeIdentifier='NDG-A0') then  
    327 (element Related_URL { 
    328 element URL_Content_Type {'NDG_A_SERVICE'}, 
    329 if (exists($DG/instance/URI)) then 
    330 element URL {escape-uri($DG/instance/URI, true())} 
    331 else if ($DG/moles:dataModelID/moles:repositoryIdentifier='badc.nerc.ac.uk') then 
    332 element URL {escape-uri('http://dmgdev1.esc.rl.ac.uk/cgi-bin/ndgDataAccess?datasetSource=dmgdev1.esc.rl.ac.uk&amp;datasetID=', true())} 
    333 else ($DG/moles:dataModelID/moles:repositoryIdentifier), 
    334 element Description {'The NDG service delivering data via NDG A metadata.'} 
    335 }, 
    336 element Related_URL { 
    337 element URL_Content_Type {'GET DATA > CSML'}, 
    338 if (exists($DG/instance)) then 
    339 element URL {escape-uri($DG/instance/URI, true())} 
    340 else if ($DG/moles:dataModelID/moles:repositoryIdentifier='badc.nerc.ac.uk') then 
    341 element URL {escape-uri('http://dmgdev1.esc.rl.ac.uk/cgi-bin/ndgDataAccess?datasetSource=dmgdev1.esc.rl.ac.uk&amp;datasetID=', true())} 
    342 else (), 
    343 element Description {'The NDG service delivering data via NDG A metadata. Additional tag to be more in line with latest version of GCMD valids'} 
    344 }) 
    345 else if ($DG/moles:dataModelID/moles:schemeIdentifier='CDML-0') then  
    346 (element Related_URL {  
    347 element URL_Content_Type {'NDG_A_SERVICE'}, 
    348 if (exists($DG/instance)) then 
    349 element URL {escape-uri($DG/instance/URI, true())} 
    350 else if ($DG/moles:dataModelID/moles:repositoryIdentifier='badc.nerc.ac.uk') then 
    351 element URL {escape-uri('http://cdat.badc.nerc.ac.uk/cgi-bin/dxui.py?datasetURI_1=/', true())} 
    352 else (), 
    353 element Description {'The NDG service delivering data via Data extractor. Additional tag to be more in line with latest version of GCMD valids'} 
    354 }, 
    355 element Related_URL { 
    356 element URL_Content_Type {'GET DATA > DX'}, 
    357 if (exists($DG/instance)) then 
    358 element URL {escape-uri($DG/instance/URI, true())} 
    359 else if ($DG/moles:dataModelID/moles:repositoryIdentifier='badc.nerc.ac.uk') then 
    360 element URL {escape-uri('http://cdat.badc.nerc.ac.uk/cgi-bin/dxui.py?datasetURI_1=/', true())} 
    361 else (), 
    362 element Description {'The NDG service delivering data via Data extractor. Additional tag to be more in line with latest version of GCMD valids'} 
    363 }) 
    364 else if ($DG/moles:dataModelID/moles:schemeIdentifier='URI') then  
    365 element Related_URL { 
    366 element URL {data($DG/moles:instance/moles:URI)}, 
    367 if (exists($DG/moles:instance/moles:instanceComment)) then  
    368 element Description {data($DG/moles:instance/moles:instanceComment)}  
    369 else  
    370 element Description {'URL to aid in delivering data. Note that this may point directly to the data or, more likely, point to the web site of the curator.'} 
    371 } 
    372 else (), 
    373 for $RelURL_Desc in ($DE/moles:dgMetadataDescription/moles:descriptionSection/moles:descriptionOnlineReference)  
    374 return 
    375 element Related_URL { 
    376 element URL {data($RelURL_Desc)}, 
    377 element Description {concat(data($RelURL_Desc/moles:name), ' - ', data($RelURL_Desc/moles:notes))}  
    378 }, 
    379 element Metadata_Name {'[CEOS IDN DIF]'}, 
    380 element Metadata_Version {'9.4'}, 
    381 if (exists($DE/moles:dgMetadataProvenance/moles:RecordCreation)) then  
    382 element DIF_Creation_Date {data($DE/moles:dgMetadataProvenance/moles:RecordCreation/moles:CreatedDate)} 
    383 else (), 
    384 for $MDUpdt in $DE/moles:dgMetadataProvenance/moles:RecordUpdate 
    385 return  
    386 element DIF_Revision_History {concat(data($MDUpdt/moles:UpdateDate), ' - ', data($MDUpdt/moles:UpdateSummary), ' - ', data($MDUpdt/moles:UpdatedBy))}, 
    387 if (exists($DE/moles:dgMetadataProvenance/moles:RecordReview)) then  
    388 element Future_DIF_Review_Date {data($DE/moles:dgMetadataProvenance/moles:RecordReview/moles:ReviewDate)} 
    389 else () 
    390 }''' 
     1236utillib:strip-namespace( 
     1237    element dgMetadataRecord { 
     1238        element dgMetadataID { 
     1239            element schemeIdentifier {'NDG-B1', 
     1240            $ObsStn/moles:dgMetadataID/moles:repositoryIdentifier, 
     1241            $ObsStn/moles:dgMetadataID/moles:localIdentifier 
     1242        } (: End </dgMetadataID> :),  
     1243        $ObsStn/moles:dgMetadataDescription, 
     1244        $ObsStn/moles:name, 
     1245        $ObsStn/moles:abbreviation, 
     1246        element dgObservationStation {  
     1247            $ObsStn/moles:dgObservationStation/moles:contactDetails, 
     1248            if (exists($ObsStn/moles:dgObservationStation/moles:dgStationaryPlatform)) then  
     1249                element dgStationaryPlatform {  
     1250                $ObsStn/moles:dgObservationStation/moles:dgStationaryPlatform/moles:position, 
     1251                if (exists($ObsStn/moles:dgObservationStation/moles:dgStationaryPlatform/moles:dgMooring)) then    
     1252                    element dgMooring {  
     1253                        $ObsStn/moles:dgObservationStation/moles:dgStationaryPlatform/moles:dgMooring/moles:dateStart, 
     1254                        $ObsStn/moles:dgObservationStation/moles:dgStationaryPlatform/moles:dgMooring/moles:dateEnd, 
     1255                        for $DepositingCruise in $ObsStn/moles:dgObservationStation/moles:dgStationaryPlatform/moles:dgMooring/moles:DepositingCruise 
     1256                        return  
     1257                            f:return-stub-activity('depositingcruise', data($DepositingCruise/moles:dgMetadataID/moles:repositoryIdentifier), data($DepositingCruise/moles:dgMetadataID/moles:localIdentifier)), 
     1258                            if (exists($ObsStn/moles:dgObservationStation/moles:dgStationaryPlatform/moles:dgMooring/moles:dgStationGrouping)) then    
     1259                                for $StationGrouping in $ObsStn/moles:dgObservationStation/moles:dgStationaryPlatform/moles:dgMooring/dgStationGrouping  
     1260                                return   
     1261                                    f:return-stub-obsstn('stationgrouping', data($StationGrouping/moles:dgMetadataID/moles:repositoryIdentifier), data($StationGrouping/moles:dgMetadataID/moles:localIdentifier)) 
     1262                            else () 
     1263                    } (: End </dgMooring> :)   
     1264                else if (exists($ObsStn/dgObservationStation/dgStationaryPlatform/dgStationGroup)) then    
     1265                    element dgStationGroup {  
     1266                        element dgGroupedStations {  
     1267                            if (exists($ObsStn/dgObservationStation/dgStationaryPlatform/dgStationGroup/dgGroupedStations)) then 
     1268                                for $GroupedStation in $ObsStn/dgObservationStation/dgStationaryPlatform/dgStationGroup/dgGroupedStation/dgGroupedStations 
     1269                                return f:return-stub-obsstn('dgGroupedStation', data($GroupedStation/moles:dgMetadataID/moles:repositoryIdentifier), data($GroupedStation/moles:dgMetadataID/moles:localIdentifier)) 
     1270                            else () 
     1271                        } (: End </dgGroupedStations> :) 
     1272                    } (: End </dgStationGroup> :)  
     1273                else $ObsStn/moles:dgObservationStation/moles:dgLandStation 
     1274                } (: End </dgStationaryPlatform>  :)   
     1275                else $ObsStn/moles:dgObservationStation/moles:dgMovingPlatform  
     1276            } ,  
     1277            for $RelDep in $ObsStn/moles:dgObservationStation/moles:ObsStationDeployment  
     1278            return  
     1279                element ObsStationDeployment { 
     1280                $RelDep/moles:DateStart, 
     1281                $RelDep/moles:DateEnd, 
     1282                $RelDep/moles:dgMetadataID, 
     1283                f:return-stub-activity('activity', data($RelDep/moles:ActivityID/moles:repositoryIdentifier), data($RelDep/moles:ActivityID/moles:localIdentifier)), 
     1284                f:return-stub-dpt('dataproductiontool', data($RelDep/moles:DataProductionToolID/moles:repositoryIdentifier), data($RelDep/moles:DataProductionToolID/moles:localIdentifier)),  
     1285                for $DE in collection($targetCollection)/moles:dgMetadata/moles:dgMetadataRecord 
     1286                    where $DE/moles:dgDataEntity/moles:RelatedDeployment/moles:dgMetadataID/moles:repositoryIdentifier=$RelDep/moles:dgMetadataID/moles:repositoryIdentifier 
     1287                            and $DE/moles:dgDataEntity/moles:RelatedDeployment/moles:dgMetadataID/moles:localIdentifier=$RelDep/moles:dgMetadataID/moles:localIdentifier 
     1288                return 
     1289                    f:return-stub-dataentity('dataentity', data($DE/moles:dgMetadataID/moles:repositoryIdentifier), data($DE/moles:dgMetadataID/moles:localIdentifier)), 
     1290                $RelDep/moles:Coverage 
     1291            } (: </RelatedDeployment> :)  
     1292        } (: </dgObservationStation> :),  
     1293    for $strkwrd in $ObsStn/moles:dgStructuredKeyword return $strkwrd, 
     1294    if (exists($ObsStn/moles:dgMetadataProvenance)) then $ObsStn/moles:dgMetadataProvenance else (), 
     1295    if (exists($ObsStn/moles:dgMetadataSecurity)) then $ObsStn/moles:dgMetadataSecurity else () 
     1296    } (: End </dgMetadataRecord> :) 
     1297) (: End namespace strip :)''' 
    3911298 
    3921299ISO19139Query = '''declare default element namespace 'http://www.isotc211.org/2005/gmd'; declare namespace moles='http://ndg.nerc.ac.uk/moles'; declare namespace gco='http://www.isotc211.org/2005/gco'; declare namespace gmd='http://www.isotc211.org/2005/gmd'; declare namespace gml='http://www.opengis.net/gml'; declare namespace xlink='http://www.w3.org/1999/xlink'; declare namespace xsi='http://www.w3.org/2001/XMLSchema-instance'; declare namespace f='http://ndg.nerc.ac.uk/moles/localfunctions'; for $DE in collection('/db/ndg_B_metadata')/moles:dgMetadata/moles:dgMetadataRecord[ exists(moles:dgDataEntity) and moles:dgMetadataID/moles:schemeIdentifier='NDG-B0' and moles:dgMetadataID/moles:repositoryIdentifier='RepositoryID' and moles:dgMetadataID/moles:localIdentifier='LocalID'] return element MD_Metadata {attribute xsi:schemaLocation {'http://www.isotc211.org/2005/gmd http://www.isotc211.org/2005/gmd/metadataEntity.xsd'}, element fileIdentifier {element gco:CharacterString {concat($DE/moles:dgMetadataID/moles:repositoryIdentifier, ':ISO19115:', $DE/moles:dgMetadataID/moles:localIdentifier)}}, element language {element gmd:LanguageCode {attribute codeList {'http://www.isotc211.org/2005/resources/Codelist/gmxCodelists.xml#LanguageCode'}, attribute codeListValue {'eng'}, 'English'}}, element hierarchyLevel {element MD_ScopeCode {attribute codeList {'http://www.isotc211.org/2005/resources/Codelist/gmxCodelists.xml#MD_ScopeCode'}, attribute codeListValue {'http://www.isotc211.org/2005/resources/Codelist/gmxCodelists.xml#MD_ScopeCode_dataset'}, 'dataset'}}, for $parent in $DE/moles:dgRelatedDataEntity[moles:RelationID/moles:dgValidTerm='is-part-of'] return element parentIdentifier {element gco:CharacterString {concat($DE/moles:dgMetadataID/moles:repositoryIdentifier, ':', $DE/moles:dgMetadataID/moles:localIdentifier, ':', $DE/moles:dgMetadataID/moles:localIdentifier)}}, for $DataCuratorRole in $DE/moles:dgDataEntity/moles:dgDataRoles/moles:dgDataCurator/moles:dgRoleHolder[not(exists(moles:endDate)) or moles:endDate=''] return for $DataCuratorRoleHolder in collection('/db/ndg_B_metadata')/moles:dgMetadata/(moles:dgPerson | moles:dgOrganisation)[ moles:dgMetadataID/moles:schemeIdentifier='NDG-B0' and moles:dgMetadataID/moles:repositoryIdentifier=$DataCuratorRole/(moles:dgPersonID | moles:dgOrganisationID)/moles:repositoryIdentifier and moles:dgMetadataID/moles:localIdentifier=$DataCuratorRole/(moles:dgPersonID | moles:dgOrganisationID)/moles:localIdentifier] return element contact {element CI_ResponsibleParty {if (string(local-name($DataCuratorRoleHolder))='dgOrganisation') then element organisationName {element gco:CharacterString {string($DataCuratorRoleHolder/moles:name)}}else if (string(local-name($DataCuratorRoleHolder))='dgPerson') then element individualName {element gco:CharacterString {string(concat(string($DataCuratorRoleHolder/moles:name/moles:initials), ' ', string($DataCuratorRoleHolder/moles:name/moles:familyName)))}}else ('empty content') , element positionName {if (exists($DataCuratorRoleHolder/moles:localName)) then element gco:CharacterString {$DataCuratorRoleHolder/moles:localName}else if (exists($DataCuratorRole/moles:roleName)) then element gco:CharacterString {$DataCuratorRole/moles:roleName}else element gco:CharacterString {'Curator'}}, element contactInfo {element gmd:CI_Contact {if (exists($DataCuratorRole/moles:contactDetails/moles:telephone or $DataCuratorRole/moles:contactDetails/moles:fax or $DataCuratorRoleHolder/moles:contactDetails/moles:telephone or $DataCuratorRoleHolder/moles:contactDetails/moles:fax)) then element phone {element gmd:CI_Telephone {if (exists($DataCuratorRole/moles:contactDetails/moles:telephone)) then element voice {element gco:CharacterString {data($DataCuratorRole/moles:contactDetails/moles:telephone)}}else if (exists($DataCuratorRoleHolder/moles:contactDetails/moles:telephone)) then element voice {element gco:CharacterString {data($DataCuratorRoleHolder/moles:contactDetails/moles:telephone)}}else (), if (exists($DataCuratorRole/moles:contactDetails/moles:fax)) then element facsimile {element gco:CharacterString {data($DataCuratorRoleHolder/moles:contactDetails/moles:fax)}}else if (exists($DataCuratorRoleHolder/moles:contactDetails/moles:telephone)) then element facsimile {element gco:CharacterString {data($DataCuratorRoleHolder/moles:contactDetails/moles:fax)}}else () }} else (), element address {element gmd:CI_Address {if (exists($DataCuratorRole/moles:contactDetails/moles:address/moles:addressline)) then for $addressline in $DataCuratorRole/moles:contactDetails/moles:address/moles:addressline return element deliveryPoint {element gco:CharacterString {data($addressline)}}else if (exists($DataCuratorRoleHolder/moles:contactDetails/moles:address/moles:addressline)) then for $addressline in $DataCuratorRoleHolder/moles:contactDetails/moles:address/moles:addressline return element deliveryPoint {element gco:CharacterString {data($addressline)}}else (), if (exists($DataCuratorRole/moles:contactDetails/moles:address/moles:city)) then element city {element gco:CharacterString {data($DataCuratorRole/moles:contactDetails/moles:address/moles:city)}}else if (exists($DataCuratorRoleHolder/moles:contactDetails/moles:address/moles:city)) then element city {element gco:CharacterString {data($DataCuratorRoleHolder/moles:contactDetails/moles:address/moles:city)}}else (), if (exists($DataCuratorRole/moles:contactDetails/moles:eMail)) then element electronicMailAddress {element gco:CharacterString {data($DataCuratorRole/moles:contactDetails/moles:eMail)}}else if (exists($DataCuratorRoleHolder/moles:contactDetails/moles:address/moles:postcode)) then element electronicMailAddress {element gco:CharacterString {data($DataCuratorRoleHolder/moles:contactDetails/moles:eMail)}}else () }}}}, element role {element CI_RoleCode {attribute codeList {'http://www.isotc211.org/2005/resources/Codelist/gmxCodelists.xml#CI_RoleCode'}, attribute codeListValue {'http://www.isotc211.org/2005/resources/Codelist/gmxCodelists.xml#CI_RoleCode_custodian'}, if (exists($DataCuratorRole/moles:localName)) then data($DataCuratorRole/moles:localName) else if (exists($DataCuratorRoleHolder/moles:localName)) then data($DataCuratorRoleHolder/moles:localName) else 'Curator' }}}}, element dateStamp {element gco:Date {adjust-date-to-timezone(current-date())}}, element metadataStandardName {element gco:CharacterString {'ISO 19115:2003'}}, element metadataStandardVersion {element gco:CharacterString {'2003'}}, element identificationInfo {element gmd:MD_DataIdentification {element citation {element gmd:CI_Citation {element title {element gco:CharacterString {data($DE/moles:name)}}, if (exists($DE/moles:abbreviation) and not(empty($DE/moles:abbreviation))) then element alternateTitle {element gco:CharacterString {data($DE/moles:abbreviation)}}else (), element date {if (exists($DE/(moles:DataProvenance | moles:dgMetadataProvenance)/moles:RecordCreation/moles:CreatedDate)) then element gmd:CI_Date {element date {element gco:Date {adjust-date-to-timezone(data($DE/(moles:DataProvenance | moles:dgMetadataProvenance)/moles:RecordCreation/moles:CreatedDate))}}, element dateType {element CI_DateTypeCode {attribute codeList {'http://www.isotc211.org/2005/resources/Codelist/moles:gmxCodelists.xml#CI_DateTypeCode'}, attribute codeListValue {'http://www.isotc211.org/2005/resources/Codelist/gmxCodelists.xml#CI_DateTypeCode_creation' }, 'Creation'}}}else attribute gco:nilReason {'unknown'}}}}, element abstract {element gco:CharacterString {data($DE/moles:dgMetadataDescription/moles:abstract/moles:abstractText)}}, element language {element gmd:LanguageCode {attribute codeList {'http://www.isotc211.org/2005/resources/Codelist/gmxCodelists.xml#LanguageCode'}, attribute codeListValue {'eng'}, 'English' }}, for $ISOTopicCategory in ($DE/moles:dgStructuredKeyword[dgValidTermID/moles:ParentListID='http://www.isotc211.org/2005/resources/Codelist/gmxCodelists.xml#MD_TopicCategoryCode']) return element topicCategory {element gmd:MD_TopicCategoryCode {attribute codeList {'http://www.isotc211.org/2005/resources/Codelist/gmxCodelists.xml#MD_TopicCategoryCode'}, attribute codeListValue {string($ISOTopicCategory/moles:dgValidTerm)}, string($ISOTopicCategory/moles:dgValidTerm)}}, if (exists($DE/moles:dgDataEntity/moles:dgDataSummary/moles:dgDataCoverage)) then element extent {element gmd:EX_Extent {(: add bounding boxes:) for $geoBBox in $DE/moles:dgDataEntity/moles:dgDataSummary/moles:dgDataCoverage//moles:BoundingBox return element geographicElement {element EX_GeographicBoundingBox {element westBoundLongitude {element gco:Decimal {data($geoBBox/moles:LimitWest)}}, element eastBoundLongitude {element gco:Decimal {data($geoBBox/moles:LimitEast)}}, element southBoundLatitude {element gco:Decimal {data($geoBBox/moles:LimitSouth)}}, element northBoundLatitude {element gco:Decimal {data($geoBBox/moles:LimitNorth)}}}}, for $geoArea in $DE/moles:dgDataEntity/moles:dgDataSummary/moles:dgDataCoverage//moles:dgArea return element geographicElement {element EX_GeographicDescription {element geographicIdentifier {element gmd:RS_Identifier {element gco:CharacterString {element code {data(dgArea/dgValidTerm)}}, element gco:CharacterString {element codeSpace {data(dgArea/dgValidTermID/ParentListID)}}}}}}}}else()}}}''' 
Note: See TracChangeset for help on using the changeset viewer.