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