authorities: start of work on reindexing
[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.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
19     <xsl:template match="kohaidx:index_defs">
20         <xslo:stylesheet version="1.0">
21             <xslo:output indent="yes" method="xml" version="1.0" encoding="UTF-8"/>
22             <xslo:template match="text()"/>
23             <xslo:template match="text()" mode="index_subfields"/>
24             <xslo:template match="text()" mode="index_heading"/>
25             <xslo:template match="text()" mode="index_subject_thesaurus"/>
26             <xslo:template match="/">
27                 <xslo:if test="marc:collection">
28                     <collection>
29                         <xslo:apply-templates select="marc:collection/marc:record"/>
30                     </collection>
31                 </xslo:if>
32                 <xslo:if test="marc:record">
33                     <xslo:apply-templates select="marc:record"/>
34                 </xslo:if>
35             </xslo:template>
36
37             <xslo:template match="marc:record">
38                 <xslo:variable name="controlField001" select="normalize-space(marc:controlfield[@tag='001'])"/>
39                 <z:record type="update">
40                     <xslo:attribute name="z:id"><xslo:value-of select="$controlField001"/></xslo:attribute>
41                     <xslo:apply-templates/>
42                     <xslo:apply-templates mode="index_subfields"/>
43                     <xslo:apply-templates mode="index_heading"/>
44                     <xslo:apply-templates mode="index_subject_thesaurus"/>
45                 </z:record>
46             </xslo:template>
47
48             <xsl:call-template name="handle-index-leader"/>
49             <xsl:call-template name="handle-index-control-field"/>
50             <xsl:call-template name="handle-index-subfields"/>
51             <xsl:call-template name="handle-index-heading"/>
52             <xsl:apply-templates/>
53         </xslo:stylesheet>
54     </xsl:template>
55
56     <xsl:template match="kohaidx:index_subject_thesaurus">   
57         <xsl:variable name="tag"><xsl:value-of select="@tag"/></xsl:variable>
58         <xsl:variable name="offset"><xsl:value-of select="@offset"/></xsl:variable>
59         <xsl:variable name="length"><xsl:value-of select="@length"/></xsl:variable>
60         <xsl:variable name="detail_tag"><xsl:value-of select="@detail_tag"/></xsl:variable>
61         <xsl:variable name="detail_subfields"><xsl:value-of select="@detail_subfields"/></xsl:variable>
62         <xsl:variable name="indexes">
63             <xsl:call-template name="get-target-indexes"/>
64         </xsl:variable>
65         <xslo:template mode="index_subject_thesaurus">
66             <xsl:attribute name="match">
67                 <xsl:text>marc:controlfield[@tag='</xsl:text>
68                 <xsl:value-of select="$tag"/>
69                 <xsl:text>']</xsl:text>
70             </xsl:attribute>
71             <xslo:variable name="thesaurus_code1">
72                 <xsl:attribute name="select">
73                     <xsl:text>substring(., </xsl:text>
74                     <xsl:value-of select="$offset + 1" />
75                     <xsl:text>, </xsl:text>
76                     <xsl:value-of select="$length" />
77                     <xsl:text>)</xsl:text>
78                 </xsl:attribute>
79             </xslo:variable>
80             <xsl:variable name="alt_select">
81                 <xsl:text>//marc:datafield[@tag='</xsl:text>
82                 <xsl:value-of select="$detail_tag"/>
83                 <xsl:text>']/marc:subfield[@code='</xsl:text>
84                 <xsl:value-of select="$detail_subfields"/>
85                 <xsl:text>']</xsl:text>
86             </xsl:variable>
87             <xslo:variable name="full_thesaurus_code">
88                 <xslo:choose>
89                     <xslo:when test="$thesaurus_code1 = 'a'"><xslo:text>lcsh</xslo:text></xslo:when>
90                     <xslo:when test="$thesaurus_code1 = 'b'"><xslo:text>lcac</xslo:text></xslo:when>
91                     <xslo:when test="$thesaurus_code1 = 'c'"><xslo:text>mesh</xslo:text></xslo:when>
92                     <xslo:when test="$thesaurus_code1 = 'd'"><xslo:text>nal</xslo:text></xslo:when>
93                     <xslo:when test="$thesaurus_code1 = 'k'"><xslo:text>cash</xslo:text></xslo:when>
94                     <xslo:when test="$thesaurus_code1 = 'n'"><xslo:text>notapplicable</xslo:text></xslo:when>
95                     <xslo:when test="$thesaurus_code1 = 'r'"><xslo:text>aat</xslo:text></xslo:when>
96                     <xslo:when test="$thesaurus_code1 = 's'"><xslo:text>sears</xslo:text></xslo:when>
97                     <xslo:when test="$thesaurus_code1 = 'v'"><xslo:text>rvm</xslo:text></xslo:when>
98                     <xslo:when test="$thesaurus_code1 = 'z'">
99                         <xslo:choose>
100                             <xslo:when>
101                                 <xsl:attribute name="test"><xsl:value-of select="$alt_select"/></xsl:attribute>
102                                 <xslo:value-of>
103                                     <xsl:attribute name="select"><xsl:value-of select="$alt_select"/></xsl:attribute>
104                                 </xslo:value-of>
105                             </xslo:when>
106                             <xslo:otherwise><xslo:text>notdefined</xslo:text></xslo:otherwise>
107                         </xslo:choose>
108                     </xslo:when>
109                     <xslo:otherwise><xslo:text>notdefined</xslo:text></xslo:otherwise>
110                 </xslo:choose>
111             </xslo:variable>
112             <z:index>
113                 <xsl:attribute name="name"><xsl:value-of select="normalize-space($indexes)"/></xsl:attribute>
114                 <xslo:value-of select="$full_thesaurus_code"/>
115             </z:index>
116         </xslo:template>
117     </xsl:template>
118
119     <xsl:template name="handle-index-leader">
120         <xsl:if test="kohaidx:index_leader">
121             <xslo:template match="marc:leader">
122                 <xsl:apply-templates select="kohaidx:index_leader" mode="secondary"/>
123             </xslo:template>
124         </xsl:if>
125     </xsl:template>
126
127     <xsl:template match="kohaidx:index_leader" mode="secondary">
128         <xsl:variable name="offset"><xsl:value-of select="@offset"/></xsl:variable>
129         <xsl:variable name="length"><xsl:value-of select="@length"/></xsl:variable>
130         <xsl:variable name="indexes">
131             <xsl:call-template name="get-target-indexes"/>
132         </xsl:variable>
133         <z:index>
134             <xsl:attribute name="name"><xsl:value-of select="normalize-space($indexes)"/></xsl:attribute>
135             <xslo:value-of>
136                 <xsl:attribute name="select">
137                     <xsl:text>substring(., </xsl:text>
138                     <xsl:value-of select="$offset + 1" />
139                     <xsl:text>, </xsl:text>
140                     <xsl:value-of select="$length" />
141                     <xsl:text>)</xsl:text>
142                 </xsl:attribute>
143             </xslo:value-of>
144         </z:index>
145     </xsl:template>
146
147     <xsl:template name="handle-index-control-field">
148         <xsl:for-each select="//kohaidx:index_control_field[generate-id() = generate-id(key('index_control_field_tag', @tag)[1])]">
149             <xslo:template>
150                 <xsl:attribute name="match">
151                     <xsl:text>marc:controlfield[@tag='</xsl:text>
152                     <xsl:value-of select="@tag"/>
153                     <xsl:text>']</xsl:text>
154                 </xsl:attribute>
155                 <xsl:for-each select="key('index_control_field_tag', @tag)">
156                     <xsl:call-template name="handle-one-index-control-field"/>
157                 </xsl:for-each>
158             </xslo:template>
159         </xsl:for-each>
160     </xsl:template>
161
162     <xsl:template name="handle-one-index-control-field">
163         <xsl:variable name="offset"><xsl:value-of select="@offset"/></xsl:variable>
164         <xsl:variable name="length"><xsl:value-of select="@length"/></xsl:variable>
165         <xsl:variable name="indexes">
166             <xsl:call-template name="get-target-indexes"/>
167         </xsl:variable>
168         <z:index>
169             <xsl:attribute name="name"><xsl:value-of select="normalize-space($indexes)"/></xsl:attribute>
170             <xslo:value-of>
171                 <xsl:attribute name="select">
172                     <xsl:choose>
173                         <xsl:when test="@length">
174                             <xsl:text>substring(., </xsl:text>
175                             <xsl:value-of select="$offset + 1" />
176                             <xsl:text>, </xsl:text>
177                             <xsl:value-of select="$length"/>
178                             <xsl:text>)</xsl:text>
179                         </xsl:when>
180                         <xsl:otherwise>
181                             <xsl:text>.</xsl:text>
182                         </xsl:otherwise>
183                     </xsl:choose>
184                 </xsl:attribute>
185             </xslo:value-of>
186         </z:index>
187     </xsl:template>
188
189     <xsl:template name="handle-index-subfields">
190         <xsl:for-each select="//kohaidx:index_subfields[generate-id() = generate-id(key('index_subfields_tag', @tag)[1])]">
191             <xslo:template mode="index_subfields">
192                 <xsl:attribute name="match">
193                     <xsl:text>marc:datafield[@tag='</xsl:text>
194                     <xsl:value-of select="@tag"/>
195                     <xsl:text>']</xsl:text>
196                 </xsl:attribute>
197                 <xsl:for-each select="key('index_subfields_tag', @tag)">
198                     <xsl:call-template name="handle-one-index-subfields"/>
199                 </xsl:for-each>
200             </xslo:template>
201         </xsl:for-each>
202     </xsl:template>
203
204     <xsl:template name="handle-one-index-subfields">
205         <xsl:variable name="indexes">
206             <xsl:call-template name="get-target-indexes"/>
207         </xsl:variable>
208             <xslo:for-each select="marc:subfield">
209                 <xslo:if>
210                     <xsl:attribute name="test">
211                         <xsl:text>contains('</xsl:text>
212                         <xsl:value-of select="@subfields"/>
213                         <xsl:text>', @code)</xsl:text>
214                     </xsl:attribute>
215                     <z:index>
216                         <xsl:attribute name="name"><xsl:value-of select="normalize-space($indexes)"/></xsl:attribute>
217                         <xslo:value-of select="."/>
218                     </z:index>
219                 </xslo:if>
220             </xslo:for-each>
221     </xsl:template>
222
223     <xsl:template name="handle-index-heading">
224         <xsl:for-each select="//kohaidx:index_heading[generate-id() = generate-id(key('index_heading_tag', @tag)[1])]">
225             <xslo:template mode="index_heading">
226                 <xsl:attribute name="match">
227                     <xsl:text>marc:datafield[@tag='</xsl:text>
228                     <xsl:value-of select="@tag"/>
229                     <xsl:text>']</xsl:text>
230                 </xsl:attribute>
231                 <xsl:for-each select="key('index_heading_tag', @tag)">
232                     <xsl:call-template name="handle-one-index-heading"/>
233                 </xsl:for-each>
234             </xslo:template>
235         </xsl:for-each>
236     </xsl:template>
237
238     <xsl:template name="handle-one-index-heading">
239         <xsl:variable name="indexes">
240             <xsl:call-template name="get-target-indexes"/>
241         </xsl:variable>
242         <z:index>
243             <xsl:attribute name="name"><xsl:value-of select="normalize-space($indexes)"/></xsl:attribute>
244             <xslo:variable name="raw_heading">
245                 <xslo:for-each select="marc:subfield">
246                     <xslo:if>
247                         <xsl:attribute name="test">
248                             <xsl:text>contains('</xsl:text>
249                             <xsl:value-of select="@subfields"/>
250                             <xsl:text>', @code)</xsl:text>
251                         </xsl:attribute>
252                         <xsl:attribute name="name"><xsl:value-of select="normalize-space($indexes)"/></xsl:attribute>
253                         <xslo:if test="position() > 1">
254                             <xslo:choose>
255                                 <xslo:when>
256                                     <xsl:attribute name="test">
257                                         <xsl:text>contains('</xsl:text>
258                                         <xsl:value-of select="@subdivisions"/>
259                                         <xsl:text>', @code)</xsl:text>
260                                     </xsl:attribute>
261                                     <xslo:text>--</xslo:text>
262                                 </xslo:when>
263                                 <xslo:otherwise>
264                                     <xslo:value-of select="substring(' ', 1, 1)"/> <!-- FIXME surely there's a better way  to specify a space -->
265                                 </xslo:otherwise>
266                             </xslo:choose>
267                         </xslo:if>
268                         <xslo:value-of select="."/>
269                     </xslo:if>
270                 </xslo:for-each>
271             </xslo:variable>
272             <xslo:value-of select="normalize-space($raw_heading)"/>
273         </z:index>
274     </xsl:template>
275
276     <xsl:template name="get-target-indexes">
277         <xsl:for-each select="kohaidx:target_index">
278             <xsl:value-of select="." /><xsl:text> </xsl:text>
279         </xsl:for-each>
280     </xsl:template>
281 </xsl:stylesheet>