Merge remote-tracking branch 'kc/new/bug_5449' into kcmaster
[koha.git] / etc / zebradb / marc_defs / marc21 / authorities / koha-indexdefs-to-zebra.xsl
1 <?xml version='1.0'?>
2 <xsl:stylesheet version="1.0" 
3                 xmlns:marc="http://www.loc.gov/MARC21/slim" 
4                 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
5                 xmlns:xslo="http://www.w3.org/1999/XSL/TransformAlias"
6                 xmlns:z="http://indexdata.com/zebra-2.0"
7                 xmlns:kohaidx="http://www.koha-community.org/schemas/index-defs">
8
9     <xsl:namespace-alias stylesheet-prefix="xslo" result-prefix="xsl"/>
10     <xsl:output indent="yes" method="xml" version="1.0" encoding="UTF-8"/>
11     <!-- disable all default text node output -->
12     <xsl:template match="text()"/>
13
14     <!-- Keys on tags referenced in the index definitions -->
15     <xsl:key name="index_control_field_tag"   match="kohaidx:index_control_field"   use="@tag"/>
16     <xsl:key name="index_subfields_tag" match="kohaidx:index_subfields" use="@tag"/>
17     <xsl:key name="index_heading_tag"   match="kohaidx:index_heading"   use="@tag"/>
18     <xsl:key name="index_match_heading_tag" match="kohaidx:index_match_heading" use="@tag"/>
19
20     <xsl:template match="kohaidx:index_defs">
21         <xslo:stylesheet version="1.0">
22             <xslo:output indent="yes" method="xml" version="1.0" encoding="UTF-8"/>
23             <xslo:template match="text()"/>
24             <xslo:template match="text()" mode="index_subfields"/>
25             <xslo:template match="text()" mode="index_heading"/>
26             <xslo:template match="text()" mode="index_match_heading"/>
27             <xslo:template match="text()" mode="index_subject_thesaurus"/>
28             <xslo:template match="/">
29                 <xslo:if test="marc:collection">
30                     <collection>
31                         <xslo:apply-templates select="marc:collection/marc:record"/>
32                     </collection>
33                 </xslo:if>
34                 <xslo:if test="marc:record">
35                     <xslo:apply-templates select="marc:record"/>
36                 </xslo:if>
37             </xslo:template>
38
39             <xslo:template match="marc:record">
40                 <xslo:variable name="controlField001" select="normalize-space(marc:controlfield[@tag='001'])"/>
41                 <z:record type="update">
42                     <xslo:attribute name="z:id"><xslo:value-of select="$controlField001"/></xslo:attribute>
43                     <xslo:apply-templates/>
44                     <xslo:apply-templates mode="index_subfields"/>
45                     <xslo:apply-templates mode="index_heading"/>
46                     <xslo:apply-templates mode="index_match_heading"/>
47                     <xslo:apply-templates mode="index_subject_thesaurus"/>
48                 </z:record>
49             </xslo:template>
50
51             <xsl:call-template name="handle-index-leader"/>
52             <xsl:call-template name="handle-index-control-field"/>
53             <xsl:call-template name="handle-index-subfields"/>
54             <xsl:call-template name="handle-index-heading"/>
55             <xsl:call-template name="handle-index-match-heading"/>
56             <xsl:apply-templates/>
57             <xslo:template match="*">
58                 <z:index name="Any:w Any:p">
59                     <xslo:value-of select="."/>
60                 </z:index>
61             </xslo:template>
62         </xslo:stylesheet>
63     </xsl:template>
64
65     <!-- map kohaidx:var to stylesheet variables -->
66     <xsl:template match="kohaidx:var">
67         <xslo:variable>
68             <xsl:attribute name="name"><xsl:value-of select="@name"/></xsl:attribute>
69             <xsl:value-of select="."/>
70         </xslo:variable>
71     </xsl:template>
72
73     <xsl:template match="kohaidx:index_subject_thesaurus">   
74         <xsl:variable name="tag"><xsl:value-of select="@tag"/></xsl:variable>
75         <xsl:variable name="offset"><xsl:value-of select="@offset"/></xsl:variable>
76         <xsl:variable name="length"><xsl:value-of select="@length"/></xsl:variable>
77         <xsl:variable name="detail_tag"><xsl:value-of select="@detail_tag"/></xsl:variable>
78         <xsl:variable name="detail_subfields"><xsl:value-of select="@detail_subfields"/></xsl:variable>
79         <xsl:variable name="indexes">
80             <xsl:call-template name="get-target-indexes"/>
81         </xsl:variable>
82         <xslo:template mode="index_subject_thesaurus">
83             <xsl:attribute name="match">
84                 <xsl:text>marc:controlfield[@tag='</xsl:text>
85                 <xsl:value-of select="$tag"/>
86                 <xsl:text>']</xsl:text>
87             </xsl:attribute>
88             <xslo:variable name="thesaurus_code1">
89                 <xsl:attribute name="select">
90                     <xsl:text>substring(., </xsl:text>
91                     <xsl:value-of select="$offset + 1" />
92                     <xsl:text>, </xsl:text>
93                     <xsl:value-of select="$length" />
94                     <xsl:text>)</xsl:text>
95                 </xsl:attribute>
96             </xslo:variable>
97             <xsl:variable name="alt_select">
98                 <xsl:text>//marc:datafield[@tag='</xsl:text>
99                 <xsl:value-of select="$detail_tag"/>
100                 <xsl:text>']/marc:subfield[@code='</xsl:text>
101                 <xsl:value-of select="$detail_subfields"/>
102                 <xsl:text>']</xsl:text>
103             </xsl:variable>
104             <xslo:variable name="full_thesaurus_code">
105                 <xslo:choose>
106                     <xslo:when test="$thesaurus_code1 = 'a'"><xslo:text>lcsh</xslo:text></xslo:when>
107                     <xslo:when test="$thesaurus_code1 = 'b'"><xslo:text>lcac</xslo:text></xslo:when>
108                     <xslo:when test="$thesaurus_code1 = 'c'"><xslo:text>mesh</xslo:text></xslo:when>
109                     <xslo:when test="$thesaurus_code1 = 'd'"><xslo:text>nal</xslo:text></xslo:when>
110                     <xslo:when test="$thesaurus_code1 = 'k'"><xslo:text>cash</xslo:text></xslo:when>
111                     <xslo:when test="$thesaurus_code1 = 'n'"><xslo:text>notapplicable</xslo:text></xslo:when>
112                     <xslo:when test="$thesaurus_code1 = 'r'"><xslo:text>aat</xslo:text></xslo:when>
113                     <xslo:when test="$thesaurus_code1 = 's'"><xslo:text>sears</xslo:text></xslo:when>
114                     <xslo:when test="$thesaurus_code1 = 'v'"><xslo:text>rvm</xslo:text></xslo:when>
115                     <xslo:when test="$thesaurus_code1 = 'z'">
116                         <xslo:choose>
117                             <xslo:when>
118                                 <xsl:attribute name="test"><xsl:value-of select="$alt_select"/></xsl:attribute>
119                                 <xslo:value-of>
120                                     <xsl:attribute name="select"><xsl:value-of select="$alt_select"/></xsl:attribute>
121                                 </xslo:value-of>
122                             </xslo:when>
123                             <xslo:otherwise><xslo:text>notdefined</xslo:text></xslo:otherwise>
124                         </xslo:choose>
125                     </xslo:when>
126                     <xslo:otherwise><xslo:text>notdefined</xslo:text></xslo:otherwise>
127                 </xslo:choose>
128             </xslo:variable>
129             <z:index>
130                 <xsl:attribute name="name"><xsl:value-of select="normalize-space($indexes)"/></xsl:attribute>
131                 <xslo:value-of select="$full_thesaurus_code"/>
132             </z:index>
133         </xslo:template>
134     </xsl:template>
135
136     <xsl:template name="handle-index-leader">
137         <xsl:if test="kohaidx:index_leader">
138             <xslo:template match="marc:leader">
139                 <xsl:apply-templates select="kohaidx:index_leader" mode="secondary"/>
140             </xslo:template>
141         </xsl:if>
142     </xsl:template>
143
144     <xsl:template match="kohaidx:index_leader" mode="secondary">
145         <xsl:variable name="offset"><xsl:value-of select="@offset"/></xsl:variable>
146         <xsl:variable name="length"><xsl:value-of select="@length"/></xsl:variable>
147         <xsl:variable name="indexes">
148             <xsl:call-template name="get-target-indexes"/>
149         </xsl:variable>
150         <z:index>
151             <xsl:attribute name="name"><xsl:value-of select="normalize-space($indexes)"/></xsl:attribute>
152             <xslo:value-of>
153                 <xsl:attribute name="select">
154                     <xsl:text>substring(., </xsl:text>
155                     <xsl:value-of select="$offset + 1" />
156                     <xsl:text>, </xsl:text>
157                     <xsl:value-of select="$length" />
158                     <xsl:text>)</xsl:text>
159                 </xsl:attribute>
160             </xslo:value-of>
161         </z:index>
162     </xsl:template>
163
164     <xsl:template name="handle-index-control-field">
165         <xsl:for-each select="//kohaidx:index_control_field[generate-id() = generate-id(key('index_control_field_tag', @tag)[1])]">
166             <xslo:template>
167                 <xsl:attribute name="match">
168                     <xsl:text>marc:controlfield[@tag='</xsl:text>
169                     <xsl:value-of select="@tag"/>
170                     <xsl:text>']</xsl:text>
171                 </xsl:attribute>
172                 <xsl:for-each select="key('index_control_field_tag', @tag)">
173                     <xsl:call-template name="handle-one-index-control-field"/>
174                 </xsl:for-each>
175             </xslo:template>
176         </xsl:for-each>
177     </xsl:template>
178
179     <xsl:template name="handle-one-index-control-field">
180         <xsl:variable name="offset"><xsl:value-of select="@offset"/></xsl:variable>
181         <xsl:variable name="length"><xsl:value-of select="@length"/></xsl:variable>
182         <xsl:variable name="indexes">
183             <xsl:call-template name="get-target-indexes"/>
184         </xsl:variable>
185         <z:index>
186             <xsl:attribute name="name"><xsl:value-of select="normalize-space($indexes)"/></xsl:attribute>
187             <xslo:value-of>
188                 <xsl:attribute name="select">
189                     <xsl:choose>
190                         <xsl:when test="@length">
191                             <xsl:text>substring(., </xsl:text>
192                             <xsl:value-of select="$offset + 1" />
193                             <xsl:text>, </xsl:text>
194                             <xsl:value-of select="$length"/>
195                             <xsl:text>)</xsl:text>
196                         </xsl:when>
197                         <xsl:otherwise>
198                             <xsl:text>.</xsl:text>
199                         </xsl:otherwise>
200                     </xsl:choose>
201                 </xsl:attribute>
202             </xslo:value-of>
203         </z:index>
204     </xsl:template>
205
206     <xsl:template name="handle-index-subfields">
207         <xsl:for-each select="//kohaidx:index_subfields[generate-id() = generate-id(key('index_subfields_tag', @tag)[1])]">
208             <xslo:template mode="index_subfields">
209                 <xsl:attribute name="match">
210                     <xsl:text>marc:datafield[@tag='</xsl:text>
211                     <xsl:value-of select="@tag"/>
212                     <xsl:text>']</xsl:text>
213                 </xsl:attribute>
214                 <xsl:for-each select="key('index_subfields_tag', @tag)">
215                     <xsl:call-template name="handle-one-index-subfields"/>
216                 </xsl:for-each>
217             </xslo:template>
218         </xsl:for-each>
219     </xsl:template>
220
221     <xsl:template name="handle-one-index-subfields">
222         <xsl:variable name="indexes">
223             <xsl:call-template name="get-target-indexes"/>
224         </xsl:variable>
225             <xslo:for-each select="marc:subfield">
226                 <xslo:if>
227                     <xsl:attribute name="test">
228                         <xsl:text>contains('</xsl:text>
229                         <xsl:value-of select="@subfields"/>
230                         <xsl:text>', @code)</xsl:text>
231                     </xsl:attribute>
232                     <z:index>
233                         <xsl:attribute name="name"><xsl:value-of select="normalize-space($indexes)"/></xsl:attribute>
234                         <xslo:value-of select="."/>
235                     </z:index>
236                 </xslo:if>
237             </xslo:for-each>
238     </xsl:template>
239
240     <xsl:template name="handle-index-heading">
241         <xsl:for-each select="//kohaidx:index_heading[generate-id() = generate-id(key('index_heading_tag', @tag)[1])]">
242             <xslo:template mode="index_heading">
243                 <xsl:attribute name="match">
244                     <xsl:text>marc:datafield[@tag='</xsl:text>
245                     <xsl:value-of select="@tag"/>
246                     <xsl:text>']</xsl:text>
247                 </xsl:attribute>
248                 <xsl:for-each select="key('index_heading_tag', @tag)">
249                     <xsl:call-template name="handle-one-index-heading"/>
250                 </xsl:for-each>
251             </xslo:template>
252         </xsl:for-each>
253     </xsl:template>
254
255     <xsl:template name="handle-one-index-heading">
256         <xsl:variable name="indexes">
257             <xsl:call-template name="get-target-indexes"/>
258         </xsl:variable>
259         <z:index>
260             <xsl:attribute name="name"><xsl:value-of select="normalize-space($indexes)"/></xsl:attribute>
261             <xslo:variable name="raw_heading">
262                 <xslo:for-each select="marc:subfield">
263                     <xslo:if>
264                         <xsl:attribute name="test">
265                             <xsl:text>contains('</xsl:text>
266                             <xsl:value-of select="@subfields"/>
267                             <xsl:text>', @code)</xsl:text>
268                         </xsl:attribute>
269                         <xsl:attribute name="name"><xsl:value-of select="normalize-space($indexes)"/></xsl:attribute>
270                         <xslo:if test="position() > 1">
271                             <xslo:choose>
272                                 <xslo:when>
273                                     <xsl:attribute name="test">
274                                         <xsl:text>contains('</xsl:text>
275                                         <xsl:value-of select="@subdivisions"/>
276                                         <xsl:text>', @code)</xsl:text>
277                                     </xsl:attribute>
278                                     <xslo:text>--</xslo:text>
279                                 </xslo:when>
280                                 <xslo:otherwise>
281                                     <xslo:value-of select="substring(' ', 1, 1)"/> <!-- FIXME surely there's a better way  to specify a space -->
282                                 </xslo:otherwise>
283                             </xslo:choose>
284                         </xslo:if>
285                         <xslo:value-of select="."/>
286                     </xslo:if>
287                 </xslo:for-each>
288             </xslo:variable>
289             <xslo:value-of select="normalize-space($raw_heading)"/>
290         </z:index>
291     </xsl:template>
292
293     <xsl:template name="handle-index-match-heading">
294         <xsl:for-each select="//kohaidx:index_match_heading[generate-id() = generate-id(key('index_match_heading_tag', @tag)[1])]">
295             <xslo:template mode="index_match_heading">
296                 <xsl:attribute name="match">
297                     <xsl:text>marc:datafield[@tag='</xsl:text>
298                     <xsl:value-of select="@tag"/>
299                     <xsl:text>']</xsl:text>
300                 </xsl:attribute>
301                 <xsl:for-each select="key('index_match_heading_tag', @tag)">
302                     <xsl:call-template name="handle-one-index-match-heading"/>
303                 </xsl:for-each>
304             </xslo:template>
305         </xsl:for-each>
306     </xsl:template>
307
308     <xsl:template name="handle-one-index-match-heading">
309         <xsl:variable name="indexes">
310             <xsl:call-template name="get-target-indexes"/>
311         </xsl:variable>
312         <z:index>
313             <xsl:attribute name="name"><xsl:value-of select="normalize-space($indexes)"/></xsl:attribute>
314             <xslo:variable name="raw_heading">
315                 <xslo:for-each select="marc:subfield">
316                     <xslo:if>
317                         <xsl:attribute name="test">
318                             <xsl:text>contains('</xsl:text>
319                             <xsl:value-of select="@subfields"/>
320                             <xsl:text>', @code)</xsl:text>
321                         </xsl:attribute>
322                         <xsl:attribute name="name"><xsl:value-of select="normalize-space($indexes)"/></xsl:attribute>
323                         <xslo:if test="position() > 1">
324                             <xslo:choose>
325                                 <xslo:when>
326                                     <xsl:attribute name="test">
327                                         <xsl:text>contains('</xsl:text>
328                                         <xsl:value-of select="@subdivisions"/>
329                                         <xsl:text>', @code)</xsl:text>
330                                     </xsl:attribute>
331                                     <xslo:choose>
332                                         <xslo:when>
333                                             <xsl:attribute name="test">
334                                                 <xsl:text>@code = $general_subdivision_subfield</xsl:text>
335                                             </xsl:attribute>
336                                             <xslo:text> generalsubdiv </xslo:text>
337                                         </xslo:when>
338                                         <xslo:when>
339                                             <xsl:attribute name="test">
340                                                 <xsl:text>@code = $form_subdivision_subfield</xsl:text>
341                                             </xsl:attribute>
342                                             <xslo:text> formsubdiv </xslo:text>
343                                         </xslo:when>
344                                         <xslo:when>
345                                             <xsl:attribute name="test">
346                                                 <xsl:text>@code = $chronological_subdivision_subfield</xsl:text>
347                                             </xsl:attribute>
348                                             <xslo:text> chronologicalsubdiv </xslo:text>
349                                         </xslo:when>
350                                         <xslo:when>
351                                             <xsl:attribute name="test">
352                                                 <xsl:text>@code = $geographic_subdivision_subfield</xsl:text>
353                                             </xsl:attribute>
354                                             <xslo:text> geographicsubdiv </xslo:text>
355                                         </xslo:when>
356                                     </xslo:choose>
357                                 </xslo:when>
358                                 <xslo:otherwise>
359                                     <xslo:value-of select="substring(' ', 1, 1)"/> <!-- FIXME surely there's a better way  to specify a space -->
360                                 </xslo:otherwise>
361                             </xslo:choose>
362                         </xslo:if>
363                         <xslo:value-of select="."/>
364                     </xslo:if>
365                 </xslo:for-each>
366             </xslo:variable>
367             <xslo:value-of select="normalize-space($raw_heading)"/>
368         </z:index>
369     </xsl:template>
370
371     <xsl:template name="get-target-indexes">
372         <xsl:for-each select="kohaidx:target_index">
373             <xsl:value-of select="." /><xsl:text> </xsl:text>
374         </xsl:for-each>
375     </xsl:template>
376 </xsl:stylesheet>