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

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