source: TI05-delivery/ows_framework/trunk/ows_server/ows_server/models/ndg_xqueries.py @ 2615

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI05-delivery/ows_framework/trunk/ows_server/ows_server/models/ndg_xqueries.py@2615
Revision 2615, 93.3 KB checked in by lawrence, 13 years ago (diff)

Moving some of the wsgi stack into the ows stack.
At this point an xml dif retrieve works in ows_common.

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