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

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

Updated DIF queries

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[
1030moles:dgMetadataID/moles:repositoryIdentifier=$repid
1031and moles:dgMetadataID/moles:localIdentifier=$locid])
1032return 
1033if ($found = 0) then
1034element {$out-element-name} {
1035element error {'not found'},
1036element repositoryIdentifier {$repid},
1037element localIdentifier {$locid}
1038}
1039else (
1040for $SRAct in collection($targetCollection)/moles:dgMetadata/moles:dgMetadataRecord[
1041moles:dgMetadataID/moles:repositoryIdentifier=$repid
1042and moles:dgMetadataID/moles:localIdentifier=$locid]
1043return
1044element  {$out-element-name} {
1045($SRAct/moles:dgMetadataID),
1046($SRAct/moles:dgMetadataDescription),
1047($SRAct/moles:name),
1048($SRAct/moles:abbreviation),
1049element dgActivity {
1050if (exists($SRAct/*/moles:dgActivityDataCollection)) then
1051($SRAct/*/moles:dgActivityDataCollection)
1052else if (exists($SRAct/*/moles:dgActivityDataProject)) then
1053($SRAct/*/moles:dgActivityDataProject)
1054else if (exists($SRAct/*/dgActivityDataCampaign)) then
1055($SRAct/moles:dgActivity/moles:dgActivityDataCampaign)
1056else
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[
1070moles:dgMetadataID/moles:repositoryIdentifier=$repid
1071and moles:dgMetadataID/moles:localIdentifier=$locid])
1072return 
1073if ($found = 0) then
1074element {$out-element-name} {
1075element error {'not found'},
1076element repositoryIdentifier {$repid},
1077element localIdentifier {$locid}
1078}
1079else (
1080for $SRDE in collection($targetCollection)/moles:dgMetadata/moles:dgMetadataRecord[
1081moles:dgMetadataID/moles:repositoryIdentifier=$repid
1082and moles:dgMetadataID/moles:localIdentifier=$locid]
1083return 
1084element {$out-element-name} {
1085($SRDE/moles:dgMetadataID),
1086($SRDE/moles:dgMetadataDescription),
1087($SRDE/moles:name),
1088($SRDE/moles:abbreviation),
1089element 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[
1103moles:dgMetadataID/moles:repositoryIdentifier=$repid
1104and moles:dgMetadataID/moles:localIdentifier=$locid])
1105return 
1106if ($found = 0) then
1107element {$out-element-name} {
1108element error {'not found'},
1109element repositoryIdentifier {$repid},
1110element localIdentifier {$locid}
1111}
1112else (
1113for $SRDPT in collection($targetCollection)/moles:dgMetadata/moles:dgMetadataRecord[
1114moles:dgMetadataID/moles:repositoryIdentifier=$repid
1115and moles:dgMetadataID/moles:localIdentifier=$locid]
1116return 
1117element {$out-element-name} {
1118$SRDPT/moles:dgMetadataID,
1119$SRDPT/moles:dgMetadataDescription,
1120$SRDPT/moles:name,
1121$SRDPT/moles:abbreviation,
1122$SRDPT/moles:logos,
1123element dgDataProductionTool {
1124$SRDPT/moles:dgDataProductionTool/moles:contactDetails,
1125if (exists($SRDPT/moles:dgDataProductionTool/moles:dgModel)) then
1126$SRDPT/moles:dgDataProductionTool/moles:dgModel
1127else (),
1128if (exists($SRDPT/moles:dgDataProductionTool/moles:dgInstrument)) then
1129$SRDPT/moles:dgDataProductionTool/moles:dgInstrument
1130else ()
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[
1141moles:dgMetadataID/moles:repositoryIdentifier=$repid
1142and moles:dgMetadataID/moles:localIdentifier=$locid])
1143return 
1144if ($found = 0) then
1145element {$out-element-name} {
1146element error {'not found'},
1147element repositoryIdentifier {$repid},
1148element localIdentifier {$locid}
1149}
1150else (
1151for $SRObsStn in collection($targetCollection)/moles:dgMetadata/moles:dgMetadataRecord[
1152moles:dgMetadataID/moles:repositoryIdentifier=$repid
1153and moles:dgMetadataID/moles:localIdentifier=$locid]
1154return
1155element {$out-element-name} {
1156($SRObsStn/moles:dgMetadataID),
1157($SRObsStn/moles:dgMetadataDescription),
1158($SRObsStn/moles:name),
1159($SRObsStn/moles:abbreviation),
1160element dgObservationStation {
1161($SRObsStn/moles:dgObservationStation/moles:contactDetails),
1162if (exists($SRObsStn/moles:dgObservationStation/moles:dgStationaryPlatform)) then
1163element dgStationaryPlatform {
1164$SRObsStn/moles:dgObservationStation/moles:dgStationaryPlatform/moles:position,
1165if (exists($SRObsStn/moles:dgObservationStation/moles:dgStationaryPlatform/dgLandStation)) then element dgLandStation {''}
1166else if (exists($SRObsStn/moles:dgObservationStation/moles:dgStationaryPlatform/dgMooring)) then element dgMooring {''}
1167else if (exists($SRObsStn/moles:dgObservationStation/moles:dgStationaryPlatform/dgStationGroup)) then element dgStationGroup {''}
1168else ()
1169}
1170else if (exists($SRObsStn/moles:dgObservationStation/moles:dgMovingPlatform)) then
1171$SRObsStn/moles:dgObservationStation/moles:dgMovingPlatform
1172else ()
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[
1183moles:dgMetadataID/moles:repositoryIdentifier=$repid
1184and moles:dgMetadataID/moles:localIdentifier=$locid])
1185return 
1186if ($found = 0) then
1187element {$out-element-name} {
1188element error {'not found'},
1189element repositoryIdentifier {$repid},
1190element localIdentifier {$locid}
1191}
1192else (
1193for $person in collection($targetCollection)/moles:dgMetadata/moles:dgPerson[
1194moles:dgMetadataID/moles:repositoryIdentifier=$repid
1195and moles:dgMetadataID/moles:localIdentifier=$locid]
1196return
1197element  {$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{
1210let $found := count(collection($targetCollection)/moles:dgMetadata/moles:dgOrganisation[
1211moles:dgMetadataID/moles:repositoryIdentifier=$repid
1212and moles:dgMetadataID/moles:localIdentifier=$locid])
1213return 
1214if ($found = 0) then
1215element {$out-element-name} {
1216element error {'not found'},
1217element repositoryIdentifier {$repid},
1218element localIdentifier {$locid}
1219}
1220else (
1221for $org in collection($targetCollection)/moles:dgMetadata/moles:dgOrganisation[
1222moles:dgMetadataID/moles:repositoryIdentifier=$repid
1223and moles:dgMetadataID/moles:localIdentifier=$locid]
1224return
1225element  {$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[
1236moles:dgObservationStation!=''
1237and moles:dgMetadataID[
1238moles:schemeIdentifier='NDG-B0'
1239and moles:repositoryIdentifier=$repositoryIdentifier
1240and moles:localIdentifier=$localIdentifier]]
1241return
1242utillib:strip-namespace(
1243element dgMetadataRecord {
1244element dgMetadataID {
1245element 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,
1252element dgObservationStation {
1253$ObsStn/moles:dgObservationStation/moles:contactDetails,
1254if (exists($ObsStn/moles:dgObservationStation/moles:dgStationaryPlatform)) then
1255element dgStationaryPlatform {
1256$ObsStn/moles:dgObservationStation/moles:dgStationaryPlatform/moles:position,
1257if (exists($ObsStn/moles:dgObservationStation/moles:dgStationaryPlatform/moles:dgMooring)) then   
1258element dgMooring {
1259$ObsStn/moles:dgObservationStation/moles:dgStationaryPlatform/moles:dgMooring/moles:dateStart,
1260$ObsStn/moles:dgObservationStation/moles:dgStationaryPlatform/moles:dgMooring/moles:dateEnd,
1261for $DepositingCruise in $ObsStn/moles:dgObservationStation/moles:dgStationaryPlatform/moles:dgMooring/moles:DepositingCruise
1262return
1263f:return-stub-activity('depositingcruise', data($DepositingCruise/moles:dgMetadataID/moles:repositoryIdentifier), data($DepositingCruise/moles:dgMetadataID/moles:localIdentifier)),
1264if (exists($ObsStn/moles:dgObservationStation/moles:dgStationaryPlatform/moles:dgMooring/moles:dgStationGrouping)) then   
1265for $StationGrouping in $ObsStn/moles:dgObservationStation/moles:dgStationaryPlatform/moles:dgMooring/dgStationGrouping
1266return 
1267f:return-stub-obsstn('stationgrouping', data($StationGrouping/moles:dgMetadataID/moles:repositoryIdentifier), data($StationGrouping/moles:dgMetadataID/moles:localIdentifier))
1268else ()
1269} (: End </dgMooring> :) 
1270else if (exists($ObsStn/dgObservationStation/dgStationaryPlatform/dgStationGroup)) then   
1271element dgStationGroup {
1272element dgGroupedStations {
1273if (exists($ObsStn/dgObservationStation/dgStationaryPlatform/dgStationGroup/dgGroupedStations)) then
1274for $GroupedStation in $ObsStn/dgObservationStation/dgStationaryPlatform/dgStationGroup/dgGroupedStation/dgGroupedStations
1275return f:return-stub-obsstn('dgGroupedStation', data($GroupedStation/moles:dgMetadataID/moles:repositoryIdentifier), data($GroupedStation/moles:dgMetadataID/moles:localIdentifier))
1276else ()
1277} (: End </dgGroupedStations> :)
1278} (: End </dgStationGroup> :)
1279else $ObsStn/moles:dgObservationStation/moles:dgLandStation
1280} (: End </dgStationaryPlatform>  :) 
1281else $ObsStn/moles:dgObservationStation/moles:dgMovingPlatform ,
1282for $RelDep in $ObsStn/moles:dgObservationStation/moles:ObsStationDeployment
1283return
1284element ObsStationDeployment {
1285$RelDep/moles:DateStart,
1286$RelDep/moles:DateEnd,
1287$RelDep/moles:dgMetadataID,
1288f:return-stub-activity('activity', data($RelDep/moles:ActivityID/moles:repositoryIdentifier), data($RelDep/moles:ActivityID/moles:localIdentifier)),
1289f:return-stub-dpt('dataproductiontool', data($RelDep/moles:DataProductionToolID/moles:repositoryIdentifier), data($RelDep/moles:DataProductionToolID/moles:localIdentifier)),
1290for $DE in collection($targetCollection)/moles:dgMetadata/moles:dgMetadataRecord
1291where $DE/moles:dgDataEntity/moles:RelatedDeployment/moles:dgMetadataID/moles:repositoryIdentifier=$RelDep/moles:dgMetadataID/moles:repositoryIdentifier
1292and $DE/moles:dgDataEntity/moles:RelatedDeployment/moles:dgMetadataID/moles:localIdentifier=$RelDep/moles:dgMetadataID/moles:localIdentifier
1293return
1294f:return-stub-dataentity('dataentity', data($DE/moles:dgMetadataID/moles:repositoryIdentifier), data($DE/moles:dgMetadataID/moles:localIdentifier)),
1295$RelDep/moles:Coverage
1296} (: </RelatedDeployment> :)
1297} (: </dgObservationStation> :),
1298for $strkwrd in $ObsStn/moles:dgStructuredKeyword return $strkwrd,
1299if (exists($ObsStn/moles:dgMetadataProvenance)) then $ObsStn/moles:dgMetadataProvenance else (),
1300if (exists($ObsStn/moles:dgMetadataSecurity)) then $ObsStn/moles:dgMetadataSecurity else ()
1301} (: End </dgMetadataRecord> :)
1302) (: End namespace strip :)
1303'''
1304
1305ISO19139Query = '''
1306import module namespace voclib='http://ndg.nerc.ac.uk/xquery/lib/vocab' at 'xmldb:exist:///db/xqueryLib/Vocabs/vocab_xquery_lib.xquery';
1307import module namespace utillib='http://ndg.nerc.ac.uk/xquery/lib/utilities' at 'xmldb:exist:///db/xqueryLib/Utilities/utility_xquery_lib.xquery';
1308
1309declare default element namespace 'http://www.isotc211.org/2005/gmd';
1310declare namespace moles='http://ndg.nerc.ac.uk/moles';
1311declare namespace gco='http://www.isotc211.org/2005/gco';
1312declare namespace gmd='http://www.isotc211.org/2005/gmd';
1313declare namespace gml='http://www.opengis.net/gml';
1314declare namespace xlink='http://www.w3.org/1999/xlink';
1315declare namespace xsi='http://www.w3.org/2001/XMLSchema-instance';
1316
1317declare variable $targetCollection as xs:string {'TargetCollection'};
1318declare variable $repositoryIdentifier as xs:string {'RepositoryID'};
1319declare variable $localIdentifier as xs:string {'LocalID'};
1320
1321for $DE in collection($targetCollection)/moles:dgMetadata/moles:dgMetadataRecord[
1322    exists(moles:dgDataEntity)
1323    and moles:dgMetadataID/moles:schemeIdentifier='NDG-B0'
1324    and moles:dgMetadataID/moles:localIdentifier=$localIdentifier]
1325return
1326element MD_Metadata {
1327    attribute xsi:schemaLocation {'http://www.isotc211.org/2005/gmd http://www.isotc211.org/2005/gmd/metadataEntity.xsd'},
1328    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)}},
1329    element language {
1330        element gmd:LanguageCode {
1331            attribute codeList {'http://www.isotc211.org/2005/resources/Codelist/gmxCodelists.xml#LanguageCode'},
1332            attribute codeListValue {'eng'},
1333            'English'}
1334    } (: </language> :),
1335    element hierarchyLevel {
1336        element MD_ScopeCode {
1337            attribute codeList {'http://www.isotc211.org/2005/resources/Codelist/gmxCodelists.xml#MD_ScopeCode'},
1338            attribute codeListValue {'http://www.isotc211.org/2005/resources/Codelist/gmxCodelists.xml#MD_ScopeCode_dataset'},
1339            'dataset'}
1340    } (: </hierarchyLevel> :),
1341    for $parent in $DE/moles:dgRelatedDataEntity[moles:RelationID/moles:dgValidTerm='is-part-of']
1342    return
1343        element parentIdentifier  {element gco:CharacterString {concat($DE/moles:dgMetadataID/moles:repositoryIdentifier, ':', $DE/moles:dgMetadataID/moles:localIdentifier, ':', $DE/moles:dgMetadataID/moles:localIdentifier)}},
1344    for $DataCuratorRole in $DE/moles:dgDataEntity/moles:dgDataRoles/moles:dgDataCurator/moles:dgRoleHolder[not(exists(moles:endDate)) or moles:endDate='']
1345    return
1346        for $DataCuratorRoleHolder in collection($targetCollection)/moles:dgMetadata/(moles:dgPerson | moles:dgOrganisation)[
1347            moles:dgMetadataID/moles:schemeIdentifier='NDG-B0'
1348            and moles:dgMetadataID/moles:repositoryIdentifier=$DataCuratorRole/(moles:dgPersonID | moles:dgOrganisationID)/moles:repositoryIdentifier
1349            and moles:dgMetadataID/moles:localIdentifier=$DataCuratorRole/(moles:dgPersonID | moles:dgOrganisationID)/moles:localIdentifier]
1350        return
1351            element contact {
1352                element CI_ResponsibleParty {
1353                    if (string(local-name($DataCuratorRoleHolder))='dgOrganisation') then
1354                        element organisationName {element gco:CharacterString {string($DataCuratorRoleHolder/moles:name)}}
1355                    else if (string(local-name($DataCuratorRoleHolder))='dgPerson') then
1356                        element individualName {element gco:CharacterString {string(concat(string($DataCuratorRoleHolder/moles:name/moles:initials), ' ', string($DataCuratorRoleHolder/moles:name/moles:familyName)))}}
1357                    else ('empty content') ,     
1358                    element positionName {
1359                        if (exists($DataCuratorRoleHolder/moles:localName)) then
1360                            element gco:CharacterString {$DataCuratorRoleHolder/moles:localName}
1361                        else if (exists($DataCuratorRole/moles:roleName)) then
1362                            element gco:CharacterString {$DataCuratorRole/moles:roleName}
1363                        else
1364                            element gco:CharacterString {'Curator'}
1365                    } (: </positionName>:),
1366                    element contactInfo {
1367                        element gmd:CI_Contact {
1368                            if (exists($DataCuratorRole/moles:contactDetails/moles:telephone
1369                                or $DataCuratorRole/moles:contactDetails/moles:fax
1370                                or $DataCuratorRoleHolder/moles:contactDetails/moles:telephone
1371                                or $DataCuratorRoleHolder/moles:contactDetails/moles:fax)) then
1372                                element phone {
1373                                    element gmd:CI_Telephone {
1374                                        if (exists($DataCuratorRole/moles:contactDetails/moles:telephone)) then
1375                                            element voice {
1376                                                element gco:CharacterString {data($DataCuratorRole/moles:contactDetails/moles:telephone)}
1377                                            } (:</voice>:)
1378                                        else if (exists($DataCuratorRoleHolder/moles:contactDetails/moles:telephone)) then
1379                                            element voice {
1380                                                element gco:CharacterString {data($DataCuratorRoleHolder/moles:contactDetails/moles:telephone)}
1381                                            } (:</voice>:)
1382                                        else (),
1383                                        if (exists($DataCuratorRole/moles:contactDetails/moles:fax)) then
1384                                            element facsimile {
1385                                                element gco:CharacterString {data($DataCuratorRoleHolder/moles:contactDetails/moles:fax)}
1386                                            } (: </facsimile> :)
1387                                        else if (exists($DataCuratorRoleHolder/moles:contactDetails/moles:telephone)) then
1388                                            element facsimile {
1389                                                element gco:CharacterString {data($DataCuratorRoleHolder/moles:contactDetails/moles:fax)}
1390                                            } (: </facsimile> :)
1391                                        else ()
1392                                    } (: </gmd:CI_Telephone> :)
1393                                } (: </phone> :)
1394                                                    else (),
1395                            element address {
1396                                element gmd:CI_Address {
1397                                    if (exists($DataCuratorRole/moles:contactDetails/moles:address/moles:addressline)) then                               
1398                                        for $addressline in $DataCuratorRole/moles:contactDetails/moles:address/moles:addressline
1399                                        return
1400                                            element deliveryPoint {element gco:CharacterString {data($addressline)}}
1401                                    else if (exists($DataCuratorRoleHolder/moles:contactDetails/moles:address/moles:addressline)) then
1402                                        for $addressline in $DataCuratorRoleHolder/moles:contactDetails/moles:address/moles:addressline
1403                                        return
1404                                            element deliveryPoint {element gco:CharacterString {data($addressline)}}
1405                                    else (),
1406                                    if (exists($DataCuratorRole/moles:contactDetails/moles:address/moles:city)) then
1407                                        element city {element gco:CharacterString {data($DataCuratorRole/moles:contactDetails/moles:address/moles:city)}}
1408                                    else if (exists($DataCuratorRoleHolder/moles:contactDetails/moles:address/moles:city)) then
1409                                        element city {element gco:CharacterString {data($DataCuratorRoleHolder/moles:contactDetails/moles:address/moles:city)}}
1410                                    else (),
1411                                    if (exists($DataCuratorRole/moles:contactDetails/moles:eMail)) then
1412                                        element electronicMailAddress {element gco:CharacterString {data($DataCuratorRole/moles:contactDetails/moles:eMail)}}
1413                                    else if (exists($DataCuratorRoleHolder/moles:contactDetails/moles:address/moles:postcode)) then
1414                                        element electronicMailAddress {element gco:CharacterString {data($DataCuratorRoleHolder/moles:contactDetails/moles:eMail)}}
1415                                    else ()
1416                                } (: </gmd:CI_Address> :)
1417                            } (: </address> :)
1418                        } (: </gmd:CI_Contact> :)
1419                    } (: </contactInfo> :),
1420                    element role {
1421                        element CI_RoleCode {
1422                            attribute codeList {'http://www.isotc211.org/2005/resources/Codelist/gmxCodelists.xml#CI_RoleCode'},
1423                            attribute codeListValue {'http://www.isotc211.org/2005/resources/Codelist/gmxCodelists.xml#CI_RoleCode_custodian'},
1424                            if (exists($DataCuratorRole/moles:localName)) then                               
1425                                data($DataCuratorRole/moles:localName)
1426                            else if (exists($DataCuratorRoleHolder/moles:localName)) then
1427                                data($DataCuratorRoleHolder/moles:localName)
1428                            else 'Curator' 
1429                        } (: </CI_RoleCode> :)
1430                    } (: </role> :)
1431                } (: </CI_ResponsibleParty> :)
1432            } (: </contact> :),
1433
1434            element dateStamp {element gco:Date {adjust-date-to-timezone(current-date())}},
1435            element metadataStandardName {element gco:CharacterString {'ISO 19115:2003'}},
1436            element metadataStandardVersion {element gco:CharacterString {'2003'}},
1437            element identificationInfo {
1438                element gmd:MD_DataIdentification {
1439                    element citation {
1440                        element gmd:CI_Citation {
1441                            element title {element gco:CharacterString {data($DE/moles:name)}},
1442                            if (exists($DE/moles:abbreviation) and not(empty($DE/moles:abbreviation))) then
1443                                element alternateTitle {element gco:CharacterString {data($DE/moles:abbreviation)}}
1444                            else (),
1445                            element date {
1446                                if (exists($DE/(moles:DataProvenance | moles:dgMetadataProvenance)/moles:RecordCreation/moles:CreatedDate)) then
1447                                    element gmd:CI_Date {
1448                                        element date {element gco:Date {adjust-date-to-timezone(data($DE/(moles:DataProvenance | moles:dgMetadataProvenance)/moles:RecordCreation/moles:CreatedDate))}},
1449                                        element dateType {
1450                                            element CI_DateTypeCode {
1451                                                attribute codeList {'http://www.isotc211.org/2005/resources/Codelist/gmxCodelists.xml#CI_DateTypeCode'},
1452                                                attribute codeListValue {'http://www.isotc211.org/2005/resources/Codelist/gmxCodelists.xml#CI_DateTypeCode_creation' },
1453                                                'Creation'}
1454                                        } (: </dataType> :)
1455                                    } (: </gmd:CI_Date> :)
1456                                else
1457                                    attribute gco:nilReason {'unknown'}
1458                            } (: </date> :)
1459                        } (: </gmd:CI_Citation> :)
1460                    } (: </citation> :),
1461                    element abstract {element gco:CharacterString {data($DE/moles:dgMetadataDescription/moles:abstract/moles:abstractText)}},
1462                    element language {
1463                        element gmd:LanguageCode {
1464                            attribute codeList {'http://www.isotc211.org/2005/resources/Codelist/gmxCodelists.xml#LanguageCode'},
1465                            attribute codeListValue {'eng'},
1466                            'English' }
1467                    } (: </language> :),
1468                    for $ISOTopicCategory in ($DE/moles:dgStructuredKeyword[dgValidTermID/moles:ParentListID='http://www.isotc211.org/2005/resources/Codelist/gmxCodelists.xml#MD_TopicCategoryCode'])
1469                    return
1470                        element topicCategory {
1471                            element gmd:MD_TopicCategoryCode {
1472                                attribute codeList {'http://www.isotc211.org/2005/resources/Codelist/gmxCodelists.xml#MD_TopicCategoryCode'},
1473                                attribute codeListValue {string($ISOTopicCategory/moles:dgValidTerm)},
1474                                string($ISOTopicCategory/moles:dgValidTerm)}
1475                        } (: </topicCategory> :),
1476                    if (exists($DE/moles:dgDataEntity/moles:dgDataSummary/moles:dgDataCoverage)) then
1477                        element extent {
1478                            element gmd:EX_Extent {
1479                                (: add bounding boxes:)
1480                                for $geoBBox in $DE/moles:dgDataEntity/moles:dgDataSummary/moles:dgDataCoverage//moles:BoundingBox
1481                                return
1482                                    element geographicElement {
1483                                        element EX_GeographicBoundingBox {
1484                                            element westBoundLongitude {element gco:Decimal {data($geoBBox/moles:LimitWest)}},
1485                                            element eastBoundLongitude {element gco:Decimal {data($geoBBox/moles:LimitEast)}},
1486                                            element southBoundLatitude {element gco:Decimal {data($geoBBox/moles:LimitSouth)}},
1487                                            element northBoundLatitude {element gco:Decimal {data($geoBBox/moles:LimitNorth)}}
1488                                        } (: </EX_GeographicBoundingBox> :)
1489                                    } (: </geographicElement> :),
1490                                (: add location terms:)
1491                                for $geoArea in $DE/moles:dgDataEntity/moles:dgDataSummary/moles:dgDataCoverage//moles:dgArea
1492                                return
1493                                    element geographicElement {
1494                                        element EX_GeographicDescription {
1495                                            element geographicIdentifier {
1496                                                element gmd:RS_Identifier {
1497                                                element gco:CharacterString {element code {data($geoArea/moles:dgValidTerm)}},
1498                                                element gco:CharacterString {element codeSpace {data($geoArea/moles:dgValidTermID/moles:ParentListID)}}
1499                                                    }
1500                                                }
1501                                        } (: </EX_GeographicDescription> :)
1502                                    } (: </geographicElement> :)
1503                            } (: </gmd:EX_Extent> :)
1504                        } (: </extent> :)
1505                    else()
1506                } (:</gmd:MD_DataIdentification> :)
1507            } (: </identificationInfo> :)
1508} (: </MD_Metadata> :)'''
1509           
1510MOLESQuery = '''declare default element namespace 'http://ndg.nerc.ac.uk/moles';
1511declare variable $targetCollection as xs:string {'TargetCollection'};
1512declare variable $repositoryIdentifier as xs:string {'RepositoryID'};
1513declare variable $localIdentifier as xs:string {'LocalID'};
1514for $Moles in collection($targetCollection)/dgMetadata/dgMetadataRecord[dgMetadataID/schemeIdentifier='NDG-B0'
1515    and dgMetadataID/repositoryIdentifier=$repositoryIdentifier
1516    and dgMetadataID/localIdentifier=$localIdentifier]
1517    return $Moles '''
1518           
1519DIFQuery = '''import module namespace voclib='http://ndg.nerc.ac.uk/xquery/lib/vocab' at 'xmldb:exist:///db/xqueryLib/Vocabs/vocab_xquery_lib.xquery';
1520import module namespace utillib='http://ndg.nerc.ac.uk/xquery/lib/utilities' at 'xmldb:exist:///db/xqueryLib/Utilities/utility_xquery_lib.xquery';
1521declare default element namespace 'http://gcmd.gsfc.nasa.gov/Aboutus/xml/dif/';
1522declare namespace xsi='http://www.w3.org/2001/XMLSchema-instance';
1523declare namespace moles='http://ndg.nerc.ac.uk/moles';
1524
1525declare variable $targetCollection as xs:string {'TargetCollection'};
1526declare variable $repositoryIdentifier as xs:string {'RepositoryID'};
1527declare variable $localIdentifier as xs:string {'LocalID'};
1528
1529for $DE in collection($targetCollection)/moles:dgMetadata/moles:dgMetadataRecord[
1530    exists('moles:dgDataEntity')
1531    and moles:dgMetadataID/moles:schemeIdentifier='NDG-B0'
1532    and moles:dgMetadataID/moles:repositoryIdentifier=$repositoryIdentifier
1533    and moles:dgMetadataID/moles:localIdentifier=$localIdentifier]
1534    return
1535element DIF {
1536    attribute xsi:schemaLocation {'http://gcmd.gsfc.nasa.gov/Aboutus/xml/dif/ http://gcmd.gsfc.nasa.gov/Aboutus/xml/dif/dif_v9.4.xsd'},
1537    element Entry_ID {concat($DE/moles:dgMetadataID/moles:repositoryIdentifier, $utillib:moles_id_separator, 'DIF', $utillib:moles_id_separator, $DE/moles:dgMetadataID/moles:localIdentifier)},
1538    element Entry_Title {string($DE/moles:name)},
1539    element Data_Set_Citation {
1540            for $DataCreatorRole in $DE/moles:dgDataEntity/moles:dgDataRoles/moles:dgDataCreator/moles:dgRoleHolder[not(exists(moles:endDate)) or moles:endDate=''][1]
1541        return
1542            for $DataCreatorRoleHolder in collection($targetCollection)/moles:dgMetadata/*[(moles:dgMetadataID/moles:schemeIdentifier='NDG-B0'
1543                   and moles:dgMetadataID/moles:repositoryIdentifier=$DataCreatorRole/*/moles:repositoryIdentifier
1544                   and moles:dgMetadataID/moles:localIdentifier=$DataCreatorRole/*/moles:localIdentifier)]
1545            return
1546                    element Dataset_Creator {
1547                if (exists($DataCreatorRoleHolder/moles:name/moles:initials)) then
1548                    string(concat(string($DataCreatorRoleHolder/moles:name/moles:initials), ' ', string($DataCreatorRoleHolder/moles:name/moles:familyName)))
1549                else
1550                   string($DataCreatorRoleHolder/moles:abbreviation)
1551                    },
1552        element Dataset_Title {string($DE/moles:name)}
1553    },
1554    for $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_categories, moles:dgValidTermID/moles:ParentListID))]
1555    )
1556        return if (exists($StructuredKeyword/*/moles:dgValidTerm)) then
1557            element Parameters {
1558            element Category {string($StructuredKeyword/*/moles:dgValidTerm)},
1559            if (exists($StructuredKeyword//moles:dgValidSubterm)
1560                and $StructuredKeyword//moles:dgValidSubterm != ''
1561                and $StructuredKeyword//moles:dgValidSubterm != ' ') then
1562            for $Subterm in $StructuredKeyword//moles:dgValidSubterm[exists(moles:dgValidTerm)]
1563                where exists($Subterm/moles:dgValidSubterm)
1564                order by $Subterm/moles:ListLevel
1565                return
1566                    if ($Subterm/moles:ListLevel=1) then element Topic {string($Subterm/moles:dgValidSubterm/moles:dgValidTerm)}
1567                    else if ($Subterm/moles:ListLevel=2) then element Term {string($Subterm/moles:dgValidSubterm/moles:dgValidTerm)}
1568                    else if ($Subterm/moles:ListLevel=3) then element Variable {string($Subterm/moles:dgValidSubterm/moles:dgValidTerm)}
1569                    else if ($Subterm/moles:ListLevel=4) then element Detailed_Variable {string($Subterm/moles:dgValidSubterm/moles:dgValidTerm)}
1570                    else element GCMD_Science_Valid {$Subterm/moles:ListLevel,($Subterm/moles:dgValidSubterm/moles:dgValidTerm)}
1571            else()
1572                }
1573        else(),
1574    for $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)])
1575    return
1576        element Parameters {data($StructuredKeyword/moles:dgValidTerm)},
1577    for $ISOTopicCategory in ($DE/moles:dgStructuredKeyword[voclib:spot-vocab($voclib:iso_topic_list, moles:dgValidTermID/moles:ParentListID)])
1578        return
1579            element ISO_Topic_Category {string($ISOTopicCategory/moles:dgValidTerm)},
1580    for $Keyword in distinct-values($DE//moles:dgStructuredKeyword[
1581    not (voclib:spot-vocab($voclib:iso_topic_list, moles:dgValidTermID/moles:ParentListID)
1582    or voclib:spot-vocab($voclib:gcmd_science_valids, moles:dgValidTermID/moles:ParentListID)
1583    or voclib:spot-vocab($voclib:gcmd_science_valids_correct, moles:dgValidTermID/moles:ParentListID)
1584    )]/moles:dgValidTerm)
1585    return element Keyword {string($Keyword)},
1586    for $DepDPT in ($DE/moles:dgDataEntity/moles:RelatedDeployment/moles:DataProductionToolID) 
1587    return
1588        element Sensor_Name {
1589            for $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))
1590                return element Short_Name {data($DepDPTAbbrev)},
1591            for $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))
1592                return element Long_Name {data($DepDPTName)}
1593            },
1594    for $DepObsStn in ($DE/moles:dgDataEntity/moles:RelatedDeployment/moles:ObservationStationID)
1595    return
1596        element Source_Name {
1597            for $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))
1598                return element Short_Name {data($DepObsStnAbbrev)},
1599            for $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))
1600                return element Long_Name {data($DepObsStnName)}
1601            },
1602    for $TemporalRange in ($DE/moles:dgDataEntity/moles:dgDataSummary/moles:dgDataCoverage//moles:DateRange)
1603        return
1604            element Temporal_Coverage
1605                {
1606                element Start_Date {data($TemporalRange/moles:DateRangeStart)},
1607                element End_Date {data($TemporalRange/moles:DateRangeEnd)}
1608                },
1609    for $TemporalTerm in ($DE/moles:dgDataEntity/moles:dgDataSummary/moles:dgDataCoverage//moles:dgArea[voclib:spot-vocab($voclib:gcmd_chronostratigraphic_valids, moles:dgValidTermID/moles:ParentListID)]/moles:dgValidTerm)
1610        return element Paleo_Temporal_Coverage {element Chronostratigraphic_Unit {data($TemporalTerm)}},
1611    for $Data_Set_Progress in ($DE/moles:dgDataEntity/moles:dgDataSummary/moles:dgDataStatus/moles:dgDatasetClosure)
1612        return element Data_Set_Progress {data($Data_Set_Progress)},
1613    for $BoundingBox in ($DE/moles:dgDataEntity/moles:dgDataSummary/moles:dgDataCoverage//moles:BoundingBox)
1614    return
1615        element Spatial_Coverage {
1616                element Southernmost_Latitude {data($BoundingBox/moles:LimitSouth)},
1617                element Northernmost_Latitude {data($BoundingBox/moles:LimitNorth)},
1618                element Westernmost_Longitude {data($BoundingBox/moles:LimitWest)},
1619                element Easternmost_Longitude {data($BoundingBox/moles:LimitEast)}
1620            },
1621    for $Location in ($DE/moles:dgDataEntity/moles:dgDataSummary/moles:dgDataCoverage//moles:dgArea[voclib:spot-vocab($voclib:gcmd_location_valids, moles:dgValidTermID/moles:ParentListID)])
1622        return element Location {data($Location/moles:dgValidTerm)},
1623    for $DepAct in ($DE/moles:dgDataEntity/moles:RelatedDeployment/moles:ActivityID)
1624    return
1625        element Project {
1626            for $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)
1627                return
1628                    element Short_Name {data($DepActAbbrev)},
1629            for $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)
1630                return
1631                    element Long_Name {data($DepActName)}
1632            },
1633    if (exists($DE/moles:dgDataEntity/moles:dgDataGranule/moles:accessControlPolicy)) then
1634            for $DG in $DE/moles:dgDataEntity/moles:dgDataGranule
1635                return
1636                                element Access_Constraints {
1637                            if (exists($DG/moles:dgGranuleSummary/moles:dgGranuleName)) then
1638                                concat('For data granule ', $DG/moles:dgGranuleSummary/moles:dgGranuleName, ': ')
1639                            else (),
1640                            if (exists($DG/moles:accessControlPolicy/moles:accessControlPolicyURL)) then
1641                                concat('See access control policy at ', escape-uri($DG/moles:accessControlPolicy/moles:accessControlPolicyURL, true()))
1642                            else if (exists($DG/moles:accessControlPolicy/moles:accessControlPolicyText)) then
1643                                data($DG/moles:accessControlPolicy/moles:accessControlPolicyText)
1644                             else
1645                                 for $securityCondition in $DG/moles:accessControlPolicy/moles:dgSecurityCondition
1646                                 return
1647                                     if (exists($securityCondition/moles:conditionExplanationText)) then
1648                                         concat('Effect: ', data($securityCondition/moles:effect), ' - ', data($securityCondition/moles:conditionExplanationText))
1649                                     else concat('Effect: ', data($securityCondition/moles:effect), 'needs ', data($securityCondition/moles:attauthRole), ' from ', data($securityCondition/moles:dgAttributeAuthority))
1650                        }
1651                else (),
1652    if (exists($DE/moles:dgDataEntity/moles:dgDataRoles/moles:dgDataCreator/moles:dgRoleHolder)) then
1653        for $DataCreatorRole in $DE/moles:dgDataEntity/moles:dgDataRoles/moles:dgDataCreator/moles:dgRoleHolder
1654            order by $DataCreatorRole/moles:startDate empty least
1655                        return
1656                    for $DataCreatorRoleHolder in collection($targetCollection)/moles:dgMetadata/(moles:dgOrganisation | moles:dgPerson)
1657                                        [(moles:dgMetadataID/moles:schemeIdentifier='NDG-B0'
1658                                        and moles:dgMetadataID/moles:repositoryIdentifier=$DataCreatorRole/*/moles:repositoryIdentifier
1659                                        and moles:dgMetadataID/moles:localIdentifier=$DataCreatorRole/*/moles:localIdentifier)]
1660                    return
1661                            element Originating_Center {
1662                    if (string(local-name($DataCreatorRoleHolder))='dgOrganisation') then
1663                        (string($DataCreatorRoleHolder/moles:name))
1664                    else if (string(local-name($DataCreatorRoleHolder))='dgPerson') then
1665                        (string(concat(string($DataCreatorRoleHolder/moles:name/moles:initials), ' ', string($DataCreatorRoleHolder/moles:name/moles:familyName))))
1666                    else ('empty content')
1667                }
1668    else (),
1669    for $DataCuratorRole in $DE/moles:dgDataEntity/moles:dgDataRoles/moles:dgDataCurator/moles:dgRoleHolder
1670    [not(exists(moles:endDate)) or moles:endDate='']
1671    return
1672    for $DataCuratorRoleHolder in collection($targetCollection)/moles:dgMetadata/(moles:dgOrganisation | moles:dgPerson)
1673    [(moles:dgMetadataID/moles:schemeIdentifier='NDG-B0'
1674    and moles:dgMetadataID/moles:repositoryIdentifier=$DataCuratorRole/*/moles:repositoryIdentifier
1675    and moles:dgMetadataID/moles:localIdentifier=$DataCuratorRole/*/moles:localIdentifier)]
1676    return
1677    element Data_Center {
1678    element Data_Center_Name {
1679    element Short_Name {
1680    if (exists($DataCuratorRoleHolder/moles:name/moles:initials)) then
1681    string(concat(string($DataCuratorRoleHolder/moles:name/moles:initials), ' ', string($DataCuratorRoleHolder/moles:name/moles:familyName)))
1682    else
1683    string($DataCuratorRoleHolder/moles:abbreviation)
1684    },
1685    element Long_Name {
1686    if (exists($DataCuratorRoleHolder/moles:name/moles:initials)) then
1687    string(concat(string($DataCuratorRoleHolder/moles:name/moles:initials), ' ', string($DataCuratorRoleHolder/moles:name/moles:familyName)))
1688    else string($DataCuratorRoleHolder/moles:name)
1689    }
1690    },
1691    if (exists($DataCuratorRoleHolder/moles:contactDetails/moles:URI)) then
1692    element Data_Center_URL {data($DataCuratorRoleHolder/moles:contactDetails/moles:URI)}
1693    else (),
1694    for $DGID in $DE/moles:dgDataEntity/moles:dgDataGranule/moles:dataModelID
1695    return
1696    element Data_Set_ID {concat($DGID/moles:repositoryIdentifier, $utillib:moles_id_separator, $DGID/moles:schemeIdentifier, $utillib:moles_id_separator, $DGID/moles:localIdentifier)},
1697    element  Personnel {
1698    element Role {'Data Center Contact'},
1699    if (exists($DataCuratorRoleHolder/name/knownAs)) then
1700    element First_Name {string($DataCuratorRoleHolder/moles:name/moles:knownAs)}
1701    else if (exists($DataCuratorRoleHolder/moles:name/moles:initials)) then
1702    element First_Name {string($DataCuratorRoleHolder/moles:name/moles:initials)}
1703    else (),                   
1704    element Last_Name {
1705    if (exists($DataCuratorRoleHolder/moles:name/moles:familyName)) then
1706    string($DataCuratorRoleHolder/moles:name/moles:familyName)
1707    else string($DataCuratorRoleHolder/moles:name)
1708    },
1709    if (exists($DataCuratorRole/moles:contactDetails/moles:eMail)) then
1710    element Email {string($DataCuratorRole/moles:contactDetails/moles:eMail)}
1711    else
1712    if (exists($DataCuratorRoleHolder/contactDetails/eMail)) then
1713    (element Email {string($DataCuratorRoleHolder/moles:contactDetails/moles:eMail)})
1714    else (),
1715    if (exists($DataCuratorRole/moles:contactDetails/moles:telephone)) then
1716    element Phone {string($DataCuratorRole/moles:contactDetails/moles:telephone)}
1717    else if (exists($DataCuratorRoleHolder/moles:contactDetails/moles:telephone)) then
1718    element Phone {string($DataCuratorRoleHolder/moles:contactDetails/moles:telephone)}
1719    else (),
1720    if (exists($DataCuratorRole/moles:contactDetails/moles:fax)) then
1721    element Fax {string($DataCuratorRole/moles:contactDetails/moles:fax)}
1722    else if (exists($DataCuratorRoleHolder/moles:contactDetails/moles:fax)) then
1723    element Fax {string($DataCuratorRoleHolder/moles:contactDetails/moles:fax)}
1724    else (),
1725    if (exists($DataCuratorRole/moles:contactDetails/moles:address)) then
1726    element Contact_Address {
1727    for $addressline in $DataCuratorRole/moles:contactDetails/moles:address/moles:addressline
1728    return element Address {data($addressline)},
1729    if (exists($DataCuratorRole/moles:contactDetails/moles:address/moles:city)) then
1730    element City {string($DataCuratorRole/moles:contactDetails/moles:address/moles:city)}
1731    else (),
1732    if (exists($DataCuratorRole/moles:contactDetails/moles:address/moles:postcode)) then
1733    element Postal_Code {string($DataCuratorRole/moles:contactDetails/moles:address/moles:postcode)}
1734    else (),
1735    if (exists($DataCuratorRole/moles:contactDetails/moles:address/moles:country)) then
1736    element Country {string($DataCuratorRole/moles:contactDetails/moles:address/moles:country)}
1737    else ()
1738    }
1739    else if (exists($DataCuratorRoleHolder/moles:contactDetails/moles:address)) then
1740    element Contact_Address {
1741    for $addressline in $DataCuratorRoleHolder/moles:contactDetails/moles:address/moles:addressline
1742    return element Address {data($addressline)},
1743    if (exists($DataCuratorRoleHolder/moles:contactDetails/moles:address/moles:city)) then
1744    element City {string($DataCuratorRoleHolder/moles:contactDetails/moles:address/moles:city)}
1745    else (),
1746    if (exists($DataCuratorRoleHolder/moles:contactDetails/moles:address/moles:postcode)) then
1747    element Postal_Code {string($DataCuratorRoleHolder/moles:contactDetails/moles:address/moles:postcode)}
1748    else (),
1749    if (exists($DataCuratorRoleHolder/moles:contactDetails/moles:address/moles:country)) then
1750    element Country {string($DataCuratorRoleHolder/moles:contactDetails/moles:address/moles:country)}
1751    else ()
1752    }
1753    else ()
1754    }
1755    },
1756    element Summary {string($DE/moles:dgMetadataDescription/moles:abstract/moles:abstractText)},
1757    for $DG in $DE/moles:dgDataEntity/moles:dgDataGranule
1758        return
1759            if ($DG/moles:dataModelID/moles:schemeIdentifier='NDG-A0') then
1760                       (element Related_URL {
1761                               element URL_Content_Type {'NDG_A_SERVICE'},
1762                               if (exists($DG/instance/URI)) then
1763                                   element URL {escape-uri($DG/instance/URI, true())}
1764                               else if ($DG/moles:dataModelID/moles:repositoryIdentifier='badc.nerc.ac.uk') then
1765                                   element URL {escape-uri('http://dmgdev1.esc.rl.ac.uk/cgi-bin/ndgDataAccess?datasetSource=dmgdev1.esc.rl.ac.uk&amp;datasetID=', true())}
1766                               else ($DG/moles:dataModelID/moles:repositoryIdentifier),
1767                             element Description {'The NDG service delivering data via NDG A metadata.'}
1768                           },
1769                      element Related_URL {
1770                               element URL_Content_Type {'GET DATA > CSML'},
1771                               if (exists($DG/instance)) then
1772                                   element URL {escape-uri($DG/instance/URI, true())}
1773                               else if ($DG/moles:dataModelID/moles:repositoryIdentifier='badc.nerc.ac.uk') then
1774                                   element URL {escape-uri('http://dmgdev1.esc.rl.ac.uk/cgi-bin/ndgDataAccess?datasetSource=dmgdev1.esc.rl.ac.uk&amp;datasetID=', true())}
1775                               else (),
1776                             element Description {'The NDG service delivering data via NDG A metadata. Additional tag to be more in line with latest version of GCMD valids'}
1777                           })
1778            else if ($DG/moles:dataModelID/moles:schemeIdentifier='CDML-0') then
1779                           (element Related_URL {
1780                                   element URL_Content_Type {'NDG_A_SERVICE'},
1781                               if (exists($DG/instance)) then
1782                                   element URL {escape-uri($DG/instance/URI, true())}
1783                               else if ($DG/moles:dataModelID/moles:repositoryIdentifier='badc.nerc.ac.uk') then
1784                                   element URL {escape-uri('http://cdat.badc.nerc.ac.uk/cgi-bin/dxui.py?datasetURI_1=/', true())}
1785                               else (),
1786                             element Description {'The NDG service delivering data via Data extractor. Additional tag to be more in line with latest version of GCMD valids'}
1787                               },
1788                           element Related_URL {
1789                               element URL_Content_Type {'GET DATA > DX'},
1790                               if (exists($DG/instance)) then
1791                                   element URL {escape-uri($DG/instance/URI, true())}
1792                               else if ($DG/moles:dataModelID/moles:repositoryIdentifier='badc.nerc.ac.uk') then
1793                                   element URL {escape-uri('http://cdat.badc.nerc.ac.uk/cgi-bin/dxui.py?datasetURI_1=/', true())}
1794                               else (),
1795                             element Description {'The NDG service delivering data via Data extractor. Additional tag to be more in line with latest version of GCMD valids'}
1796                               })
1797            else if ($DG/moles:dataModelID/moles:schemeIdentifier='URI') then
1798                       element Related_URL {
1799                             element URL {data($DG/moles:instance/moles:URI)},
1800                             if (exists($DG/moles:instance/moles:instanceComment)) then
1801                                     element Description {data($DG/moles:instance/moles:instanceComment)}           
1802                             else
1803                                     element Description {'URL to aid in delivering data. Note that this may point directly to the data or, more likely, point to the web site of the curator.'}
1804                          }
1805        else (),
1806        for $RelURL_Desc in ($DE/moles:dgMetadataDescription/moles:descriptionSection/moles:descriptionOnlineReference)
1807        return
1808                       if (exists($RelURL_Desc/moles:dgSimpleLink)) then
1809                       element Related_URL {
1810                       element URL {data($RelURL_Desc/moles:dgSimpleLink/URI)},
1811                       element Description {concat(data($RelURL_Desc/dgSimpleLink/moles:name), ' - ', data($RelURL_Desc/dgSimpleLink/moles:notes))}
1812                       }
1813                       else (),
1814    element Metadata_Name {'[CEOS IDN DIF]'},
1815    element Metadata_Version {'9.4'},
1816    if  (exists($DE/moles:dgMetadataProvenance/moles:RecordCreation)) then
1817            element DIF_Creation_Date {data($DE/moles:dgMetadataProvenance/moles:RecordCreation/moles:CreatedDate)}
1818    else (),
1819    for $MDUpdt in $DE/moles:dgMetadataProvenance/moles:RecordUpdate
1820    return 
1821        element DIF_Revision_History {concat(data($MDUpdt/moles:UpdateDate), ' - ', data($MDUpdt/moles:UpdateSummary), ' - ', data($MDUpdt/moles:UpdatedBy))},
1822    if  (exists($DE/moles:dgMetadataProvenance/moles:RecordReview)) then
1823            element Future_DIF_Review_Date {data($DE/moles:dgMetadataProvenance/moles:RecordReview/moles:ReviewDate)}
1824    else ()
1825        }'''
1826       
1827DublinCoreDEQuery='''import module namespace voclib='http://ndg.nerc.ac.uk/xquery/lib/vocab' at 'xmldb:exist:///db/xqueryLib/Vocabs/vocab_xquery_lib.xquery';
1828import module namespace utillib='http://ndg.nerc.ac.uk/xquery/lib/utilities' at 'xmldb:exist:///db/xqueryLib/Utilities/utility_xquery_lib.xquery';
1829declare default element namespace 'http://ndg.nerc.ac.uk/moles';
1830declare namespace xsi='http://www.w3.org/2001/XMLSchema-instance';
1831declare namespace dc='http://purl.org/dc/elements/1.1/';
1832declare namespace oai_dc='http://www.openarchives.org/OAI/2.0/oai_dc';
1833declare variable $targetCollection as xs:string {'TargetCollection'};
1834declare variable $repositoryIdentifier as xs:string {'RepositoryID'};
1835declare variable $localIdentifier as xs:string {'LocalID'};
1836
1837for $DE in collection($targetCollection)/dgMetadata/dgMetadataRecord[
1838exists('dgDataEntity')
1839and dgMetadataID/schemeIdentifier='NDG-B0'
1840and dgMetadataID/repositoryIdentifier=$repositoryIdentifier
1841and dgMetadataID/localIdentifier=$localIdentifier]
1842return
1843element oai_dc:dc {
1844    attribute xsi:schemaLocation {'http://www.openarchives.org/OAI/2.0/oai_dc/ http://www.openarchives.org/OAI/2.0/oai_dc.xsd'},
1845    element dc:title {string($DE/name)},
1846    element dc:type {'Dataset'},
1847    element dc:identifier {concat($DE/dgMetadataID/repositoryIdentifier, $utillib:moles_id_separator, 'DC', $utillib:moles_id_separator, $DE/dgMetadataID/localIdentifier)},
1848    element dc:description {string($DE/dgMetadataDescription/abstract/abstractText)},
1849    element dc:date
1850        {
1851        if (exists($DE/(dgMetadataProvenance | DataProvenance)/RecordUpdate/UpdateDate)) then
1852            for $updatedate in ($DE/(dgMetadataProvenance | DataProvenance)/RecordUpdate/UpdateDate)
1853                order by xs:dateTime($updatedate)
1854            return data($updatedate[1])
1855        else
1856            string($DE/(dgMetadataProvenance | DataProvenance)/RecordCreation/CreatedDate)
1857        },
1858    for $StructuredKeyword in distinct-values($DE/dgStructuredKeyword[
1859    voclib:spot-vocab($voclib:cf_standard_names, dgValidTermID/ParentListID)
1860    or voclib:spot-vocab($voclib:gcmd_science_valids, dgValidTermID/ParentListID)
1861    or voclib:spot-vocab($voclib:gcmd_project_valids, dgValidTermID/ParentListID)
1862    or voclib:spot-vocab($voclib:iso_topic_list, dgValidTermID/ParentListID)
1863    or voclib:spot-vocab($voclib:bodc_parameter_usage_vocab, dgValidTermID/ParentListID)
1864    or voclib:spot-vocab($voclib:bodc_parameter_discovery_vocab, dgValidTermID/ParentListID)
1865    ]/dgValidTerm)
1866    order by $StructuredKeyword
1867    return
1868        element dc:subject {string($StructuredKeyword)},
1869   
1870    for $StructuredKeyword1 in distinct-values($DE/dgDataEntity/dgDataSummary/dgParameterSummary/dgStdParameterMeasured[
1871    voclib:spot-vocab($voclib:gcmd_science_valids, dgValidTermID/ParentListID)
1872    or voclib:spot-vocab($voclib:gcmd_project_valids, dgValidTermID/ParentListID)
1873    or voclib:spot-vocab($voclib:bodc_parameter_usage_vocab, dgValidTermID/ParentListID)
1874    or voclib:spot-vocab($voclib:bodc_parameter_discovery_vocab, dgValidTermID/ParentListID)
1875    ]/dgValidTerm)
1876        order by $StructuredKeyword1
1877    return
1878        element dc:subject  {string($StructuredKeyword1)},
1879    for $DataCreatorRole in $DE/dgDataEntity/dgDataRoles/dgDataCreator/dgRoleHolder
1880        order by $DataCreatorRole/startDate empty least
1881    return
1882        for $DataCreatorRoleHolder in collection($targetCollection)/dgMetadata/(dgOrganisation | dgPerson)
1883            [(dgMetadataID/schemeIdentifier='NDG-B0'
1884            and dgMetadataID/repositoryIdentifier=$DataCreatorRole/(dgOrganisationID | dgPersonID)/repositoryIdentifier
1885            and dgMetadataID/localIdentifier=$DataCreatorRole/(dgOrganisationID | dgPersonID)/localIdentifier)]
1886        return
1887            element dc:creator {
1888                if (string(local-name($DataCreatorRoleHolder))='dgOrganisation') then
1889                    (string($DataCreatorRoleHolder/name))
1890                else if (string(local-name($DataCreatorRoleHolder))='dgPerson') then
1891                    (string(concat(string($DataCreatorRoleHolder/name/initials), ' ', string($DataCreatorRoleHolder/name/familyName))))
1892                else ('empty content')
1893            },
1894    for $DataCuratorRole in $DE/dgDataEntity/dgDataRoles/dgDataCurator/dgRoleHolder[not(exists(endDate)) or endDate='']
1895        order by $DataCuratorRole/startDate empty least
1896    return
1897        for $DataCuratorRoleHolder in collection($targetCollection)/dgMetadata/(dgOrganisation | dgPerson)
1898            [(dgMetadataID/schemeIdentifier='NDG-B0'
1899            and dgMetadataID/repositoryIdentifier=$DataCuratorRole/(dgOrganisationID | dgPersonID)/repositoryIdentifier
1900            and dgMetadataID/localIdentifier=$DataCuratorRole/(dgOrganisationID | dgPersonID)/localIdentifier)]
1901        return
1902            element dc:publisher {
1903                if (string(local-name($DataCuratorRoleHolder))='dgOrganisation') then
1904                    (string($DataCuratorRoleHolder/name))
1905                else if (string(local-name($DataCuratorRoleHolder))='dgPerson') then
1906                    (string(concat(string($DataCuratorRoleHolder/name/initials), ' ', string($DataCuratorRoleHolder/name/familyName))))
1907                else ('empty content')
1908            }
1909}  (: </oai_dc:dc> :)'''
1910
1911MDIPQuery='''import module namespace voclib='http://ndg.nerc.ac.uk/xquery/lib/vocab' at 'xmldb:exist:///db/xqueryLib/Vocabs/vocab_xquery_lib.xquery';
1912import module namespace utillib='http://ndg.nerc.ac.uk/xquery/lib/utilities' at 'xmldb:exist:///db/xqueryLib/Utilities/utility_xquery_lib.xquery';
1913declare default element namespace 'http://www.oceannet.org/mdip/xml';
1914declare namespace moles='http://ndg.nerc.ac.uk/moles';
1915declare namespace gco='http://www.isotc211.org/2005/gco';
1916declare namespace xsi='http://www.w3.org/2001/XMLSchema-instance';
1917declare variable $targetCollection as xs:string {'TargetCollection'};
1918declare variable $repositoryIdentifier as xs:string {'RepositoryID'};
1919declare variable $localIdentifier as xs:string {'LocalID'};
1920declare variable $ISO_639-2_ns as xs:string{$voclib:ISO_639-2_ns};
1921declare variable $ISO_3166_ns as xs:string{$voclib:ISO_3166_ns};
1922
1923for $DE in collection($targetCollection)/moles:dgMetadata/moles:dgMetadataRecord[
1924    exists('moles:dgDataEntity')
1925    and moles:dgMetadataID/moles:schemeIdentifier='NDG-B0'
1926    and moles:dgMetadataID/moles:repositoryIdentifier=$repositoryIdentifier
1927    and moles:dgMetadataID/moles:localIdentifier=$localIdentifier]
1928return
1929element Metadata {
1930    element Title {data($DE/moles:name)},
1931    if (exists($DE/moles:dgDataEntity/moles:dgDataSummary/moles:dgDatasetLanguage)) then
1932        for $lang in $DE/moles:dgDataEntity/moles:dgDataSummary/moles:dgDatasetLanguage
1933        return
1934            element Language{
1935                element LanguageName {data($lang/moles:dgValidTerm)},
1936                element LanguageVocab {data($lang/moles:dgValidTermID/moles:ParentListID)},
1937                element LanguageCode {data($lang/moles:dgValidTermID/moles:TermID)}
1938            }
1939               
1940    else (),
1941
1942    element Abstract {string($DE/moles:dgMetadataDescription/moles:abstract/moles:abstractText)},
1943    for $isoTopic in $DE/moles:dgStructuredKeyword[voclib:spot-vocab($voclib:iso_topic_list, moles:dgValidTermID/moles:ParentListID)]
1944    return element TopicCategory {
1945    element TopicCategoryName {data($isoTopic/moles:dgValidTerm)},
1946    element TopicCategoryVocab {data($isoTopic/moles:dgValidTermID/moles:ParentListID)},
1947    element TopicCategoryCode {data($isoTopic/moles:dgValidTermID/moles:TermID)}
1948    },
1949    for $subject in ($DE/moles:dgDataEntity/moles:dgDataSummary/moles:dgParameterSummary/moles:dgStdParameterMeasured[
1950    voclib:spot-vocab($voclib:gcmd_science_valids, moles:dgValidTermID/moles:ParentListID)
1951    or voclib:spot-vocab($voclib:gcmd_project_valids, moles:dgValidTermID/moles:ParentListID)
1952    or voclib:spot-vocab($voclib:gcmd_science_valids_categories, moles:dgValidTermID/moles:ParentListID)
1953    or voclib:spot-vocab($voclib:bodc_parameter_usage_vocab, moles:dgValidTermID/moles:ParentListID)
1954    or voclib:spot-vocab($voclib:bodc_parameter_discovery_vocab, moles:dgValidTermID/moles:ParentListID)
1955    ])
1956    return (element Subject {
1957    element SubjectName {data($subject/moles:dgValidTerm)},
1958    element SubjectVocab {data($subject/moles:dgValidTermID/moles:ParentListID)},
1959    element SubjectCode {data($subject/moles:dgValidTermID/moles:TermID)}
1960    },
1961    for $subterm in $subject//moles:dgValidSubterm
1962    return (element Subject {
1963    element SubjectName {data($subterm/moles:dgValidTerm)},
1964    element SubjectVocab {data($subterm/moles:dgValidTermID/moles:ParentListID)},
1965    element SubjectCode {data($subterm/moles:dgValidTermID/moles:TermID)}
1966    }
1967    )
1968    ),
1969    if (exists($DE//moles:dgTemporalCoverage/(moles:DateSingle | moles:DateRange))) then
1970    element Date {
1971        element DatasetStartDate {
1972        let $dateStart:=min($DE//moles:dgTemporalCoverage/(moles:DateSingle | moles:DateRange/moles:DateRangeStart) cast as xs:date)
1973        return
1974        if (empty($dateStart)) then ()
1975        else data($dateStart)
1976        },
1977        element DatasetEndDate {
1978        let $dateEnd:=max($DE//moles:dgTemporalCoverage/(moles:DateSingle | moles:DateRange/moles:DateRangeEnd) cast as xs:date)
1979        return
1980        if (empty($dateEnd)) then ()
1981        else data($dateEnd)
1982        }
1983    }
1984    else (),
1985    let $west := min($DE/moles:dgDataEntity/moles:dgDataSummary//moles:BoundingBox/moles:LimitWest cast as xs:decimal)
1986    return element WestCoOrdinate {data($west)},
1987    let $east := max($DE/moles:dgDataEntity/moles:dgDataSummary//moles:BoundingBox/moles:LimitEast cast as xs:decimal)
1988    return element EastCoOrdinate {data($east)},
1989    let $north := max($DE/moles:dgDataEntity/moles:dgDataSummary//moles:BoundingBox/moles:LimitNorth cast as xs:decimal)
1990    return element NorthCoOrdinate {data($north)},
1991    let $south := min($DE/moles:dgDataEntity/moles:dgDataSummary//moles:BoundingBox/moles:LimitSouth cast as xs:decimal)
1992    return element SouthCoOrdinate {data($south)},
1993    element DatasetIdentifier {concat($DE/moles:dgMetadataID/moles:repositoryIdentifier, $utillib:moles_id_separator, 'MDIP', $utillib:moles_id_separator, $DE/moles:dgMetadataID/moles:localIdentifier)},
1994    for $target in $DE/moles:dgStructuredKeyword[voclib:spot-vocab($voclib:ndg_data_provider_vocab, moles:dgValidTermID/moles:ParentListID) ]
1995    return element Target {
1996    element TargetName {data($target/moles:dgValidTerm)},
1997    element TargetVocab {data($target/moles:dgValidTermID/moles:ParentListID)},
1998    element TargetCode {data($target/moles:dgValidTermID/moles:TermID)}
1999    }
2000    }
2001'''
Note: See TracBrowser for help on using the repository browser.