Bug 14716: URI-encode URLs in NORMARC xsl templates.
[koha.git] / koha-tmpl / intranet-tmpl / prog / en / xslt / NORMARCslimUtils.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   xmlns:str="http://exslt.org/strings">
9         <xsl:template name="datafield">
10                 <xsl:param name="tag"/>
11                 <xsl:param name="ind1"><xsl:text> </xsl:text></xsl:param>
12                 <xsl:param name="ind2"><xsl:text> </xsl:text></xsl:param>
13                 <xsl:param name="subfields"/>
14                 <xsl:element name="datafield">
15                         <xsl:attribute name="tag">
16                                 <xsl:value-of select="$tag"/>
17                         </xsl:attribute>
18                         <xsl:attribute name="ind1">
19                                 <xsl:value-of select="$ind1"/>
20                         </xsl:attribute>
21                         <xsl:attribute name="ind2">
22                                 <xsl:value-of select="$ind2"/>
23                         </xsl:attribute>
24                         <xsl:copy-of select="$subfields"/>
25                 </xsl:element>
26         </xsl:template>
27
28         <xsl:template name="subfieldSelect">
29                 <xsl:param name="codes"/>
30                 <xsl:param name="delimeter"><xsl:text> </xsl:text></xsl:param>
31                 <xsl:param name="subdivCodes"/>
32                 <xsl:param name="subdivDelimiter"/>
33         <xsl:param name="prefix"/>
34         <xsl:param name="suffix"/>
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="$prefix"/><xsl:value-of select="text()"/><xsl:value-of select="$suffix"/><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="buildBiblioDefaultViewURL">
61         <xsl:param name="IntranetBiblioDefaultView"/>
62         <xsl:choose>
63             <xsl:when test="$IntranetBiblioDefaultView='normal'">
64                 <xsl:text>/cgi-bin/koha/catalogue/detail.pl?biblionumber=</xsl:text>
65             </xsl:when>
66             <xsl:when test="$IntranetBiblioDefaultView='isbd'">
67                 <xsl:text>/cgi-bin/koha/catalogue/ISBDdetail.pl?biblionumber=</xsl:text>
68             </xsl:when>
69             <xsl:when test="$IntranetBiblioDefaultView='labeled_marc'">
70                 <xsl:text>/cgi-bin/koha/catalogue/labeledMARCdetail.pl?biblionumber=</xsl:text>
71             </xsl:when>
72             <xsl:when test="$IntranetBiblioDefaultView='marc'">
73                 <xsl:text>/cgi-bin/koha/catalogue/MARCdetail.pl?biblionumber=</xsl:text>
74             </xsl:when>
75             <xsl:otherwise>
76                 <xsl:text>/cgi-bin/koha/catalogue/detail.pl?biblionumber=</xsl:text>
77             </xsl:otherwise>
78         </xsl:choose>
79     </xsl:template>
80
81         <xsl:template name="chopPunctuation">
82                 <xsl:param name="chopString"/>
83                 <xsl:variable name="length" select="string-length($chopString)"/>
84                 <xsl:choose>
85                         <xsl:when test="$length=0"/>
86                         <xsl:when test="contains('.:,;/ ', substring($chopString,$length,1))">
87                                 <xsl:call-template name="chopPunctuation">
88                                         <xsl:with-param name="chopString" select="substring($chopString,1,$length - 1)"/>
89                                 </xsl:call-template>
90                         </xsl:when>
91                         <xsl:when test="not($chopString)"/>
92                         <xsl:otherwise><xsl:value-of select="$chopString"/></xsl:otherwise>
93                 </xsl:choose>
94 <xsl:text> </xsl:text>
95         </xsl:template>
96
97         <!-- Function extractControlNumber is used to extract the control number (record number) from MARC tags 773/80/85 [etc.] subfield $w.
98              Parameter: control number string.
99              Assumes LOC convention: (OrgCode)recordNumber.
100              If OrgCode is not present, return full string.
101              Additionally, handle various brackets/parentheses. Chop leading and trailing spaces.
102         -->
103         <xsl:template name="extractControlNumber">
104             <xsl:param name="subfieldW"/>
105             <xsl:variable name="tranW" select="translate($subfieldW,']})&gt;','))))')"/>
106             <xsl:choose>
107               <xsl:when test="contains($tranW,')')">
108                 <xsl:value-of select="normalize-space(translate(substring-after($tranW,')'),'[]{}()&lt;&gt;',''))"/>
109               </xsl:when>
110               <xsl:otherwise>
111                 <xsl:value-of select="normalize-space($subfieldW)"/>
112               </xsl:otherwise>
113             </xsl:choose>
114         </xsl:template>
115
116 <xsl:template name="nameABCDQ">
117 <xsl:call-template name="chopPunctuation">
118 <xsl:with-param name="chopString">
119 <xsl:call-template name="subfieldSelect">
120 <xsl:with-param name="codes">aq</xsl:with-param>
121 </xsl:call-template>
122 </xsl:with-param>
123 <xsl:with-param name="punctuation">
124 <xsl:text>:,;/ </xsl:text>
125 </xsl:with-param>
126 </xsl:call-template>
127 <xsl:call-template name="termsOfAddress"/>
128 </xsl:template>
129
130 <xsl:template name="nameABCDN">
131 <xsl:for-each select="marc:subfield[@code='a']">
132 <xsl:call-template name="chopPunctuation">
133 <xsl:with-param name="chopString" select="."/>
134 </xsl:call-template>
135 </xsl:for-each>
136 <xsl:for-each select="marc:subfield[@code='b']">
137 <xsl:value-of select="."/>
138 </xsl:for-each>
139 <xsl:if test="marc:subfield[@code='c'] or marc:subfield[@code='d'] or marc:subfield[@code='n']">
140 <xsl:call-template name="subfieldSelect">
141 <xsl:with-param name="codes">cdn</xsl:with-param>
142 </xsl:call-template>
143 </xsl:if>
144 </xsl:template>
145
146 <xsl:template name="nameACDEQ">
147 <xsl:call-template name="subfieldSelect">
148 <xsl:with-param name="codes">acdeq</xsl:with-param>
149 </xsl:call-template>
150 </xsl:template>
151
152 <xsl:template name="termsOfAddress">
153 <xsl:if test="marc:subfield[@code='b' or @code='c']">
154 <xsl:call-template name="chopPunctuation">
155 <xsl:with-param name="chopString">
156 <xsl:call-template name="subfieldSelect">
157 <xsl:with-param name="codes">bc</xsl:with-param>
158 </xsl:call-template>
159 </xsl:with-param>
160 </xsl:call-template>
161 </xsl:if>
162 </xsl:template>
163
164     <!-- Function m880Select:  Display Alternate Graphic Representation (MARC 880) for selected latin "base"tags
165         - should be called immediately before the corresonding latin tags are processed 
166         - tags in right-to-left languages are displayed floating right
167         * Parameter:
168            + basetags: display these tags if found in linkage section ( subfield 6) of tag 880
169            + codes: display these subfields codes
170         * Options: 
171             - class: wrap output in <span class="$class">...</span>
172             - label: prefix each(!) tag with label $label
173             - bibno: link to biblionumber $bibno
174             - index: build a search link using index $index with subfield $a as key; if subfield $9 is present use index 'an' with key $9 instead.
175          * Limitations:
176             - displays every field on a separate line (to switch between rtl and ltr)
177          * Pitfalls:
178            (!) output might be empty
179     -->
180     <xsl:template name="m880Select">
181          <xsl:param name="basetags"/> <!-- e.g.  100,700,110,710 -->
182         <xsl:param name="codes"/> <!-- e.g. abc  -->
183         <xsl:param name="class"/> <!-- e.g. results_summary -->
184         <xsl:param name="label"/> <!-- e.g.  Edition -->
185         <xsl:param name="bibno"/>
186         <xsl:param name="index"/> <!-- e.g.  au -->
187
188         <xsl:for-each select="marc:datafield[@tag=880]">
189             <xsl:variable name="code6" select="marc:subfield[@code=6]"/>
190             <xsl:if test="contains(string($basetags), substring($code6,1,3))">
191                 <span>
192                     <xsl:if test="boolean($class)">
193                         <xsl:attribute name="class"><xsl:value-of select="$class"/></xsl:attribute>
194                     </xsl:if>
195                     <xsl:choose>
196                         <!-- display right-to-left tags floating right of their left-to-right counterparts -->
197                         <xsl:when test="substring($code6,10,2) ='/r'">
198                             <xsl:attribute name="style">display:block; text-align:right; float:right; width:50%; padding-left:20px</xsl:attribute>
199                             <xsl:attribute name="dir">rtl</xsl:attribute>
200                         </xsl:when>
201                         <xsl:otherwise>
202                             <xsl:attribute name="style">display:block; </xsl:attribute>
203                         </xsl:otherwise>
204                     </xsl:choose>
205                     <xsl:if test="boolean($label)">
206                         <span class="label">
207                             <xsl:value-of select="$label"/>
208                         </span>
209                     </xsl:if>
210                     <xsl:variable name="str">
211                         <xsl:for-each select="marc:subfield">
212                             <xsl:if test="contains($codes, @code)">
213                                 <xsl:value-of select="text()"/>
214                                 <xsl:text> </xsl:text>
215                             </xsl:if>
216                         </xsl:for-each>
217                     </xsl:variable>
218                     <xsl:choose>
219                         <xsl:when test="boolean($bibno)">
220                             <a>
221                                 <xsl:attribute name="href">/cgi-bin/koha/opac-detail.pl?biblionumber=<xsl:value-of  select="$bibno"/></xsl:attribute>
222                                 <xsl:value-of select="$str"/>
223                             </a>
224                         </xsl:when>
225                        <xsl:when test="boolean($index) and boolean(marc:subfield[@code=9])">
226                             <a>
227                                 <xsl:attribute name="href">/cgi-bin/koha/opac-search.pl?q=an:<xsl:value-of  select="marc:subfield[@code=9]"/></xsl:attribute>
228                                   <xsl:value-of select="$str"/>
229                             </a>
230                         </xsl:when>
231                         <xsl:when test="boolean($index)">
232                             <a>
233                                 <xsl:attribute name="href">/cgi-bin/koha/opac-search.pl?q=<xsl:value-of  select="$index"/>:<xsl:value-of  select="str:encode-uri(marc:subfield[@code='a'], true())"/></xsl:attribute>
234                                 <xsl:value-of select="$str"/>
235                             </a>
236                         </xsl:when>
237                         <xsl:otherwise>
238                             <xsl:value-of select="$str"/>
239                         </xsl:otherwise>
240                     </xsl:choose>
241                 </span>
242             </xsl:if>
243         </xsl:for-each>
244
245     </xsl:template>
246
247 </xsl:stylesheet>
248
249 <!-- Stylus Studio meta-information - (c)1998-2002 eXcelon Corp.
250 <metaInformation>
251 <scenarios/><MapperInfo srcSchemaPath="" srcSchemaRoot="" srcSchemaPathIsRelative="yes" srcSchemaInterpretAsXML="no" destSchemaPath="" destSchemaRoot="" destSchemaPathIsRelative="yes" destSchemaInterpretAsXML="no"/>
252 </metaInformation>
253 -->