source: exist/trunk/xquery/moles2dif.xq @ 4326

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/exist/trunk/xquery/moles2dif.xq@4326
Revision 4326, 20.8 KB checked in by sdonegan, 11 years ago (diff)

Updated handling of URLs in conversion from MOLES. SJD

Line 
1declare default element namespace 'http://gcmd.gsfc.nasa.gov/Aboutus/xml/dif/';
2declare namespace xsi='http://www.w3.org/2001/XMLSchema-instance';
3declare namespace moles='http://ndg.nerc.ac.uk/moles';
4declare namespace f='http://ndg.nerc.ac.uk/moles/localfunctions';
5
6(:
7Updated descriptionSection to use from URI to URL in moles doc.. can now return links..
8SJD 10/10/08
9:)
10
11(: Keep this in official lib version :)
12declare variable $targetCollection as xs:string := 'TargetCollection';
13declare variable $repositoryIdentifier as xs:string := 'RepositoryID';
14declare variable $localIdentifier as xs:string := 'LocalID';
15
16(: Keep
17declare variable $targetCollection as xs:string := '/db/ndg_B_metadata';
18declare variable $repositoryIdentifier as xs:string := 'badc.nerc.ac.uk';
19declare variable $localIdentifier as xs:string := 'dataent_CIRA';
20:)
21
22
23for $DE in collection($targetCollection)/moles:dgMetadata/moles:dgMetadataRecord[
24        exists('moles:dgDataEntity')
25        and moles:dgMetadataID/moles:schemeIdentifier='NDG-B0'
26        and moles:dgMetadataID/moles:repositoryIdentifier=$repositoryIdentifier
27        and moles:dgMetadataID/moles:localIdentifier=$localIdentifier]
28return
29element DIF {
30        attribute xsi:schemaLocation {'http://gcmd.gsfc.nasa.gov/Aboutus/xml/dif/ http://gcmd.gsfc.nasa.gov/Aboutus/xml/dif/dif_v9.4.xsd'},
31        element Entry_ID {concat($DE/moles:dgMetadataID/moles:repositoryIdentifier, ':DIF:', $DE/moles:dgMetadataID/moles:localIdentifier)},
32        element Entry_Title {string($DE/moles:name)},
33        element Data_Set_Citation {
34            for $DataCreatorRole in $DE/moles:dgDataEntity/moles:dgDataRoles/moles:dgDataCreator/moles:dgRoleHolder[not(exists(moles:endDate)) or moles:endDate=''][1]
35                return
36                    for $DataCreatorRoleHolder in collection($targetCollection)/moles:dgMetadata/*[(moles:dgMetadataID/moles:schemeIdentifier='NDG-B0'
37                               and moles:dgMetadataID/moles:repositoryIdentifier=$DataCreatorRole/*/moles:repositoryIdentifier
38                               and moles:dgMetadataID/moles:localIdentifier=$DataCreatorRole/*/moles:localIdentifier)]
39                        return
40                            element Dataset_Creator {
41                                if (exists($DataCreatorRoleHolder/moles:name/moles:initials)) then
42                                    string(concat(string($DataCreatorRoleHolder/moles:name/moles:initials), ' ', string($DataCreatorRoleHolder/moles:name/moles:familyName)))
43                                else
44                                   string($DataCreatorRoleHolder/moles:abbreviation)
45                                },
46            element Dataset_Title {string($DE/moles:name)}
47        },
48        element Personnel
49        {element Role {''},
50        element Last_Name {''}
51        },
52        element Discipline { element Discipline_Name {''} },
53        for $StructuredKeyword in ($DE//(moles:dgStructuredKeyword | moles:dgStdParameterMeasured)[moles:dgValidTermID/moles:ParentListID='http://gcmd.gsfc.nasa.gov/Resources/valids/gcmd_parameters.html' and moles:ListLevel = 0])
54                return if (exists($StructuredKeyword/*/moles:dgValidTerm)) then
55                    element Parameters {
56                        element Category {string($StructuredKeyword/*/moles:dgValidTerm)},
57                        if (exists($StructuredKeyword//moles:dgValidSubterm)
58                                and $StructuredKeyword//moles:dgValidSubterm != ''
59                                and $StructuredKeyword//moles:dgValidSubterm != ' ') then
60                        for $Subterm in $StructuredKeyword//moles:dgValidSubterm[exists(moles:dgValidTerm)]
61                            where exists($Subterm/moles:dgValidSubterm)
62                            order by $Subterm/moles:ListLevel
63                                return
64                                        if ($Subterm/moles:ListLevel=1) then element Topic {string($Subterm/moles:dgValidSubterm/moles:dgValidTerm)}
65                                        else if ($Subterm/moles:ListLevel=2) then element Term {string($Subterm/moles:dgValidSubterm/moles:dgValidTerm)}
66                                        else if ($Subterm/moles:ListLevel=3) then element Variable {string($Subterm/moles:dgValidSubterm/moles:dgValidTerm)}
67                                        else if ($Subterm/moles:ListLevel=4) then element Detailed_Variable {string($Subterm/moles:dgValidSubterm/moles:dgValidTerm)}
68                                        else element GCMD_Science_Valid {string($Subterm/moles:dgValidSubterm/moles:dgValidTerm)}
69                        else ()
70                    }
71                else (),
72        (: Parameters - this is fail safe :)
73        element Parameters { element Category {''}, element Topic {''}, element Term {''} },
74
75        (: ISOTopic subcategory :)
76        for $ISOTopicCategory in ($DE/moles:dgStructuredKeyword[moles:dgValidTermID/moles:ParentListID='http://www.isotc211.org/2005/resources/Codelist/gmxCodelists.xml#MD_TopicCategoryCode'])
77            return
78                element ISO_Topic_Category {string($ISOTopicCategory/moles:dgValidTerm)},
79
80        (: ok from here :)
81        for $Keyword in distinct-values($DE//moles:dgStructuredKeyword[
82        moles:dgValidTermID/moles:ParentListID!='http://gcmd.gsfc.nasa.gov/Resources/valids/gcmd_parameters.html' 
83        and
84        moles:dgValidTermID/moles:ParentListID!='http://www.isotc211.org/2005/resources/Codelist/gmxCodelists.xml#MD_TopicCategoryCode' 
85        ]/moles:dgValidTerm)
86       
87        return element Keyword {string($Keyword)},
88        for $DepDPT in ($DE/moles:dgDataEntity/moles:RelatedDeployment/moles:DataProductionToolID) 
89        return
90            element Sensor_Name {
91                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))
92                    return element Short_Name {data($DepDPTAbbrev)},
93                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))
94                    return element Long_Name {data($DepDPTName)}
95                },
96        for $DepObsStn in ($DE/moles:dgDataEntity/moles:RelatedDeployment/moles:ObservationStationID)
97        return
98            element Source_Name {
99                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))
100                    return element Short_Name {data($DepObsStnAbbrev)},
101                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))
102                    return element Long_Name {data($DepObsStnName)}
103                },
104    for $TemporalRange in $DE/moles:dgDataEntity/moles:dgDataSummary/moles:dgDataCoverage/(moles:dgTemporalCoverage | moles:dgSpatioTemporalCoverage/moles:dgSpatioTemporalRange/moles:dgTemporalCoverage)/moles:DateRange
105        return
106            element Temporal_Coverage
107                        {
108                        element Start_Date {data($TemporalRange/moles:DateRangeStart)},
109                        element Stop_Date {data($TemporalRange/moles:DateRangeEnd)}
110                        },
111        for $TemporalTerm in ($DE/moles:dgDataEntity/moles:dgDataSummary/moles:dgDataCoverage//moles:dgArea/moles:dgValidTerm)
112                return element Paleo_Temporal_Coverage {element Chronostratigraphic_Unit {data($TemporalTerm)}},
113        for $Data_Set_Progress in ($DE/moles:dgDataEntity/moles:dgDataSummary/moles:dgDataStatus/moles:dgDatasetClosure)
114                return element Data_Set_Progress {data($Data_Set_Progress)},
115        for $BoundingBox in ($DE/moles:dgDataEntity/moles:dgDataSummary/moles:dgDataCoverage//moles:BoundingBox)
116        return
117            element Spatial_Coverage {
118                    element Southernmost_Latitude {data($BoundingBox/moles:LimitSouth)},
119                    element Northernmost_Latitude {data($BoundingBox/moles:LimitNorth)},
120                    element Westernmost_Longitude {data($BoundingBox/moles:LimitWest)},
121                    element Easternmost_Longitude {data($BoundingBox/moles:LimitEast)}
122                },
123        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'])
124                return element Location {$Location},
125        element Data_Resolution {''},
126        for $DepAct in ($DE/moles:dgDataEntity/moles:RelatedDeployment/moles:ActivityID)
127        return
128            element Project {
129                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)
130                    return
131                        element Short_Name {data($DepActAbbrev)},
132                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)
133                    return
134                        element Long_Name {data($DepActName)}
135                },
136        element Quality {''},
137        if (exists($DE/moles:dgDataEntity/moles:dgDataGranule/moles:accessControlPolicy)) then
138                for $DG in $DE/moles:dgDataEntity/moles:dgDataGranule
139                    return
140                                element Access_Constraints {
141                                if (exists($DG/moles:dgGranuleSummary/moles:dgGranuleName)) then
142                                    concat('For data granule ', $DG/moles:dgGranuleSummary/moles:dgGranuleName, ': ')
143                                else (),
144                                if (exists($DG/moles:accessControlPolicy/moles:accessControlPolicyURL)) then
145                                    concat('See access control policy at ', encode-for-uri($DG/moles:accessControlPolicy/moles:accessControlPolicyURL))
146                                else if (exists($DG/moles:accessControlPolicy/moles:accessControlPolicyText)) then
147                                    data($DG/moles:accessControlPolicy/moles:accessControlPolicyText)
148                                 else
149                                     for $securityCondition in $DG/moles:accessControlPolicy/moles:dgSecurityCondition
150                                     return
151                                         if (exists($securityCondition/moles:conditionExplanationText)) then
152                                             concat('Effect: ', data($securityCondition/moles:effect), ' - ', data($securityCondition/moles:conditionExplanationText))
153                                         else concat('Effect: ', data($securityCondition/moles:effect), 'needs ', data($securityCondition/moles:attauthRole), ' from ', data($securityCondition/moles:dgAttributeAuthority))
154                            }
155                else (),
156        element Use_Constraints {''},
157        element Data_Set_Language {''},
158        if (exists($DE/moles:dgDataEntity/moles:dgDataRoles/moles:dgDataCreator/moles:dgRoleHolder)) then
159                for $DataCreatorRole in $DE/moles:dgDataEntity/moles:dgDataRoles/moles:dgDataCreator/moles:dgRoleHolder
160                    order by $DataCreatorRole/moles:startDate empty least
161                            return
162                              (:##for $DataCreatorRoleHolder in collection($targetCollection)/moles:dgMetadata/(moles:dgOrganisation | moles:dgPerson):)
163                                for $DataCreatorRoleHolder in $targetCollection/moles:dgMetadata/(moles:dgOrganisation | moles:dgPerson)
164                                                [(moles:dgMetadataID/moles:schemeIdentifier='NDG-B0'
165                                                and moles:dgMetadataID/moles:repositoryIdentifier=$DataCreatorRole/(moles:dgOrganisationID | moles:dgPersonID)/moles:repositoryIdentifier
166                                                and moles:dgMetadataID/moles:localIdentifier=$DataCreatorRole/(moles:dgOrganisationID | moles:dgPersonID)/moles:localIdentifier)]
167                                return
168                                        element Originating_Center {
169                                        if (string(local-name($DataCreatorRoleHolder))='dgOrganisation') then
170                                                (string($DataCreatorRoleHolder/moles:name))
171                                        else if (string(local-name($DataCreatorRoleHolder))='dgPerson') then
172                                                (string(concat(string($DataCreatorRoleHolder/moles:name/moles:initials), ' ', string($DataCreatorRoleHolder/moles:name/moles:familyName))))
173                                        else ('empty content')
174                                }
175        else (),
176        (: Data Centre fail safe code in here.. somewhere :)
177        for $DataCuratorRole in $DE/moles:dgDataEntity/moles:dgDataRoles/moles:dgDataCurator/moles:dgRoleHolder[not(exists(moles:endDate)) or moles:endDate='']
178            return
179                for $DataCuratorRoleHolder in collection($targetCollection)/moles:dgMetadata/*[(moles:dgMetadataID/moles:schemeIdentifier='NDG-B0'             
180                   and moles:dgMetadataID/moles:repositoryIdentifier=$DataCuratorRole/*/moles:repositoryIdentifier
181                   and moles:dgMetadataID/moles:localIdentifier=$DataCuratorRole/*/moles:localIdentifier)]
182                    return
183                        element Data_Center {
184                        element Data_Center_Name {
185                                element Short_Name {
186                                    if (exists($DataCuratorRoleHolder/moles:name/moles:initials)) then
187                                    string(concat(string($DataCuratorRoleHolder/moles:name/moles:initials), ' ', string($DataCuratorRoleHolder/moles:name/moles:familyName)))
188                            else
189                                string($DataCuratorRoleHolder/moles:abbreviation)
190                            },
191                        element Long_Name {
192                                if (exists($DataCuratorRoleHolder/moles:name/moles:initials)) then
193                                    string(concat(string($DataCuratorRoleHolder/moles:name/moles:initials), ' ', string($DataCuratorRoleHolder/moles:name/moles:familyName)))
194                                else string($DataCuratorRoleHolder/moles:name)
195                            }
196                        },
197                        if (exists($DataCuratorRoleHolder/moles:contactDetails/moles:URI)) then
198                                element Data_Center_URL {data($DataCuratorRoleHolder/moles:contactDetails/moles:URI)}
199                        else (),
200                        for $DGID in $DE/moles:dgDataEntity/moles:dgDataGranule/moles:dataModelID
201                        return
202                            element Data_Set_ID {concat($DGID/moles:repositoryIdentifier, ':', $DGID/moles:schemeIdentifier, ':', $DGID/moles:localIdentifier)},
203                        element  Personnel {
204                                element Role {'Data Center Contact'},
205                                if (exists($DataCuratorRoleHolder/name/knownAs)) then
206                                    element First_Name {string($DataCuratorRoleHolder/moles:name/moles:knownAs)}
207                                else if (exists($DataCuratorRoleHolder/moles:name/moles:initials)) then
208                                    element First_Name {string($DataCuratorRoleHolder/moles:name/moles:initials)}
209                                 else (),                                       
210                                 element Last_Name {
211                                          if (exists($DataCuratorRoleHolder/moles:name/moles:familyName)) then
212                                              string($DataCuratorRoleHolder/moles:name/moles:familyName)
213                                    else string($DataCuratorRoleHolder/moles:name)
214                                    },
215                                if (exists($DataCuratorRole/moles:contactDetails/moles:eMail)) then
216                                    element Email {string($DataCuratorRole/moles:contactDetails/moles:eMail)}
217                                else
218                                    if (exists($DataCuratorRoleHolder/contactDetails/eMail)) then
219                                            (element Email {string($DataCuratorRoleHolder/moles:contactDetails/moles:eMail)})
220                                        else (),
221                                if (exists($DataCuratorRole/moles:contactDetails/moles:telephone)) then
222                                    element Phone {string($DataCuratorRole/moles:contactDetails/moles:telephone)}
223                                else if (exists($DataCuratorRoleHolder/moles:contactDetails/moles:telephone)) then
224                                            element Phone {string($DataCuratorRoleHolder/moles:contactDetails/moles:telephone)}
225                                 else (),
226                                if (exists($DataCuratorRole/moles:contactDetails/moles:fax)) then
227                                    element Fax {string($DataCuratorRole/moles:contactDetails/moles:fax)}
228                                else if (exists($DataCuratorRoleHolder/moles:contactDetails/moles:fax)) then
229                                            element Fax {string($DataCuratorRoleHolder/moles:contactDetails/moles:fax)}
230                                        else (),
231                                if (exists($DataCuratorRole/moles:contactDetails/moles:address)) then
232                                    element Contact_Address {
233                                        for $addressline in $DataCuratorRole/moles:contactDetails/moles:address/moles:addressline
234                                            return element Address {data($addressline)},
235                                                        if (exists($DataCuratorRole/moles:contactDetails/moles:address/moles:city)) then
236                                        element City {string($DataCuratorRole/moles:contactDetails/moles:address/moles:city)}
237                                    else (),
238                                    if (exists($DataCuratorRole/moles:contactDetails/moles:address/moles:postcode)) then
239                                        element Postal_Code {string($DataCuratorRole/moles:contactDetails/moles:address/moles:postcode)}
240                                    else (),
241                                     if (exists($DataCuratorRole/moles:contactDetails/moles:address/moles:country)) then
242                                         element Country {string($DataCuratorRole/moles:contactDetails/moles:address/moles:country)}
243                                     else ()
244                                     }
245                                else if (exists($DataCuratorRoleHolder/moles:contactDetails/moles:address)) then
246                                    element Contact_Address {
247                                        for $addressline in $DataCuratorRoleHolder/moles:contactDetails/moles:address/moles:addressline
248                                            return element Address {data($addressline)},
249                                                            if (exists($DataCuratorRoleHolder/moles:contactDetails/moles:address/moles:city)) then
250                                            element City {string($DataCuratorRoleHolder/moles:contactDetails/moles:address/moles:city)}
251                                        else (),
252                                        if (exists($DataCuratorRoleHolder/moles:contactDetails/moles:address/moles:postcode)) then
253                                            element Postal_Code {string($DataCuratorRole/moles:contactDetails/moles:postcode)}
254                                        else (),
255                                         if (exists($DataCuratorRoleHolder/moles:contactDetails/moles:address/moles:country)) then
256                                             element Country {string($DataCuratorRoleHolder/moles:contactDetails/moles:address/moles:country)}
257                                         else ()
258                                     }
259                                else ()
260                                 }
261                        },
262                        (: fail safe... :)
263                        element Data_Center {
264                        element Data_Center_Name { element Short_Name {''} },
265                        element Personnel { element Role {''}, element Last_Name {''} }
266                        },
267        element Distribution {''},
268        element Multimedia_Sample {''},
269        element Reference {''},
270        element Summary {string($DE/moles:dgMetadataDescription/moles:abstract/moles:abstractText)},
271        for $DG in $DE/moles:dgDataEntity/moles:dgDataGranule
272            (: BNL need to make sure we have a function for doing related URLs :)
273            return
274                if ($DG/moles:dataModelID/moles:schemeIdentifier='NDG-A0') then
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)}, this bit just loops through everything in the xpath and conmcats it -just as I thought -put in mroe structure!
289                            element URL {data($DE/moles:dgMetadataDescription/moles:descriptionSection/moles:descriptionOnlineReference/moles:dgSimpleLink/moles:URI)},:)
290                           
291                        element URL {data($RelURL_Desc/moles:dgSimpleLink/moles:URL)},
292
293                            (: According to Sue, the description can be the standard definition as served by the NDG vocalb server :)
294                            element Description {data($RelURL_Desc/moles:dgSimpleLink/moles:name)}         
295                         },
296        element Parent_DIF {''},
297        element IDN_Node { element Short_Name {''} },
298        element Originating_Metadata_Node {''},
299        element Metadata_Name {'[CEOS IDN DIF]'},
300        element Metadata_Version {'9.4'},
301        if  (exists($DE/moles:dgMetadataProvenance/moles:RecordCreation)) then
302                element DIF_Creation_Date {data($DE/moles:dgMetadataProvenance/moles:RecordCreation/moles:CreatedDate)}
303        else (),
304        for $MDUpdt in $DE/moles:dgMetadataProvenance/moles:RecordUpdate
305        return 
306            element DIF_Revision_History {concat(data($MDUpdt/moles:UpdateDate), ' - ', data($MDUpdt/moles:UpdateSummary), ' - ', data($MDUpdt/moles:UpdatedBy))},
307        if  (exists($DE/moles:dgMetadataProvenance/moles:RecordReview)) then
308                element Future_DIF_Review_Date {data($DE/moles:dgMetadataProvenance/moles:RecordReview/moles:ReviewDate)}
309        else ()
310        } (:    </DIF> :)
Note: See TracBrowser for help on using the repository browser.