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

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/exist/trunk/xquery/moles2dif.xq@3151
Revision 3151, 21.0 KB checked in by lawrence, 12 years ago (diff)

Fixing the xqueries to support the "right" browse query
in the ndgUtils egg.

Line 
1(: This is the xquery to produce a dif document from a full moles database. It is sometimes used for miniMoles, but it's
2main aim should be to produce DIF documents from MOLES for NDG participants.
3Key parameters to be changed when used in anger are
4 TargetCollection, Repository ID, LocalID, BrowseServer :)
5
6import module namespace voclib='http://ndg.nerc.ac.uk/xquery/lib/vocab' at 'xmldb:exist:///db/xqueryLib/Vocabs/vocab_xquery_lib.xquery';
7import module namespace utillib='http://ndg.nerc.ac.uk/xquery/lib/utilities' at 'xmldb:exist:///db/xqueryLib/Utilities/utility_xquery_lib.xquery';
8declare default element namespace 'http://gcmd.gsfc.nasa.gov/Aboutus/xml/dif/';
9declare namespace xsi='http://www.w3.org/2001/XMLSchema-instance';
10declare namespace moles='http://ndg.nerc.ac.uk/moles';
11declare namespace f='http://ndg.nerc.ac.uk/moles/localfunctions';
12
13
14declare variable $targetCollection as xs:string {'TargetCollection'};
15declare variable $repositoryIdentifier as xs:string {'RepositoryID'};
16declare variable $localIdentifier as xs:string {'LocalID'};
17declare variable $browseServer as xs:string {'BrowseServer'};
18(: Keep
19declare variable $targetCollection as xs:string {'/db/ndg_B_metadata'};
20declare variable $repositoryIdentifier as xs:string {'badc.nerc.ac.uk'};
21declare variable $localIdentifier as xs:string {'dataent_CIRA'};
22declare variable $browseServer as xs:string {'ndgbeta.badc.rl.ac.uk'};
23:)
24for $DE in collection($targetCollection)/moles:dgMetadata/moles:dgMetadataRecord[
25        exists('moles:dgDataEntity')
26        and moles:dgMetadataID/moles:schemeIdentifier='NDG-B0'
27        and moles:dgMetadataID/moles:repositoryIdentifier=$repositoryIdentifier
28        and moles:dgMetadataID/moles:localIdentifier=$localIdentifier]
29return
30    element DIF {
31        attribute xsi:schemaLocation {'http://gcmd.gsfc.nasa.gov/Aboutus/xml/dif/ http://gcmd.gsfc.nasa.gov/Aboutus/xml/dif/dif_v9.4.xsd'},
32        element Entry_ID {concat($DE/moles:dgMetadataID/moles:repositoryIdentifier, ':DIF:', $DE/moles:dgMetadataID/moles:localIdentifier)},
33        element Entry_Title {string($DE/moles:name)},
34        element Data_Set_Citation {
35            for $DataCreatorRole in $DE/moles:dgDataEntity/moles:dgDataRoles/moles:dgDataCreator/moles:dgRoleHolder[not(exists(moles:endDate)) or moles:endDate=''][1]
36                return
37                    for $DataCreatorRoleHolder in collection($targetCollection)/moles:dgMetadata/*[(moles:dgMetadataID/moles:schemeIdentifier='NDG-B0'
38                               and moles:dgMetadataID/moles:repositoryIdentifier=$DataCreatorRole/*/moles:repositoryIdentifier
39                               and moles:dgMetadataID/moles:localIdentifier=$DataCreatorRole/*/moles:localIdentifier)]
40                        return
41                            element Data_Creator {
42                                if (exists($DataCreatorRoleHolder/moles:name/moles:initials)) then
43                                    string(concat(string($DataCreatorRoleHolder/moles:name/moles:initials), ' ', string($DataCreatorRoleHolder/moles:name/moles:familyName)))
44                                else
45                                   string($DataCreatorRoleHolder/moles:abbreviation)
46                                },
47            element Dataset_Title {string($DE/moles:name)}
48        },
49        element Personnel {''},
50        element Discipline {''},
51        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])
52                return if (exists($StructuredKeyword/*/moles:dgValidTerm)) then
53                    element Parameters {
54                        element Category {string($StructuredKeyword/*/moles:dgValidTerm)},
55                        if (exists($StructuredKeyword//moles:dgValidSubterm)
56                                and $StructuredKeyword//moles:dgValidSubterm != ''
57                                and $StructuredKeyword//moles:dgValidSubterm != ' ') then
58                        for $Subterm in $StructuredKeyword//moles:dgValidSubterm[exists(moles:dgValidTerm)]
59                            where exists($Subterm/moles:dgValidSubterm)
60                            order by $Subterm/moles:ListLevel
61                                return
62                                        if ($Subterm/moles:ListLevel=1) then element Topic {string($Subterm/moles:dgValidSubterm/moles:dgValidTerm)}
63                                        else if ($Subterm/moles:ListLevel=2) then element Term {string($Subterm/moles:dgValidSubterm/moles:dgValidTerm)}
64                                        else if ($Subterm/moles:ListLevel=3) then element Variable {string($Subterm/moles:dgValidSubterm/moles:dgValidTerm)}
65                                        else if ($Subterm/moles:ListLevel=4) then element Detailed_Variable {string($Subterm/moles:dgValidSubterm/moles:dgValidTerm)}
66                                        else element GCMD_Science_Valid {string($Subterm/moles:dgValidSubterm/moles:dgValidTerm)}
67                        else()
68                    }
69                else(),
70        for $ISOTopicCategory in ($DE/moles:dgStructuredKeyword[moles:dgValidTermID/moles:ParentListID='http://www.isotc211.org/2005/resources/Codelist/gmxCodelists.xml#MD_TopicCategoryCode'])
71            return
72                element ISO_Topic_Category {string($ISOTopicCategory/moles:dgValidTerm)},
73        for $Keyword in distinct-values($DE//moles:dgStructuredKeyword[
74        moles:dgValidTermID/moles:ParentListID!='http://gcmd.gsfc.nasa.gov/Resources/valids/gcmd_parameters.html' 
75        and
76        moles:dgValidTermID/moles:ParentListID!='http://www.isotc211.org/2005/resources/Codelist/gmxCodelists.xml#MD_TopicCategoryCode' 
77        ]/moles:dgValidTerm)
78        return element Keyword {string($Keyword)},
79        for $DepDPT in ($DE/moles:dgDataEntity/moles:RelatedDeployment/moles:DataProductionToolID) 
80        return
81            element Sensor_Name {
82                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))
83                    return element Short_Name {data($DepDPTAbbrev)},
84                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))
85                    return element Long_Name {data($DepDPTName)}
86                },
87        for $DepObsStn in ($DE/moles:dgDataEntity/moles:RelatedDeployment/moles:ObservationStationID)
88        return
89            element Source_Name {
90                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))
91                    return element Short_Name {data($DepObsStnAbbrev)},
92                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))
93                    return element Long_Name {data($DepObsStnName)}
94                },
95        for $TemporalRange in ($DE/moles:dgDataEntity/moles:dgDataSummary/moles:dgDataCoverage//moles:dgDateRange)
96                return
97                    element Temporal_Coverage
98                        {
99                        element Start_Date {data($TemporalRange/moles:DateRangeStart)},
100                        element End_Date {data($TemporalRange/moles:DateRangeEnd)}
101                        },
102        for $TemporalTerm in ($DE/moles:dgDataEntity/moles:dgDataSummary/moles:dgDataCoverage//moles:dgArea/moles:dgValidTerm)
103                return element Paleo_Temporal_Coverage {element Chronostratigraphic_Unit {data($TemporalTerm)}},
104        for $Data_Set_Progress in ($DE/moles:dgDataEntity/moles:dgDataSummary/moles:dgDataStatus/moles:dgDatasetClosure)
105                return element Data_Set_Progress {data($Data_Set_Progress)},
106        for $BoundingBox in ($DE/moles:dgDataEntity/moles:dgDataSummary/moles:dgDataCoverage//moles:BoundingBox)
107        return
108            element Spatial_Coverage {
109                    element Southernmost_Latitude {data($BoundingBox/moles:LimitSouth)},
110                    element Northernmost_Latitude {data($BoundingBox/moles:LimitNorth)},
111                    element Westernmost_Longitude {data($BoundingBox/moles:LimitWest)},
112                    element Easternmost_Longitude {data($BoundingBox/moles:LimitEast)}
113                },
114        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'])
115                return element Location {$Location},
116        element Data_Resolution {''},
117        for $DepAct in ($DE/moles:dgDataEntity/moles:RelatedDeployment/moles:ActivityID)
118        return
119            element Project {
120                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)
121                    return
122                        element Short_Name {data($DepActAbbrev)},
123                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)
124                    return
125                        element Long_Name {data($DepActName)}
126                },
127        element Quality {''},
128        if (exists($DE/moles:dgDataEntity/moles:dgDataGranule/moles:accessControlPolicy)) then
129                for $DG in $DE/moles:dgDataEntity/moles:dgDataGranule
130                    return
131                                element Access_Constraints {
132                                if (exists($DG/moles:dgGranuleSummary/moles:dgGranuleName)) then
133                                    concat('For data granule ', $DG/moles:dgGranuleSummary/moles:dgGranuleName, ': ')
134                                else (),
135                                if (exists($DG/moles:accessControlPolicy/moles:accessControlPolicyURL)) then
136                                    concat('See access control policy at ', escape-uri($DG/moles:accessControlPolicy/moles:accessControlPolicyURL, true()))
137                                else if (exists($DG/moles:accessControlPolicy/moles:accessControlPolicyText)) then
138                                    data($DG/moles:accessControlPolicy/moles:accessControlPolicyText)
139                                 else
140                                     for $securityCondition in $DG/moles:accessControlPolicy/moles:dgSecurityCondition
141                                     return
142                                         if (exists($securityCondition/moles:conditionExplanationText)) then
143                                             concat('Effect: ', data($securityCondition/moles:effect), ' - ', data($securityCondition/moles:conditionExplanationText))
144                                         else concat('Effect: ', data($securityCondition/moles:effect), 'needs ', data($securityCondition/moles:attauthRole), ' from ', data($securityCondition/moles:dgAttributeAuthority))
145                            }
146                else (),
147        element Use_Constraints {''},
148        element Data_Set_Language {''},
149        if (exists($DE/moles:dgDataEntity/moles:dgDataRoles/moles:dgDataCreator/moles:dgRoleHolder)) then
150                for $DataCreatorRole in $DE/moles:dgDataEntity/moles:dgDataRoles/moles:dgDataCreator/moles:dgRoleHolder
151                    order by $DataCreatorRole/moles:startDate empty least
152                            return
153                                for $DataCreatorRoleHolder in collection($targetCollection)/moles:dgMetadata/(moles:dgOrganisation | moles:dgPerson)
154                                                [(moles:dgMetadataID/moles:schemeIdentifier='NDG-B0'
155                                                and moles:dgMetadataID/moles:repositoryIdentifier=$DataCreatorRole/(moles:dgOrganisationID | moles:dgPersonID)/moles:repositoryIdentifier
156                                                and moles:dgMetadataID/moles:localIdentifier=$DataCreatorRole/(moles:dgOrganisationID | moles:dgPersonID)/moles:localIdentifier)]
157                                return
158                                        element Originating_Center {
159                                        if (string(local-name($DataCreatorRoleHolder))='dgOrganisation') then
160                                                (string($DataCreatorRoleHolder/moles:name))
161                                        else if (string(local-name($DataCreatorRoleHolder))='dgPerson') then
162                                                (string(concat(string($DataCreatorRoleHolder/moles:name/moles:initials), ' ', string($DataCreatorRoleHolder/moles:name/moles:familyName))))
163                                        else ('empty content')
164                                }
165        else (),
166        for $DataCuratorRole in $DE/moles:dgDataEntity/moles:dgDataRoles/moles:dgDataCurator/moles:dgRoleHolder[not(exists(moles:endDate)) or moles:endDate='']
167            return
168                for $DataCuratorRoleHolder in collection($targetCollection)/moles:dgMetadata/*[(moles:dgMetadataID/moles:schemeIdentifier='NDG-B0'
169                   and moles:dgMetadataID/moles:repositoryIdentifier=$DataCuratorRole/*/moles:repositoryIdentifier
170                   and moles:dgMetadataID/moles:localIdentifier=$DataCuratorRole/*/moles:localIdentifier)]
171                    return
172                        element Data_Center {
173                        element Data_Center_Name {
174                                element Short_Name {
175                                    if (exists($DataCuratorRoleHolder/moles:name/moles:initials)) then
176                                    string(concat(string($DataCuratorRoleHolder/moles:name/moles:initials), ' ', string($DataCuratorRoleHolder/moles:name/moles:familyName)))
177                            else
178                                string($DataCuratorRoleHolder/moles:abbreviation)
179                            },
180                        element Long_Name {
181                                if (exists($DataCuratorRoleHolder/moles:name/moles:initials)) then
182                                    string(concat(string($DataCuratorRoleHolder/moles:name/moles:initials), ' ', string($DataCuratorRoleHolder/moles:name/moles:familyName)))
183                                else string($DataCuratorRoleHolder/moles:name)
184                            }
185                        },
186                        if (exists($DataCuratorRoleHolder/moles:contactDetails/moles:URI)) then
187                                element Data_Center_URL {data($DataCuratorRoleHolder/moles:contactDetails/moles:URI)}
188                        else (),
189                        for $DGID in $DE/moles:dgDataEntity/moles:dgDataGranule/moles:dataModelID
190                        return
191                            element Data_Set_ID {concat($DGID/moles:repositoryIdentifier, ':', $DGID/moles:schemeIdentifier, ':', $DGID/moles:localIdentifier)},
192                        element  Personnel {
193                                element Role {'Data Center Contact'},
194                                if (exists($DataCuratorRoleHolder/name/knownAs)) then
195                                    element First_Name {string($DataCuratorRoleHolder/moles:name/moles:knownAs)}
196                                else if (exists($DataCuratorRoleHolder/moles:name/moles:initials)) then
197                                    element First_Name {string($DataCuratorRoleHolder/moles:name/moles:initials)}
198                                 else (),                                       
199                                 element Last_Name {
200                                          if (exists($DataCuratorRoleHolder/moles:name/moles:familyName)) then
201                                              string($DataCuratorRoleHolder/moles:name/moles:familyName)
202                                    else string($DataCuratorRoleHolder/moles:name)
203                                    },
204                                if (exists($DataCuratorRole/moles:contactDetails/moles:eMail)) then
205                                    element Email {string($DataCuratorRole/moles:contactDetails/moles:eMail)}
206                                else
207                                    if (exists($DataCuratorRoleHolder/contactDetails/eMail)) then
208                                            (element Email {string($DataCuratorRoleHolder/moles:contactDetails/moles:eMail)})
209                                        else (),
210                                if (exists($DataCuratorRole/moles:contactDetails/moles:telephone)) then
211                                    element Phone {string($DataCuratorRole/moles:contactDetails/moles:telephone)}
212                                else if (exists($DataCuratorRoleHolder/moles:contactDetails/moles:telephone)) then
213                                            element Phone {string($DataCuratorRoleHolder/moles:contactDetails/moles:telephone)}
214                                 else (),
215                                if (exists($DataCuratorRole/moles:contactDetails/moles:fax)) then
216                                    element Fax {string($DataCuratorRole/moles:contactDetails/moles:fax)}
217                                else if (exists($DataCuratorRoleHolder/moles:contactDetails/moles:fax)) then
218                                            element Fax {string($DataCuratorRoleHolder/moles:contactDetails/moles:fax)}
219                                        else (),
220                                if (exists($DataCuratorRole/moles:contactDetails/moles:address)) then
221                                    element Contact_Address {
222                                        for $addressline in $DataCuratorRole/moles:contactDetails/moles:address/moles:addressline
223                                            return element address {data($addressline)},
224                                                        if (exists($DataCuratorRole/moles:contactDetails/moles:address/moles:city)) then
225                                        element City {string($DataCuratorRole/moles:contactDetails/moles:address/moles:city)}
226                                    else (),
227                                    if (exists($DataCuratorRole/moles:contactDetails/moles:address/moles:postcode)) then
228                                        element Postal_Code {string($DataCuratorRole/moles:contactDetails/moles:address/moles:postcode)}
229                                    else (),
230                                     if (exists($DataCuratorRole/moles:contactDetails/moles:address/moles:country)) then
231                                         element Country {string($DataCuratorRole/moles:contactDetails/moles:address/moles:country)}
232                                     else ()
233                                     }
234                                else if (exists($DataCuratorRoleHolder/moles:contactDetails/moles:address)) then
235                                    element Contact_Address {
236                                        for $addressline in $DataCuratorRoleHolder/moles:contactDetails/moles:address/moles:addressline
237                                            return element address {data($addressline)},
238                                                            if (exists($DataCuratorRoleHolder/moles:contactDetails/moles:address/moles:city)) then
239                                            element City {string($DataCuratorRoleHolder/moles:contactDetails/moles:address/moles:city)}
240                                        else (),
241                                        if (exists($DataCuratorRoleHolder/moles:contactDetails/moles:address/moles:postcode)) then
242                                            element Postal_Code {string($DataCuratorRole/moles:contactDetails/moles:postcode)}
243                                        else (),
244                                         if (exists($DataCuratorRoleHolder/moles:contactDetails/moles:address/moles:country)) then
245                                             element Country {string($DataCuratorRoleHolder/moles:contactDetails/moles:address/moles:country)}
246                                         else ()
247                                     }
248                                else ()
249                                 }
250                        },
251        element Distribution {''},
252        element Multimedia_Sample {''},
253        element Reference {''},
254        element Summary {string($DE/moles:dgMetadataDescription/moles:abstract/moles:abstractText)},
255        element Related_URL  {
256            element URL_Content_Type {'VIEW_EXTENDED_METADATA'},
257            element URL {concat($browseServer,'/view/',data($DE/moles:dgMetadataID/moles:repositoryIdentifier),'__NDG-B1__',
258            data($DE/moles:dgMetadataID/moles:localIdentifier))},
259            element Description {'A NERC DataGrid MOLES record is available at this URL.'}
260            },
261        for $DG in $DE/moles:dgDataEntity/moles:dgDataGranule
262            (: BNL need to make sure we have a function for doing related URLs :)
263            return
264                if ($DG/moles:dataModelID/moles:schemeIdentifier='NDG-A0') then
265                    ()
266                else if ($DG/moles:dataModelID/moles:schemeIdentifier='URI') then
267                    element Related_URL {
268                            element URL {data($DG/moles:instance/moles:URI)},
269                            if (exists($DG/moles:instance/moles:instanceComment)) then
270                                    element Description {data($DG/moles:instance/moles:instanceComment)}           
271                            else
272                                    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.'}
273                            }
274            else (),
275            for $RelURL_Desc in ($DE/moles:dgMetadataDescription/moles:descriptionSection/moles:descriptionOnlineReference)
276            return
277                    element Related_URL {
278                            (: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!
279                            element URL {data($DE/moles:dgMetadataDescription/moles:descriptionSection/moles:descriptionOnlineReference/moles:dgSimpleLink/moles:URI)},:)
280                           
281                        element URL {data($RelURL_Desc/moles:dgSimpleLink/moles:URI)},
282
283                            (: According to Sue, the description can be the standard definition as served by the NDG vocalb server :)
284                            element Description {data($RelURL_Desc/moles:dgSimpleLink/moles:name)}         
285                         },
286        element Parent_DIF {''},
287        element IDN_Node {''},
288        element Originating_Metadata_Node {''},
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        } (:    </DIF> :)
Note: See TracBrowser for help on using the repository browser.