source: TI07-MOLES/trunk/DIF/XQuery/NDG-DIF.xquery @ 2171

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI07-MOLES/trunk/DIF/XQuery/NDG-DIF.xquery@2171
Revision 2171, 22.2 KB checked in by ko23, 13 years ago (diff)

Refactoring intermediate

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1import module namespace voclib='http://ndg.nerc.ac.uk/xquery/lib/vocab' at 'xmldb:exist:///db/xqueryLib/Vocabs/vocab_xquery_lib.xquery';
2import module namespace utillib='http://ndg.nerc.ac.uk/xquery/lib/utilities' at 'xmldb:exist:///db/xqueryLib/Utilities/utility_xquery_lib.xquery';
3declare default element namespace 'http://gcmd.gsfc.nasa.gov/Aboutus/xml/dif/';
4declare namespace xsi='http://www.w3.org/2001/XMLSchema-instance';
5declare namespace moles='http://ndg.nerc.ac.uk/moles';
6declare variable $targetCollection as xs:string {'TargetCollection'};
7declare variable $repositoryIdentifier as xs:string {'RepositoryID'};
8declare variable $localIdentifier as xs:string {'LocalID'};
9
10for $DE in collection($targetCollection)/moles:dgMetadata/moles:dgMetadataRecord[
11        exists('moles:dgDataEntity')
12        and moles:dgMetadataID/moles:schemeIdentifier='NDG-B0'
13        and moles:dgMetadataID/moles:repositoryIdentifier=$repositoryIdentifier
14        and moles:dgMetadataID/moles:localIdentifier=$localIdentifier]
15return
16element DIF {
17        attribute xsi:schemaLocation {'http://gcmd.gsfc.nasa.gov/Aboutus/xml/dif/ http://gcmd.gsfc.nasa.gov/Aboutus/xml/dif/dif_v9.4.xsd'},
18        element Entry_ID {concat($DE/moles:dgMetadataID/moles:repositoryIdentifier, $utillib:moles_id_separator, 'DIF', $utillib:moles_id_separator, $DE/moles:dgMetadataID/moles:localIdentifier)},
19        element Entry_Title {string($DE/moles:name)},
20        element Data_Set_Citation {
21            for $DataCreatorRole in $DE/moles:dgDataEntity/moles:dgDataRoles/moles:dgDataCreator/moles:dgRoleHolder[not(exists(moles:endDate)) or moles:endDate=''][1]
22                return
23                    for $DataCreatorRoleHolder in collection($targetCollection)/moles:dgMetadata/*[(moles:dgMetadataID/moles:schemeIdentifier='NDG-B0'
24                               and moles:dgMetadataID/moles:repositoryIdentifier=$DataCreatorRole/*/moles:repositoryIdentifier
25                               and moles:dgMetadataID/moles:localIdentifier=$DataCreatorRole/*/moles:localIdentifier)]
26                        return
27                            element Data_Creator {
28                                if (exists($DataCreatorRoleHolder/moles:name/moles:initials)) then
29                                    string(concat(string($DataCreatorRoleHolder/moles:name/moles:initials), ' ', string($DataCreatorRoleHolder/moles:name/moles:familyName)))
30                                else
31                                   string($DataCreatorRoleHolder/moles:abbreviation)
32                                },
33            element Dataset_Title {string($DE/moles:name)}
34        },
35        for $StructuredKeyword in ($DE//(moles:dgStructuredKeyword | moles:dgStdParameterMeasured)[(voclib:spot-vocab($voclib:gcmd_science_valids, moles:dgValidTermID/moles:ParentListID) or voclib:spot-vocab($voclib:gcmd_science_valids_correct, moles:dgValidTermID/moles:ParentListID)) and moles:ListLevel = 0])
36                return if (exists($StructuredKeyword/*/moles:dgValidTerm)) then
37                    element Parameters {
38                        element Category {string($StructuredKeyword/*/moles:dgValidTerm)},
39                        if (exists($StructuredKeyword//moles:dgValidSubterm)
40                                and $StructuredKeyword//moles:dgValidSubterm != ''
41                                and $StructuredKeyword//moles:dgValidSubterm != ' ') then
42                        for $Subterm in $StructuredKeyword//moles:dgValidSubterm[exists(moles:dgValidTerm)]
43                            where exists($Subterm/moles:dgValidSubterm)
44                            order by $Subterm/moles:ListLevel
45                                return
46                                        if ($Subterm/moles:ListLevel=1) then element Topic {string($Subterm/moles:dgValidSubterm/moles:dgValidTerm)}
47                                        else if ($Subterm/moles:ListLevel=2) then element Term {string($Subterm/moles:dgValidSubterm/moles:dgValidTerm)}
48                                        else if ($Subterm/moles:ListLevel=3) then element Variable {string($Subterm/moles:dgValidSubterm/moles:dgValidTerm)}
49                                        else if ($Subterm/moles:ListLevel=4) then element Detailed_Variable {string($Subterm/moles:dgValidSubterm/moles:dgValidTerm)}
50                                        else element GCMD_Science_Valid {string($Subterm/moles:dgValidSubterm/moles:dgValidTerm)}
51                        else()
52                    }
53                else(),
54        for $ISOTopicCategory in ($DE/moles:dgStructuredKeyword[voclib:spot-vocab($voclib:iso_topic_list, moles:dgValidTermID/moles:ParentListID)])
55            return
56                element ISO_Topic_Category {string($ISOTopicCategory/moles:dgValidTerm)},
57        for $Keyword in distinct-values($DE//moles:dgStructuredKeyword[
58        not (voclib:spot-vocab($voclib:iso_topic_list, moles:dgValidTermID/moles:ParentListID)
59        or voclib:spot-vocab($voclib:gcmd_science_valids, moles:dgValidTermID/moles:ParentListID)
60        or voclib:spot-vocab($voclib:gcmd_science_valids_correct, moles:dgValidTermID/moles:ParentListID)
61        )]/moles:dgValidTerm)
62        return element Keyword {string($Keyword)},
63        for $DepDPT in ($DE/moles:dgDataEntity/moles:RelatedDeployment/moles:DataProductionToolID) 
64        return
65            element Sensor_Name {
66                for $DepDPTAbbrev in distinct-values((collection($targetCollection)/moles:dgMetadata/moles:dgMetadataRecord[moles:dgMetadataID[moles:schemeIdentifier='NDG-B0' and moles:repositoryIdentifier=$DepDPT/moles:repositoryIdentifier and moles:localIdentifier=$DepDPT/moles:localIdentifier]]/moles:abbreviation))
67                    return element Short_Name {data($DepDPTAbbrev)},
68                for $DepDPTName in distinct-values((collection($targetCollection)/moles:dgMetadata/moles:dgMetadataRecord[moles:dgMetadataID[moles:schemeIdentifier='NDG-B0' and moles:repositoryIdentifier=$DepDPT/moles:repositoryIdentifier and moles:localIdentifier=$DepDPT/moles:localIdentifier]]/moles:name))
69                    return element Long_Name {data($DepDPTName)}
70                },
71        for $DepObsStn in ($DE/moles:dgDataEntity/moles:RelatedDeployment/moles:ObservationStationID)
72        return
73            element Source_Name {
74                for $DepObsStnAbbrev in distinct-values((collection($targetCollection)/moles:dgMetadata/moles:dgMetadataRecord[moles:dgMetadataID[moles:schemeIdentifier='NDG-B0' and moles:repositoryIdentifier=$DepObsStn/moles:repositoryIdentifier and moles:localIdentifier=$DepObsStn/moles:localIdentifier]]/moles:abbreviation))
75                    return element Short_Name {data($DepObsStnAbbrev)},
76                for $DepObsStnName in distinct-values((collection($targetCollection)/moles:dgMetadata/moles:dgMetadataRecord[moles:dgMetadataID[moles:schemeIdentifier='NDG-B0' and moles:repositoryIdentifier=$DepObsStn/moles:repositoryIdentifier and moles:localIdentifier=$DepObsStn/moles:localIdentifier]]/moles:name))
77                    return element Long_Name {data($DepObsStnName)}
78                },
79        for $TemporalRange in ($DE/moles:dgDataEntity/moles:dgDataSummary/moles:dgDataCoverage//moles:dgDateRange)
80                return
81                    element Temporal_Coverage
82                        {
83                        element Start_Date {data($TemporalRange/moles:DateRangeStart)},
84                        element End_Date {data($TemporalRange/moles:DateRangeEnd)}
85                        },
86        for $TemporalTerm in ($DE/moles:dgDataEntity/moles:dgDataSummary/moles:dgDataCoverage//moles:dgArea[voclib:spot-vocab($voclib:gcmd_chronostratigraphic_valids, moles:dgValidTermID/moles:ParentListID)]/moles:dgValidTerm)
87                return element Paleo_Temporal_Coverage {element Chronostratigraphic_Unit {data($TemporalTerm)}},
88        for $Data_Set_Progress in ($DE/moles:dgDataEntity/moles:dgDataSummary/moles:dgDataStatus/moles:dgDatasetClosure)
89                return element Data_Set_Progress {data($Data_Set_Progress)},
90        for $BoundingBox in ($DE/moles:dgDataEntity/moles:dgDataSummary/moles:dgDataCoverage//moles:BoundingBox)
91        return
92            element Spatial_Coverage {
93                    element Southernmost_Latitude {data($BoundingBox/moles:LimitSouth)},
94                    element Northernmost_Latitude {data($BoundingBox/moles:LimitNorth)},
95                    element Westernmost_Longitude {data($BoundingBox/moles:LimitWest)},
96                    element Easternmost_Longitude {data($BoundingBox/moles:LimitEast)}
97                },
98        for $Location in ($DE/moles:dgDataEntity/moles:dgDataSummary/moles:dgDataCoverage//moles:dgArea[voclib:spot-vocab($voclib:gcmd_location_valids, moles:dgValidTermID/moles:ParentListID)])
99                return element Location {$Location},
100        for $DepAct in ($DE/moles:dgDataEntity/moles:RelatedDeployment/moles:ActivityID)
101        return
102            element Project {
103                for $DepActAbbrev in distinct-values(collection($targetCollection)/moles:dgMetadata/moles:dgMetadataRecord[moles:dgMetadataID[moles:schemeIdentifier='NDG-B0' and moles:repositoryIdentifier=$DepAct/moles:repositoryIdentifier and moles:localIdentifier=$DepAct/moles:localIdentifier]]/moles:abbreviation)
104                    return
105                        element Short_Name {data($DepActAbbrev)},
106                for $DepActName in distinct-values(collection($targetCollection)/moles:dgMetadata/moles:dgMetadataRecord[moles:dgMetadataID[moles:schemeIdentifier='NDG-B0' and moles:repositoryIdentifier=$DepAct/moles:repositoryIdentifier and moles:localIdentifier=$DepAct/moles:localIdentifier]]/moles:name)
107                    return
108                        element Long_Name {data($DepActName)}
109                },
110        if (exists($DE/moles:dgDataEntity/moles:dgDataGranule/moles:accessControlPolicy)) then
111                for $DG in $DE/moles:dgDataEntity/moles:dgDataGranule
112                    return
113                                element Access_Constraints {
114                                if (exists($DG/moles:dgGranuleSummary/moles:dgGranuleName)) then
115                                    concat('For data granule ', $DG/moles:dgGranuleSummary/moles:dgGranuleName, ': ')
116                                else (),
117                                if (exists($DG/moles:accessControlPolicy/moles:accessControlPolicyURL)) then
118                                    concat('See access control policy at ', escape-uri($DG/moles:accessControlPolicy/moles:accessControlPolicyURL, true()))
119                                else if (exists($DG/moles:accessControlPolicy/moles:accessControlPolicyText)) then
120                                    data($DG/moles:accessControlPolicy/moles:accessControlPolicyText)
121                                 else
122                                     for $securityCondition in $DG/moles:accessControlPolicy/moles:dgSecurityCondition
123                                     return
124                                         if (exists($securityCondition/moles:conditionExplanationText)) then
125                                             concat('Effect: ', data($securityCondition/moles:effect), ' - ', data($securityCondition/moles:conditionExplanationText))
126                                         else concat('Effect: ', data($securityCondition/moles:effect), 'needs ', data($securityCondition/moles:attauthRole), ' from ', data($securityCondition/moles:dgAttributeAuthority))
127                            }
128                else (),
129        if (exists($DE/moles:dgDataEntity/moles:dgDataRoles/moles:dgDataCreator/moles:dgRoleHolder)) then
130                for $DataCreatorRole in $DE/moles:dgDataEntity/moles:dgDataRoles/moles:dgDataCreator/moles:dgRoleHolder
131                    order by $DataCreatorRole/moles:startDate empty least
132                            return
133                                for $DataCreatorRoleHolder in collection($targetCollection)/moles:dgMetadata/(moles:dgOrganisation | moles:dgPerson)
134                                                [(moles:dgMetadataID/moles:schemeIdentifier='NDG-B0'
135                                                and moles:dgMetadataID/moles:repositoryIdentifier=$DataCreatorRole/(moles:dgOrganisationID | moles:dgPersonID)/moles:repositoryIdentifier
136                                                and moles:dgMetadataID/moles:localIdentifier=$DataCreatorRole/(moles:dgOrganisationID | moles:dgPersonID)/moles:localIdentifier)]
137                                return
138                                        element Originating_Center {
139                                        if (string(local-name($DataCreatorRoleHolder))='dgOrganisation') then
140                                                (string($DataCreatorRoleHolder/moles:name))
141                                        else if (string(local-name($DataCreatorRoleHolder))='dgPerson') then
142                                                (string(concat(string($DataCreatorRoleHolder/moles:name/moles:initials), ' ', string($DataCreatorRoleHolder/moles:name/moles:familyName))))
143                                        else ('empty content')
144                                }
145        else (),
146        for $DataCuratorRole in $DE/moles:dgDataEntity/moles:dgDataRoles/moles:dgDataCurator/moles:dgRoleHolder[not(exists(moles:endDate)) or moles:endDate='']
147            return
148                for $DataCuratorRoleHolder in collection($targetCollection)/moles:dgMetadata/*[(moles:dgMetadataID/moles:schemeIdentifier='NDG-B0'
149                   and moles:dgMetadataID/moles:repositoryIdentifier=$DataCuratorRole/*/moles:repositoryIdentifier
150                   and moles:dgMetadataID/moles:localIdentifier=$DataCuratorRole/*/moles:localIdentifier)]
151                    return
152                        element Data_Center {
153                        element Data_Center_Name {
154                                element Short_Name {
155                                    if (exists($DataCuratorRoleHolder/moles:name/moles:initials)) then
156                                    string(concat(string($DataCuratorRoleHolder/moles:name/moles:initials), ' ', string($DataCuratorRoleHolder/moles:name/moles:familyName)))
157                            else
158                                string($DataCuratorRoleHolder/moles:abbreviation)
159                            },
160                        element Long_Name {
161                                if (exists($DataCuratorRoleHolder/moles:name/moles:initials)) then
162                                    string(concat(string($DataCuratorRoleHolder/moles:name/moles:initials), ' ', string($DataCuratorRoleHolder/moles:name/moles:familyName)))
163                                else string($DataCuratorRoleHolder/moles:name)
164                            }
165                        },
166                        if (exists($DataCuratorRoleHolder/moles:contactDetails/moles:URI)) then
167                                element Data_Center_URL {data($DataCuratorRoleHolder/moles:contactDetails/moles:URI)}
168                        else (),
169                        for $DGID in $DE/moles:dgDataEntity/moles:dgDataGranule/moles:dataModelID
170                        return
171                            element Data_Set_ID {concat($DGID/moles:repositoryIdentifier, '__', $DGID/moles:schemeIdentifier, '__', $DGID/moles:localIdentifier)},
172                        element  Personnel {
173                                element Role {'Data Center Contact'},
174                                if (exists($DataCuratorRoleHolder/name/knownAs)) then
175                                    element First_Name {string($DataCuratorRoleHolder/moles:name/moles:knownAs)}
176                                else if (exists($DataCuratorRoleHolder/moles:name/moles:initials)) then
177                                    element First_Name {string($DataCuratorRoleHolder/moles:name/moles:initials)}
178                                 else (),                                       
179                                 element Last_Name {
180                                          if (exists($DataCuratorRoleHolder/moles:name/moles:familyName)) then
181                                              string($DataCuratorRoleHolder/moles:name/moles:familyName)
182                                    else string($DataCuratorRoleHolder/moles:name)
183                                    },
184                                if (exists($DataCuratorRole/moles:contactDetails/moles:eMail)) then
185                                    element Email {string($DataCuratorRole/moles:contactDetails/moles:eMail)}
186                                else
187                                    if (exists($DataCuratorRoleHolder/contactDetails/eMail)) then
188                                            (element Email {string($DataCuratorRoleHolder/moles:contactDetails/moles:eMail)})
189                                        else (),
190                                if (exists($DataCuratorRole/moles:contactDetails/moles:telephone)) then
191                                    element Phone {string($DataCuratorRole/moles:contactDetails/moles:telephone)}
192                                else if (exists($DataCuratorRoleHolder/moles:contactDetails/moles:telephone)) then
193                                            element Phone {string($DataCuratorRoleHolder/moles:contactDetails/moles:telephone)}
194                                 else (),
195                                if (exists($DataCuratorRole/moles:contactDetails/moles:fax)) then
196                                    element Fax {string($DataCuratorRole/moles:contactDetails/moles:fax)}
197                                else if (exists($DataCuratorRoleHolder/moles:contactDetails/moles:fax)) then
198                                            element Fax {string($DataCuratorRoleHolder/moles:contactDetails/moles:fax)}
199                                        else (),
200                                if (exists($DataCuratorRole/moles:contactDetails/moles:address)) then
201                                    element Contact_Address {
202                                        for $addressline in $DataCuratorRole/moles:contactDetails/moles:address/moles:addressline
203                                            return element Address {data($addressline)},
204                                                        if (exists($DataCuratorRole/moles:contactDetails/moles:address/moles:city)) then
205                                        element City {string($DataCuratorRole/moles:contactDetails/moles:address/moles:city)}
206                                    else (),
207                                    if (exists($DataCuratorRole/moles:contactDetails/moles:address/moles:postcode)) then
208                                        element Postal_Code {string($DataCuratorRole/moles:contactDetails/moles:address/moles:postcode)}
209                                    else (),
210                                     if (exists($DataCuratorRole/moles:contactDetails/moles:address/moles:country)) then
211                                         element Country {string($DataCuratorRole/moles:contactDetails/moles:address/moles:country)}
212                                     else ()
213                                     }
214                                else if (exists($DataCuratorRoleHolder/moles:contactDetails/moles:address)) then
215                                    element Contact_Address {
216                                        for $addressline in $DataCuratorRoleHolder/moles:contactDetails/moles:address/moles:addressline
217                                            return element Address {data($addressline)},
218                                                            if (exists($DataCuratorRoleHolder/moles:contactDetails/moles:address/moles:city)) then
219                                            element City {string($DataCuratorRoleHolder/moles:contactDetails/moles:address/moles:city)}
220                                        else (),
221                                        if (exists($DataCuratorRoleHolder/moles:contactDetails/moles:address/moles:postcode)) then
222                                            element Postal_Code {string($DataCuratorRole/moles:contactDetails/moles:postcode)}
223                                        else (),
224                                         if (exists($DataCuratorRoleHolder/moles:contactDetails/moles:address/moles:country)) then
225                                             element Country {string($DataCuratorRoleHolder/moles:contactDetails/moles:address/moles:country)}
226                                         else ()
227                                     }
228                                else ()
229                                 }
230                        },
231        element Summary {string($DE/moles:dgMetadataDescription/moles:abstract/moles:abstractText)},
232        element Related_URL  {
233            element URL_Content_Type {'NDG_B_SERVICE'},
234            element URL {data($DE/moles:dgMetadataID/moles:repositoryIdentifier)},
235            element Description {'The NDG service for browsing metadata.'}
236            },
237        for $DG in $DE/moles:dgDataEntity/moles:dgDataGranule
238            return
239                if ($DG/moles:dataModelID/moles:schemeIdentifier='NDG-A0') then
240                    (element Related_URL {
241                            element URL_Content_Type {'NDG_A_SERVICE'},
242                            if (exists($DG/instance/URI)) then
243                                element URL {escape-uri($DG/instance/URI, true())}
244                            else if ($DG/moles:dataModelID/moles:repositoryIdentifier='badc.nerc.ac.uk') then
245                                element URL {escape-uri('http://dmgdev1.esc.rl.ac.uk/cgi-bin/ndgDataAccess?datasetSource=dmgdev1.esc.rl.ac.uk&datasetID=', true())}
246                            else ($DG/moles:dataModelID/moles:repositoryIdentifier),
247                            element Description {'The NDG service delivering data via NDG A metadata.'}
248                        },
249                   element Related_URL {
250                            element URL_Content_Type {'GET DATA > CSML'},
251                            if (exists($DG/instance)) then
252                                element URL {escape-uri($DG/instance/URI, true())}
253                            else if ($DG/moles:dataModelID/moles:repositoryIdentifier='badc.nerc.ac.uk') then
254                                element URL {escape-uri('http://dmgdev1.esc.rl.ac.uk/cgi-bin/ndgDataAccess?datasetSource=dmgdev1.esc.rl.ac.uk&datasetID=', true())}
255                            else (),
256                            element Description {'The NDG service delivering data via NDG A metadata. Additional tag to be more in line with latest version of GCMD valids'}
257                        })
258                else if ($DG/moles:dataModelID/moles:schemeIdentifier='CDML-0') then
259                        (element Related_URL {
260                                   element URL_Content_Type {'NDG_A_SERVICE'},
261                            if (exists($DG/instance)) then
262                                element URL {escape-uri($DG/instance/URI, true())}
263                            else if ($DG/moles:dataModelID/moles:repositoryIdentifier='badc.nerc.ac.uk') then
264                                element URL {escape-uri('http://cdat.badc.nerc.ac.uk/cgi-bin/dxui.py?datasetURI_1=/', true())}
265                            else (),
266                            element Description {'The NDG service delivering data via Data extractor. Additional tag to be more in line with latest version of GCMD valids'}
267                            },
268                        element Related_URL {
269                            element URL_Content_Type {'GET DATA > DX'},
270                            if (exists($DG/instance)) then
271                                element URL {escape-uri($DG/instance/URI, true())}
272                            else if ($DG/moles:dataModelID/moles:repositoryIdentifier='badc.nerc.ac.uk') then
273                                element URL {escape-uri('http://cdat.badc.nerc.ac.uk/cgi-bin/dxui.py?datasetURI_1=/', true())}
274                            else (),
275                            element Description {'The NDG service delivering data via Data extractor. Additional tag to be more in line with latest version of GCMD valids'}
276                            })
277                else if ($DG/moles:dataModelID/moles:schemeIdentifier='URI') then
278                    element Related_URL {
279                            element URL {data($DG/moles:instance/moles:URI)},
280                            if (exists($DG/moles:instance/moles:instanceComment)) then
281                                    element Description {data($DG/moles:instance/moles:instanceComment)}           
282                            else
283                                    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.'}
284                            }
285            else (),
286            for $RelURL_Desc in ($DE/moles:dgMetadataDescription/moles:descriptionSection/moles:descriptionOnlineReference)
287            return
288                    element Related_URL {
289                            element URL {data($RelURL_Desc)},
290                            element Description {concat(data($RelURL_Desc/moles:name), ' - ', data($RelURL_Desc/moles:notes))}             
291                         },
292        element Metadata_Name {'[CEOS IDN DIF]'},
293        element Metadata_Version {'9.4'},
294        if  (exists($DE/moles:dgMetadataProvenance/moles:RecordCreation)) then
295                element DIF_Creation_Date {data($DE/moles:dgMetadataProvenance/moles:RecordCreation/moles:CreatedDate)}
296        else (),
297        for $MDUpdt in $DE/moles:dgMetadataProvenance/moles:RecordUpdate
298        return 
299            element DIF_Revision_History {concat(data($MDUpdt/moles:UpdateDate), ' - ', data($MDUpdt/moles:UpdateSummary), ' - ', data($MDUpdt/moles:UpdatedBy))},
300        if  (exists($DE/moles:dgMetadataProvenance/moles:RecordReview)) then
301                element Future_DIF_Review_Date {data($DE/moles:dgMetadataProvenance/moles:RecordReview/moles:ReviewDate)}
302        else ()
303        }
Note: See TracBrowser for help on using the repository browser.