Bug 9613: items namespace missing in XSLT
[koha.git] / koha-tmpl / opac-tmpl / prog / en / xslt / UNIMARCslimUtils.xsl
1 <?xml version='1.0'?>
2
3 <!DOCTYPE stylesheet [<!ENTITY nbsp "&#160;" >]>
4
5 <xsl:stylesheet version="1.0"
6   xmlns:marc="http://www.loc.gov/MARC21/slim"
7   xmlns:items="http://www.koha-community.org/items"
8   xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
9   exclude-result-prefixes="marc items">
10
11   <xsl:template name="datafield">
12     <xsl:param name="tag"/>
13     <xsl:param name="ind1"><xsl:text> </xsl:text></xsl:param>
14     <xsl:param name="ind2"><xsl:text> </xsl:text></xsl:param>
15     <xsl:param name="subfields"/>
16     <xsl:element name="datafield">
17       <xsl:attribute name="tag">
18         <xsl:value-of select="$tag"/>
19       </xsl:attribute>
20       <xsl:attribute name="ind1">
21         <xsl:value-of select="$ind1"/>
22       </xsl:attribute>
23       <xsl:attribute name="ind2">
24        <xsl:value-of select="$ind2"/>
25          </xsl:attribute>
26        <xsl:copy-of select="$subfields"/>
27     </xsl:element>
28   </xsl:template>
29
30   <xsl:template name="subfieldSelect">
31     <xsl:param name="codes"/>
32     <xsl:param name="delimeter"><xsl:text> </xsl:text></xsl:param>
33     <xsl:param name="subdivCodes"/>
34     <xsl:param name="subdivDelimiter"/>
35     <xsl:variable name="str">
36       <xsl:for-each select="marc:subfield">
37         <xsl:if test="contains($codes, @code)">
38           <xsl:if test="contains($subdivCodes, @code)">
39             <xsl:value-of select="$subdivDelimiter"/>
40           </xsl:if>
41           <xsl:value-of select="text()"/><xsl:value-of select="$delimeter"/>
42         </xsl:if>
43       </xsl:for-each>
44     </xsl:variable>
45     <xsl:value-of select="substring($str,1,string-length($str)-string-length($delimeter))"/>
46   </xsl:template>
47
48   <xsl:template name="buildSpaces">
49     <xsl:param name="spaces"/>
50     <xsl:param name="char"><xsl:text> </xsl:text></xsl:param>
51     <xsl:if test="$spaces>0">
52       <xsl:value-of select="$char"/>
53       <xsl:call-template name="buildSpaces">
54         <xsl:with-param name="spaces" select="$spaces - 1"/>
55         <xsl:with-param name="char" select="$char"/>
56       </xsl:call-template>
57     </xsl:if>
58   </xsl:template>
59
60   <xsl:template name="chopPunctuation">
61     <xsl:param name="chopString"/>
62     <xsl:variable name="length" select="string-length($chopString)"/>
63     <xsl:choose>
64       <xsl:when test="$length=0"/>
65       <xsl:when test="contains('.:,;/ ', substring($chopString,$length,1))">
66         <xsl:call-template name="chopPunctuation">
67           <xsl:with-param name="chopString" select="substring($chopString,1,$length - 1)"/>
68         </xsl:call-template>
69       </xsl:when>
70       <xsl:when test="not($chopString)"/>
71       <xsl:otherwise><xsl:value-of select="$chopString"/></xsl:otherwise>
72     </xsl:choose>
73     <xsl:text> </xsl:text>
74   </xsl:template>
75
76   <xsl:template name="addClassRtl">
77     <xsl:variable name="lang" select="marc:subfield[@code='7']" />
78     <xsl:if test="$lang = 'ha' or $lang = 'Hebrew' or $lang = 'fa' or $lang = 'Arabe'">
79       <xsl:attribute name="class">rtl</xsl:attribute>
80     </xsl:if>
81   </xsl:template>
82
83   <xsl:template name="tag_title">
84     <xsl:param name="tag" />
85     <xsl:param name="label" />
86     <xsl:if test="marc:datafield[@tag=$tag]">
87       <span class="results_summary">
88         <span class="label">
89         <xsl:value-of select="$label"/>: </span>
90         <xsl:for-each select="marc:datafield[@tag=$tag]">
91           <xsl:call-template name="addClassRtl" />
92           <xsl:for-each select="marc:subfield">
93             <xsl:choose>
94               <xsl:when test="@code='a'">
95                 <xsl:variable name="title" select="."/>
96                 <xsl:variable name="ntitle"
97                  select="translate($title, '&#x0088;&#x0089;&#x0098;&#x009C;','')"/>
98                 <xsl:value-of select="$ntitle" />
99               </xsl:when>
100               <xsl:when test="@code='b'">
101                 <xsl:text>[</xsl:text>
102                 <xsl:value-of select="."/>
103                 <xsl:text>]</xsl:text>
104               </xsl:when>
105               <xsl:when test="@code='d'">
106                 <xsl:text> = </xsl:text>
107                 <xsl:value-of select="."/>
108               </xsl:when>
109               <xsl:when test="@code='e'">
110                 <xsl:text> : </xsl:text>
111                 <xsl:value-of select="."/>
112               </xsl:when>
113               <xsl:when test="@code='f'">
114                 <xsl:text> / </xsl:text>
115                 <xsl:value-of select="."/>
116               </xsl:when>
117               <xsl:when test="@code='g'">
118                 <xsl:text> ; </xsl:text>
119                 <xsl:value-of select="."/>
120               </xsl:when>
121               <xsl:otherwise>
122                 <xsl:if test="position()>1">
123                   <xsl:text>, </xsl:text>
124                 </xsl:if>
125                 <xsl:value-of select="."/>
126               </xsl:otherwise>
127             </xsl:choose>
128           </xsl:for-each>
129           <xsl:if test="not (position() = last())">
130             <xsl:text> • </xsl:text>
131           </xsl:if>
132         </xsl:for-each>
133       </span>
134     </xsl:if>
135   </xsl:template>
136
137   <xsl:template name="tag_comma">
138     <xsl:param name="tag" />
139     <xsl:param name="label" />
140     <xsl:if test="marc:datafield[@tag=$tag]">
141       <span class="results_summary">
142         <span class="label">
143         <xsl:value-of select="$label"/>: </span>
144         <xsl:for-each select="marc:datafield[@tag=$tag]">
145           <xsl:call-template name="addClassRtl" />
146           <xsl:for-each select="marc:subfield">
147             <xsl:if test="position()>1">
148               <xsl:text>, </xsl:text>
149             </xsl:if>
150             <xsl:value-of select="."/>
151           </xsl:for-each>
152           <xsl:if test="not (position() = last())">
153             <xsl:text> • </xsl:text>
154           </xsl:if>
155         </xsl:for-each>
156       </span>
157     </xsl:if>
158   </xsl:template>
159
160   <xsl:template name="tag_210">
161     <span class="results_summary">
162       <span class="label">Publication:</span>
163       <xsl:for-each select="marc:datafield[@tag=210]">
164         <span>
165           <xsl:call-template name="addClassRtl" />
166           <xsl:for-each select="marc:subfield">
167             <xsl:choose>
168               <xsl:when test="@code='c' or @code='g'">
169                 <xsl:if test="position()>1">
170                   <xsl:text> : </xsl:text>
171                 </xsl:if>
172                 <xsl:value-of select="."/>
173               </xsl:when>
174               <xsl:otherwise>
175                 <xsl:if test="position()>1">
176                   <xsl:text>, </xsl:text>
177                 </xsl:if>
178                 <xsl:value-of select="."/>
179               </xsl:otherwise>
180             </xsl:choose>
181           </xsl:for-each>
182           <xsl:if test="not (position() = last())">
183             <xsl:text> • </xsl:text>
184           </xsl:if>
185         </span>
186       </xsl:for-each>
187     </span>
188   </xsl:template>
189
190   <xsl:template name="tag_215">
191     <xsl:for-each select="marc:datafield[@tag=215]">
192           <span class="results_summary">
193         <span class="label">Description: </span>
194         <xsl:if test="marc:subfield[@code='a']">
195           <xsl:value-of select="marc:subfield[@code='a']"/>
196         </xsl:if>
197         <xsl:if test="marc:subfield[@code='c']"> :
198           <xsl:value-of select="marc:subfield[@code='c']"/>
199         </xsl:if>
200         <xsl:if test="marc:subfield[@code='d']"> ;
201           <xsl:value-of select="marc:subfield[@code='d']"/>
202         </xsl:if>
203         <xsl:if test="marc:subfield[@code='e']"> +
204           <xsl:value-of select="marc:subfield[@code='e']"/>
205         </xsl:if>
206       </span>
207     </xsl:for-each>
208   </xsl:template>
209
210   <xsl:template name="tag_onesubject">
211     <xsl:choose>
212       <xsl:when test="marc:subfield[@code=9]">
213         <xsl:for-each select="marc:subfield">
214           <xsl:if test="@code='9'">
215             <xsl:variable name="start" select="position()"/>
216             <xsl:variable name="ends">
217               <xsl:for-each select="../marc:subfield[position() &gt; $start]">
218                 <xsl:if test="@code=9">
219                   <xsl:variable name="end" select="position() + $start"/>
220                   <xsl:value-of select="$end"/>
221                   <xsl:text>,</xsl:text>
222                 </xsl:if>
223               </xsl:for-each>
224             </xsl:variable>
225             <xsl:variable name="end">
226               <xsl:choose>
227                 <xsl:when test="string-length($ends) > 0">
228                   <xsl:value-of select="substring-before($ends,',')"/>
229                 </xsl:when>
230                 <xsl:otherwise>
231                   <xsl:text>1000</xsl:text>
232                 </xsl:otherwise>
233               </xsl:choose>
234             </xsl:variable>
235             <xsl:variable name="display">
236               <xsl:for-each select="../marc:subfield[position() &gt; $start and position() &lt; $end and @code!=2 and @code!=3]">
237                 <xsl:value-of select="."/>
238                 <xsl:if test="not(position()=last())">
239                   <xsl:text>, </xsl:text>
240                 </xsl:if>
241               </xsl:for-each>
242             </xsl:variable>
243             <a>
244               <xsl:attribute name="href">
245                 <xsl:text>/cgi-bin/koha/opac-search.pl?q=an:</xsl:text>
246                 <xsl:value-of select="."/>
247               </xsl:attribute>
248               <xsl:choose>
249                 <xsl:when test="string-length($display) &gt; 0">
250                   <xsl:call-template name="chopPunctuation">
251                     <xsl:with-param name="chopString">
252                       <xsl:value-of select="$display"/>
253                     </xsl:with-param>
254                   </xsl:call-template>
255                 </xsl:when>
256                 <xsl:otherwise>
257                   <xsl:value-of select="."/>
258                 </xsl:otherwise>
259               </xsl:choose>
260             </a>
261             <xsl:variable name="ncommas"
262                  select="string-length($ends) - string-length(translate($ends, ',', ''))" />
263             <xsl:if test="$ncommas &gt; 1">
264               <xsl:text> -- </xsl:text>
265             </xsl:if>
266           </xsl:if>
267         </xsl:for-each>
268       </xsl:when>
269       <xsl:when test="marc:subfield[@code=a]">
270         <a>
271           <xsl:attribute name="href">
272             <xsl:text>/cgi-bin/koha/opac-search.pl?q=su:</xsl:text>
273             <xsl:value-of select="marc:subfield[@code='a']"/>
274           </xsl:attribute>
275           <xsl:call-template name="chopPunctuation">
276             <xsl:with-param name="chopString">
277               <xsl:call-template name="subfieldSelect">
278                 <xsl:with-param name="codes">abcdfijkmnpvxyz</xsl:with-param>
279                 <xsl:with-param name="subdivCodes">ijknpxyz</xsl:with-param>
280                 <xsl:with-param name="subdivDelimiter">-- </xsl:with-param>
281               </xsl:call-template>
282             </xsl:with-param>
283           </xsl:call-template>
284         </a>
285       </xsl:when>
286       <xsl:otherwise/>
287     </xsl:choose>
288     <xsl:if test="not(position()=last())">
289       <xsl:text> | </xsl:text>
290     </xsl:if>
291   </xsl:template>
292
293   <xsl:template name="tag_subject">
294     <xsl:param name="tag" />
295     <xsl:param name="label" />
296     <xsl:if test="marc:datafield[@tag=$tag]">
297       <span class="results_summary">
298         <span class="label">
299           <xsl:value-of select="$label"/>
300           <xsl:text>: </xsl:text>
301         </span>
302         <span class="value">
303           <xsl:for-each select="marc:datafield[@tag=$tag]">
304             <xsl:call-template name="tag_onesubject">
305             </xsl:call-template>
306           </xsl:for-each>
307         </span>
308       </span>
309     </xsl:if>
310   </xsl:template>
311
312   <xsl:template name="tag_7xx">
313     <xsl:param name="tag" />
314     <xsl:param name="label" />
315     <xsl:if test="marc:datafield[@tag=$tag]">
316       <span class="results_summary">
317         <span class="label">
318           <xsl:value-of select="$label" />
319           <xsl:text>: </xsl:text>
320         </span>
321         <span class="value">
322           <xsl:for-each select="marc:datafield[@tag=$tag]">
323             <a>
324               <xsl:choose>
325                 <xsl:when test="marc:subfield[@code=9]">
326                   <xsl:attribute name="href">
327                     <xsl:text>/cgi-bin/koha/opac-search.pl?q=an:</xsl:text>
328                     <xsl:value-of select="marc:subfield[@code=9]"/>
329                   </xsl:attribute>
330                 </xsl:when>
331                 <xsl:otherwise>
332                   <xsl:attribute name="href">
333                     <xsl:text>/cgi-bin/koha/opac-search.pl?q=au:</xsl:text>
334                     <xsl:value-of select="marc:subfield[@code='a']"/>
335                     <xsl:text> </xsl:text>
336                     <xsl:value-of select="marc:subfield[@code='b']"/>
337                   </xsl:attribute>
338                 </xsl:otherwise>
339               </xsl:choose>
340               <xsl:for-each select="marc:subfield[@code='a' or @code='b' or @code='4' or @code='c' or @code='d' or @code='f' or @code='g' or @code='p']">
341                 <xsl:choose>
342                   <xsl:when test="@code='9'">
343                   </xsl:when>
344                   <xsl:otherwise>
345                     <xsl:value-of select="."/>
346                   </xsl:otherwise>
347                 </xsl:choose>
348                 <xsl:if test="not(position() = last())">
349                   <xsl:text>, </xsl:text>
350                 </xsl:if>
351               </xsl:for-each>
352             </a>
353             <xsl:if test="not(position() = last())">
354               <span style="padding: 3px;">
355                 <xsl:text>;</xsl:text>
356               </span>
357             </xsl:if>
358           </xsl:for-each>
359         </span>
360       </span>
361     </xsl:if>
362   </xsl:template>
363
364 </xsl:stylesheet>