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