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

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

XQuery refactoring process - part one...

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