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

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

Various updates to the xquery files - to remove usage of invalid specs
and dependency on obsolete function calls. Also add the inputParse_xquery_lib.xquery
file - referenced from the xquery files but not currently included in codebase.
NB, changes made to allow use of Saxon java library to do xqueries - to
remove the need for the discovery service eXist DB.

  • NB, also changes tested by comparing saxon transforms to eXist ones; these

were found to be identical.

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