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