source: TI07-MOLES/trunk/DIF/XQuery/DIF2MOLES.xquery @ 3146

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI07-MOLES/trunk/DIF/XQuery/DIF2MOLES.xquery@3146
Revision 3146, 13.4 KB checked in by mpritcha, 12 years ago (diff)

oops should be DPPP

RevLine 
[1800]1(: Version for one DIF per dgMetadata instance :)
[1782]2(: Note algoritm for creating non-pre-existing organisations :)
[2008]3(: dgPersons are not created as one can't tell automatically which are people and which are orgs, and orgs are simpler :) 
[2077]4import module namespace voclib='http://ndg.nerc.ac.uk/xquery/lib/vocab' at 'xmldb:exist:///db/xqueryLib/Vocabs/vocab_xquery_lib.xquery';
[2142]5import module namespace inputParse='http://ndg.nerc.ac.uk/xquery/lib/inputParse' at 'xmldb:exist:///db/xqueryLib/Utilities/inputParse_xquery_lib.xquery';
[1782]6declare default element namespace 'http://ndg.nerc.ac.uk/moles';
7declare namespace dif='http://gcmd.gsfc.nasa.gov/Aboutus/xml/dif/';
8declare namespace xsi='http://www.w3.org/2001/XMLSchema-instance';
[2142]9
[2056]10(: Replacable parameters :)
[2358]11declare variable $targetCollection as xs:string {'/db/discovery'};
12declare variable $input_repository as xs:string {'Input_Repository_Code'};
13declare variable $input_repository_local as xs:string {'Input_Repository_LocalID'};
[2449]14declare variable $input_entry_id as xs:string {'ndg.noc.soton.ac.uk__DIF__NOCSDAT110'};
15declare variable $localIdentifier as xs:string {'ndg.noc.soton.ac.uk__DIF__NOCSDAT110'};
[2358]16
17declare variable $output_local_id as xs:string {'Output_LocalID'};
18(: Keep
[2174]19declare variable $targetCollection as xs:string {'TargetCollection'};
[2056]20declare variable $input_repository as xs:string {'Input_Repository_Code'};
21declare variable $input_repository_local as xs:string {'Input_Repository_LocalID'};
22declare variable $input_entry_id as xs:string {'Input_Entry_ID'};
23declare variable $output_local_id as xs:string {'Output_LocalID'};
[2358]24:)
[2174]25for $DIF in collection($targetCollection)/dif:DIF[dif:Entry_ID=$input_entry_id]
[1782]26return
[1800]27element dgMetadata {
[2192]28element dgMetadataRecord {
29element dgMetadataID {
30element schemeIdentifier {'NDG-B0'},
31element repositoryIdentifier {$input_repository},
32if ($output_local_id != 'Output_LocalID') then
33element localIdentifier {$output_local_id}
34else
[2291]35element localIdentifier {data($DIF/dif:Entry_ID)}
[2192]36},
37element dgMetadataDescription {
38element metadataDescriptionID {
39element schemeIdentifier {'NDG-B0'},
40element repositoryIdentifier {$input_repository},
41if ($output_local_id != 'Output_LocalID') then
42element localIdentifier {concat('generated_desc-', $output_local_id)}
43else
44element localIdentifier {concat('generated_desc-', escape-uri(string($DIF/dif:Entry_ID), true()))}
45},
46element metadataDescriptionLastUpdated {current-date()}                 ,
47element abstract {
[2449]48element abstractText {string($DIF/dif:Summary)}
49}
50,
[2358]51for $descOnline in $DIF/dif:Related_URL
52return
53element descriptionSection {
54element descriptionOnlineReference {
55element dgSimpleLink {escape-uri(data($descOnline/dif:URL), true())}},
56if (exists($descOnline/dif:URL_Content_Type)) then
[2449]57element dgReferenceName {data($descOnline/dif:URL_Content_Type)}
[2358]58else ()
[2192]59}
60},
61element name {string($DIF/dif:Entry_Title)},
62element abbreviation {string($DIF/dif:Entry_Title)},
63element dgDataEntity {
64element dgDataSetType {''},
65element dgDataSummary {
66for $parameter in $DIF/dif:Parameters
67return
68element dgParameterSummary {
69element dgParameterValue {
70element dgValueDataParameter {
71element Value {''},
72element dgStandardUnit {
73element dgValidTerm {'dummy'},
74element dgValidTermID {
75element ParentListID {$voclib:unknown_vocab_id},
76element TermID {escape-uri('dummy unit', true())}
77}
78}
79}
80},
81element dgStdParameterMeasured {
[2358]82if ($parameter/dif:Category!='') then element dgValidTerm {string($parameter/dif:Category)}
83else  element dgValidTerm {'unknown'},
[2291]84element dgValidTermID {
85element ParentListID {concat($voclib:gcmd_science_valids_categories, '/current')},
86element TermID {escape-uri($parameter/dif:Category, true())}
87},
88element dgValidSubterm {
[2358]89if ($parameter/dif:Topic!='') then element dgValidTerm {string($parameter/dif:Topic)}
90else  element dgValidTerm {'unknown'},
[2291]91element dgValidTermID {
92element ParentListID {concat($voclib:gcmd_science_valids_topics, '/current')},
[2358]93element TermID {escape-uri($parameter/dif:Topic, true())}
[2291]94},
95element dgValidSubterm {
[2358]96if ($parameter/dif:Term!='') then element dgValidTerm {string($parameter/dif:Term)}
97else  element dgValidTerm {'unknown'},
[2291]98element dgValidTermID {
99element ParentListID {concat($voclib:gcmd_science_valids_terms, '/current')},
100element TermID {escape-uri($parameter/dif:Term, true())}
101},
[2358]102if (exists($parameter/dif:Variable) and $parameter/dif:Variable!='') then
[2291]103element dgValidSubterm {
104element dgValidTerm {string($parameter/dif:Variable)},
105element dgValidTermID {
106element ParentListID {concat($voclib:gcmd_science_valids_variables, '/current')},
107element TermID {escape-uri($parameter/dif:Variable, true())}
108},
[2358]109if (exists($parameter/dif:Detailed_Variable) and $parameter/dif:Detailed_Variable!='') then
[2291]110element dgValidSubterm {
111element dgValidTerm {string($parameter/dif:Detailed_Variable)},
112element dgValidTermID {
113element ParentListID {$voclib:unknown_vocab_id},
114element TermID {escape-uri($parameter/dif:Detailed_Variable, true())}
[2192]115},
[2291]116element ListLevel {4}}
117else (),
118element ListLevel {3}
[2192]119}
[2291]120else (),
121element ListLevel {2}
[2192]122},
[2291]123element ListLevel {1}
124},
125element ListLevel {0}
126},
[2192]127element ParameterName {
128concat (string($parameter/dif:Category), ' > ', string($parameter/dif:Topic), ' > ', string($parameter/dif:Term)),
[2358]129if (exists($parameter/dif:Variable) and $parameter/dif:Variable!='') then
[2192]130concat(' > ', string($parameter/dif:Variable))
131else (),
[2358]132if (exists($parameter/dif:Detailed_Variable) and $parameter/dif:Detailed_Variable!='') then
[2192]133concat(' > ', string($parameter/dif:Detailed_Variable))
134else ()
135},
136element ParameterAbbreviation {
137concat (string($parameter/dif:Category), ' > ', string($parameter/dif:Topic), ' > ', string($parameter/dif:Term)),
[2358]138if (exists($parameter/dif:Variable) and $parameter/dif:Variable!='') then
[2192]139concat(' > ', string($parameter/dif:Variable))
140else (),
[2358]141if (exists($parameter/dif:Detailed_Variable) and $parameter/dif:Detailed_Variable!='') then
[2192]142concat(' > ', string($parameter/dif:Detailed_Variable))
143else ()
144}
145},
146if (exists($DIF/dif:Spatial_Coverage) or exists($DIF/dif:Paleo_Temporal_Coverage) or exists($DIF/dif:Location) or exists($DIF/dif:Temporal_Coverage)) then
147element dgDataCoverage {
148if (exists($DIF/dif:Spatial_Coverage)  or exists($DIF/dif:Location))  then
149element dgSpatialCoverage {
150for $boundingbox in $DIF/dif:Spatial_Coverage[exists(dif:Northernmost_Latitude)
151and exists(dif:Southernmost_Latitude)
152and exists(dif:Easternmost_Longitude)
153and exists(dif:Westernmost_Longitude)]
154return
155element BoundingBox {
156element LimitNorth {data(inputParse:fix-coord($boundingbox/dif:Northernmost_Latitude))},
157element LimitSouth {data(inputParse:fix-coord($boundingbox/dif:Southernmost_Latitude))},
158element LimitWest {data(inputParse:fix-coord($boundingbox/dif:Westernmost_Longitude))},
159element LimitEast {data(inputParse:fix-coord($boundingbox/dif:Easternmost_Longitude))}
160},
161for $location in $DIF/dif:Location
162return
163element dgArea {
164element dgValidTerm {string($location)},
165element dgValidTermID {
166element ParentListID {concat($voclib:gcmd_location_valids, '/current')},
167element TermID {escape-uri($location, true())}
168}
169}
170}
171else (),
[2291]172if (exists($DIF/dif:Temporal_Coverage) or exists($DIF/dif:Paleo_Temporal_Coverage) or exists($DIF/dif:Chronostratigraphic_Unit)) then
[2192]173element dgTemporalCoverage {
174for $temporalcoverage in $DIF/dif:Temporal_Coverage[exists(dif:Start_Date)]
175return
176element DateRange {
177element DateRangeStart {string($temporalcoverage/dif:Start_Date)},
178element DateRangeEnd {string($temporalcoverage/dif:Stop_Date)}
179},
180for $paleotemporalcoverage in $DIF/dif:Paleo_Temporal_Coverage[exists(dif:Paleo_Start_Date)]
181return
182element DateRange {
183element DateRangeStart {string($paleotemporalcoverage/dif:Paleo_Start_Date)},
184element DateRangeEnd {string($paleotemporalcoverage/dif:Paleo_Stop_Date)}
185},
186for $chronostratigraphic in $DIF/dif:Chronostratigraphic_Unit
187return
188element dgChronostratigraphicTerm {
189element dgValidTerm {string($chronostratigraphic)},
190element dgValidTermID {
191element ParentListID {concat($voclib:gcmd_chronostratigraphic_valids, '/current')},
192element TermID {escape-uri($chronostratigraphic, true())}
193}
194}
195}
196else ()
197}
198else ()
199},
200element dgDataRoles {
201if (exists($DIF/dif:Data_Set_Citation/dif:Dataset_Creator)) then
202element dgDataCreator {
203element dgMetadataID {
204element schemeIdentifier {'NDG-B0'},
205element repositoryIdentifier {$input_repository},
206if ($output_local_id != 'Output_LocalID') then
207element localIdentifier {concat('generated_creator-', $output_local_id)}
208else
209element localIdentifier {concat('generated_creator-', escape-uri(string($DIF/dif:Entry_ID), true()))}
210},
211element roleName {'Data Creator'},
212element abbreviation {'Creator'},
213for $creatorID in $DIF/dif:Data_Set_Citation/dif:Dataset_Creator
214return
215element dgRoleHolder {
[2358]216element dgMetadataID {
[2192]217element schemeIdentifier {'NDG-B0'},
218element repositoryIdentifier {$input_repository},
219if ($output_local_id != 'Output_LocalID') then
220element localIdentifier  {escape-uri(concat('generated_orgcit-', string($creatorID), '-', $output_local_id), true())}
221else
222element localIdentifier  {escape-uri(concat('generated_orgcit-', string($creatorID), '-', data($DIF/dif:Entry_ID)), true())}
223},
224element startDate {current-date()}
225}
226}
227else if (exists($DIF/dif:Originating_Center)) then
228element dgDataCreator {
229element dgMetadataID {
230element schemeIdentifier {'NDG-B0'},
231element repositoryIdentifier {$input_repository},
232if ($output_local_id != 'Output_LocalID') then
233element localIdentifier  {escape-uri(concat('generated_creator-', $output_local_id), true())}
234else
235element localIdentifier  {escape-uri(concat('generated_creator-', data($DIF/dif:Entry_ID)), true())}
236},
237element roleName {'Data Creator'},
238element abbreviation {'Creator'},
239for $creatorID in $DIF/dif:Originating_Center
240return
241element dgRoleHolder {
242element dgOrganisationID {
243element schemeIdentifier {'NDG-B0'},
244element repositoryIdentifier {$input_repository},
245if ($output_local_id != 'Output_LocalID') then
246element localIdentifier  {escape-uri(concat('generated_orgcit-', string($creatorID), '-', $output_local_id), true())}
247else
248element localIdentifier  {escape-uri(concat('generated_orgcit-', string($creatorID), '-', data($DIF/dif:Entry_ID)), true())}
249},
250element startDate {current-date()}
251}
252}
253else (),
254element dgDataCurator {
255element dgMetadataID {
256element schemeIdentifier {'NDG-B0'},
257element repositoryIdentifier {$input_repository},
258if ($output_local_id != 'Output_LocalID') then
259element localIdentifier  {escape-uri(concat('generated_curator-', $output_local_id), true())}
260else
261element localIdentifier  {escape-uri(concat('generated_curator-', data($DIF/dif:Entry_ID)), true())}
262},
263element roleName {'Data Curator'},
264element abbreviation {'Curator'},
265element dgRoleHolder {
266element dgOrganisationID {
267element schemeIdentifier {'NDG-B0'},
268element repositoryIdentifier {$input_repository},
269element localIdentifier {$input_repository_local}
270},
271element startDate {current-date()}
272}
273}
274}
275},
276element dgStructuredKeyword {
277element dgValidTerm {'d2b converted record'},
278element dgValidTermID {
279element ParentListID {$voclib:unknown_vocab_id},
280element TermID {'d2b'}
281}
282},
283for $structuredKeywords in $DIF/dif:Keyword
284return
285element dgStructuredKeyword {
286element dgValidTerm {string($structuredKeywords)},
287element dgValidTermID {
[3146]288if ($structuredKeywords='MDIP' or $structuredKeywords='NERC' or $structuredKeywords='NERC_DDC' or $structuredKeywords='DPPP') then
[2201]289element ParentListID {concat($voclib:ndg_data_provider_vocab, '/current')}
[2192]290else
291element ParentListID {$voclib:unknown_vocab_id}
292,
293element TermID {escape-uri($structuredKeywords, true())}
294}
295},
296for $structuredKeywords in $DIF/dif:ISO_Topic_Category
297return
298element dgStructuredKeyword {
299element dgValidTerm {string($structuredKeywords)},
300element dgValidTermID {
301element ParentListID {concat($voclib:iso_topic_list, '/current')},
302element TermID {escape-uri($structuredKeywords, true())}
303}
304},
305if (exists($DIF/dif:DIF_Creation_Date) or exists($DIF/dif:Last_DIF_Revision_Date)) then
306element dgMetadataProvenance {
307if (exists($DIF/dif:DIF_Creation_Date)) then
308element RecordCreation {
309element CreatedDate {
310if (string($DIF/dif:DIF_Creation_Date) castable as xs:date) then
311string($DIF/dif:DIF_Creation_Date) cast as xs:date
312else (current-date())
313},
314element CreatedBy {$input_repository}
315}
316else
317element RecordCreation {
318element CreatedDate {current-date()},
319element CreatedBy {'MOLES Import'}
320},
321if (exists($DIF/dif:Last_DIF_Revision_Date)) then
322element RecordUpdate {
323element UpdateDate {
324if (string($DIF/dif:Last_DIF_Revision_Date) castable as xs:dateTime) then
325string($DIF/dif:Last_DIF_Revision_Date) cast as xs:dateTime
326else (current-dateTime())
327},
328element UpdatedBy {$input_repository}
329}
330else ()
331}
332else ()
333} (: </dgMetadataRecord>:),
334for $creator in distinct-values($DIF/(dif:Data_Set_Citation/dif:Dataset_Creator | dif:Originating_Center))
335return
336element dgOrganisation {
337element dgMetadataID {
338element schemeIdentifier {'NDG-B0'},
339element repositoryIdentifier {$input_repository},
340if ($output_local_id != 'Output_LocalID') then
341element localIdentifier  {escape-uri(concat('generated_orgcit-', string($creator), '-', $output_local_id), true())}
342else
343element localIdentifier  {escape-uri(concat('generated_orgcit-', string($creator), '-', data($DIF/dif:Entry_ID)), true())}
344},
345element name {string($creator)},
346element abbreviation {string($creator)},
347element contactDetails {''}
348}
[1782]349} (:    </dgMetadata> :)
Note: See TracBrowser for help on using the repository browser.