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