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

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