source: TI07-MOLES/trunk/PythonCode/wsgi/ndg_xqueries.py @ 2372

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI07-MOLES/trunk/PythonCode/wsgi/ndg_xqueries.py@2372
Revision 2372, 115.8 KB checked in by ko23, 12 years ago (diff)

Fix MDIP2MOLES cast (see #655)

Line 
1# This version using xqueries from rev 1914 of
2# TI07-MOLES/trunk/JavaCode/returnmolesxmldb/ndg/services/returnmoles/Main.java
3# Update DIF/DC queries
4listingQuery='''
5declare default element namespace 'http://ndg.nerc.ac.uk/moles';for $DE
6in
7collection('TargetCollection')/dgMetadata/dgMetadataRecord[exists(dgDataEntity)] return
8<dgMetadataRecord>{$DE/dgMetadataID/repositoryIdentifier}
9{$DE/dgMetadataID/localIdentifier}<fileName>{util:document-name($DE)}</fileName></dgMetadataRecord>
10'''
11
12ObjectTypeQuery = '''declare default element namespace 'http://ndg.nerc.ac.uk/moles';declare variable $targetCollection as xs:string {'TargetCollection'};for $Obj in collection($targetCollection)/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>'''
13
14StubBDEQuery = '''declare default element namespace 'http://ndg.nerc.ac.uk/moles/dataentity';
15declare namespace f='http://ndg.nerc.ac.uk/moles/localfunctions';
16declare namespace moles='http://ndg.nerc.ac.uk/moles';
17
18import module namespace voclib='http://ndg.nerc.ac.uk/xquery/lib/vocab' at 'xmldb:exist:///db/xqueryLib/Vocabs/vocab_xquery_lib.xquery';
19import module namespace utillib='http://ndg.nerc.ac.uk/xquery/lib/utilities' at 'xmldb:exist:///db/xqueryLib/Utilities/utility_xquery_lib.xquery';
20(:
21Will return when scope problem sussed... ;(
22import module namespace stubblib='http://ndg.nerc.ac.uk/xquery/lib/stubb' at 'xmldb:exist:///db/xqueryLib/StubB/stubb_xquery_lib.xquery';
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=$repositoryIdentifier
243and moles:localIdentifier=$localIdentifier]]
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'''
352
353StubBActQuery = '''declare default element namespace 'http://ndg.nerc.ac.uk/moles/activity';
354declare namespace f='http://ndg.nerc.ac.uk/moles/localfunctions';
355declare namespace moles='http://ndg.nerc.ac.uk/moles';
356
357import module namespace voclib='http://ndg.nerc.ac.uk/xquery/lib/vocab' at 'xmldb:exist:///db/xqueryLib/Vocabs/vocab_xquery_lib.xquery';
358import module namespace utillib='http://ndg.nerc.ac.uk/xquery/lib/utilities' at 'xmldb:exist:///db/xqueryLib/Utilities/utility_xquery_lib.xquery';
359(:
360Will return when scope problem sussed... ;(
361import module namespace stubblib='http://ndg.nerc.ac.uk/xquery/lib/stubb' at 'xmldb:exist:///db/xqueryLib/StubB/stubb_xquery_lib.xquery';
362:)
363declare variable $targetCollection as xs:string {'TargetCollection'};
364declare variable $repositoryIdentifier as xs:string {'RepositoryID'};
365declare variable $localIdentifier as xs:string {'LocalID'};
366
367declare function f:return-stub-activity($out-element-name as xs:string, $repid as xs:string, $locid as xs:string) as element()
368(: Returns a 'stubB' style activity record to expand an activity record :)
369(: Security not implemented here yet :)
370{
371let $found := count(collection($targetCollection)/moles:dgMetadata/moles:dgMetadataRecord[
372            moles:dgMetadataID/moles:repositoryIdentifier=$repid
373            and moles:dgMetadataID/moles:localIdentifier=$locid])
374return 
375    if ($found = 0) then
376        element {$out-element-name} {
377            element error {'not found'},
378            element repositoryIdentifier {$repid},
379            element localIdentifier {$locid}
380        }
381    else (
382        for $SRAct in collection($targetCollection)/moles:dgMetadata/moles:dgMetadataRecord[
383            moles:dgMetadataID/moles:repositoryIdentifier=$repid
384            and moles:dgMetadataID/moles:localIdentifier=$locid]
385        return
386            element  {$out-element-name} {
387                ($SRAct/moles:dgMetadataID),
388                ($SRAct/moles:dgMetadataDescription),
389                ($SRAct/moles:name),
390                ($SRAct/moles:abbreviation),
391                element dgActivity {
392                    if (exists($SRAct/*/moles:dgActivityDataCollection)) then
393                        ($SRAct/*/moles:dgActivityDataCollection)
394                    else if (exists($SRAct/*/moles:dgActivityDataProject)) then
395                        ($SRAct/*/moles:dgActivityDataProject)
396                    else if (exists($SRAct/*/dgActivityDataCampaign)) then
397                        ($SRAct/moles:dgActivity/moles:dgActivityDataCampaign)
398                    else
399                        ($SRAct/moles:dgActivity/moles:dgActivityDataInvestigation),
400                    ($SRAct/moles:dgActivity/moles:dgActivityCoverage),
401                    ($SRAct/moles:dgActivity/moles:dgActivityDuration)
402                } (: </dgActivity> :)
403            } (: </activity> :)
404    )
405} ;
406
407declare function f:return-stub-dataentity($out-element-name as xs:string, $repid as xs:string, $locid as xs:string) as element()
408(: Returns a 'stubB' style observation station record to expand an data entity record :)
409(: Security not implemented yet :)
410{
411let $found := count(collection($targetCollection)/moles:dgMetadata/moles:dgMetadataRecord[
412        moles:dgMetadataID/moles:repositoryIdentifier=$repid
413        and moles:dgMetadataID/moles:localIdentifier=$locid])
414return 
415    if ($found = 0) then
416        element {$out-element-name} {
417            element error {'not found'},
418            element repositoryIdentifier {$repid},
419            element localIdentifier {$locid}
420        }
421    else (
422        for $SRDE in collection($targetCollection)/moles:dgMetadata/moles:dgMetadataRecord[
423            moles:dgMetadataID/moles:repositoryIdentifier=$repid
424            and moles:dgMetadataID/moles:localIdentifier=$locid]
425        return 
426            element {$out-element-name} {
427                ($SRDE/moles:dgMetadataID),
428                ($SRDE/moles:dgMetadataDescription),
429                ($SRDE/moles:name),
430                ($SRDE/moles:abbreviation),
431                element dgDataEntity {
432                    ($SRDE/moles:dgDataEntity/moles:dgDataSetType),
433                    ($SRDE/moles:dgDataEntity/moles:dgDataGranule),
434                    ($SRDE/moles:dgDataEntity/moles:dgDataSummary)
435                } (: </dgDataEntity> :)       
436            }
437    )
438} ;
439
440declare function f:return-stub-dpt($out-element-name as xs:string, $repid as xs:string, $locid as xs:string) as element()
441(: Returns a 'stubB' style dpt record to expand a dpt record :)
442(: Security not implemented yet :)
443{
444let $found := count(collection($targetCollection)/moles:dgMetadata/moles:dgMetadataRecord[
445        moles:dgMetadataID/moles:repositoryIdentifier=$repid
446        and moles:dgMetadataID/moles:localIdentifier=$locid])
447return 
448    if ($found = 0) then
449        element {$out-element-name} {
450            element error {'not found'},
451            element repositoryIdentifier {$repid},
452            element localIdentifier {$locid}
453        }
454    else (
455        for $SRDPT in collection($targetCollection)/moles:dgMetadata/moles:dgMetadataRecord[
456                moles:dgMetadataID/moles:repositoryIdentifier=$repid
457                and moles:dgMetadataID/moles:localIdentifier=$locid]
458        return 
459            element {$out-element-name} {
460                $SRDPT/moles:dgMetadataID,
461                $SRDPT/moles:dgMetadataDescription,
462                $SRDPT/moles:name,
463                $SRDPT/moles:abbreviation,
464                $SRDPT/moles:logos,
465                element dgDataProductionTool {
466                    $SRDPT/moles:dgDataProductionTool/moles:contactDetails,
467                    if (exists($SRDPT/moles:dgDataProductionTool/moles:dgModel)) then
468                        $SRDPT/moles:dgDataProductionTool/moles:dgModel
469                    else (),
470                    if (exists($SRDPT/moles:dgDataProductionTool/moles:dgInstrument)) then
471                        $SRDPT/moles:dgDataProductionTool/moles:dgInstrument
472                    else ()
473                    } (: </dgDataProductionTool> :)
474                } (: </dataproductiontool> :)
475    )
476} ;
477
478declare function f:return-stub-obsstn($out-element-name as xs:string, $repid as xs:string, $locid as xs:string) as element()
479(: Returns a 'stubB' style observation station record to expand an observation station record :)
480(: Security not implemented here yet;  :)
481{
482let $found := count(collection($targetCollection)/moles:dgMetadata/moles:dgMetadataRecord[
483            moles:dgMetadataID/moles:repositoryIdentifier=$repid
484            and moles:dgMetadataID/moles:localIdentifier=$locid])
485return 
486    if ($found = 0) then
487        element {$out-element-name} {
488            element error {'not found'},
489            element repositoryIdentifier {$repid},
490            element localIdentifier {$locid}
491        }
492    else (
493        for $SRObsStn in collection($targetCollection)/moles:dgMetadata/moles:dgMetadataRecord[
494            moles:dgMetadataID/moles:repositoryIdentifier=$repid
495            and moles:dgMetadataID/moles:localIdentifier=$locid]
496        return
497            element {$out-element-name} {
498                ($SRObsStn/moles:dgMetadataID),
499                ($SRObsStn/moles:dgMetadataDescription),
500                ($SRObsStn/moles:name),
501                ($SRObsStn/moles:abbreviation),
502                element dgObservationStation {
503                    ($SRObsStn/moles:dgObservationStation/moles:contactDetails),
504                    if (exists($SRObsStn/moles:dgObservationStation/moles:dgStationaryPlatform)) then
505                        element dgStationaryPlatform {
506                            $SRObsStn/moles:dgObservationStation/moles:dgStationaryPlatform/moles:position,
507                            if (exists($SRObsStn/moles:dgObservationStation/moles:dgStationaryPlatform/dgLandStation)) then element dgLandStation {''}
508                            else if (exists($SRObsStn/moles:dgObservationStation/moles:dgStationaryPlatform/dgMooring)) then element dgMooring {''}
509                            else if (exists($SRObsStn/moles:dgObservationStation/moles:dgStationaryPlatform/dgStationGroup)) then element dgStationGroup {''}
510                            else ()
511                            }
512                    else if (exists($SRObsStn/moles:dgObservationStation/moles:dgMovingPlatform)) then
513                        $SRObsStn/moles:dgObservationStation/moles:dgMovingPlatform
514                    else ()
515                } (: </dgObservationStation> :)
516            }
517    )
518} ;
519
520declare function f:return-stub-person($out-element-name as xs:string, $repid as xs:string, $locid as xs:string) as element()
521(: Returns a 'stubB' style observation station record to expand a person record :)
522(: Security not implemented yet :)
523{
524let $found := count(collection($targetCollection)/moles:dgMetadata/moles:dgPerson[
525            moles:dgMetadataID/moles:repositoryIdentifier=$repid
526            and moles:dgMetadataID/moles:localIdentifier=$locid])
527return 
528    if ($found = 0) then
529        element {$out-element-name} {
530            element error {'not found'},
531            element repositoryIdentifier {$repid},
532            element localIdentifier {$locid}
533        }
534    else (
535    for $person in collection($targetCollection)/moles:dgMetadata/moles:dgPerson[
536        moles:dgMetadataID/moles:repositoryIdentifier=$repid
537        and moles:dgMetadataID/moles:localIdentifier=$locid]
538    return
539        element  {$out-element-name}
540        {
541            ($person/moles:name),
542            ($person/moles:contactDetails),
543            ($person/moles:dgMetadataID)
544        }
545    )
546} ;
547
548declare function f:return-stub-organisation($out-element-name as xs:string, $repid as xs:string, $locid as xs:string) as element()
549(: Returns a 'stubB' style observation station record to expand an organisation record :)
550(: Security not implemented yet :)
551{
552    let $found := count(collection($targetCollection)/moles:dgMetadata/moles:dgOrganisation[
553                moles:dgMetadataID/moles:repositoryIdentifier=$repid
554                and moles:dgMetadataID/moles:localIdentifier=$locid])
555    return 
556        if ($found = 0) then
557            element {$out-element-name} {
558                element error {'not found'},
559                element repositoryIdentifier {$repid},
560                element localIdentifier {$locid}
561            }
562        else (
563            for $org in collection($targetCollection)/moles:dgMetadata/moles:dgOrganisation[
564                moles:dgMetadataID/moles:repositoryIdentifier=$repid
565                and moles:dgMetadataID/moles:localIdentifier=$locid]
566            return
567                element  {$out-element-name} {
568                    $org/moles:dgMetadataID,
569                    $org/moles:name,
570                    $org/moles:abbreviation,
571                    $org/moles:contactDetails,
572                    $org/moles:logos
573                }
574        )
575} ;
576
577for $Act in collection($targetCollection)/moles:dgMetadata/moles:dgMetadataRecord[
578    moles:dgActivity!=''
579    and moles:dgMetadataID[
580    moles:schemeIdentifier='NDG-B0'
581    and moles:repositoryIdentifier=$repositoryIdentifier
582    and moles:localIdentifier=$localIdentifier]]
583    return
584utillib:strip-namespace(element dgMetadataRecord {
585    element dgMetadataID {
586        element schemeIdentifier {'NDG-B1'},
587        (element repositoryIdentifier {data($Act/moles:dgMetadataID/moles:repositoryIdentifier)}),
588        element localIdentifier {data($Act/moles:dgMetadataID/moles:localIdentifier)}
589    },
590    $Act/moles:dgMetadataDescription,
591    $Act/moles:name,
592    $Act/moles:abbreviation,
593    element dgActivity {
594        for $RelatedActivities in $Act/dgActivity/moles:relatedActivity
595            return
596                element relatedActivity {
597                    $RelatedActivities/moles:activityRelation,
598                    f:return-stub-activity('activity', data($RelatedActivities/moles:relatedActivityID/moles:repositoryIdentifier), data($RelatedActivities/moles:relatedActivityID/moles:localIdentifier))
599            },    (: </relatedActivity> :)
600        if (exists($Act/moles:dgActivity/moles:dgActivityDataCollection)) then ($Act/moles:dgActivity/moles:dgActivityDataCollection)
601        else if (exists($Act/moles:dgActivity/moles:dgActivityDataProject)) then ($Act/moles:dgActivity/moles:dgActivityDataProject)
602        else if (exists($Act/moles:dgActivity/moles:dgActivityDataInvestigation)) then ($Act/moles:dgActivity/moles:dgActivityDataInvestigation)
603        else if (exists($Act/moles:dgActivity/moles:dgActivityDataCampaign)) then ($Act/moles:dgActivity/moles:dgActivityDataCampaign)
604        else (),
605        element dgActivityRole {
606            element dgInvestigator {
607                element dgPrincipalInvestigator {
608                    ($Act/moles:dgActivity/moles:dgActivityRole/moles:dgInvestigator/moles:dgPrincipalInvestigator/moles:dgMetadataID),
609                    ($Act/moles:dgActivity/moles:dgActivityRole/moles:dgInvestigator/moles:dgPrincipalInvestigator/moles:roleName),
610                    ($Act/moles:dgActivity/moles:dgActivityRole/moles:dgInvestigator/moles:dgPrincipalInvestigator/moles:abbreviation),
611                    ($Act/moles:dgActivity/moles:dgActivityRole/moles:dgInvestigator/moles:dgPrincipalInvestigator/moles:contactDetails),
612                    for $PIRole in $Act/moles:dgActivity/moles:dgActivityRole/moles:dgInvestigator/moles:dgPrincipalInvestigator/moles:dgRoleHolder
613                        order by $PIRole/moles:startDate empty least, $PIRole/moles:endDate empty least
614                        return
615                        element dgRoleHolder {
616                            if (exists($PIRole/moles:dgOrganisationID)) then
617                                f:return-stub-organisation('organisation', data($PIRole/moles:dgOrganisationID/moles:repositoryIdentifier), data($PIRole/moles:dgOrganisationID/moles:localIdentifier))
618                             else if (exists($PIRole/moles:dgPersonID)) then
619                                f:return-stub-person('person', string($PIRole/moles:dgPersonID/moles:repositoryIdentifier), string($PIRole/moles:dgPersonID/moles:localIdentifier))
620                             else (),
621                            ($PIRole/moles:startDate),
622                            if (exists($PIRole/moles:endDate)) then
623                                ($PIRole/moles:endDate)
624                            else (),
625                            if (exists($PIRole/moles:localName)) then
626                                ($PIRole/moles:localName)
627                            else ()
628                        } (: </dgRoleHolder> :)
629                }, (: </dgPrincipalInvestigator> :)
630                for $CI in $Act/moles:dgActivity/moles:dgActivityRole/moles:dgInvestigator/moles:dgCoInvestigator
631                return 
632                    element dgCoInvestigator {
633                        ($CI/moles:dgMetadataID),
634                        ($CI/moles:roleName),
635                        ($CI/moles:abbreviation),
636                        ($CI/moles:contactDetails),
637                        for $CIRole in $CI/moles:dgRoleHolder
638                            order by $CIRole/moles:startDate empty least, $CIRole/moles:endDate empty least
639                        return
640                        element dgRoleHolder {
641                            if (exists(CIRole/moles:dgOrganisationID)) then
642                                f:return-stub-organisation('organisation', data($CIRole/moles:dgOrganisationID/moles:repositoryIdentifier), data($CIRole/moles:dgOrganisationID/moles:localIdentifier))
643                             else if (exists($CIRole/moles:dgPersonID)) then
644                                f:return-stub-person('person', string($CIRole/moles:dgPersonID/moles:repositoryIdentifier), string($CIRole/moles:dgPersonID/moles:localIdentifier))
645                             else (),
646                            ($CIRole/moles:startDate),
647                            if (exists($CIRole/moles:endDate)) then
648                                ($CIRole/moles:endDate)
649                            else (),
650                            if (exists($CIRole/moles:localName)) then
651                                ($CIRole/moles:localName)
652                            else ()
653                        } (: </dgRoleHolder> :)
654                } (: </dgCoInvestigator> :)
655        }, (: </dgInvestigator> :)
656        if (exists($Act/moles:dgActivity/moles:dgActivityRole/moles:dgTechnicalContact)) then (
657            element dgTechnicalContact {
658                ($Act/moles:dgActivity/moles:dgActivityRole/moles:dgTechnicalContact/moles:dgMetadataID),
659                ($Act/moles:dgActivity/moles:dgActivityRole/moles:dgTechnicalContact/moles:roleName),
660                ($Act/moles:dgActivity/moles:dgActivityRole/moles:dgTechnicalContact/moles:abbreviation),
661                ($Act/moles:dgActivity/moles:dgActivityRole/moles:dgTechnicalContact/moles:contactDetails),
662                for $TechConRole in $Act/moles:dgActivity/moles:dgActivityRole/moles:dgTechnicalContact/moles:dgRoleHolder
663                    order by $TechConRole/moles:startDate empty least, $TechConRole/moles:endDate empty least
664                    return
665                    element dgRoleHolder {
666                        if (exists($TechConRole/moles:dgOrganisationID)) then
667                            f:return-stub-organisation('organisation', data($TechConRole/moles:dgOrganisationID/moles:repositoryIdentifier), data($TechConRole/moles:dgOrganisationID/moles:localIdentifier))
668                         else if (exists($TechConRole/moles:dgPersonID)) then
669                            f:return-stub-person('person', string($TechConRole/moles:dgPersonID/moles:repositoryIdentifier), string($TechConRole/moles:dgPersonID/moles:localIdentifier))
670                         else (),
671                        ($TechConRole/moles:startDate),
672                        if (exists($TechConRole/moles:endDate)) then
673                            ($TechConRole/moles:endDate)
674                        else (),
675                        if (exists($TechConRole/moles:localName)) then
676                                ($TechConRole/moles:localName)
677                           else ()
678                    } (: </dgRoleHolder> :)
679            } (: </dgTechnicalContact> :)
680        )
681        else (),
682        if (exists($Act/dgActivity/dgActivityRole/dgProjectManager)) then (
683            element dgProjectManager {
684                ($Act/dgActivity/dgActivityRole/dgProjectManager/dgMetadataID),
685                ($Act/dgActivity/dgActivityRole/dgProjectManager/roleName),
686                ($Act/dgActivity/dgActivityRole/dgProjectManager/abbreviation),
687                ($Act/dgActivity/dgActivityRole/dgProjectManager/contactDetails),
688                for $PMRole in $Act/dgActivity/dgActivityRole/dgProjectManager/dgRoleHolder
689                    order by $PMRole/startDate empty least, $PMRole/endDate empty least
690                    return
691                    element dgRoleHolder {
692                        if (exists($PMRole/moles:dgOrganisationID)) then
693                            f:return-stub-organisation('organisation', data($PMRole/moles:dgOrganisationID/moles:repositoryIdentifier), data($PMRole/moles:dgOrganisationID/moles:localIdentifier))
694                         else if (exists($PMRole/moles:dgPersonID)) then
695                            f:return-stub-person('person', string($PMRole/moles:dgPersonID/moles:repositoryIdentifier), string($PMRole/moles:dgPersonID/moles:localIdentifier))
696                         else (),
697                        ($PMRole/startDate),
698                        if (exists($PMRole/moles:endDate)) then
699                            ($PMRole/moles:endDate)
700                        else (),
701                        if (exists($PMRole/moles:localName)) then
702                            ($PMRole/moles:localName)
703                        else ()
704                    } (: </dgRoleHolder> :)
705            } (: </dgProjectManager> :)
706        )
707        else ()
708    }, (: </dgActivityRole> :)
709    for $RelDep in $Act/moles:dgActivity/moles:ActivityDeployment
710        return
711        element ActivityDeployment {
712            ($RelDep/moles:DateStart),
713            if (exists($RelDep/moles:DateEnd)) then
714                ($RelDep/moles:DateEnd)
715            else (),
716            ($RelDep/moles:dgMetadataID),
717            f:return-stub-dpt('dataproductiontool', data($RelDep/moles:DataProductionToolID/moles:repositoryIdentifier), data($RelDep/moles:DataProductionToolID/moles:localIdentifier)),
718            f:return-stub-obsstn('observationstation', data($RelDep/moles:ObservationStationID/moles:repositoryIdentifier), data($RelDep/moles:ObservationStationID/moles:localIdentifier)),
719            for $DE in collection($targetCollection)/moles:dgMetadata/moles:dgMetadataRecord
720                where $DE/moles:dgDataEntity/moles:RelatedDeployment/moles:dgMetadataID/moles:repositoryIdentifier=$RelDep/moles:dgMetadataID/moles:repositoryIdentifier
721                        and $DE/moles:dgDataEntity/moles:RelatedDeployment/moles:dgMetadataID/moles:localIdentifier=$RelDep/moles:dgMetadataID/moles:localIdentifier
722            return
723                f:return-stub-dataentity('dataentity', data($DE/moles:dgMetadataID/moles:repositoryIdentifier), data($DE/moles:dgMetadataID/moles:localIdentifier))
724        }, (: </ActivityDeployment> :)
725        if (exists($Act/dgActivityCoverage)) then
726            ($Act/dgActivityCoverage)
727            else (),
728        if (exists($Act/dgActivityDuration)) then
729            ($Act/dgActivityDuration)
730            else ()
731    } , (: </dgActivity> :)
732    for $strkwrd in $Act/moles:dgStructuredKeyword return $strkwrd,
733    if (exists($Act/moles:dgMetadataProvenance)) then $Act/moles:dgMetadataProvenance else (),
734    if (exists($Act/moles:dgMetadataSecurity)) then $Act/moles:dgMetadataSecurity else ()
735} (:</dgMRecord>:)
736) (: End namespace strip :)
737'''
738
739StubBDPTQuery = '''declare default element namespace 'http://ndg.nerc.ac.uk/moles/dataproductiontool';
740import module namespace voclib='http://ndg.nerc.ac.uk/xquery/lib/vocab' at 'xmldb:exist:///db/xqueryLib/Vocabs/vocab_xquery_lib.xquery';
741import module namespace utillib='http://ndg.nerc.ac.uk/xquery/lib/utilities' at 'xmldb:exist:///db/xqueryLib/Utilities/utility_xquery_lib.xquery';
742(:
743Will return when scope problem sussed... ;(
744import module namespace stubblib='http://ndg.nerc.ac.uk/xquery/lib/stubb' at 'xmldb:exist:///db/xqueryLib/StubB/stubb_xquery_lib.xquery';
745:)
746declare namespace f='http://ndg.nerc.ac.uk/moles/localfunctions';
747declare namespace moles='http://ndg.nerc.ac.uk/moles';
748
749declare variable $targetCollection as xs:string {'TargetCollection'};
750declare variable $repositoryIdentifier as xs:string {'RepositoryID'};
751declare variable $localIdentifier as xs:string {'LocalID'};
752
753declare function f:return-stub-activity($out-element-name as xs:string, $repid as xs:string, $locid as xs:string) as element()
754(: Returns a 'stubB' style activity record to expand an activity record :)
755(: Security not implemented here yet :)
756{
757let $found := count(collection($targetCollection)/moles:dgMetadata/moles:dgMetadataRecord[
758            moles:dgMetadataID/moles:repositoryIdentifier=$repid
759            and moles:dgMetadataID/moles:localIdentifier=$locid])
760return 
761    if ($found = 0) then
762        element {$out-element-name} {
763            element error {'not found'},
764            element repositoryIdentifier {$repid},
765            element localIdentifier {$locid}
766        }
767    else (
768        for $SRAct in collection($targetCollection)/moles:dgMetadata/moles:dgMetadataRecord[
769            moles:dgMetadataID/moles:repositoryIdentifier=$repid
770            and moles:dgMetadataID/moles:localIdentifier=$locid]
771        return
772            element  {$out-element-name} {
773                ($SRAct/moles:dgMetadataID),
774                ($SRAct/moles:dgMetadataDescription),
775                ($SRAct/moles:name),
776                ($SRAct/moles:abbreviation),
777                element dgActivity {
778                    if (exists($SRAct/*/moles:dgActivityDataCollection)) then
779                        ($SRAct/*/moles:dgActivityDataCollection)
780                    else if (exists($SRAct/*/moles:dgActivityDataProject)) then
781                        ($SRAct/*/moles:dgActivityDataProject)
782                    else if (exists($SRAct/*/dgActivityDataCampaign)) then
783                        ($SRAct/moles:dgActivity/moles:dgActivityDataCampaign)
784                    else
785                        ($SRAct/moles:dgActivity/moles:dgActivityDataInvestigation),
786                    ($SRAct/moles:dgActivity/moles:dgActivityCoverage),
787                    ($SRAct/moles:dgActivity/moles:dgActivityDuration)
788                } (: </dgActivity> :)
789            } (: </activity> :)
790    )
791} ;
792
793declare function f:return-stub-dataentity($out-element-name as xs:string, $repid as xs:string, $locid as xs:string) as element()
794(: Returns a 'stubB' style observation station record to expand an data entity record :)
795(: Security not implemented yet :)
796{
797let $found := count(collection($targetCollection)/moles:dgMetadata/moles:dgMetadataRecord[
798        moles:dgMetadataID/moles:repositoryIdentifier=$repid
799        and moles:dgMetadataID/moles:localIdentifier=$locid])
800return 
801    if ($found = 0) then
802        element {$out-element-name} {
803            element error {'not found'},
804            element repositoryIdentifier {$repid},
805            element localIdentifier {$locid}
806        }
807    else (
808        for $SRDE in collection($targetCollection)/moles:dgMetadata/moles:dgMetadataRecord[
809            moles:dgMetadataID/moles:repositoryIdentifier=$repid
810            and moles:dgMetadataID/moles:localIdentifier=$locid]
811        return 
812            element {$out-element-name} {
813                ($SRDE/moles:dgMetadataID),
814                ($SRDE/moles:dgMetadataDescription),
815                ($SRDE/moles:name),
816                ($SRDE/moles:abbreviation),
817                element dgDataEntity {
818                    ($SRDE/moles:dgDataEntity/moles:dgDataSetType),
819                    ($SRDE/moles:dgDataEntity/moles:dgDataGranule),
820                    ($SRDE/moles:dgDataEntity/moles:dgDataSummary)
821                } (: </dgDataEntity> :)       
822            }
823    )
824} ;
825
826declare function f:return-stub-dpt($out-element-name as xs:string, $repid as xs:string, $locid as xs:string) as element()
827(: Returns a 'stubB' style dpt record to expand a dpt record :)
828(: Security not implemented yet :)
829{
830let $found := count(collection($targetCollection)/moles:dgMetadata/moles:dgMetadataRecord[
831        moles:dgMetadataID/moles:repositoryIdentifier=$repid
832        and moles:dgMetadataID/moles:localIdentifier=$locid])
833return 
834    if ($found = 0) then
835        element {$out-element-name} {
836            element error {'not found'},
837            element repositoryIdentifier {$repid},
838            element localIdentifier {$locid}
839        }
840    else (
841        for $SRDPT in collection($targetCollection)/moles:dgMetadata/moles:dgMetadataRecord[
842                moles:dgMetadataID/moles:repositoryIdentifier=$repid
843                and moles:dgMetadataID/moles:localIdentifier=$locid]
844        return 
845            element {$out-element-name} {
846                $SRDPT/moles:dgMetadataID,
847                $SRDPT/moles:dgMetadataDescription,
848                $SRDPT/moles:name,
849                $SRDPT/moles:abbreviation,
850                $SRDPT/moles:logos,
851                element dgDataProductionTool {
852                    $SRDPT/moles:dgDataProductionTool/moles:contactDetails,
853                    if (exists($SRDPT/moles:dgDataProductionTool/moles:dgModel)) then
854                        $SRDPT/moles:dgDataProductionTool/moles:dgModel
855                    else (),
856                    if (exists($SRDPT/moles:dgDataProductionTool/moles:dgInstrument)) then
857                        $SRDPT/moles:dgDataProductionTool/moles:dgInstrument
858                    else ()
859                    } (: </dgDataProductionTool> :)
860                } (: </dataproductiontool> :)
861    )
862} ;
863
864declare function f:return-stub-obsstn($out-element-name as xs:string, $repid as xs:string, $locid as xs:string) as element()
865(: Returns a 'stubB' style observation station record to expand an observation station record :)
866(: Security not implemented here yet;  :)
867{
868let $found := count(collection($targetCollection)/moles:dgMetadata/moles:dgMetadataRecord[
869            moles:dgMetadataID/moles:repositoryIdentifier=$repid
870            and moles:dgMetadataID/moles:localIdentifier=$locid])
871return 
872    if ($found = 0) then
873        element {$out-element-name} {
874            element error {'not found'},
875            element repositoryIdentifier {$repid},
876            element localIdentifier {$locid}
877        }
878    else (
879        for $SRObsStn in collection($targetCollection)/moles:dgMetadata/moles:dgMetadataRecord[
880            moles:dgMetadataID/moles:repositoryIdentifier=$repid
881            and moles:dgMetadataID/moles:localIdentifier=$locid]
882        return
883            element {$out-element-name} {
884                ($SRObsStn/moles:dgMetadataID),
885                ($SRObsStn/moles:dgMetadataDescription),
886                ($SRObsStn/moles:name),
887                ($SRObsStn/moles:abbreviation),
888                element dgObservationStation {
889                    ($SRObsStn/moles:dgObservationStation/moles:contactDetails),
890                    if (exists($SRObsStn/moles:dgObservationStation/moles:dgStationaryPlatform)) then
891                        element dgStationaryPlatform {
892                            $SRObsStn/moles:dgObservationStation/moles:dgStationaryPlatform/moles:position,
893                            if (exists($SRObsStn/moles:dgObservationStation/moles:dgStationaryPlatform/dgLandStation)) then element dgLandStation {''}
894                            else if (exists($SRObsStn/moles:dgObservationStation/moles:dgStationaryPlatform/dgMooring)) then element dgMooring {''}
895                            else if (exists($SRObsStn/moles:dgObservationStation/moles:dgStationaryPlatform/dgStationGroup)) then element dgStationGroup {''}
896                            else ()
897                            }
898                    else if (exists($SRObsStn/moles:dgObservationStation/moles:dgMovingPlatform)) then
899                        $SRObsStn/moles:dgObservationStation/moles:dgMovingPlatform
900                    else ()
901                } (: </dgObservationStation> :)
902            }
903    )
904} ;
905
906declare function f:return-stub-person($out-element-name as xs:string, $repid as xs:string, $locid as xs:string) as element()
907(: Returns a 'stubB' style observation station record to expand a person record :)
908(: Security not implemented yet :)
909{
910let $found := count(collection($targetCollection)/moles:dgMetadata/moles:dgPerson[
911            moles:dgMetadataID/moles:repositoryIdentifier=$repid
912            and moles:dgMetadataID/moles:localIdentifier=$locid])
913return 
914    if ($found = 0) then
915        element {$out-element-name} {
916            element error {'not found'},
917            element repositoryIdentifier {$repid},
918            element localIdentifier {$locid}
919        }
920    else (
921    for $person in collection($targetCollection)/moles:dgMetadata/moles:dgPerson[
922        moles:dgMetadataID/moles:repositoryIdentifier=$repid
923        and moles:dgMetadataID/moles:localIdentifier=$locid]
924    return
925        element  {$out-element-name}
926        {
927            ($person/moles:name),
928            ($person/moles:contactDetails),
929            ($person/moles:dgMetadataID)
930        }
931    )
932} ;
933
934declare function f:return-stub-organisation($out-element-name as xs:string, $repid as xs:string, $locid as xs:string) as element()
935(: Returns a 'stubB' style observation station record to expand an organisation record :)
936(: Security not implemented yet :)
937{
938    let $found := count(collection($targetCollection)/moles:dgMetadata/moles:dgOrganisation[
939                moles:dgMetadataID/moles:repositoryIdentifier=$repid
940                and moles:dgMetadataID/moles:localIdentifier=$locid])
941    return 
942        if ($found = 0) then
943            element {$out-element-name} {
944                element error {'not found'},
945                element repositoryIdentifier {$repid},
946                element localIdentifier {$locid}
947            }
948        else (
949            for $org in collection($targetCollection)/moles:dgMetadata/moles:dgOrganisation[
950                moles:dgMetadataID/moles:repositoryIdentifier=$repid
951                and moles:dgMetadataID/moles:localIdentifier=$locid]
952            return
953                element  {$out-element-name} {
954                    $org/moles:dgMetadataID,
955                    $org/moles:name,
956                    $org/moles:abbreviation,
957                    $org/moles:contactDetails,
958                    $org/moles:logos
959                }
960        )
961} ;
962
963for $DPT in collection($targetCollection)/moles:dgMetadata/moles:dgMetadataRecord[
964    moles:dgDataProductionTool!=''
965    and moles:dgMetadataID[
966    moles:schemeIdentifier='NDG-B0'
967    and moles:repositoryIdentifier=$repositoryIdentifier
968    and moles:localIdentifier=$localIdentifier]]
969    return
970utillib:strip-namespace(element dgMetadataRecord {
971    $DPT/dgMetadataID,
972        element dgMetadataID {
973            element schemeIdentifier {'NDG-B1'},
974            $DPT/moles:dgMetadataID/moles:repositoryIdentifier,
975            $DPT/moles:dgMetadataID/moles:localIdentifier
976        } (: End </dgMetadataID> :),
977    $DPT/moles:dgMetadataDescription,
978    $DPT/moles:name,
979    $DPT/moles:abbreviation,
980    element dgDataProductionTool {
981        $DPT/moles:contactDetails,
982        if (exists($DPT/moles:dgModel)) then $DPT/moles:dgModel
983        else $DPT/moles:dgInstrument
984    ,
985    if (exists($DPT/moles:dgDPTRoles)) then
986        element dgDPTRoles {'Roles are available, and will be added soon'}
987     else(),
988    for $RelDep in $DPT/moles:dgDataProductionTool/moles:DPTDeployment
989    return
990        element DPTDeployment {
991            $RelDep/moles:DateStart,
992            $RelDep/moles:DateEnd,
993            $RelDep/moles:dgMetadataID,
994            f:return-stub-activity('activity', data($RelDep/moles:ActivityID/moles:repositoryIdentifier), data($RelDep/moles:ActivityID/moles:localIdentifier)),
995            f:return-stub-obsstn('observationstation', data($RelDep/moles:ObservationStationID/moles:repositoryIdentifier), data($RelDep/moles:ObservationStationID/moles:localIdentifier)),
996            for $DE in collection($targetCollection)/moles:dgMetadata/moles:dgMetadataRecord
997                where $DE/moles:dgDataEntity/moles:RelatedDeployment/moles:dgMetadataID/moles:repositoryIdentifier=$RelDep/moles:dgMetadataID/moles:repositoryIdentifier
998                        and $DE/moles:dgDataEntity/moles:RelatedDeployment/moles:dgMetadataID/moles:localIdentifier=$RelDep/moles:dgMetadataID/moles:localIdentifier
999            return
1000                f:return-stub-dataentity('dataentity', data($DE/moles:dgMetadataID/moles:repositoryIdentifier), data($DE/moles:dgMetadataID/moles:localIdentifier)),
1001            $RelDep/moles:Coverage
1002        } (: </DPTDeployment> :)
1003    } (: </dgDataProductionTool> :),
1004    for $strkwrd in $DPT/moles:dgStructuredKeyword return $strkwrd,
1005    if (exists($DPT/moles:dgMetadataProvenance)) then $DPT/moles:dgMetadataProvenance else (),
1006    if (exists($DPT/moles:dgMetadataSecurity)) then $DPT/moles:dgMetadataSecurity else ()
1007} (:End </dgMetadataRecord> :)
1008) (: End namespace strip :)
1009'''
1010
1011StubBObsStnQuery = '''declare default element namespace 'http://ndg.nerc.ac.uk/moles/observationstation';
1012import module namespace voclib='http://ndg.nerc.ac.uk/xquery/lib/vocab' at 'xmldb:exist:///db/xqueryLib/Vocabs/vocab_xquery_lib.xquery';
1013import module namespace utillib='http://ndg.nerc.ac.uk/xquery/lib/utilities' at 'xmldb:exist:///db/xqueryLib/Utilities/utility_xquery_lib.xquery';
1014(:
1015Will return when scope problem sussed... ;(
1016import module namespace stubblib='http://ndg.nerc.ac.uk/xquery/lib/stubb' at 'xmldb:exist:///db/xqueryLib/StubB/stubb_xquery_lib.xquery';
1017:)
1018declare namespace f='http://ndg.nerc.ac.uk/moles/localfunctions';
1019declare namespace moles='http://ndg.nerc.ac.uk/moles';
1020
1021declare variable $targetCollection as xs:string {'TargetCollection'};
1022declare variable $repositoryIdentifier as xs:string {'RepositoryID'};
1023declare variable $localIdentifier as xs:string {'LocalID'};
1024
1025declare function f:return-stub-activity($out-element-name as xs:string, $repid as xs:string, $locid as xs:string) as element()
1026(: Returns a 'stubB' style activity record to expand an activity record :)
1027(: Security not implemented here yet :)
1028{
1029let $found := count(collection($targetCollection)/moles:dgMetadata/moles:dgMetadataRecord[
1030            moles:dgMetadataID/moles:repositoryIdentifier=$repid
1031            and moles:dgMetadataID/moles:localIdentifier=$locid])
1032return 
1033    if ($found = 0) then
1034        element {$out-element-name} {
1035            element error {'not found'},
1036            element repositoryIdentifier {$repid},
1037            element localIdentifier {$locid}
1038        }
1039    else (
1040        for $SRAct in collection($targetCollection)/moles:dgMetadata/moles:dgMetadataRecord[
1041            moles:dgMetadataID/moles:repositoryIdentifier=$repid
1042            and moles:dgMetadataID/moles:localIdentifier=$locid]
1043        return
1044            element  {$out-element-name} {
1045                ($SRAct/moles:dgMetadataID),
1046                ($SRAct/moles:dgMetadataDescription),
1047                ($SRAct/moles:name),
1048                ($SRAct/moles:abbreviation),
1049                element dgActivity {
1050                    if (exists($SRAct/*/moles:dgActivityDataCollection)) then
1051                        ($SRAct/*/moles:dgActivityDataCollection)
1052                    else if (exists($SRAct/*/moles:dgActivityDataProject)) then
1053                        ($SRAct/*/moles:dgActivityDataProject)
1054                    else if (exists($SRAct/*/dgActivityDataCampaign)) then
1055                        ($SRAct/moles:dgActivity/moles:dgActivityDataCampaign)
1056                    else
1057                        ($SRAct/moles:dgActivity/moles:dgActivityDataInvestigation),
1058                    ($SRAct/moles:dgActivity/moles:dgActivityCoverage),
1059                    ($SRAct/moles:dgActivity/moles:dgActivityDuration)
1060                } (: </dgActivity> :)
1061            } (: </activity> :)
1062    )
1063} ;
1064
1065declare function f:return-stub-dataentity($out-element-name as xs:string, $repid as xs:string, $locid as xs:string) as element()
1066(: Returns a 'stubB' style observation station record to expand an data entity record :)
1067(: Security not implemented yet :)
1068{
1069let $found := count(collection($targetCollection)/moles:dgMetadata/moles:dgMetadataRecord[
1070        moles:dgMetadataID/moles:repositoryIdentifier=$repid
1071        and moles:dgMetadataID/moles:localIdentifier=$locid])
1072return 
1073    if ($found = 0) then
1074        element {$out-element-name} {
1075            element error {'not found'},
1076            element repositoryIdentifier {$repid},
1077            element localIdentifier {$locid}
1078        }
1079    else (
1080        for $SRDE in collection($targetCollection)/moles:dgMetadata/moles:dgMetadataRecord[
1081            moles:dgMetadataID/moles:repositoryIdentifier=$repid
1082            and moles:dgMetadataID/moles:localIdentifier=$locid]
1083        return 
1084            element {$out-element-name} {
1085                ($SRDE/moles:dgMetadataID),
1086                ($SRDE/moles:dgMetadataDescription),
1087                ($SRDE/moles:name),
1088                ($SRDE/moles:abbreviation),
1089                element dgDataEntity {
1090                    ($SRDE/moles:dgDataEntity/moles:dgDataSetType),
1091                    ($SRDE/moles:dgDataEntity/moles:dgDataGranule),
1092                    ($SRDE/moles:dgDataEntity/moles:dgDataSummary)
1093                } (: </dgDataEntity> :)       
1094            }
1095    )
1096} ;
1097
1098declare function f:return-stub-dpt($out-element-name as xs:string, $repid as xs:string, $locid as xs:string) as element()
1099(: Returns a 'stubB' style dpt record to expand a dpt record :)
1100(: Security not implemented yet :)
1101{
1102let $found := count(collection($targetCollection)/moles:dgMetadata/moles:dgMetadataRecord[
1103        moles:dgMetadataID/moles:repositoryIdentifier=$repid
1104        and moles:dgMetadataID/moles:localIdentifier=$locid])
1105return 
1106    if ($found = 0) then
1107        element {$out-element-name} {
1108            element error {'not found'},
1109            element repositoryIdentifier {$repid},
1110            element localIdentifier {$locid}
1111        }
1112    else (
1113        for $SRDPT in collection($targetCollection)/moles:dgMetadata/moles:dgMetadataRecord[
1114                moles:dgMetadataID/moles:repositoryIdentifier=$repid
1115                and moles:dgMetadataID/moles:localIdentifier=$locid]
1116        return 
1117            element {$out-element-name} {
1118                $SRDPT/moles:dgMetadataID,
1119                $SRDPT/moles:dgMetadataDescription,
1120                $SRDPT/moles:name,
1121                $SRDPT/moles:abbreviation,
1122                $SRDPT/moles:logos,
1123                element dgDataProductionTool {
1124                    $SRDPT/moles:dgDataProductionTool/moles:contactDetails,
1125                    if (exists($SRDPT/moles:dgDataProductionTool/moles:dgModel)) then
1126                        $SRDPT/moles:dgDataProductionTool/moles:dgModel
1127                    else (),
1128                    if (exists($SRDPT/moles:dgDataProductionTool/moles:dgInstrument)) then
1129                        $SRDPT/moles:dgDataProductionTool/moles:dgInstrument
1130                    else ()
1131                    } (: </dgDataProductionTool> :)
1132                } (: </dataproductiontool> :)
1133    )
1134} ;
1135
1136declare function f:return-stub-obsstn($out-element-name as xs:string, $repid as xs:string, $locid as xs:string) as element()
1137(: Returns a 'stubB' style observation station record to expand an observation station record :)
1138(: Security not implemented here yet;  :)
1139{
1140let $found := count(collection($targetCollection)/moles:dgMetadata/moles:dgMetadataRecord[
1141            moles:dgMetadataID/moles:repositoryIdentifier=$repid
1142            and moles:dgMetadataID/moles:localIdentifier=$locid])
1143return 
1144    if ($found = 0) then
1145        element {$out-element-name} {
1146            element error {'not found'},
1147            element repositoryIdentifier {$repid},
1148            element localIdentifier {$locid}
1149        }
1150    else (
1151        for $SRObsStn in collection($targetCollection)/moles:dgMetadata/moles:dgMetadataRecord[
1152            moles:dgMetadataID/moles:repositoryIdentifier=$repid
1153            and moles:dgMetadataID/moles:localIdentifier=$locid]
1154        return
1155            element {$out-element-name} {
1156                ($SRObsStn/moles:dgMetadataID),
1157                ($SRObsStn/moles:dgMetadataDescription),
1158                ($SRObsStn/moles:name),
1159                ($SRObsStn/moles:abbreviation),
1160                element dgObservationStation {
1161                    ($SRObsStn/moles:dgObservationStation/moles:contactDetails),
1162                    if (exists($SRObsStn/moles:dgObservationStation/moles:dgStationaryPlatform)) then
1163                        element dgStationaryPlatform {
1164                            $SRObsStn/moles:dgObservationStation/moles:dgStationaryPlatform/moles:position,
1165                            if (exists($SRObsStn/moles:dgObservationStation/moles:dgStationaryPlatform/dgLandStation)) then element dgLandStation {''}
1166                            else if (exists($SRObsStn/moles:dgObservationStation/moles:dgStationaryPlatform/dgMooring)) then element dgMooring {''}
1167                            else if (exists($SRObsStn/moles:dgObservationStation/moles:dgStationaryPlatform/dgStationGroup)) then element dgStationGroup {''}
1168                            else ()
1169                            }
1170                    else if (exists($SRObsStn/moles:dgObservationStation/moles:dgMovingPlatform)) then
1171                        $SRObsStn/moles:dgObservationStation/moles:dgMovingPlatform
1172                    else ()
1173                } (: </dgObservationStation> :)
1174            }
1175    )
1176} ;
1177
1178declare function f:return-stub-person($out-element-name as xs:string, $repid as xs:string, $locid as xs:string) as element()
1179(: Returns a 'stubB' style observation station record to expand a person record :)
1180(: Security not implemented yet :)
1181{
1182let $found := count(collection($targetCollection)/moles:dgMetadata/moles:dgPerson[
1183            moles:dgMetadataID/moles:repositoryIdentifier=$repid
1184            and moles:dgMetadataID/moles:localIdentifier=$locid])
1185return 
1186    if ($found = 0) then
1187        element {$out-element-name} {
1188            element error {'not found'},
1189            element repositoryIdentifier {$repid},
1190            element localIdentifier {$locid}
1191        }
1192    else (
1193    for $person in collection($targetCollection)/moles:dgMetadata/moles:dgPerson[
1194        moles:dgMetadataID/moles:repositoryIdentifier=$repid
1195        and moles:dgMetadataID/moles:localIdentifier=$locid]
1196    return
1197        element  {$out-element-name}
1198        {
1199            ($person/moles:name),
1200            ($person/moles:contactDetails),
1201            ($person/moles:dgMetadataID)
1202        }
1203    )
1204} ;
1205
1206declare function f:return-stub-organisation($out-element-name as xs:string, $repid as xs:string, $locid as xs:string) as element()
1207(: Returns a 'stubB' style observation station record to expand an organisation record :)
1208(: Security not implemented yet :)
1209{
1210    let $found := count(collection($targetCollection)/moles:dgMetadata/moles:dgOrganisation[
1211                moles:dgMetadataID/moles:repositoryIdentifier=$repid
1212                and moles:dgMetadataID/moles:localIdentifier=$locid])
1213    return 
1214        if ($found = 0) then
1215            element {$out-element-name} {
1216                element error {'not found'},
1217                element repositoryIdentifier {$repid},
1218                element localIdentifier {$locid}
1219            }
1220        else (
1221            for $org in collection($targetCollection)/moles:dgMetadata/moles:dgOrganisation[
1222                moles:dgMetadataID/moles:repositoryIdentifier=$repid
1223                and moles:dgMetadataID/moles:localIdentifier=$locid]
1224            return
1225                element  {$out-element-name} {
1226                    $org/moles:dgMetadataID,
1227                    $org/moles:name,
1228                    $org/moles:abbreviation,
1229                    $org/moles:contactDetails,
1230                    $org/moles:logos
1231                }
1232        )
1233} ;
1234
1235for $ObsStn in collection($targetCollection)/moles:dgMetadata/moles:dgMetadataRecord[
1236    moles:dgObservationStation!=''
1237    and moles:dgMetadataID[
1238    moles:schemeIdentifier='NDG-B0'
1239    and moles:repositoryIdentifier=$repositoryIdentifier
1240    and moles:localIdentifier=$localIdentifier]]
1241    return
1242    utillib:strip-namespace(
1243    element dgMetadataRecord {
1244        element dgMetadataID {
1245            element schemeIdentifier {'NDG-B1',
1246            $ObsStn/moles:dgMetadataID/moles:repositoryIdentifier,
1247            $ObsStn/moles:dgMetadataID/moles:localIdentifier
1248        } (: End </dgMetadataID> :),
1249        $ObsStn/moles:dgMetadataDescription,
1250        $ObsStn/moles:name,
1251        $ObsStn/moles:abbreviation,
1252        element dgObservationStation {
1253            $ObsStn/moles:dgObservationStation/moles:contactDetails,
1254            if (exists($ObsStn/moles:dgObservationStation/moles:dgStationaryPlatform)) then
1255                element dgStationaryPlatform {
1256                $ObsStn/moles:dgObservationStation/moles:dgStationaryPlatform/moles:position,
1257                if (exists($ObsStn/moles:dgObservationStation/moles:dgStationaryPlatform/moles:dgMooring)) then   
1258                    element dgMooring {
1259                        $ObsStn/moles:dgObservationStation/moles:dgStationaryPlatform/moles:dgMooring/moles:dateStart,
1260                        $ObsStn/moles:dgObservationStation/moles:dgStationaryPlatform/moles:dgMooring/moles:dateEnd,
1261                        for $DepositingCruise in $ObsStn/moles:dgObservationStation/moles:dgStationaryPlatform/moles:dgMooring/moles:DepositingCruise
1262                        return
1263                            f:return-stub-activity('depositingcruise', data($DepositingCruise/moles:dgMetadataID/moles:repositoryIdentifier), data($DepositingCruise/moles:dgMetadataID/moles:localIdentifier)),
1264                            if (exists($ObsStn/moles:dgObservationStation/moles:dgStationaryPlatform/moles:dgMooring/moles:dgStationGrouping)) then   
1265                                for $StationGrouping in $ObsStn/moles:dgObservationStation/moles:dgStationaryPlatform/moles:dgMooring/dgStationGrouping
1266                                return 
1267                                    f:return-stub-obsstn('stationgrouping', data($StationGrouping/moles:dgMetadataID/moles:repositoryIdentifier), data($StationGrouping/moles:dgMetadataID/moles:localIdentifier))
1268                            else ()
1269                    } (: End </dgMooring> :) 
1270                else if (exists($ObsStn/dgObservationStation/dgStationaryPlatform/dgStationGroup)) then   
1271                    element dgStationGroup {
1272                        element dgGroupedStations {
1273                            if (exists($ObsStn/dgObservationStation/dgStationaryPlatform/dgStationGroup/dgGroupedStations)) then
1274                                for $GroupedStation in $ObsStn/dgObservationStation/dgStationaryPlatform/dgStationGroup/dgGroupedStation/dgGroupedStations
1275                                return f:return-stub-obsstn('dgGroupedStation', data($GroupedStation/moles:dgMetadataID/moles:repositoryIdentifier), data($GroupedStation/moles:dgMetadataID/moles:localIdentifier))
1276                            else ()
1277                        } (: End </dgGroupedStations> :)
1278                    } (: End </dgStationGroup> :)
1279                else $ObsStn/moles:dgObservationStation/moles:dgLandStation
1280                } (: End </dgStationaryPlatform>  :) 
1281                else $ObsStn/moles:dgObservationStation/moles:dgMovingPlatform
1282            } ,
1283            for $RelDep in $ObsStn/moles:dgObservationStation/moles:ObsStationDeployment
1284            return
1285                element ObsStationDeployment {
1286                $RelDep/moles:DateStart,
1287                $RelDep/moles:DateEnd,
1288                $RelDep/moles:dgMetadataID,
1289                f:return-stub-activity('activity', data($RelDep/moles:ActivityID/moles:repositoryIdentifier), data($RelDep/moles:ActivityID/moles:localIdentifier)),
1290                f:return-stub-dpt('dataproductiontool', data($RelDep/moles:DataProductionToolID/moles:repositoryIdentifier), data($RelDep/moles:DataProductionToolID/moles:localIdentifier)),
1291                for $DE in collection($targetCollection)/moles:dgMetadata/moles:dgMetadataRecord
1292                    where $DE/moles:dgDataEntity/moles:RelatedDeployment/moles:dgMetadataID/moles:repositoryIdentifier=$RelDep/moles:dgMetadataID/moles:repositoryIdentifier
1293                            and $DE/moles:dgDataEntity/moles:RelatedDeployment/moles:dgMetadataID/moles:localIdentifier=$RelDep/moles:dgMetadataID/moles:localIdentifier
1294                return
1295                    f:return-stub-dataentity('dataentity', data($DE/moles:dgMetadataID/moles:repositoryIdentifier), data($DE/moles:dgMetadataID/moles:localIdentifier)),
1296                $RelDep/moles:Coverage
1297            } (: </RelatedDeployment> :)
1298        } (: </dgObservationStation> :),
1299    for $strkwrd in $ObsStn/moles:dgStructuredKeyword return $strkwrd,
1300    if (exists($ObsStn/moles:dgMetadataProvenance)) then $ObsStn/moles:dgMetadataProvenance else (),
1301    if (exists($ObsStn/moles:dgMetadataSecurity)) then $ObsStn/moles:dgMetadataSecurity else ()
1302    } (: End </dgMetadataRecord> :)
1303) (: End namespace strip :)
1304'''
1305
1306ISO19139Query = '''
1307import module namespace voclib='http://ndg.nerc.ac.uk/xquery/lib/vocab' at 'xmldb:exist:///db/xqueryLib/Vocabs/vocab_xquery_lib.xquery';
1308import module namespace utillib='http://ndg.nerc.ac.uk/xquery/lib/utilities' at 'xmldb:exist:///db/xqueryLib/Utilities/utility_xquery_lib.xquery';
1309
1310declare default element namespace 'http://www.isotc211.org/2005/gmd';
1311declare namespace moles='http://ndg.nerc.ac.uk/moles';
1312declare namespace gco='http://www.isotc211.org/2005/gco';
1313declare namespace gmd='http://www.isotc211.org/2005/gmd';
1314declare namespace gml='http://www.opengis.net/gml';
1315declare namespace xlink='http://www.w3.org/1999/xlink';
1316declare namespace xsi='http://www.w3.org/2001/XMLSchema-instance';
1317
1318declare variable $targetCollection as xs:string {'TargetCollection'};
1319declare variable $repositoryIdentifier as xs:string {'RepositoryID'};
1320declare variable $localIdentifier as xs:string {'LocalID'};
1321
1322for $DE in collection($targetCollection)/moles:dgMetadata/moles:dgMetadataRecord[
1323    exists(moles:dgDataEntity)
1324    and moles:dgMetadataID/moles:schemeIdentifier='NDG-B0'
1325    and moles:dgMetadataID/moles:localIdentifier=$localIdentifier]
1326return
1327element MD_Metadata {
1328    attribute xsi:schemaLocation {'http://www.isotc211.org/2005/gmd http://www.isotc211.org/2005/gmd/metadataEntity.xsd'},
1329    element fileIdentifier {element gco:CharacterString {concat($DE/moles:dgMetadataID/moles:repositoryIdentifier, $utillib:moles_id_separator, 'ISO19115', $utillib:moles_id_separator, $DE/moles:dgMetadataID/moles:localIdentifier)}},
1330    element language {
1331        element gmd:LanguageCode {
1332            attribute codeList {'http://www.isotc211.org/2005/resources/Codelist/gmxCodelists.xml#LanguageCode'},
1333            attribute codeListValue {'eng'},
1334            'English'}
1335    } (: </language> :),
1336    element hierarchyLevel {
1337        element MD_ScopeCode {
1338            attribute codeList {'http://www.isotc211.org/2005/resources/Codelist/gmxCodelists.xml#MD_ScopeCode'},
1339            attribute codeListValue {'http://www.isotc211.org/2005/resources/Codelist/gmxCodelists.xml#MD_ScopeCode_dataset'},
1340            'dataset'}
1341    } (: </hierarchyLevel> :),
1342    for $parent in $DE/moles:dgRelatedDataEntity[moles:RelationID/moles:dgValidTerm='is-part-of']
1343    return
1344        element parentIdentifier  {element gco:CharacterString {concat($DE/moles:dgMetadataID/moles:repositoryIdentifier, ':', $DE/moles:dgMetadataID/moles:localIdentifier, ':', $DE/moles:dgMetadataID/moles:localIdentifier)}},
1345    for $DataCuratorRole in $DE/moles:dgDataEntity/moles:dgDataRoles/moles:dgDataCurator/moles:dgRoleHolder[not(exists(moles:endDate)) or moles:endDate='']
1346    return
1347        for $DataCuratorRoleHolder in collection($targetCollection)/moles:dgMetadata/(moles:dgPerson | moles:dgOrganisation)[
1348            moles:dgMetadataID/moles:schemeIdentifier='NDG-B0'
1349            and moles:dgMetadataID/moles:repositoryIdentifier=$DataCuratorRole/(moles:dgPersonID | moles:dgOrganisationID)/moles:repositoryIdentifier
1350            and moles:dgMetadataID/moles:localIdentifier=$DataCuratorRole/(moles:dgPersonID | moles:dgOrganisationID)/moles:localIdentifier]
1351        return
1352            element contact {
1353                element CI_ResponsibleParty {
1354                    if (string(local-name($DataCuratorRoleHolder))='dgOrganisation') then
1355                        element organisationName {element gco:CharacterString {string($DataCuratorRoleHolder/moles:name)}}
1356                    else if (string(local-name($DataCuratorRoleHolder))='dgPerson') then
1357                        element individualName {element gco:CharacterString {string(concat(string($DataCuratorRoleHolder/moles:name/moles:initials), ' ', string($DataCuratorRoleHolder/moles:name/moles:familyName)))}}
1358                    else ('empty content') ,     
1359                    element positionName {
1360                        if (exists($DataCuratorRoleHolder/moles:localName)) then
1361                            element gco:CharacterString {$DataCuratorRoleHolder/moles:localName}
1362                        else if (exists($DataCuratorRole/moles:roleName)) then
1363                            element gco:CharacterString {$DataCuratorRole/moles:roleName}
1364                        else
1365                            element gco:CharacterString {'Curator'}
1366                    } (: </positionName>:),
1367                    element contactInfo {
1368                        element gmd:CI_Contact {
1369                            if (exists($DataCuratorRole/moles:contactDetails/moles:telephone
1370                                or $DataCuratorRole/moles:contactDetails/moles:fax
1371                                or $DataCuratorRoleHolder/moles:contactDetails/moles:telephone
1372                                or $DataCuratorRoleHolder/moles:contactDetails/moles:fax)) then
1373                                element phone {
1374                                    element gmd:CI_Telephone {
1375                                        if (exists($DataCuratorRole/moles:contactDetails/moles:telephone)) then
1376                                            element voice {
1377                                                element gco:CharacterString {data($DataCuratorRole/moles:contactDetails/moles:telephone)}
1378                                            } (:</voice>:)
1379                                        else if (exists($DataCuratorRoleHolder/moles:contactDetails/moles:telephone)) then
1380                                            element voice {
1381                                                element gco:CharacterString {data($DataCuratorRoleHolder/moles:contactDetails/moles:telephone)}
1382                                            } (:</voice>:)
1383                                        else (),
1384                                        if (exists($DataCuratorRole/moles:contactDetails/moles:fax)) then
1385                                            element facsimile {
1386                                                element gco:CharacterString {data($DataCuratorRoleHolder/moles:contactDetails/moles:fax)}
1387                                            } (: </facsimile> :)
1388                                        else if (exists($DataCuratorRoleHolder/moles:contactDetails/moles:telephone)) then
1389                                            element facsimile {
1390                                                element gco:CharacterString {data($DataCuratorRoleHolder/moles:contactDetails/moles:fax)}
1391                                            } (: </facsimile> :)
1392                                        else ()
1393                                    } (: </gmd:CI_Telephone> :)
1394                                } (: </phone> :)
1395                                                    else (),
1396                            element address {
1397                                element gmd:CI_Address {
1398                                    if (exists($DataCuratorRole/moles:contactDetails/moles:address/moles:addressline)) then                               
1399                                        for $addressline in $DataCuratorRole/moles:contactDetails/moles:address/moles:addressline
1400                                        return
1401                                            element deliveryPoint {element gco:CharacterString {data($addressline)}}
1402                                    else if (exists($DataCuratorRoleHolder/moles:contactDetails/moles:address/moles:addressline)) then
1403                                        for $addressline in $DataCuratorRoleHolder/moles:contactDetails/moles:address/moles:addressline
1404                                        return
1405                                            element deliveryPoint {element gco:CharacterString {data($addressline)}}
1406                                    else (),
1407                                    if (exists($DataCuratorRole/moles:contactDetails/moles:address/moles:city)) then
1408                                        element city {element gco:CharacterString {data($DataCuratorRole/moles:contactDetails/moles:address/moles:city)}}
1409                                    else if (exists($DataCuratorRoleHolder/moles:contactDetails/moles:address/moles:city)) then
1410                                        element city {element gco:CharacterString {data($DataCuratorRoleHolder/moles:contactDetails/moles:address/moles:city)}}
1411                                    else (),
1412                                    if (exists($DataCuratorRole/moles:contactDetails/moles:eMail)) then
1413                                        element electronicMailAddress {element gco:CharacterString {data($DataCuratorRole/moles:contactDetails/moles:eMail)}}
1414                                    else if (exists($DataCuratorRoleHolder/moles:contactDetails/moles:address/moles:postcode)) then
1415                                        element electronicMailAddress {element gco:CharacterString {data($DataCuratorRoleHolder/moles:contactDetails/moles:eMail)}}
1416                                    else ()
1417                                } (: </gmd:CI_Address> :)
1418                            } (: </address> :)
1419                        } (: </gmd:CI_Contact> :)
1420                    } (: </contactInfo> :),
1421                    element role {
1422                        element CI_RoleCode {
1423                            attribute codeList {'http://www.isotc211.org/2005/resources/Codelist/gmxCodelists.xml#CI_RoleCode'},
1424                            attribute codeListValue {'http://www.isotc211.org/2005/resources/Codelist/gmxCodelists.xml#CI_RoleCode_custodian'},
1425                            if (exists($DataCuratorRole/moles:localName)) then                               
1426                                data($DataCuratorRole/moles:localName)
1427                            else if (exists($DataCuratorRoleHolder/moles:localName)) then
1428                                data($DataCuratorRoleHolder/moles:localName)
1429                            else 'Curator' 
1430                        } (: </CI_RoleCode> :)
1431                    } (: </role> :)
1432                } (: </CI_ResponsibleParty> :)
1433            } (: </contact> :),
1434
1435            element dateStamp {element gco:Date {adjust-date-to-timezone(current-date())}},
1436            element metadataStandardName {element gco:CharacterString {'ISO 19115:2003'}},
1437            element metadataStandardVersion {element gco:CharacterString {'2003'}},
1438            element identificationInfo {
1439                element gmd:MD_DataIdentification {
1440                    element citation {
1441                        element gmd:CI_Citation {
1442                            element title {element gco:CharacterString {data($DE/moles:name)}},
1443                            if (exists($DE/moles:abbreviation) and not(empty($DE/moles:abbreviation))) then
1444                                element alternateTitle {element gco:CharacterString {data($DE/moles:abbreviation)}}
1445                            else (),
1446                            element date {
1447                                if (exists($DE/(moles:DataProvenance | moles:dgMetadataProvenance)/moles:RecordCreation/moles:CreatedDate)) then
1448                                    element gmd:CI_Date {
1449                                        element date {element gco:Date {adjust-date-to-timezone(data($DE/(moles:DataProvenance | moles:dgMetadataProvenance)/moles:RecordCreation/moles:CreatedDate))}},
1450                                        element dateType {
1451                                            element CI_DateTypeCode {
1452                                                attribute codeList {'http://www.isotc211.org/2005/resources/Codelist/gmxCodelists.xml#CI_DateTypeCode'},
1453                                                attribute codeListValue {'http://www.isotc211.org/2005/resources/Codelist/gmxCodelists.xml#CI_DateTypeCode_creation' },
1454                                                'Creation'}
1455                                        } (: </dataType> :)
1456                                    } (: </gmd:CI_Date> :)
1457                                else
1458                                    attribute gco:nilReason {'unknown'}
1459                            } (: </date> :)
1460                        } (: </gmd:CI_Citation> :)
1461                    } (: </citation> :),
1462                    element abstract {element gco:CharacterString {data($DE/moles:dgMetadataDescription/moles:abstract/moles:abstractText)}},
1463                    element language {
1464                        element gmd:LanguageCode {
1465                            attribute codeList {'http://www.isotc211.org/2005/resources/Codelist/gmxCodelists.xml#LanguageCode'},
1466                            attribute codeListValue {'eng'},
1467                            'English' }
1468                    } (: </language> :),
1469                    for $ISOTopicCategory in ($DE/moles:dgStructuredKeyword[dgValidTermID/moles:ParentListID='http://www.isotc211.org/2005/resources/Codelist/gmxCodelists.xml#MD_TopicCategoryCode'])
1470                    return
1471                        element topicCategory {
1472                            element gmd:MD_TopicCategoryCode {
1473                                attribute codeList {'http://www.isotc211.org/2005/resources/Codelist/gmxCodelists.xml#MD_TopicCategoryCode'},
1474                                attribute codeListValue {string($ISOTopicCategory/moles:dgValidTerm)},
1475                                string($ISOTopicCategory/moles:dgValidTerm)}
1476                        } (: </topicCategory> :),
1477                    if (exists($DE/moles:dgDataEntity/moles:dgDataSummary/moles:dgDataCoverage)) then
1478                        element extent {
1479                            element gmd:EX_Extent {
1480                                (: add bounding boxes:)
1481                                for $geoBBox in $DE/moles:dgDataEntity/moles:dgDataSummary/moles:dgDataCoverage//moles:BoundingBox
1482                                return
1483                                    element geographicElement {
1484                                        element EX_GeographicBoundingBox {
1485                                            element westBoundLongitude {element gco:Decimal {data($geoBBox/moles:LimitWest)}},
1486                                            element eastBoundLongitude {element gco:Decimal {data($geoBBox/moles:LimitEast)}},
1487                                            element southBoundLatitude {element gco:Decimal {data($geoBBox/moles:LimitSouth)}},
1488                                            element northBoundLatitude {element gco:Decimal {data($geoBBox/moles:LimitNorth)}}
1489                                        } (: </EX_GeographicBoundingBox> :)
1490                                    } (: </geographicElement> :),
1491                                (: add location terms:)
1492                                for $geoArea in $DE/moles:dgDataEntity/moles:dgDataSummary/moles:dgDataCoverage//moles:dgArea
1493                                return
1494                                    element geographicElement {
1495                                        element EX_GeographicDescription {
1496                                            element geographicIdentifier {
1497                                                element gmd:RS_Identifier {
1498                                                element gco:CharacterString {element code {data($geoArea/moles:dgValidTerm)}},
1499                                                element gco:CharacterString {element codeSpace {data($geoArea/moles:dgValidTermID/moles:ParentListID)}}
1500                                                    }
1501                                                }
1502                                        } (: </EX_GeographicDescription> :)
1503                                    } (: </geographicElement> :)
1504                            } (: </gmd:EX_Extent> :)
1505                        } (: </extent> :)
1506                    else()
1507                } (:</gmd:MD_DataIdentification> :)
1508            } (: </identificationInfo> :)
1509} (: </MD_Metadata> :)'''
1510           
1511MOLESQuery = '''declare default element namespace 'http://ndg.nerc.ac.uk/moles';
1512declare variable $targetCollection as xs:string {'TargetCollection'};
1513declare variable $repositoryIdentifier as xs:string {'RepositoryID'};
1514declare variable $localIdentifier as xs:string {'LocalID'};
1515for $Moles in collection($targetCollection)/dgMetadata/dgMetadataRecord[dgMetadataID/schemeIdentifier='NDG-B0'
1516    and dgMetadataID/repositoryIdentifier=$repositoryIdentifier
1517    and dgMetadataID/localIdentifier=$localIdentifier]
1518    return $Moles '''
1519           
1520DIFQuery = '''import module namespace voclib='http://ndg.nerc.ac.uk/xquery/lib/vocab' at 'xmldb:exist:///db/xqueryLib/Vocabs/vocab_xquery_lib.xquery';
1521import module namespace utillib='http://ndg.nerc.ac.uk/xquery/lib/utilities' at 'xmldb:exist:///db/xqueryLib/Utilities/utility_xquery_lib.xquery';
1522declare default element namespace 'http://gcmd.gsfc.nasa.gov/Aboutus/xml/dif/';
1523declare namespace xsi='http://www.w3.org/2001/XMLSchema-instance';
1524declare namespace moles='http://ndg.nerc.ac.uk/moles';
1525declare variable $targetCollection as xs:string {'TargetCollection'};
1526declare variable $repositoryIdentifier as xs:string {'RepositoryID'};
1527declare variable $localIdentifier as xs:string {'LocalID'};
1528for $DE in collection($targetCollection)/moles:dgMetadata/moles:dgMetadataRecord[
1529exists('moles:dgDataEntity')
1530and moles:dgMetadataID/moles:schemeIdentifier='NDG-B0'
1531and moles:dgMetadataID/moles:repositoryIdentifier=$repositoryIdentifier
1532and moles:dgMetadataID/moles:localIdentifier=$localIdentifier]
1533return
1534element DIF {
1535attribute xsi:schemaLocation {'http://gcmd.gsfc.nasa.gov/Aboutus/xml/dif/ http://gcmd.gsfc.nasa.gov/Aboutus/xml/dif/dif_v9.4.xsd'},
1536element Entry_ID {concat($DE/moles:dgMetadataID/moles:repositoryIdentifier, $utillib:moles_id_separator, 'DIF', $utillib:moles_id_separator, $DE/moles:dgMetadataID/moles:localIdentifier)},
1537element Entry_Title {string($DE/moles:name)},
1538element Data_Set_Citation {
1539for $DataCreatorRole in $DE/moles:dgDataEntity/moles:dgDataRoles/moles:dgDataCreator/moles:dgRoleHolder[not(exists(moles:endDate)) or moles:endDate=''][1]
1540return
1541for $DataCreatorRoleHolder in collection($targetCollection)/moles:dgMetadata/*[(moles:dgMetadataID/moles:schemeIdentifier='NDG-B0'
1542and moles:dgMetadataID/moles:repositoryIdentifier=$DataCreatorRole/*/moles:repositoryIdentifier
1543and moles:dgMetadataID/moles:localIdentifier=$DataCreatorRole/*/moles:localIdentifier)]
1544return
1545element Dataset_Creator {
1546if (exists($DataCreatorRoleHolder/moles:name/moles:initials)) then
1547string(concat(string($DataCreatorRoleHolder/moles:name/moles:initials), ' ', string($DataCreatorRoleHolder/moles:name/moles:familyName)))
1548else
1549string($DataCreatorRoleHolder/moles:abbreviation)
1550},
1551element Dataset_Title {string($DE/moles:name)}
1552},
1553for $StructuredKeyword in ($DE//(moles:dgStructuredKeyword | moles:dgStdParameterMeasured)[(voclib:spot-vocab($voclib:gcmd_science_valids, moles:dgValidTermID/moles:ParentListID) or voclib:spot-vocab($voclib:gcmd_science_valids_correct, moles:dgValidTermID/moles:ParentListID) or voclib:spot-vocab($voclib:gcmd_science_valids_categories, moles:dgValidTermID/moles:ParentListID))]
1554)
1555return if (exists($StructuredKeyword/*/moles:dgValidTerm)) then
1556element Parameters {
1557element Category {string($StructuredKeyword/*/moles:dgValidTerm)},
1558if (exists($StructuredKeyword//moles:dgValidSubterm)
1559and $StructuredKeyword//moles:dgValidSubterm != ''
1560and $StructuredKeyword//moles:dgValidSubterm != ' ') then
1561for $Subterm in $StructuredKeyword//moles:dgValidSubterm[exists(moles:dgValidTerm)]
1562where exists($Subterm/moles:dgValidSubterm)
1563order by $Subterm/moles:ListLevel
1564return
1565if ($Subterm/moles:ListLevel=1) then element Topic {string($Subterm/moles:dgValidSubterm/moles:dgValidTerm)}
1566else if ($Subterm/moles:ListLevel=2) then element Term {string($Subterm/moles:dgValidSubterm/moles:dgValidTerm)}
1567else if ($Subterm/moles:ListLevel=3) then element Variable {string($Subterm/moles:dgValidSubterm/moles:dgValidTerm)}
1568else if ($Subterm/moles:ListLevel=4) then element Detailed_Variable {string($Subterm/moles:dgValidSubterm/moles:dgValidTerm)}
1569else element GCMD_Science_Valid {string($Subterm/moles:dgValidSubterm/moles:dgValidTerm)}
1570else()
1571}
1572else(),
1573for $StructuredKeyword in ($DE//(moles:dgStructuredKeyword | moles:dgDataEntity/moles:dgDataSummary/moles:dgParameterSummary/moles:dgStdParameterMeasured)[voclib:spot-vocab($voclib:gcmd_science_valids, moles:dgValidTermID/moles:ParentListID) or voclib:spot-vocab($voclib:gcmd_science_valids_correct, moles:dgValidTermID/moles:ParentListID) or voclib:spot-vocab($voclib:gcmd_science_valids_deprecated, moles:dgValidTermID/moles:ParentListID) and (not(exists(moles:ListLevel)) or moles:ListLevel=0)])
1574return
1575element Parameters {data($StructuredKeyword/moles:dgValidTerm)},
1576for $ISOTopicCategory in ($DE/moles:dgStructuredKeyword[voclib:spot-vocab($voclib:iso_topic_list, moles:dgValidTermID/moles:ParentListID)])
1577return
1578element ISO_Topic_Category {string($ISOTopicCategory/moles:dgValidTerm)},
1579for $Keyword in distinct-values($DE//moles:dgStructuredKeyword[
1580not (voclib:spot-vocab($voclib:iso_topic_list, moles:dgValidTermID/moles:ParentListID)
1581or voclib:spot-vocab($voclib:gcmd_science_valids, moles:dgValidTermID/moles:ParentListID)
1582or voclib:spot-vocab($voclib:gcmd_science_valids_correct, moles:dgValidTermID/moles:ParentListID)
1583)]/moles:dgValidTerm)
1584return element Keyword {string($Keyword)},
1585for $DepDPT in ($DE/moles:dgDataEntity/moles:RelatedDeployment/moles:DataProductionToolID)
1586return
1587element Sensor_Name {
1588for $DepDPTAbbrev in ((collection($targetCollection)/moles:dgMetadata/moles:dgMetadataRecord[moles:dgMetadataID[moles:schemeIdentifier='NDG-B0' and moles:repositoryIdentifier=$DepDPT/moles:repositoryIdentifier and moles:localIdentifier=$DepDPT/moles:localIdentifier]]/moles:abbreviation))
1589return element Short_Name {data($DepDPTAbbrev)},
1590for $DepDPTName in ((collection($targetCollection)/moles:dgMetadata/moles:dgMetadataRecord[moles:dgMetadataID[moles:schemeIdentifier='NDG-B0' and moles:repositoryIdentifier=$DepDPT/moles:repositoryIdentifier and moles:localIdentifier=$DepDPT/moles:localIdentifier]]/moles:name))
1591return element Long_Name {data($DepDPTName)}
1592},
1593for $DepObsStn in ($DE/moles:dgDataEntity/moles:RelatedDeployment/moles:ObservationStationID)
1594return
1595element Source_Name {
1596for $DepObsStnAbbrev in ((collection($targetCollection)/moles:dgMetadata/moles:dgMetadataRecord[moles:dgMetadataID[moles:schemeIdentifier='NDG-B0' and moles:repositoryIdentifier=$DepObsStn/moles:repositoryIdentifier and moles:localIdentifier=$DepObsStn/moles:localIdentifier]]/moles:abbreviation))
1597return element Short_Name {data($DepObsStnAbbrev)},
1598for $DepObsStnName in ((collection($targetCollection)/moles:dgMetadata/moles:dgMetadataRecord[moles:dgMetadataID[moles:schemeIdentifier='NDG-B0' and moles:repositoryIdentifier=$DepObsStn/moles:repositoryIdentifier and moles:localIdentifier=$DepObsStn/moles:localIdentifier]]/moles:name))
1599return element Long_Name {data($DepObsStnName)}
1600},
1601for $TemporalRange in ($DE/moles:dgDataEntity/moles:dgDataSummary/moles:dgDataCoverage//moles:dgDateRange)
1602return
1603element Temporal_Coverage
1604{
1605element Start_Date {data($TemporalRange/moles:DateRangeStart)},
1606element End_Date {data($TemporalRange/moles:DateRangeEnd)}
1607},
1608for $TemporalTerm in ($DE/moles:dgDataEntity/moles:dgDataSummary/moles:dgDataCoverage//moles:dgArea[voclib:spot-vocab($voclib:gcmd_chronostratigraphic_valids, moles:dgValidTermID/moles:ParentListID)]/moles:dgValidTerm)
1609return element Paleo_Temporal_Coverage {element Chronostratigraphic_Unit {data($TemporalTerm)}},
1610for $Data_Set_Progress in ($DE/moles:dgDataEntity/moles:dgDataSummary/moles:dgDataStatus/moles:dgDatasetClosure)
1611return element Data_Set_Progress {data($Data_Set_Progress)},
1612for $BoundingBox in ($DE/moles:dgDataEntity/moles:dgDataSummary/moles:dgDataCoverage//moles:BoundingBox)
1613return
1614element Spatial_Coverage {
1615element Southernmost_Latitude {data($BoundingBox/moles:LimitSouth)},
1616element Northernmost_Latitude {data($BoundingBox/moles:LimitNorth)},
1617element Westernmost_Longitude {data($BoundingBox/moles:LimitWest)},
1618element Easternmost_Longitude {data($BoundingBox/moles:LimitEast)}
1619},
1620for $Location in ($DE/moles:dgDataEntity/moles:dgDataSummary/moles:dgDataCoverage//moles:dgArea[voclib:spot-vocab($voclib:gcmd_location_valids, moles:dgValidTermID/moles:ParentListID)])
1621return element Location {data($Location/moles:dgValidTerm)},
1622for $DepAct in ($DE/moles:dgDataEntity/moles:RelatedDeployment/moles:ActivityID)
1623return
1624element Project {
1625for $DepActAbbrev in (collection($targetCollection)/moles:dgMetadata/moles:dgMetadataRecord[moles:dgMetadataID[moles:schemeIdentifier='NDG-B0' and moles:repositoryIdentifier=$DepAct/moles:repositoryIdentifier and moles:localIdentifier=$DepAct/moles:localIdentifier]]/moles:abbreviation)
1626return
1627element Short_Name {data($DepActAbbrev)},
1628for $DepActName in (collection($targetCollection)/moles:dgMetadata/moles:dgMetadataRecord[moles:dgMetadataID[moles:schemeIdentifier='NDG-B0' and moles:repositoryIdentifier=$DepAct/moles:repositoryIdentifier and moles:localIdentifier=$DepAct/moles:localIdentifier]]/moles:name)
1629return
1630element Long_Name {data($DepActName)}
1631},
1632if (exists($DE/moles:dgDataEntity/moles:dgDataGranule/moles:accessControlPolicy)) then
1633for $DG in $DE/moles:dgDataEntity/moles:dgDataGranule
1634return
1635element Access_Constraints {
1636if (exists($DG/moles:dgGranuleSummary/moles:dgGranuleName)) then
1637concat('For data granule ', $DG/moles:dgGranuleSummary/moles:dgGranuleName, ': ')
1638else (),
1639if (exists($DG/moles:accessControlPolicy/moles:accessControlPolicyURL)) then
1640concat('See access control policy at ', escape-uri($DG/moles:accessControlPolicy/moles:accessControlPolicyURL, true()))
1641else if (exists($DG/moles:accessControlPolicy/moles:accessControlPolicyText)) then
1642data($DG/moles:accessControlPolicy/moles:accessControlPolicyText)
1643else
1644for $securityCondition in $DG/moles:accessControlPolicy/moles:dgSecurityCondition
1645return
1646if (exists($securityCondition/moles:conditionExplanationText)) then
1647concat('Effect: ', data($securityCondition/moles:effect), ' - ', data($securityCondition/moles:conditionExplanationText))
1648else concat('Effect: ', data($securityCondition/moles:effect), 'needs ', data($securityCondition/moles:attauthRole), ' from ', data($securityCondition/moles:dgAttributeAuthority))
1649}
1650else (),
1651if (exists($DE/moles:dgDataEntity/moles:dgDataRoles/moles:dgDataCreator/moles:dgRoleHolder)) then
1652for $DataCreatorRole in $DE/moles:dgDataEntity/moles:dgDataRoles/moles:dgDataCreator/moles:dgRoleHolder
1653order by $DataCreatorRole/moles:startDate empty least
1654return
1655for $DataCreatorRoleHolder in collection($targetCollection)/moles:dgMetadata/(moles:dgOrganisation | moles:dgPerson)
1656[(moles:dgMetadataID/moles:schemeIdentifier='NDG-B0'
1657and moles:dgMetadataID/moles:repositoryIdentifier=$DataCreatorRole/*/moles:repositoryIdentifier
1658and moles:dgMetadataID/moles:localIdentifier=$DataCreatorRole/*/moles:localIdentifier)]
1659return
1660element Originating_Center {
1661if (string(local-name($DataCreatorRoleHolder))='dgOrganisation') then
1662(string($DataCreatorRoleHolder/moles:name))
1663else if (string(local-name($DataCreatorRoleHolder))='dgPerson') then
1664(string(concat(string($DataCreatorRoleHolder/moles:name/moles:initials), ' ', string($DataCreatorRoleHolder/moles:name/moles:familyName))))
1665else ('empty content')
1666}
1667else (),
1668for $DataCuratorRole in $DE/moles:dgDataEntity/moles:dgDataRoles/moles:dgDataCurator/moles:dgRoleHolder
1669[not(exists(moles:endDate)) or moles:endDate='']
1670return
1671for $DataCuratorRoleHolder in collection($targetCollection)/moles:dgMetadata/(moles:dgOrganisation | moles:dgPerson)
1672[(moles:dgMetadataID/moles:schemeIdentifier='NDG-B0'
1673and moles:dgMetadataID/moles:repositoryIdentifier=$DataCuratorRole/*/moles:repositoryIdentifier
1674and moles:dgMetadataID/moles:localIdentifier=$DataCuratorRole/*/moles:localIdentifier)]
1675return
1676element Data_Center {
1677element Data_Center_Name {
1678element Short_Name {
1679if (exists($DataCuratorRoleHolder/moles:name/moles:initials)) then
1680string(concat(string($DataCuratorRoleHolder/moles:name/moles:initials), ' ', string($DataCuratorRoleHolder/moles:name/moles:familyName)))
1681else
1682string($DataCuratorRoleHolder/moles:abbreviation)
1683},
1684element Long_Name {
1685if (exists($DataCuratorRoleHolder/moles:name/moles:initials)) then
1686string(concat(string($DataCuratorRoleHolder/moles:name/moles:initials), ' ', string($DataCuratorRoleHolder/moles:name/moles:familyName)))
1687else string($DataCuratorRoleHolder/moles:name)
1688}
1689},
1690if (exists($DataCuratorRoleHolder/moles:contactDetails/moles:URI)) then
1691element Data_Center_URL {data($DataCuratorRoleHolder/moles:contactDetails/moles:URI)}
1692else (),
1693for $DGID in $DE/moles:dgDataEntity/moles:dgDataGranule/moles:dataModelID
1694return
1695element Data_Set_ID {concat($DGID/moles:repositoryIdentifier, $utillib:moles_id_separator, $DGID/moles:schemeIdentifier, $utillib:moles_id_separator, $DGID/moles:localIdentifier)},
1696element Personnel {
1697element Role {'Data Center Contact'},
1698if (exists($DataCuratorRoleHolder/name/knownAs)) then
1699element First_Name {string($DataCuratorRoleHolder/moles:name/moles:knownAs)}
1700else if (exists($DataCuratorRoleHolder/moles:name/moles:initials)) then
1701element First_Name {string($DataCuratorRoleHolder/moles:name/moles:initials)}
1702else (),
1703element Last_Name {
1704if (exists($DataCuratorRoleHolder/moles:name/moles:familyName)) then
1705string($DataCuratorRoleHolder/moles:name/moles:familyName)
1706else string($DataCuratorRoleHolder/moles:name)
1707},
1708if (exists($DataCuratorRole/moles:contactDetails/moles:eMail)) then
1709element Email {string($DataCuratorRole/moles:contactDetails/moles:eMail)}
1710else
1711if (exists($DataCuratorRoleHolder/contactDetails/eMail)) then
1712(element Email {string($DataCuratorRoleHolder/moles:contactDetails/moles:eMail)})
1713else (),
1714if (exists($DataCuratorRole/moles:contactDetails/moles:telephone)) then
1715element Phone {string($DataCuratorRole/moles:contactDetails/moles:telephone)}
1716else if (exists($DataCuratorRoleHolder/moles:contactDetails/moles:telephone)) then
1717element Phone {string($DataCuratorRoleHolder/moles:contactDetails/moles:telephone)}
1718else (),
1719if (exists($DataCuratorRole/moles:contactDetails/moles:fax)) then
1720element Fax {string($DataCuratorRole/moles:contactDetails/moles:fax)}
1721else if (exists($DataCuratorRoleHolder/moles:contactDetails/moles:fax)) then
1722element Fax {string($DataCuratorRoleHolder/moles:contactDetails/moles:fax)}
1723else (),
1724if (exists($DataCuratorRole/moles:contactDetails/moles:address)) then
1725element Contact_Address {
1726for $addressline in $DataCuratorRole/moles:contactDetails/moles:address/moles:addressline
1727return element Address {data($addressline)},
1728if (exists($DataCuratorRole/moles:contactDetails/moles:address/moles:city)) then
1729element City {string($DataCuratorRole/moles:contactDetails/moles:address/moles:city)}
1730else (),
1731if (exists($DataCuratorRole/moles:contactDetails/moles:address/moles:postcode)) then
1732element Postal_Code {string($DataCuratorRole/moles:contactDetails/moles:address/moles:postcode)}
1733else (),
1734if (exists($DataCuratorRole/moles:contactDetails/moles:address/moles:country)) then
1735element Country {string($DataCuratorRole/moles:contactDetails/moles:address/moles:country)}
1736else ()
1737}
1738else if (exists($DataCuratorRoleHolder/moles:contactDetails/moles:address)) then
1739element Contact_Address {
1740for $addressline in $DataCuratorRoleHolder/moles:contactDetails/moles:address/moles:addressline
1741return element Address {data($addressline)},
1742if (exists($DataCuratorRoleHolder/moles:contactDetails/moles:address/moles:city)) then
1743element City {string($DataCuratorRoleHolder/moles:contactDetails/moles:address/moles:city)}
1744else (),
1745if (exists($DataCuratorRoleHolder/moles:contactDetails/moles:address/moles:postcode)) then
1746element Postal_Code {string($DataCuratorRoleHolder/moles:contactDetails/moles:address/moles:postcode)}
1747else (),
1748if (exists($DataCuratorRoleHolder/moles:contactDetails/moles:address/moles:country)) then
1749element Country {string($DataCuratorRoleHolder/moles:contactDetails/moles:address/moles:country)}
1750else ()
1751}
1752else ()
1753}
1754},
1755element Summary {string($DE/moles:dgMetadataDescription/moles:abstract/moles:abstractText)},
1756for $DG in $DE/moles:dgDataEntity/moles:dgDataGranule
1757return
1758if ($DG/moles:dataModelID/moles:schemeIdentifier='NDG-A0') then
1759(element Related_URL {
1760element URL_Content_Type {'NDG_A_SERVICE'},
1761if (exists($DG/instance/URI)) then
1762element URL {escape-uri($DG/instance/URI, true())}
1763else if ($DG/moles:dataModelID/moles:repositoryIdentifier='badc.nerc.ac.uk') then
1764element URL {escape-uri('http://dmgdev1.esc.rl.ac.uk/cgi-bin/ndgDataAccess?datasetSource=dmgdev1.esc.rl.ac.uk&amp;datasetID=', true())}
1765else ($DG/moles:dataModelID/moles:repositoryIdentifier),
1766element Description {'The NDG service delivering data via NDG A metadata.'}
1767},
1768element Related_URL {
1769element URL_Content_Type {'GET DATA > CSML'},
1770if (exists($DG/instance)) then
1771element URL {escape-uri($DG/instance/URI, true())}
1772else if ($DG/moles:dataModelID/moles:repositoryIdentifier='badc.nerc.ac.uk') then
1773element URL {escape-uri('http://dmgdev1.esc.rl.ac.uk/cgi-bin/ndgDataAccess?datasetSource=dmgdev1.esc.rl.ac.uk&amp;datasetID=', true())}
1774else (),
1775element Description {'The NDG service delivering data via NDG A metadata. Additional tag to be more in line with latest version of GCMD valids'}
1776})
1777else if ($DG/moles:dataModelID/moles:schemeIdentifier='CDML-0') then
1778(element Related_URL {
1779element URL_Content_Type {'NDG_A_SERVICE'},
1780if (exists($DG/instance)) then
1781element URL {escape-uri($DG/instance/URI, true())}
1782else if ($DG/moles:dataModelID/moles:repositoryIdentifier='badc.nerc.ac.uk') then
1783element URL {escape-uri('http://cdat.badc.nerc.ac.uk/cgi-bin/dxui.py?datasetURI_1=/', true())}
1784else (),
1785element Description {'The NDG service delivering data via Data extractor. Additional tag to be more in line with latest version of GCMD valids'}
1786},
1787element Related_URL {
1788element URL_Content_Type {'GET DATA > DX'},
1789if (exists($DG/instance)) then
1790element URL {escape-uri($DG/instance/URI, true())}
1791else if ($DG/moles:dataModelID/moles:repositoryIdentifier='badc.nerc.ac.uk') then
1792element URL {escape-uri('http://cdat.badc.nerc.ac.uk/cgi-bin/dxui.py?datasetURI_1=/', true())}
1793else (),
1794element Description {'The NDG service delivering data via Data extractor. Additional tag to be more in line with latest version of GCMD valids'}
1795})
1796else if ($DG/moles:dataModelID/moles:schemeIdentifier='URI') then
1797element Related_URL {
1798element URL {data($DG/moles:instance/moles:URI)},
1799if (exists($DG/moles:instance/moles:instanceComment)) then
1800element Description {data($DG/moles:instance/moles:instanceComment)}
1801else
1802element 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.'}
1803}
1804else (),
1805for $RelURL_Desc in ($DE/moles:dgMetadataDescription/moles:descriptionSection/moles:descriptionOnlineReference)
1806return
1807if (exists($RelURL_Desc/moles:dgSimpleLink)) then
1808element Related_URL {
1809element URL {data($RelURL_Desc/moles:dgSimpleLink/URI)},
1810element Description {concat(data($RelURL_Desc/dgSimpleLink/moles:name), ' - ', data($RelURL_Desc/dgSimpleLink/moles:notes))}
1811}
1812else (),
1813element Metadata_Name {'[CEOS IDN DIF]'},
1814element Metadata_Version {'9.4'},
1815if (exists($DE/moles:dgMetadataProvenance/moles:RecordCreation)) then
1816element DIF_Creation_Date {data($DE/moles:dgMetadataProvenance/moles:RecordCreation/moles:CreatedDate)}
1817else (),
1818for $MDUpdt in $DE/moles:dgMetadataProvenance/moles:RecordUpdate
1819return
1820element DIF_Revision_History {concat(data($MDUpdt/moles:UpdateDate), ' - ', data($MDUpdt/moles:UpdateSummary), ' - ', data($MDUpdt/moles:UpdatedBy))},
1821if (exists($DE/moles:dgMetadataProvenance/moles:RecordReview)) then
1822element Future_DIF_Review_Date {data($DE/moles:dgMetadataProvenance/moles:RecordReview/moles:ReviewDate)}
1823else ()
1824}'''
1825       
1826DublinCoreDEQuery='''import module namespace voclib='http://ndg.nerc.ac.uk/xquery/lib/vocab' at 'xmldb:exist:///db/xqueryLib/Vocabs/vocab_xquery_lib.xquery';
1827import module namespace utillib='http://ndg.nerc.ac.uk/xquery/lib/utilities' at 'xmldb:exist:///db/xqueryLib/Utilities/utility_xquery_lib.xquery';
1828declare default element namespace 'http://ndg.nerc.ac.uk/moles';
1829declare namespace xsi='http://www.w3.org/2001/XMLSchema-instance';
1830declare namespace dc='http://purl.org/dc/elements/1.1/';
1831declare namespace oai_dc='http://www.openarchives.org/OAI/2.0/oai_dc';
1832declare variable $targetCollection as xs:string {'TargetCollection'};
1833declare variable $repositoryIdentifier as xs:string {'RepositoryID'};
1834declare variable $localIdentifier as xs:string {'LocalID'};
1835
1836for $DE in collection($targetCollection)/dgMetadata/dgMetadataRecord[
1837exists('dgDataEntity')
1838and dgMetadataID/schemeIdentifier='NDG-B0'
1839and dgMetadataID/repositoryIdentifier=$repositoryIdentifier
1840and dgMetadataID/localIdentifier=$localIdentifier]
1841return
1842element oai_dc:dc {
1843    attribute xsi:schemaLocation {'http://www.openarchives.org/OAI/2.0/oai_dc/ http://www.openarchives.org/OAI/2.0/oai_dc.xsd'},
1844    element dc:title {string($DE/name)},
1845    element dc:type {'Dataset'},
1846    element dc:identifier {concat($DE/dgMetadataID/repositoryIdentifier, $utillib:moles_id_separator, 'DC', $utillib:moles_id_separator, $DE/dgMetadataID/localIdentifier)},
1847    element dc:description {string($DE/dgMetadataDescription/abstract/abstractText)},
1848    element dc:date
1849        {
1850        if (exists($DE/(dgMetadataProvenance | DataProvenance)/RecordUpdate/UpdateDate)) then
1851            for $updatedate in ($DE/(dgMetadataProvenance | DataProvenance)/RecordUpdate/UpdateDate)
1852                order by xs:dateTime($updatedate)
1853            return data($updatedate[1])
1854        else
1855            string($DE/(dgMetadataProvenance | DataProvenance)/RecordCreation/CreatedDate)
1856        },
1857    for $StructuredKeyword in distinct-values($DE/dgStructuredKeyword[
1858    voclib:spot-vocab($voclib:cf_standard_names, dgValidTermID/ParentListID)
1859    or voclib:spot-vocab($voclib:gcmd_science_valids, dgValidTermID/ParentListID)
1860    or voclib:spot-vocab($voclib:gcmd_project_valids, dgValidTermID/ParentListID)
1861    or voclib:spot-vocab($voclib:iso_topic_list, dgValidTermID/ParentListID)
1862    or voclib:spot-vocab($voclib:bodc_parameter_usage_vocab, dgValidTermID/ParentListID)
1863    or voclib:spot-vocab($voclib:bodc_parameter_discovery_vocab, dgValidTermID/ParentListID)
1864    ]/dgValidTerm)
1865    order by $StructuredKeyword
1866    return
1867        element dc:subject {string($StructuredKeyword)},
1868   
1869    for $StructuredKeyword1 in distinct-values($DE/dgDataEntity/dgDataSummary/dgParameterSummary/dgStdParameterMeasured[
1870    voclib:spot-vocab($voclib:gcmd_science_valids, dgValidTermID/ParentListID)
1871    or voclib:spot-vocab($voclib:gcmd_project_valids, dgValidTermID/ParentListID)
1872    or voclib:spot-vocab($voclib:bodc_parameter_usage_vocab, dgValidTermID/ParentListID)
1873    or voclib:spot-vocab($voclib:bodc_parameter_discovery_vocab, dgValidTermID/ParentListID)
1874    ]/dgValidTerm)
1875        order by $StructuredKeyword1
1876    return
1877        element dc:subject  {string($StructuredKeyword1)},
1878    for $DataCreatorRole in $DE/dgDataEntity/dgDataRoles/dgDataCreator/dgRoleHolder
1879        order by $DataCreatorRole/startDate empty least
1880    return
1881        for $DataCreatorRoleHolder in collection($targetCollection)/dgMetadata/(dgOrganisation | dgPerson)
1882            [(dgMetadataID/schemeIdentifier='NDG-B0'
1883            and dgMetadataID/repositoryIdentifier=$DataCreatorRole/(dgOrganisationID | dgPersonID)/repositoryIdentifier
1884            and dgMetadataID/localIdentifier=$DataCreatorRole/(dgOrganisationID | dgPersonID)/localIdentifier)]
1885        return
1886            element dc:creator {
1887                if (string(local-name($DataCreatorRoleHolder))='dgOrganisation') then
1888                    (string($DataCreatorRoleHolder/name))
1889                else if (string(local-name($DataCreatorRoleHolder))='dgPerson') then
1890                    (string(concat(string($DataCreatorRoleHolder/name/initials), ' ', string($DataCreatorRoleHolder/name/familyName))))
1891                else ('empty content')
1892            },
1893    for $DataCuratorRole in $DE/dgDataEntity/dgDataRoles/dgDataCurator/dgRoleHolder[not(exists(endDate)) or endDate='']
1894        order by $DataCuratorRole/startDate empty least
1895    return
1896        for $DataCuratorRoleHolder in collection($targetCollection)/dgMetadata/(dgOrganisation | dgPerson)
1897            [(dgMetadataID/schemeIdentifier='NDG-B0'
1898            and dgMetadataID/repositoryIdentifier=$DataCuratorRole/(dgOrganisationID | dgPersonID)/repositoryIdentifier
1899            and dgMetadataID/localIdentifier=$DataCuratorRole/(dgOrganisationID | dgPersonID)/localIdentifier)]
1900        return
1901            element dc:publisher {
1902                if (string(local-name($DataCuratorRoleHolder))='dgOrganisation') then
1903                    (string($DataCuratorRoleHolder/name))
1904                else if (string(local-name($DataCuratorRoleHolder))='dgPerson') then
1905                    (string(concat(string($DataCuratorRoleHolder/name/initials), ' ', string($DataCuratorRoleHolder/name/familyName))))
1906                else ('empty content')
1907            }
1908}  (: </oai_dc:dc> :)'''
1909
1910MDIPQuery='''import module namespace voclib='http://ndg.nerc.ac.uk/xquery/lib/vocab' at 'xmldb:exist:///db/xqueryLib/Vocabs/vocab_xquery_lib.xquery';
1911import module namespace utillib='http://ndg.nerc.ac.uk/xquery/lib/utilities' at 'xmldb:exist:///db/xqueryLib/Utilities/utility_xquery_lib.xquery';
1912declare default element namespace 'http://www.oceannet.org/mdip/xml';
1913declare namespace moles='http://ndg.nerc.ac.uk/moles';
1914declare namespace gco='http://www.isotc211.org/2005/gco';
1915declare namespace xsi='http://www.w3.org/2001/XMLSchema-instance';
1916declare variable $targetCollection as xs:string {'TargetCollection'};
1917declare variable $repositoryIdentifier as xs:string {'RepositoryID'};
1918declare variable $localIdentifier as xs:string {'LocalID'};
1919declare variable $ISO_639-2_ns as xs:string{$voclib:ISO_639-2_ns};
1920declare variable $ISO_3166_ns as xs:string{$voclib:ISO_3166_ns};
1921
1922for $DE in collection($targetCollection)/moles:dgMetadata/moles:dgMetadataRecord[
1923    exists('moles:dgDataEntity')
1924    and moles:dgMetadataID/moles:schemeIdentifier='NDG-B0'
1925    and moles:dgMetadataID/moles:repositoryIdentifier=$repositoryIdentifier
1926    and moles:dgMetadataID/moles:localIdentifier=$localIdentifier]
1927return
1928element Metadata {
1929    element Title {data($DE/moles:name)},
1930    if (exists($DE/moles:dgDataEntity/moles:dgDataSummary/moles:dgDatasetLanguage)) then
1931        for $lang in $DE/moles:dgDataEntity/moles:dgDataSummary/moles:dgDatasetLanguage
1932        return
1933            element Language{
1934                element LanguageName {data($lang/moles:dgValidTerm)},
1935                element LanguageVocab {data($lang/moles:dgValidTermID/moles:ParentListID)},
1936                element LanguageCode {data($lang/moles:dgValidTermID/moles:TermID)}
1937            }
1938               
1939    else (),
1940
1941    element Abstract {string($DE/moles:dgMetadataDescription/moles:abstract/moles:abstractText)},
1942    for $isoTopic in $DE/moles:dgStructuredKeyword[voclib:spot-vocab($voclib:iso_topic_list, moles:dgValidTermID/moles:ParentListID)]
1943    return element TopicCategory {
1944    element TopicCategoryName {data($isoTopic/moles:dgValidTerm)},
1945    element TopicCategoryVocab {data($isoTopic/moles:dgValidTermID/moles:ParentListID)},
1946    element TopicCategoryCode {data($isoTopic/moles:dgValidTermID/moles:TermID)}
1947    },
1948    for $subject in ($DE/moles:dgDataEntity/moles:dgDataSummary/moles:dgParameterSummary/moles:dgStdParameterMeasured[
1949    voclib:spot-vocab($voclib:gcmd_science_valids, moles:dgValidTermID/moles:ParentListID)
1950    or voclib:spot-vocab($voclib:gcmd_project_valids, moles:dgValidTermID/moles:ParentListID)
1951    or voclib:spot-vocab($voclib:gcmd_science_valids_categories, moles:dgValidTermID/moles:ParentListID)
1952    or voclib:spot-vocab($voclib:bodc_parameter_usage_vocab, moles:dgValidTermID/moles:ParentListID)
1953    or voclib:spot-vocab($voclib:bodc_parameter_discovery_vocab, moles:dgValidTermID/moles:ParentListID)
1954    ])
1955    return (element Subject {
1956    element SubjectName {data($subject/moles:dgValidTerm)},
1957    element SubjectVocab {data($subject/moles:dgValidTermID/moles:ParentListID)},
1958    element SubjectCode {data($subject/moles:dgValidTermID/moles:TermID)}
1959    },
1960    for $subterm in $subject//moles:dgValidSubterm
1961    return (element Subject {
1962    element SubjectName {data($subterm/moles:dgValidTerm)},
1963    element SubjectVocab {data($subterm/moles:dgValidTermID/moles:ParentListID)},
1964    element SubjectCode {data($subterm/moles:dgValidTermID/moles:TermID)}
1965    }
1966    )
1967    ),
1968    if (exists($DE//moles:dgTemporalCoverage/(moles:DateSingle | moles:DateRange))) then
1969    element Date {
1970        element DatasetStartDate {
1971        let $dateStart:=min($DE//moles:dgTemporalCoverage/(moles:DateSingle | moles:DateRange/moles:DateRangeStart) cast as xs:date)
1972        return
1973        if (empty($dateStart)) then ()
1974        else data($dateStart)
1975        },
1976        element DatasetEndDate {
1977        let $dateEnd:=max($DE//moles:dgTemporalCoverage/(moles:DateSingle | moles:DateRange/moles:DateRangeEnd) cast as xs:date)
1978        return
1979        if (empty($dateEnd)) then ()
1980        else data($dateEnd)
1981        }
1982    }
1983    else (),
1984    let $west := min($DE/moles:dgDataEntity/moles:dgDataSummary//moles:BoundingBox/moles:LimitWest cast as xs:decimal)
1985    return element WestCoOrdinate {data($west)},
1986    let $east := max($DE/moles:dgDataEntity/moles:dgDataSummary//moles:BoundingBox/moles:LimitEast cast as xs:decimal)
1987    return element EastCoOrdinate {data($east)},
1988    let $north := max($DE/moles:dgDataEntity/moles:dgDataSummary//moles:BoundingBox/moles:LimitNorth cast as xs:decimal)
1989    return element NorthCoOrdinate {data($north)},
1990    let $south := min($DE/moles:dgDataEntity/moles:dgDataSummary//moles:BoundingBox/moles:LimitSouth cast as xs:decimal)
1991    return element SouthCoOrdinate {data($south)},
1992    element DatasetIdentifier {concat($DE/moles:dgMetadataID/moles:repositoryIdentifier, $utillib:moles_id_separator, 'MDIP', $utillib:moles_id_separator, $DE/moles:dgMetadataID/moles:localIdentifier)},
1993    for $target in $DE/moles:dgStructuredKeyword[voclib:spot-vocab($voclib:ndg_data_provider_vocab, moles:dgValidTermID/moles:ParentListID) ]
1994    return element Target {
1995    element TargetName {data($target/moles:dgValidTerm)},
1996    element TargetVocab {data($target/moles:dgValidTermID/moles:ParentListID)},
1997    element TargetCode {data($target/moles:dgValidTermID/moles:TermID)}
1998    }
1999    }
2000'''
Note: See TracBrowser for help on using the repository browser.