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

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

Fix for ticket:971 and ticket:972 in moles2dif.

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