Bug 6152: Document DOM authority index config
[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:stylesheet>
71     </xsl:template>
72
73     <!-- map kohaidx:var to stylesheet variables -->
74     <xsl:template match="kohaidx:var">
75         <xslo:variable>
76             <xsl:attribute name="name"><xsl:value-of select="@name"/></xsl:attribute>
77             <xsl:value-of select="."/>
78         </xslo:variable>
79     </xsl:template>
80
81     <xsl:template match="kohaidx:index_subject_thesaurus">   
82         <xsl:variable name="tag"><xsl:value-of select="@tag"/></xsl:variable>
83         <xsl:variable name="offset"><xsl:value-of select="@offset"/></xsl:variable>
84         <xsl:variable name="length"><xsl:value-of select="@length"/></xsl:variable>
85         <xsl:variable name="detail_tag"><xsl:value-of select="@detail_tag"/></xsl:variable>
86         <xsl:variable name="detail_subfields"><xsl:value-of select="@detail_subfields"/></xsl:variable>
87         <xsl:variable name="indexes">
88             <xsl:call-template name="get-target-indexes"/>
89         </xsl:variable>
90         <xslo:template mode="index_subject_thesaurus">
91             <xsl:attribute name="match">
92                 <xsl:text>marc:controlfield[@tag='</xsl:text>
93                 <xsl:value-of select="$tag"/>
94                 <xsl:text>']</xsl:text>
95             </xsl:attribute>
96             <xslo:variable name="thesaurus_code1">
97                 <xsl:attribute name="select">
98                     <xsl:text>substring(., </xsl:text>
99                     <xsl:value-of select="$offset + 1" />
100                     <xsl:text>, </xsl:text>
101                     <xsl:value-of select="$length" />
102                     <xsl:text>)</xsl:text>
103                 </xsl:attribute>
104             </xslo:variable>
105             <xsl:variable name="alt_select">
106                 <xsl:text>//marc:datafield[@tag='</xsl:text>
107                 <xsl:value-of select="$detail_tag"/>
108                 <xsl:text>']/marc:subfield[@code='</xsl:text>
109                 <xsl:value-of select="$detail_subfields"/>
110                 <xsl:text>']</xsl:text>
111             </xsl:variable>
112             <xslo:variable name="full_thesaurus_code">
113                 <xslo:choose>
114                     <xslo:when test="$thesaurus_code1 = 'a'"><xslo:text>lcsh</xslo:text></xslo:when>
115                     <xslo:when test="$thesaurus_code1 = 'b'"><xslo:text>lcac</xslo:text></xslo:when>
116                     <xslo:when test="$thesaurus_code1 = 'c'"><xslo:text>mesh</xslo:text></xslo:when>
117                     <xslo:when test="$thesaurus_code1 = 'd'"><xslo:text>nal</xslo:text></xslo:when>
118                     <xslo:when test="$thesaurus_code1 = 'k'"><xslo:text>cash</xslo:text></xslo:when>
119                     <xslo:when test="$thesaurus_code1 = 'n'"><xslo:text>notapplicable</xslo:text></xslo:when>
120                     <xslo:when test="$thesaurus_code1 = 'r'"><xslo:text>aat</xslo:text></xslo:when>
121                     <xslo:when test="$thesaurus_code1 = 's'"><xslo:text>sears</xslo:text></xslo:when>
122                     <xslo:when test="$thesaurus_code1 = 'v'"><xslo:text>rvm</xslo:text></xslo:when>
123                     <xslo:when test="$thesaurus_code1 = 'z'">
124                         <xslo:choose>
125                             <xslo:when>
126                                 <xsl:attribute name="test"><xsl:value-of select="$alt_select"/></xsl:attribute>
127                                 <xslo:value-of>
128                                     <xsl:attribute name="select"><xsl:value-of select="$alt_select"/></xsl:attribute>
129                                 </xslo:value-of>
130                             </xslo:when>
131                             <xslo:otherwise><xslo:text>notdefined</xslo:text></xslo:otherwise>
132                         </xslo:choose>
133                     </xslo:when>
134                     <xslo:otherwise><xslo:text>notdefined</xslo:text></xslo:otherwise>
135                 </xslo:choose>
136             </xslo:variable>
137             <z:index>
138                 <xsl:attribute name="name"><xsl:value-of select="normalize-space($indexes)"/></xsl:attribute>
139                 <xslo:value-of select="$full_thesaurus_code"/>
140             </z:index>
141         </xslo:template>
142     </xsl:template>
143
144     <xsl:template name="handle-index-leader">
145         <xsl:if test="kohaidx:index_leader">
146             <xslo:template match="marc:leader">
147                 <xsl:apply-templates select="kohaidx:index_leader" mode="secondary"/>
148             </xslo:template>
149         </xsl:if>
150     </xsl:template>
151
152     <xsl:template match="kohaidx:index_leader" mode="secondary">
153         <xsl:variable name="offset"><xsl:value-of select="@offset"/></xsl:variable>
154         <xsl:variable name="length"><xsl:value-of select="@length"/></xsl:variable>
155         <xsl:variable name="indexes">
156             <xsl:call-template name="get-target-indexes"/>
157         </xsl:variable>
158         <z:index>
159             <xsl:attribute name="name"><xsl:value-of select="normalize-space($indexes)"/></xsl:attribute>
160             <xslo:value-of>
161                 <xsl:attribute name="select">
162                     <xsl:text>substring(., </xsl:text>
163                     <xsl:value-of select="$offset + 1" />
164                     <xsl:text>, </xsl:text>
165                     <xsl:value-of select="$length" />
166                     <xsl:text>)</xsl:text>
167                 </xsl:attribute>
168             </xslo:value-of>
169         </z:index>
170     </xsl:template>
171
172     <xsl:template name="handle-index-control-field">
173         <xsl:for-each select="//kohaidx:index_control_field[generate-id() = generate-id(key('index_control_field_tag', @tag)[1])]">
174             <xslo:template>
175                 <xsl:attribute name="match">
176                     <xsl:text>marc:controlfield[@tag='</xsl:text>
177                     <xsl:value-of select="@tag"/>
178                     <xsl:text>']</xsl:text>
179                 </xsl:attribute>
180                 <xsl:for-each select="key('index_control_field_tag', @tag)">
181                     <xsl:call-template name="handle-one-index-control-field"/>
182                 </xsl:for-each>
183             </xslo:template>
184         </xsl:for-each>
185     </xsl:template>
186
187     <xsl:template name="handle-one-index-control-field">
188         <xsl:variable name="offset"><xsl:value-of select="@offset"/></xsl:variable>
189         <xsl:variable name="length"><xsl:value-of select="@length"/></xsl:variable>
190         <xsl:variable name="indexes">
191             <xsl:call-template name="get-target-indexes"/>
192         </xsl:variable>
193         <z:index>
194             <xsl:attribute name="name"><xsl:value-of select="normalize-space($indexes)"/></xsl:attribute>
195             <xslo:value-of>
196                 <xsl:attribute name="select">
197                     <xsl:choose>
198                         <xsl:when test="@length">
199                             <xsl:text>substring(., </xsl:text>
200                             <xsl:value-of select="$offset + 1" />
201                             <xsl:text>, </xsl:text>
202                             <xsl:value-of select="$length"/>
203                             <xsl:text>)</xsl:text>
204                         </xsl:when>
205                         <xsl:otherwise>
206                             <xsl:text>.</xsl:text>
207                         </xsl:otherwise>
208                     </xsl:choose>
209                 </xsl:attribute>
210             </xslo:value-of>
211         </z:index>
212     </xsl:template>
213
214     <xsl:template name="handle-index-subfields">
215         <xsl:for-each select="//kohaidx:index_subfields[generate-id() = generate-id(key('index_subfields_tag', @tag)[1])]">
216             <xslo:template mode="index_subfields">
217                 <xsl:attribute name="match">
218                     <xsl:text>marc:datafield[@tag='</xsl:text>
219                     <xsl:value-of select="@tag"/>
220                     <xsl:text>']</xsl:text>
221                 </xsl:attribute>
222                 <xsl:for-each select="key('index_subfields_tag', @tag)">
223                     <xsl:call-template name="handle-one-index-subfields"/>
224                 </xsl:for-each>
225             </xslo:template>
226         </xsl:for-each>
227     </xsl:template>
228
229     <xsl:template name="handle-one-index-subfields">
230         <xsl:variable name="indexes">
231             <xsl:call-template name="get-target-indexes"/>
232         </xsl:variable>
233             <xslo:for-each select="marc:subfield">
234                 <xslo:if>
235                     <xsl:attribute name="test">
236                         <xsl:text>contains('</xsl:text>
237                         <xsl:value-of select="@subfields"/>
238                         <xsl:text>', @code)</xsl:text>
239                     </xsl:attribute>
240                     <z:index>
241                         <xsl:attribute name="name"><xsl:value-of select="normalize-space($indexes)"/></xsl:attribute>
242                         <xslo:value-of select="."/>
243                     </z:index>
244                 </xslo:if>
245             </xslo:for-each>
246     </xsl:template>
247
248     <xsl:template name="handle-index-heading">
249         <xsl:for-each select="//kohaidx:index_heading[generate-id() = generate-id(key('index_heading_tag', @tag)[1])]">
250             <xslo:template mode="index_heading">
251                 <xsl:attribute name="match">
252                     <xsl:text>marc:datafield[@tag='</xsl:text>
253                     <xsl:value-of select="@tag"/>
254                     <xsl:text>']</xsl:text>
255                 </xsl:attribute>
256                 <xsl:for-each select="key('index_heading_tag', @tag)">
257                     <xsl:call-template name="handle-one-index-heading"/>
258                 </xsl:for-each>
259             </xslo:template>
260         </xsl:for-each>
261     </xsl:template>
262
263     <xsl:template name="handle-one-index-heading">
264         <xsl:variable name="indexes">
265             <xsl:call-template name="get-target-indexes"/>
266         </xsl:variable>
267         <z:index>
268             <xsl:attribute name="name"><xsl:value-of select="normalize-space($indexes)"/></xsl:attribute>
269             <xslo:variable name="raw_heading">
270                 <xslo:for-each select="marc:subfield">
271                     <xslo:if>
272                         <xsl:attribute name="test">
273                             <xsl:text>contains('</xsl:text>
274                             <xsl:value-of select="@subfields"/>
275                             <xsl:text>', @code)</xsl:text>
276                         </xsl:attribute>
277                         <xsl:attribute name="name"><xsl:value-of select="normalize-space($indexes)"/></xsl:attribute>
278                         <xslo:if test="position() > 1">
279                             <xslo:choose>
280                                 <xslo:when>
281                                     <xsl:attribute name="test">
282                                         <xsl:text>contains('</xsl:text>
283                                         <xsl:value-of select="@subdivisions"/>
284                                         <xsl:text>', @code)</xsl:text>
285                                     </xsl:attribute>
286                                     <xslo:text>--</xslo:text>
287                                 </xslo:when>
288                                 <xslo:otherwise>
289                                     <xslo:value-of select="substring(' ', 1, 1)"/> <!-- FIXME surely there's a better way  to specify a space -->
290                                 </xslo:otherwise>
291                             </xslo:choose>
292                         </xslo:if>
293                         <xslo:value-of select="."/>
294                     </xslo:if>
295                 </xslo:for-each>
296             </xslo:variable>
297             <xslo:value-of select="normalize-space($raw_heading)"/>
298         </z:index>
299     </xsl:template>
300
301     <xsl:template name="handle-index-match-heading">
302         <xsl:for-each select="//kohaidx:index_match_heading[generate-id() = generate-id(key('index_match_heading_tag', @tag)[1])]">
303             <xslo:template mode="index_match_heading">
304                 <xsl:attribute name="match">
305                     <xsl:text>marc:datafield[@tag='</xsl:text>
306                     <xsl:value-of select="@tag"/>
307                     <xsl:text>']</xsl:text>
308                 </xsl:attribute>
309                 <xsl:for-each select="key('index_match_heading_tag', @tag)">
310                     <xsl:call-template name="handle-one-index-match-heading"/>
311                 </xsl:for-each>
312             </xslo:template>
313         </xsl:for-each>
314     </xsl:template>
315
316     <xsl:template name="handle-one-index-match-heading">
317         <xsl:variable name="indexes">
318             <xsl:call-template name="get-target-indexes"/>
319         </xsl:variable>
320         <z:index>
321             <xsl:attribute name="name"><xsl:value-of select="normalize-space($indexes)"/></xsl:attribute>
322             <xslo:variable name="raw_heading">
323                 <xslo:for-each select="marc:subfield">
324                     <xslo:if>
325                         <xsl:attribute name="test">
326                             <xsl:text>contains('</xsl:text>
327                             <xsl:value-of select="@subfields"/>
328                             <xsl:text>', @code)</xsl:text>
329                         </xsl:attribute>
330                         <xsl:attribute name="name"><xsl:value-of select="normalize-space($indexes)"/></xsl:attribute>
331                         <xslo:if test="position() > 1">
332                             <xslo:choose>
333                                 <xslo:when>
334                                     <xsl:attribute name="test">
335                                         <xsl:text>contains('</xsl:text>
336                                         <xsl:value-of select="@subdivisions"/>
337                                         <xsl:text>', @code)</xsl:text>
338                                     </xsl:attribute>
339                                     <xslo:choose>
340                                         <xslo:when>
341                                             <xsl:attribute name="test">
342                                                 <xsl:text>@code = $general_subdivision_subfield</xsl:text>
343                                             </xsl:attribute>
344                                             <xslo:text> generalsubdiv </xslo:text>
345                                         </xslo:when>
346                                         <xslo:when>
347                                             <xsl:attribute name="test">
348                                                 <xsl:text>@code = $form_subdivision_subfield</xsl:text>
349                                             </xsl:attribute>
350                                             <xslo:text> formsubdiv </xslo:text>
351                                         </xslo:when>
352                                         <xslo:when>
353                                             <xsl:attribute name="test">
354                                                 <xsl:text>@code = $chronological_subdivision_subfield</xsl:text>
355                                             </xsl:attribute>
356                                             <xslo:text> chronologicalsubdiv </xslo:text>
357                                         </xslo:when>
358                                         <xslo:when>
359                                             <xsl:attribute name="test">
360                                                 <xsl:text>@code = $geographic_subdivision_subfield</xsl:text>
361                                             </xsl:attribute>
362                                             <xslo:text> geographicsubdiv </xslo:text>
363                                         </xslo:when>
364                                     </xslo:choose>
365                                 </xslo:when>
366                                 <xslo:otherwise>
367                                     <xslo:value-of select="substring(' ', 1, 1)"/> <!-- FIXME surely there's a better way  to specify a space -->
368                                 </xslo:otherwise>
369                             </xslo:choose>
370                         </xslo:if>
371                         <xslo:value-of select="."/>
372                     </xslo:if>
373                 </xslo:for-each>
374             </xslo:variable>
375             <xslo:value-of select="normalize-space($raw_heading)"/>
376         </z:index>
377     </xsl:template>
378
379     <xsl:template name="get-target-indexes">
380         <xsl:for-each select="kohaidx:target_index">
381             <xsl:value-of select="." /><xsl:text> </xsl:text>
382         </xsl:for-each>
383     </xsl:template>
384 </xsl:stylesheet>