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