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

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

Fix for #799

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