-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathstudy_parser.xslt
496 lines (416 loc) · 17.5 KB
/
study_parser.xslt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
<?xml version="1.0" encoding="UTF-8"?>
<!--
Transforms the elements from the study and documentation description into a flat XML format for
importing into the database for searching
Author: Mehmood Asghar (IHSN)
Version: Feb 2011
Platform: XSL 1.0
License:
Copyright 2010-2011 The World Bank
This program is free software; you can redistribute it and/or modify it under the terms of the
GNU Lesser General Public License as published by the Free Software Foundation; either version
2.1 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU Lesser General Public License for more details.
The full text of the license is available at http://www.gnu.org/copyleft/lesser.html
-->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.w3.org/1999/XSL/Format" xmlns:ddi="http://www.icpsr.umich.edu/DDI" exclude-result-prefixes="ddi">
<xsl:output method="xml" version="1.0" encoding="UTF-8" />
<xsl:variable name="from">></xsl:variable>
<xsl:variable name="to"></xsl:variable>
<xsl:variable name="codebook_id" select="ddi:codeBook/@ID"/>
<xsl:param name="column-seperator" select="'{TAB}'"/>
<xsl:template match="/">
<xsl:element name="study">
<xsl:apply-templates select="ddi:codeBook" />
</xsl:element>
</xsl:template>
<xsl:template match="ddi:codeBook">
<!-- ID -->
<xsl:element name="id">
<xsl:value-of select="normalize-space(//ddi:codeBook/ddi:stdyDscr/ddi:citation/ddi:titlStmt/ddi:IDNo)" />
</xsl:element>
<!-- titl -->
<xsl:element name="titl">
<xsl:apply-templates select="ddi:stdyDscr/ddi:citation/ddi:titlStmt/ddi:titl"/>
</xsl:element>
<!--titlStmt -->
<xsl:element name="titlstmt">
<xsl:apply-templates select="ddi:stdyDscr/ddi:citation/ddi:titlStmt"/>
</xsl:element>
<!-- abbreviation -->
<xsl:element name="abbreviation">
<xsl:apply-templates select="ddi:stdyDscr/ddi:citation/ddi:titlStmt/ddi:altTitl"/>
</xsl:element>
<!-- Collections-->
<xsl:element name="collections">
<xsl:for-each select="//ddi:docDscr/ddi:citation/ddi:rspStmt/ddi:othId">
<xsl:value-of select="normalize-space(.)"/>{BR}
</xsl:for-each>
</xsl:element>
<!-- kind of data -->
<xsl:element name="kindofdata">
<xsl:apply-templates select="ddi:stdyDscr/ddi:stdyInfo/ddi:sumDscr/ddi:dataKind"/>
</xsl:element>
<!-- authEnty -->
<xsl:element name="authenty">
<xsl:call-template name="primary-investigator" />
</xsl:element>
<!-- geogcover -->
<xsl:element name="geogcover">
<xsl:apply-templates select="ddi:stdyDscr/ddi:stdyInfo/ddi:sumDscr/ddi:geogCover"/>
</xsl:element>
<!-- nation -->
<xsl:element name="nation">
<xsl:apply-templates select="ddi:stdyDscr/ddi:stdyInfo/ddi:sumDscr/ddi:nation"/>
</xsl:element>
<!-- topic -->
<xsl:call-template name="topics"/>
<!-- Scope-->
<xsl:element name="scope">
<xsl:apply-templates select="ddi:stdyDscr/ddi:stdyInfo/ddi:notes"/>
</xsl:element>
<!-- Keywords-->
<xsl:element name="keywords">
<!--<xsl:call-template name="keywords"/>-->
<xsl:value-of select="normalize-space(//ddi:docDscr)"/>
<xsl:value-of select="normalize-space(//ddi:stdyDscr)"/>
</xsl:element>
<!--Study type/serName-->
<xsl:element name="sername">
<xsl:value-of select="//ddi:codeBook/ddi:stdyDscr/ddi:citation/ddi:serStmt/ddi:serName" />
</xsl:element>
<!-- producer -->
<xsl:element name="producer">
<xsl:apply-templates select="ddi:docDscr/ddi:citation/ddi:prodStmt/ddi:producer"/>
</xsl:element>
<!-- Reference no -->
<xsl:element name="refno">
<xsl:variable name="refid">
<xsl:choose>
<xsl:when test="normalize-space(ddi:stdyDscr/ddi:citation/ddi:titlStmt/ddi:IDNo )">
<xsl:value-of select="translate(normalize-space(ddi:stdyDscr/ddi:citation/ddi:titlStmt/ddi:IDNo ),$from, $to)"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="@ID"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:value-of select="$refid"/>
</xsl:element>
<!-- prodDate -->
<xsl:element name="proddate">
<xsl:value-of select="substring(normalize-space(ddi:stdyDscr/ddi:stdyInfo/ddi:sumDscr/ddi:collDate[@event='start']/@date),1,4)"/>
</xsl:element>
<!-- Sponsor-->
<xsl:element name="sponsor">
<xsl:apply-templates select="//ddi:stdyDscr/ddi:citation/ddi:prodStmt/ddi:fundAg"/>
</xsl:element>
<xsl:choose>
<!-- when single date is specified, use it for start and end -->
<xsl:when test="normalize-space(ddi:stdyDscr/ddi:stdyInfo/ddi:sumDscr/ddi:collDate[@event='single']/@date)">
<!-- data collection start date -->
<xsl:element name="data_coll_start">
<xsl:value-of select="substring(normalize-space(ddi:stdyDscr/ddi:stdyInfo/ddi:sumDscr/ddi:collDate[@event='single']/@date),1,4)"/>
</xsl:element>
<!-- data collection end date -->
<xsl:element name="data_coll_end">
<xsl:value-of select="substring(normalize-space(ddi:stdyDscr/ddi:stdyInfo/ddi:sumDscr/ddi:collDate[@event='single']/@date),1,4)"/>
</xsl:element>
</xsl:when>
<!--look for start and end dates -->
<xsl:otherwise>
<!-- data collection start date -->
<xsl:element name="data_coll_start">
<xsl:value-of select="substring(normalize-space(ddi:stdyDscr/ddi:stdyInfo/ddi:sumDscr/ddi:collDate[@event='start']/@date),1,4)"/>
</xsl:element>
<!-- data collection end date -->
<xsl:element name="data_coll_end">
<xsl:value-of select="substring(normalize-space(ddi:stdyDscr/ddi:stdyInfo/ddi:sumDscr/ddi:collDate[@event='end'][last()]/@date),1,4)"/>
</xsl:element>
</xsl:otherwise>
</xsl:choose>
<!-- additional fields for dime -->
<!-- project name -->
<xsl:element name="ie_project_name">
<xsl:apply-templates select="//ddi:codeBook/ddi:docDscr/ddi:citation//ddi:titl"/>
</xsl:element>
<!-- project id -->
<xsl:element name="ie_project_id">
<xsl:apply-templates select="//ddi:codeBook/ddi:docDscr/ddi:citation//ddi:subTitl"/>
</xsl:element>
<!-- program -->
<xsl:element name="program">
<xsl:apply-templates select="//ddi:codeBook/ddi:docDscr/ddi:citation//ddi:altTitl"/>
</xsl:element>
<!-- lead researchers/ie_team_leaders -->
<xsl:call-template name="ie_team_leaders"/>
<!-- project name -->
<xsl:element name="project_name">
<xsl:apply-templates select="//ddi:codeBook/ddi:docDscr/ddi:docSrc//ddi:titl"/>
</xsl:element>
<!-- project id -->
<xsl:element name="project_id">
<xsl:apply-templates select="//ddi:codeBook/ddi:docDscr/ddi:docSrc//ddi:subTitl"/>
</xsl:element>
<!-- ie project url -->
<xsl:element name="ie_project_uri">
<xsl:apply-templates select="//ddi:codeBook/ddi:docDscr/ddi:citation/ddi:distStmt/ddi:depositr"/>
</xsl:element>
<!--project url-->
<xsl:element name="project_uri">
<xsl:apply-templates select="//ddi:codeBook/ddi:docDscr/ddi:docSrc/ddi:distStmt/ddi:depositr"/>
</xsl:element>
<!--origArch - center -->
<xsl:element name="center">
<xsl:value-of select="normalize-space(//ddi:codeBook/ddi:stdyDscr/ddi:dataAccs/ddi:setAvail/ddi:origArch)"/>
</xsl:element>
</xsl:template>
<!-- templates -->
<!--get all team leaders-->
<xsl:template name="ie_team_leaders">
<xsl:call-template name="toxml">
<xsl:with-param name="path" select="//ddi:codeBook/ddi:docDscr/ddi:citation//ddi:distStmt/ddi:contact"/>
<xsl:with-param name="name" select="'ie_team_leaders'"/>
<xsl:with-param name="sub-name" select="'leader'"/>
</xsl:call-template>
</xsl:template>
<xsl:template match="ddi:fundAg">
<xsl:variable name="fundAg">
<xsl:call-template name="escape-apos" >
<xsl:with-param name="string" select="normalize-space(concat(., ' - ', @abbr, ' - ' , @role ))"/>
</xsl:call-template>
</xsl:variable>
<xsl:value-of select="$fundAg"/>
<xsl:text><BR /></xsl:text>
</xsl:template>
<xsl:template match="ddi:titl">
<xsl:variable name="subtitl">
<xsl:if test="normalize-space(//ddi:codeBook/ddi:stdyDscr/ddi:citation/ddi:titlStmt/ddi:subTitl)">
<!--
<xsl:call-template name="escape-apos" >
<xsl:with-param name="string" select="normalize-space(//ddi:codeBook/ddi:stdyDscr/ddi:citation/ddi:titlStmt/ddi:subTitl)"/>
</xsl:call-template> -->
<xsl:value-of select="normalize-space(//ddi:codeBook/ddi:stdyDscr/ddi:citation/ddi:titlStmt/ddi:subTitl)"/>
</xsl:if>
</xsl:variable>
<xsl:variable name="titl"><!--
<xsl:call-template name="escape-apos" >
<xsl:with-param name="string" select="normalize-space(.)"/>
</xsl:call-template>-->
<xsl:value-of select="normalize-space(.)"/>
</xsl:variable>
<xsl:value-of select="$titl"/>
<xsl:if test="normalize-space($subtitl)">
<xsl:value-of select="concat(', ', $subtitl)" />
</xsl:if>
</xsl:template>
<xsl:template match="ddi:subTitl">
<!--
<xsl:call-template name="escape-apos" >
<xsl:with-param name="string" select="normalize-space(.)"/>
</xsl:call-template>-->
<xsl:value-of select="normalize-space(.)"/>
</xsl:template>
<xsl:template match="ddi:titlStmt">
<!--
<xsl:call-template name="escape-apos" >
<xsl:with-param name="string" select="normalize-space(//ddi:codeBook/ddi:stdyDscr/ddi:citation/ddi:titlStmt[*/@* | *])"/>
</xsl:call-template>-->
<xsl:value-of select="normalize-space(//ddi:codeBook/ddi:stdyDscr/ddi:citation/ddi:titlStmt[*/@* | *])"/>
</xsl:template>
<xsl:template match="ddi:AuthEnty">
<!--
<xsl:call-template name="escape-apos" >
<xsl:with-param name="string" select="normalize-space(.)"/>
</xsl:call-template>-->
<xsl:value-of select="normalize-space(.)"/>
</xsl:template>
<xsl:template match="ddi:geogCover">
<xsl:call-template name="escape-apos" >
<xsl:with-param name="string" select="substring(normalize-space(.),1,100)"/>
</xsl:call-template>
</xsl:template>
<xsl:template match="ddi:nation">
<xsl:call-template name="escape-apos" >
<xsl:with-param name="string" select="normalize-space(.)"/>
</xsl:call-template>
{BR}
</xsl:template>
<xsl:template match="ddi:topcClas"><!--
<xsl:call-template name="escape-apos" >
<xsl:with-param name="string" select="normalize-space(.)"/>
</xsl:call-template>-->
<xsl:value-of select="normalize-space(.)"/>
</xsl:template>
<xsl:template name="topics">
<!--get topics as xml elements-->
<xsl:call-template name="toxml">
<xsl:with-param name="path" select="//ddi:stdyDscr/ddi:stdyInfo//ddi:topcClas"/>
<xsl:with-param name="name" select="'topics'"/>
<xsl:with-param name="sub-name" select="'topic'"/>
</xsl:call-template>
<!--topics as plain text-->
<!--
<xsl:for-each select="//ddi:stdyDscr/ddi:stdyInfo//ddi:topcClas">
<xsl:value-of select="normalize-space(.)"/><xsl:text> </xsl:text>
</xsl:for-each>
-->
</xsl:template>
<xsl:template name="keywords">
<xsl:for-each select="//ddi:stdyDscr/ddi:stdyInfo/ddi:subject/ddi:keyword">
<xsl:text> </xsl:text><xsl:value-of select="normalize-space(.)"/> <xsl:text> </xsl:text>
</xsl:for-each>
</xsl:template>
<xsl:template match="ddi:notes"><!--
<xsl:call-template name="escape-apos" >
<xsl:with-param name="string" select="normalize-space(.)"/>
</xsl:call-template>-->
<xsl:value-of select="normalize-space(.)"/>
</xsl:template>
<xsl:template match="ddi:serName">
<xsl:call-template name="escape-apos" >
<xsl:with-param name="string" select="normalize-space(.)"/>
</xsl:call-template>
</xsl:template>
<xsl:template match="ddi:producer"> <!--
<xsl:call-template name="escape-apos" >
<xsl:with-param name="string" select="normalize-space(.)"/>
</xsl:call-template>-->
<xsl:value-of select="normalize-space(.)"/>
</xsl:template>
<!-- primary investigator - returns the first one -->
<xsl:template name="primary-investigator">
<xsl:variable name="pi">
<xsl:for-each select="//ddi:codeBook/ddi:stdyDscr/ddi:citation/ddi:rspStmt/ddi:AuthEnty">
<xsl:if test="normalize-space(.)">
<xsl:choose>
<!--name with affiliation-->
<xsl:when test="normalize-space(@affiliation)">
<xsl:value-of select="normalize-space(.)"/> - <xsl:value-of select="@affiliation"/>{BR}
</xsl:when>
<!--without affiliation-->
<xsl:otherwise>
<xsl:value-of select="normalize-space(.)"/>{BR}
</xsl:otherwise>
</xsl:choose>
</xsl:if>
</xsl:for-each>
</xsl:variable>
<xsl:value-of select="normalize-space($pi)"/>
<!--
<xsl:variable name="pi">
<xsl:value-of select="normalize-space(//ddi:codeBook/ddi:stdyDscr/ddi:citation/ddi:rspStmt/ddi:AuthEnty)"/>
</xsl:variable>
<xsl:choose>
<xsl:when test="normalize-space(//ddi:codeBook/ddi:stdyDscr/ddi:citation/ddi:rspStmt/ddi:AuthEnty/@affiliation)">
<xsl:variable name="affiliation">
<xsl:value-of select="normalize-space(//ddi:codeBook/ddi:stdyDscr/ddi:citation/ddi:rspStmt/ddi:AuthEnty/@affiliation)"/>
</xsl:variable>
<xsl:value-of select="concat($pi, ', ',$affiliation ) " />
</xsl:when>
<xsl:otherwise>
<xsl:variable name="auth">
<xsl:value-of select="normalize-space(//ddi:codeBook/ddi:stdyDscr/ddi:citation/ddi:rspStmt/ddi:AuthEnty)"/>
</xsl:variable>
<xsl:value-of select="normalize-space($auth)"/>
</xsl:otherwise>
</xsl:choose>
-->
</xsl:template>
<xsl:template name="search-and-replace">
<xsl:param name="input"/>
<xsl:param name="search-string"/>
<xsl:param name="replace-string"/>
<xsl:choose>
<!-- See if the input contains the search string -->
<xsl:when test="$search-string and
contains($input,$search-string)">
<!-- If so, then concatenate the substring before the search
string to the replacement string and to the result of
recursively applying this template to the remaining substring.
-->
<xsl:value-of
select="substring-before($input,$search-string)"/>
<xsl:value-of select="$replace-string"/>
<xsl:call-template name="search-and-replace">
<xsl:with-param name="input"
select="substring-after($input,$search-string)"/>
<xsl:with-param name="search-string"
select="$search-string"/>
<xsl:with-param name="replace-string"
select="$replace-string"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<!-- There are no more occurrences of the search string so
just return the current input string -->
<xsl:value-of select="$input"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<!-- replace function -->
<xsl:template name="replace-string">
<xsl:param name="text"/>
<xsl:param name="replace"/>
<xsl:param name="with"/>
<xsl:choose>
<xsl:when test="contains($text,$replace)">
<xsl:value-of select="substring-before($text,$replace)"/>
<xsl:value-of select="$with"/>
<xsl:call-template name="replace-string">
<xsl:with-param name="text"
select="substring-after($text,$replace)"/>
<xsl:with-param name="replace" select="$replace"/>
<xsl:with-param name="with" select="$with"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$text"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template name="escape-apos">
<xsl:param name="string" />
<xsl:choose>
<xsl:when test='contains($string, "'")'>
<xsl:value-of select='substring-before($string, "'")' />
<xsl:text>'</xsl:text>
<xsl:call-template name="escape-apos">
<xsl:with-param name="string"
select='substring-after($string, "'")' />
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$string" />
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template name="toxml">
<xsl:param name="path"/>
<xsl:param name="name"/>
<xsl:param name="sub-name"/>
<xsl:element name="{$name}">
<xsl:for-each select="$path">
<xsl:element name="{$sub-name}">
<name><xsl:value-of select="normalize-space(.)"/></name>
<xsl:if test="@*">
<xsl:for-each select="@*">
<xsl:element name="{name()}">
<xsl:value-of select="." />
</xsl:element>
</xsl:for-each>
</xsl:if>
<xsl:if test="*">
<xsl:for-each select="*">
<xsl:element name="{name()}">
<xsl:value-of select="." />
</xsl:element>
</xsl:for-each>
</xsl:if>
</xsl:element>
</xsl:for-each>
</xsl:element>
</xsl:template>
</xsl:stylesheet>