Bug 7475: Update configuration
[koha.git] / etc / zebradb / xsl / 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_data_field_tag"   match="kohaidx:index_data_field"   use="@tag"/>
19     <xsl:key name="index_heading_conditional_tag" match="kohaidx:index_heading_conditional" use="@tag"/>
20     <xsl:key name="index_match_heading_tag" match="kohaidx:index_match_heading" use="@tag"/>
21
22     <xsl:template match="kohaidx:index_defs">
23     <xsl:comment>
24 This file has been automatically generated from a Koha index definition file
25 with the stylesheet koha-indexdefs-to-zebra.xsl. Do not manually edit this file,
26 as it may be overwritten. To regenerate, edit the appropriate Koha index
27 definition file (probably something like {biblio,authority}-koha-indexdefs.xml) and run:
28 `xsltproc koha-indexdefs-to-zebra.xsl {biblio,authority}-koha-indexdefs.xml >
29 {biblio,authority}-zebra-indexdefs.xsl` (substituting the appropriate file names).
30 </xsl:comment>
31         <xslo:stylesheet version="1.0">
32             <xslo:output indent="yes" method="xml" version="1.0" encoding="UTF-8"/>
33             <xslo:template match="text()"/>
34             <xslo:template match="text()" mode="index_subfields"/>
35             <xslo:template match="text()" mode="index_data_field"/>
36             <xslo:template match="text()" mode="index_heading"/>
37             <xslo:template match="text()" mode="index_heading_conditional"/>
38             <xslo:template match="text()" mode="index_match_heading"/>
39             <xslo:template match="text()" mode="index_subject_thesaurus"/>
40             <xslo:template match="/">
41                 <xslo:if test="marc:collection">
42                     <collection>
43                         <xslo:apply-templates select="marc:collection/marc:record"/>
44                     </collection>
45                 </xslo:if>
46                 <xslo:if test="marc:record">
47                     <xslo:apply-templates select="marc:record"/>
48                 </xslo:if>
49             </xslo:template>
50
51             <xslo:template match="marc:record">
52                 <xslo:variable name="controlField001" select="normalize-space(marc:controlfield[@tag='001'])"/>
53                 <z:record type="update">
54                     <xslo:attribute name="z:id"><xslo:value-of select="$controlField001"/></xslo:attribute>
55                     <xslo:apply-templates/>
56                     <xslo:apply-templates mode="index_subfields"/>
57                     <xslo:apply-templates mode="index_data_field"/>
58                     <xslo:apply-templates mode="index_heading"/>
59                     <xslo:apply-templates mode="index_heading_conditional"/>
60                     <xslo:apply-templates mode="index_match_heading"/>
61                     <xslo:apply-templates mode="index_subject_thesaurus"/>
62                 </z:record>
63             </xslo:template>
64
65             <xsl:call-template name="handle-index-leader"/>
66             <xsl:call-template name="handle-index-control-field"/>
67             <xsl:call-template name="handle-index-subfields"/>
68             <xsl:call-template name="handle-index-data-field"/>
69             <xsl:call-template name="handle-index-heading"/>
70             <xsl:call-template name="handle-index-heading-conditional"/>
71             <xsl:call-template name="handle-index-match-heading"/>
72             <xsl:apply-templates/>
73             <xslo:template match="*">
74                 <z:index name="Any:w Any:p">
75                     <xslo:value-of select="."/>
76                 </z:index>
77             </xslo:template>
78             <xslo:template name="chopPunctuation">
79             <xslo:param name="chopString"/>
80                 <xslo:variable name="length" select="string-length($chopString)"/>
81                 <xslo:choose>
82                 <xslo:when test="$length=0"/>
83                 <xslo:when test="contains('-,.:=;!%/', substring($chopString,$length,1))">
84                     <xslo:call-template name="chopPunctuation">
85                     <xslo:with-param name="chopString" select="substring($chopString,1,$length - 1)"/>
86                     </xslo:call-template>
87                 </xslo:when>
88                 <xslo:when test="not($chopString)"/>
89                 <xslo:otherwise><xslo:value-of select="$chopString"/></xslo:otherwise>
90                 </xslo:choose>
91                 <xslo:text> </xslo:text>
92             </xslo:template>
93         </xslo:stylesheet>
94     </xsl:template>
95
96     <!-- map kohaidx:var to stylesheet variables -->
97     <xsl:template match="kohaidx:var">
98         <xslo:variable>
99             <xsl:attribute name="name"><xsl:value-of select="@name"/></xsl:attribute>
100             <xsl:value-of select="."/>
101         </xslo:variable>
102     </xsl:template>
103
104     <xsl:template match="kohaidx:index_subject_thesaurus">   
105         <xsl:variable name="tag"><xsl:value-of select="@tag"/></xsl:variable>
106         <xsl:variable name="offset"><xsl:value-of select="@offset"/></xsl:variable>
107         <xsl:variable name="length"><xsl:value-of select="@length"/></xsl:variable>
108         <xsl:variable name="detail_tag"><xsl:value-of select="@detail_tag"/></xsl:variable>
109         <xsl:variable name="detail_subfields"><xsl:value-of select="@detail_subfields"/></xsl:variable>
110         <xsl:variable name="indexes">
111             <xsl:call-template name="get-target-indexes"/>
112         </xsl:variable>
113         <xslo:template mode="index_subject_thesaurus">
114             <xsl:attribute name="match">
115                 <xsl:text>marc:controlfield[@tag='</xsl:text>
116                 <xsl:value-of select="$tag"/>
117                 <xsl:text>']</xsl:text>
118             </xsl:attribute>
119             <xslo:variable name="thesaurus_code1">
120                 <xsl:attribute name="select">
121                     <xsl:text>substring(., </xsl:text>
122                     <xsl:value-of select="$offset + 1" />
123                     <xsl:text>, </xsl:text>
124                     <xsl:value-of select="$length" />
125                     <xsl:text>)</xsl:text>
126                 </xsl:attribute>
127             </xslo:variable>
128             <xsl:variable name="alt_select">
129                 <xsl:text>//marc:datafield[@tag='</xsl:text>
130                 <xsl:value-of select="$detail_tag"/>
131                 <xsl:text>']/marc:subfield[@code='</xsl:text>
132                 <xsl:value-of select="$detail_subfields"/>
133                 <xsl:text>']</xsl:text>
134             </xsl:variable>
135             <xslo:variable name="full_thesaurus_code">
136                 <xslo:choose>
137                     <xslo:when test="$thesaurus_code1 = 'a'"><xslo:text>lcsh</xslo:text></xslo:when>
138                     <xslo:when test="$thesaurus_code1 = 'b'"><xslo:text>lcac</xslo:text></xslo:when>
139                     <xslo:when test="$thesaurus_code1 = 'c'"><xslo:text>mesh</xslo:text></xslo:when>
140                     <xslo:when test="$thesaurus_code1 = 'd'"><xslo:text>nal</xslo:text></xslo:when>
141                     <xslo:when test="$thesaurus_code1 = 'k'"><xslo:text>cash</xslo:text></xslo:when>
142                     <xslo:when test="$thesaurus_code1 = 'n'"><xslo:text>notapplicable</xslo:text></xslo:when>
143                     <xslo:when test="$thesaurus_code1 = 'r'"><xslo:text>aat</xslo:text></xslo:when>
144                     <xslo:when test="$thesaurus_code1 = 's'"><xslo:text>sears</xslo:text></xslo:when>
145                     <xslo:when test="$thesaurus_code1 = 'v'"><xslo:text>rvm</xslo:text></xslo:when>
146                     <xslo:when test="$thesaurus_code1 = 'z'">
147                         <xslo:choose>
148                             <xslo:when>
149                                 <xsl:attribute name="test"><xsl:value-of select="$alt_select"/></xsl:attribute>
150                                 <xslo:value-of>
151                                     <xsl:attribute name="select"><xsl:value-of select="$alt_select"/></xsl:attribute>
152                                 </xslo:value-of>
153                             </xslo:when>
154                             <xslo:otherwise><xslo:text>notdefined</xslo:text></xslo:otherwise>
155                         </xslo:choose>
156                     </xslo:when>
157                     <xslo:otherwise><xslo:text>notdefined</xslo:text></xslo:otherwise>
158                 </xslo:choose>
159             </xslo:variable>
160             <z:index>
161                 <xsl:attribute name="name"><xsl:value-of select="normalize-space($indexes)"/></xsl:attribute>
162                 <xslo:value-of select="$full_thesaurus_code"/>
163             </z:index>
164         </xslo:template>
165     </xsl:template>
166
167     <xsl:template name="handle-index-leader">
168         <xsl:if test="kohaidx:index_leader">
169             <xslo:template match="marc:leader">
170                 <xsl:apply-templates select="kohaidx:index_leader" mode="secondary"/>
171             </xslo:template>
172         </xsl:if>
173     </xsl:template>
174
175     <xsl:template match="kohaidx:index_leader" mode="secondary">
176         <xsl:variable name="offset"><xsl:value-of select="@offset"/></xsl:variable>
177         <xsl:variable name="length"><xsl:value-of select="@length"/></xsl:variable>
178         <xsl:variable name="indexes">
179             <xsl:call-template name="get-target-indexes"/>
180         </xsl:variable>
181         <z:index>
182             <xsl:attribute name="name"><xsl:value-of select="normalize-space($indexes)"/></xsl:attribute>
183             <xslo:value-of>
184                 <xsl:attribute name="select">
185                     <xsl:text>substring(., </xsl:text>
186                     <xsl:value-of select="$offset + 1" />
187                     <xsl:text>, </xsl:text>
188                     <xsl:value-of select="$length" />
189                     <xsl:text>)</xsl:text>
190                 </xsl:attribute>
191             </xslo:value-of>
192         </z:index>
193     </xsl:template>
194
195     <xsl:template name="handle-index-control-field">
196         <xsl:for-each select="//kohaidx:index_control_field[generate-id() = generate-id(key('index_control_field_tag', @tag)[1])]">
197             <xslo:template>
198                 <xsl:attribute name="match">
199                     <xsl:text>marc:controlfield[@tag='</xsl:text>
200                     <xsl:value-of select="@tag"/>
201                     <xsl:text>']</xsl:text>
202                 </xsl:attribute>
203                 <xsl:for-each select="key('index_control_field_tag', @tag)">
204                     <xsl:call-template name="handle-one-index-control-field"/>
205                 </xsl:for-each>
206             </xslo:template>
207         </xsl:for-each>
208     </xsl:template>
209
210     <xsl:template name="handle-one-index-control-field">
211         <xsl:variable name="offset"><xsl:value-of select="@offset"/></xsl:variable>
212         <xsl:variable name="length"><xsl:value-of select="@length"/></xsl:variable>
213         <xsl:variable name="indexes">
214             <xsl:call-template name="get-target-indexes"/>
215         </xsl:variable>
216         <z:index>
217             <xsl:attribute name="name"><xsl:value-of select="normalize-space($indexes)"/></xsl:attribute>
218             <xslo:value-of>
219                 <xsl:attribute name="select">
220                     <xsl:choose>
221                         <xsl:when test="@length">
222                             <xsl:text>substring(., </xsl:text>
223                             <xsl:value-of select="$offset + 1" />
224                             <xsl:text>, </xsl:text>
225                             <xsl:value-of select="$length"/>
226                             <xsl:text>)</xsl:text>
227                         </xsl:when>
228                         <xsl:otherwise>
229                             <xsl:text>.</xsl:text>
230                         </xsl:otherwise>
231                     </xsl:choose>
232                 </xsl:attribute>
233             </xslo:value-of>
234         </z:index>
235     </xsl:template>
236
237     <xsl:template name="handle-index-subfields">
238         <xsl:for-each select="//kohaidx:index_subfields[generate-id() = generate-id(key('index_subfields_tag', @tag)[1])]">
239             <xslo:template mode="index_subfields">
240                 <xsl:attribute name="match">
241                     <xsl:text>marc:datafield[@tag='</xsl:text>
242                     <xsl:value-of select="@tag"/>
243                     <xsl:text>']</xsl:text>
244                 </xsl:attribute>
245                 <xsl:for-each select="key('index_subfields_tag', @tag)">
246                     <xsl:call-template name="handle-one-index-subfields"/>
247                 </xsl:for-each>
248             </xslo:template>
249         </xsl:for-each>
250     </xsl:template>
251
252     <xsl:template name="handle-one-index-subfields">
253         <xsl:variable name="indexes">
254             <xsl:call-template name="get-target-indexes"/>
255         </xsl:variable>
256             <xslo:for-each select="marc:subfield">
257                 <xslo:if>
258                     <xsl:attribute name="test">
259                         <xsl:text>contains('</xsl:text>
260                         <xsl:value-of select="@subfields"/>
261                         <xsl:text>', @code)</xsl:text>
262                     </xsl:attribute>
263                     <z:index>
264                         <xsl:attribute name="name"><xsl:value-of select="normalize-space($indexes)"/></xsl:attribute>
265                         <xslo:value-of select="."/>
266                     </z:index>
267                 </xslo:if>
268             </xslo:for-each>
269     </xsl:template>
270
271     <xsl:template name="handle-index-data-field">
272         <xsl:for-each select="//kohaidx:index_data_field[generate-id() = generate-id(key('index_data_field_tag', @tag)[1])]">
273             <xslo:template mode="index_data_field">
274                 <xsl:attribute name="match">
275                     <xsl:text>marc:datafield[@tag='</xsl:text>
276                     <xsl:value-of select="@tag"/>
277                     <xsl:text>']</xsl:text>
278                 </xsl:attribute>
279                 <xsl:for-each select="key('index_data_field_tag', @tag)">
280                     <xsl:call-template name="handle-one-data-field"/>
281                 </xsl:for-each>
282             </xslo:template>
283         </xsl:for-each>
284     </xsl:template>
285
286     <xsl:template name="handle-index-heading-conditional">
287         <xsl:for-each select="//kohaidx:index_heading_conditional[generate-id() = generate-id(key('index_heading_conditional_tag', @tag)[1])]">
288             <xslo:template mode="index_heading_conditional">
289                 <xslo:if>
290                     <xsl:attribute name="test"><xsl:value-of select="@test"/></xsl:attribute>
291                     <xsl:for-each select="key('index_heading_conditional_tag', @tag)">
292                         <xsl:call-template name="handle-one-index-heading"/>
293                     </xsl:for-each>
294                 </xslo:if>
295             </xslo:template>
296         </xsl:for-each>
297     </xsl:template>
298
299     <xsl:template name="handle-one-data-field">
300         <xsl:variable name="indexes">
301             <xsl:call-template name="get-target-indexes"/>
302         </xsl:variable>
303         <z:index>
304             <xsl:attribute name="name"><xsl:value-of select="normalize-space($indexes)"/></xsl:attribute>
305             <xslo:variable name="raw_heading">
306                 <xslo:for-each select="marc:subfield">
307                         <xslo:if test="position() > 1">
308                             <xslo:value-of select="substring(' ', 1, 1)"/> <!-- FIXME surely there's a better way  to specify a space -->
309                         </xslo:if>
310                         <xslo:value-of select="."/>
311                 </xslo:for-each>
312             </xslo:variable>
313             <xslo:value-of select="normalize-space($raw_heading)"/>
314         </z:index>
315     </xsl:template>
316
317     <xsl:template name="handle-index-heading">
318         <xsl:for-each select="//kohaidx:index_heading[generate-id() = generate-id(key('index_heading_tag', @tag)[1])]">
319             <xslo:template mode="index_heading">
320                 <xsl:attribute name="match">
321                     <xsl:text>marc:datafield[@tag='</xsl:text>
322                     <xsl:value-of select="@tag"/>
323                     <xsl:text>']</xsl:text>
324                 </xsl:attribute>
325                 <xsl:for-each select="key('index_heading_tag', @tag)">
326                     <xsl:call-template name="handle-one-index-heading"/>
327                 </xsl:for-each>
328             </xslo:template>
329         </xsl:for-each>
330     </xsl:template>
331
332     <xsl:template name="handle-one-index-heading">
333         <xsl:variable name="indexes">
334             <xsl:call-template name="get-target-indexes"/>
335         </xsl:variable>
336         <z:index>
337             <xsl:attribute name="name"><xsl:value-of select="normalize-space($indexes)"/></xsl:attribute>
338             <xslo:variable name="raw_heading">
339                 <xslo:for-each select="marc:subfield">
340                     <xslo:if>
341                         <xsl:attribute name="test">
342                             <xsl:text>contains('</xsl:text>
343                             <xsl:value-of select="@subfields"/>
344                             <xsl:text>', @code)</xsl:text>
345                         </xsl:attribute>
346                         <xsl:attribute name="name"><xsl:value-of select="normalize-space($indexes)"/></xsl:attribute>
347                         <xslo:if test="position() > 1">
348                             <xslo:choose>
349                                 <xslo:when>
350                                     <xsl:attribute name="test">
351                                         <xsl:text>contains('</xsl:text>
352                                         <xsl:value-of select="@subdivisions"/>
353                                         <xsl:text>', @code)</xsl:text>
354                                     </xsl:attribute>
355                                     <xslo:text>--</xslo:text>
356                                 </xslo:when>
357                                 <xslo:otherwise>
358                                     <xslo:value-of select="substring(' ', 1, 1)"/> <!-- FIXME surely there's a better way  to specify a space -->
359                                 </xslo:otherwise>
360                             </xslo:choose>
361                         </xslo:if>
362                         <xslo:value-of select="."/>
363                     </xslo:if>
364                 </xslo:for-each>
365             </xslo:variable>
366             <xslo:value-of select="normalize-space($raw_heading)"/>
367         </z:index>
368     </xsl:template>
369
370     <xsl:template name="handle-index-match-heading">
371         <xsl:for-each select="//kohaidx:index_match_heading[generate-id() = generate-id(key('index_match_heading_tag', @tag)[1])]">
372             <xslo:template mode="index_match_heading">
373                 <xsl:attribute name="match">
374                     <xsl:text>marc:datafield[@tag='</xsl:text>
375                     <xsl:value-of select="@tag"/>
376                     <xsl:text>']</xsl:text>
377                 </xsl:attribute>
378                 <xsl:for-each select="key('index_match_heading_tag', @tag)">
379                     <xsl:call-template name="handle-one-index-match-heading"/>
380                 </xsl:for-each>
381             </xslo:template>
382         </xsl:for-each>
383     </xsl:template>
384
385     <xsl:template name="handle-one-index-match-heading">
386         <xsl:variable name="indexes">
387             <xsl:call-template name="get-target-indexes"/>
388         </xsl:variable>
389         <z:index>
390             <xsl:attribute name="name"><xsl:value-of select="normalize-space($indexes)"/></xsl:attribute>
391             <xslo:variable name="raw_heading">
392                 <xslo:for-each select="marc:subfield">
393                     <xslo:if>
394                         <xsl:attribute name="test">
395                             <xsl:text>contains('</xsl:text>
396                             <xsl:value-of select="@subfields"/>
397                             <xsl:text>', @code)</xsl:text>
398                         </xsl:attribute>
399                         <xsl:attribute name="name"><xsl:value-of select="normalize-space($indexes)"/></xsl:attribute>
400                         <xslo:if test="position() > 1">
401                             <xslo:choose>
402                                 <xslo:when>
403                                     <xsl:attribute name="test">
404                                         <xsl:text>contains('</xsl:text>
405                                         <xsl:value-of select="@subdivisions"/>
406                                         <xsl:text>', @code)</xsl:text>
407                                     </xsl:attribute>
408                                     <xslo:choose>
409                                         <xslo:when>
410                                             <xsl:attribute name="test">
411                                                 <xsl:text>@code = $general_subdivision_subfield</xsl:text>
412                                             </xsl:attribute>
413                                             <xslo:text> generalsubdiv </xslo:text>
414                                         </xslo:when>
415                                         <xslo:when>
416                                             <xsl:attribute name="test">
417                                                 <xsl:text>@code = $form_subdivision_subfield</xsl:text>
418                                             </xsl:attribute>
419                                             <xslo:text> formsubdiv </xslo:text>
420                                         </xslo:when>
421                                         <xslo:when>
422                                             <xsl:attribute name="test">
423                                                 <xsl:text>@code = $chronological_subdivision_subfield</xsl:text>
424                                             </xsl:attribute>
425                                             <xslo:text> chronologicalsubdiv </xslo:text>
426                                         </xslo:when>
427                                         <xslo:when>
428                                             <xsl:attribute name="test">
429                                                 <xsl:text>@code = $geographic_subdivision_subfield</xsl:text>
430                                             </xsl:attribute>
431                                             <xslo:text> geographicsubdiv </xslo:text>
432                                         </xslo:when>
433                                     </xslo:choose>
434                                 </xslo:when>
435                                 <xslo:otherwise>
436                                     <xslo:value-of select="substring(' ', 1, 1)"/> <!-- FIXME surely there's a better way  to specify a space -->
437                                 </xslo:otherwise>
438                             </xslo:choose>
439                         </xslo:if>
440                         <xslo:call-template name="chopPunctuation">
441                             <xslo:with-param name="chopString">
442                                 <xslo:value-of select="."/>
443                             </xslo:with-param>
444                         </xslo:call-template>
445                     </xslo:if>
446                 </xslo:for-each>
447             </xslo:variable>
448             <xslo:value-of select="normalize-space($raw_heading)"/>
449         </z:index>
450     </xsl:template>
451
452     <xsl:template name="get-target-indexes">
453         <xsl:for-each select="kohaidx:target_index">
454             <xsl:value-of select="." /><xsl:text> </xsl:text>
455         </xsl:for-each>
456     </xsl:template>
457 </xsl:stylesheet>