Merge remote-tracking branch 'origin/new/bug_5327'
[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:value-of select="$display"/>
248             </a>
249             <xsl:variable name="ncommas"
250                  select="string-length($ends) - string-length(translate($ends, ',', ''))" />
251             <xsl:if test="$ncommas &gt; 1">
252               <xsl:text> -- </xsl:text>
253             </xsl:if>
254           </xsl:if>
255         </xsl:for-each>
256       </xsl:when>
257       <xsl:otherwise>
258         <a>
259           <xsl:attribute name="href">
260             <xsl:text>/cgi-bin/koha/opac-search.pl?q=su:</xsl:text>
261             <xsl:value-of select="marc:subfield[@code='a']"/>
262           </xsl:attribute>
263           <xsl:call-template name="chopPunctuation">
264             <xsl:with-param name="chopString">
265               <xsl:call-template name="subfieldSelect">
266                 <xsl:with-param name="codes">abcdfijkmnpvxyz</xsl:with-param>
267                 <xsl:with-param name="subdivCodes">ijknpxyz</xsl:with-param>
268                 <xsl:with-param name="subdivDelimiter">-- </xsl:with-param>
269               </xsl:call-template>
270             </xsl:with-param>
271           </xsl:call-template>
272         </a>
273       </xsl:otherwise>
274     </xsl:choose>
275     <xsl:if test="not(position()=last())">
276       <xsl:text> | </xsl:text>
277     </xsl:if>
278   </xsl:template>
279
280   <xsl:template name="tag_subject">
281     <xsl:param name="tag" />
282     <xsl:param name="label" />
283     <xsl:if test="marc:datafield[@tag=$tag]">
284       <span class="results_summary">
285         <span class="label">
286           <xsl:value-of select="$label"/>
287           <xsl:text>: </xsl:text>
288         </span>
289         <span class="value">
290           <xsl:for-each select="marc:datafield[@tag=$tag]">
291             <xsl:call-template name="tag_onesubject">
292             </xsl:call-template>
293           </xsl:for-each>
294         </span>
295       </span>
296     </xsl:if>
297   </xsl:template>
298
299   <xsl:template name="tag_7xx">
300     <xsl:param name="tag" />
301     <xsl:param name="label" />
302     <xsl:if test="marc:datafield[@tag=$tag]">
303       <span class="results_summary">
304         <span class="label">
305           <xsl:value-of select="$label" />
306           <xsl:text>: </xsl:text>
307         </span>
308         <span class="value">
309           <xsl:for-each select="marc:datafield[@tag=$tag]">
310             <a>
311               <xsl:choose>
312                 <xsl:when test="marc:subfield[@code=9]">
313                   <xsl:attribute name="href">
314                     <xsl:text>/cgi-bin/koha/opac-search.pl?q=an:</xsl:text>
315                     <xsl:value-of select="marc:subfield[@code=9]"/>
316                   </xsl:attribute>
317                 </xsl:when>
318                 <xsl:otherwise>
319                   <xsl:attribute name="href">
320                     <xsl:text>/cgi-bin/koha/opac-search.pl?q=au:</xsl:text>
321                     <xsl:value-of select="marc:subfield[@code='a']"/>
322                     <xsl:text> </xsl:text>
323                     <xsl:value-of select="marc:subfield[@code='b']"/>
324                   </xsl:attribute>
325                 </xsl:otherwise>
326               </xsl:choose>
327               <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']">
328                 <xsl:choose>
329                   <xsl:when test="@code='9'">
330                   </xsl:when>
331                   <xsl:otherwise>
332                     <xsl:value-of select="."/>
333                   </xsl:otherwise>
334                 </xsl:choose>
335                 <xsl:if test="not(position() = last())">
336                   <xsl:text>, </xsl:text>
337                 </xsl:if>
338               </xsl:for-each>
339             </a>
340             <xsl:if test="not(position() = last())">
341               <span style="padding: 3px;">
342                 <xsl:text>;</xsl:text>
343               </span>
344             </xsl:if>
345           </xsl:for-each>
346         </span>
347       </span>
348     </xsl:if>
349   </xsl:template>
350
351 </xsl:stylesheet>