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