Merge branch 'master' of git.evergreen-ils.org:Evergreen into template-toolkit-opac
[working/Evergreen.git] / Open-ILS / xsl / MARC21slimUtils.xsl
1 <?xml version='1.0'?>
2 <xsl:stylesheet version="1.0" xmlns:marc="http://www.loc.gov/MARC21/slim"
3         xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
4
5         <!-- 08/08/08: tmee added corrected chopPunctuation templates for 260c -->
6         <!-- 08/19/04: ntra added "marc:" prefix to datafield element -->
7         <!-- 12/14/07: ntra added url encoding template -->
8         <!-- url encoding -->
9
10         <xsl:variable name="ascii">
11                 <xsl:text> !"#$%&amp;'()*+,-./0123456789:;&lt;=&gt;?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~</xsl:text>
12         </xsl:variable>
13
14         <xsl:variable name="latin1">
15                 <xsl:text> ¡¢£¤¥¦§¨©ª«¬­®¯°±²³´µ¶·¸¹º»¼½¾¿ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖ×ØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþÿ</xsl:text>
16         </xsl:variable>
17         <!-- Characters that usually don't need to be escaped -->
18         <xsl:variable name="safe">
19                 <xsl:text>!'()*-.0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz~</xsl:text>
20         </xsl:variable>
21
22         <xsl:variable name="hex">0123456789ABCDEF</xsl:variable>
23
24
25         <xsl:template name="datafield">
26                 <xsl:param name="tag"/>
27                 <xsl:param name="ind1">
28                         <xsl:text> </xsl:text>
29                 </xsl:param>
30                 <xsl:param name="ind2">
31                         <xsl:text> </xsl:text>
32                 </xsl:param>
33                 <xsl:param name="subfields"/>
34                 <xsl:element name="marc:datafield">
35                         <xsl:attribute name="tag">
36                                 <xsl:value-of select="$tag"/>
37                         </xsl:attribute>
38                         <xsl:attribute name="ind1">
39                                 <xsl:value-of select="$ind1"/>
40                         </xsl:attribute>
41                         <xsl:attribute name="ind2">
42                                 <xsl:value-of select="$ind2"/>
43                         </xsl:attribute>
44                         <xsl:copy-of select="$subfields"/>
45                 </xsl:element>
46         </xsl:template>
47
48         <xsl:template name="subfieldSelect">
49                 <xsl:param name="codes">abcdefghijklmnopqrstuvwxyz</xsl:param>
50                 <xsl:param name="delimeter">
51                         <xsl:text> </xsl:text>
52                 </xsl:param>
53                 <xsl:variable name="str">
54                         <xsl:for-each select="marc:subfield">
55                                 <xsl:if test="contains($codes, @code)">
56                                         <xsl:value-of select="text()"/>
57                                         <xsl:value-of select="$delimeter"/>
58                                 </xsl:if>
59                         </xsl:for-each>
60                 </xsl:variable>
61                 <xsl:value-of select="substring($str,1,string-length($str)-string-length($delimeter))"/>
62         </xsl:template>
63
64         <xsl:template name="buildSpaces">
65                 <xsl:param name="spaces"/>
66                 <xsl:param name="char">
67                         <xsl:text> </xsl:text>
68                 </xsl:param>
69                 <xsl:if test="$spaces>0">
70                         <xsl:value-of select="$char"/>
71                         <xsl:call-template name="buildSpaces">
72                                 <xsl:with-param name="spaces" select="$spaces - 1"/>
73                                 <xsl:with-param name="char" select="$char"/>
74                         </xsl:call-template>
75                 </xsl:if>
76         </xsl:template>
77
78         <xsl:template name="chopPunctuation">
79                 <xsl:param name="chopString"/>
80                 <xsl:param name="punctuation">
81                         <xsl:text>.:,;/ </xsl:text>
82                 </xsl:param>
83                 <xsl:variable name="length" select="string-length($chopString)"/>
84                 <xsl:choose>
85                         <xsl:when test="$length=0"/>
86                         <xsl:when test="contains($punctuation, substring($chopString,$length,1))">
87                                 <xsl:call-template name="chopPunctuation">
88                                         <xsl:with-param name="chopString" select="substring($chopString,1,$length - 1)"/>
89                                         <xsl:with-param name="punctuation" select="$punctuation"/>
90                                 </xsl:call-template>
91                         </xsl:when>
92                         <xsl:when test="not($chopString)"/>
93                         <xsl:otherwise>
94                                 <xsl:value-of select="$chopString"/>
95                         </xsl:otherwise>
96                 </xsl:choose>
97         </xsl:template>
98
99         <xsl:template name="chopPunctuationFront">
100                 <xsl:param name="chopString"/>
101                 <xsl:variable name="length" select="string-length($chopString)"/>
102                 <xsl:choose>
103                         <xsl:when test="$length=0"/>
104                         <xsl:when test="contains('.:,;/[ ', substring($chopString,1,1))">
105                                 <xsl:call-template name="chopPunctuationFront">
106                                         <xsl:with-param name="chopString" select="substring($chopString,2,$length - 1)"
107                                         />
108                                 </xsl:call-template>
109                         </xsl:when>
110                         <xsl:when test="not($chopString)"/>
111                         <xsl:otherwise>
112                                 <xsl:value-of select="$chopString"/>
113                         </xsl:otherwise>
114                 </xsl:choose>
115         </xsl:template>
116
117         <xsl:template name="chopPunctuationBack">
118                 <xsl:param name="chopString"/>
119                 <xsl:param name="punctuation">
120                         <xsl:text>.:,;/] </xsl:text>
121                 </xsl:param>
122                 <xsl:variable name="length" select="string-length($chopString)"/>
123                 <xsl:choose>
124                         <xsl:when test="$length=0"/>
125                         <xsl:when test="contains($punctuation, substring($chopString,$length,1))">
126                                 <xsl:call-template name="chopPunctuation">
127                                         <xsl:with-param name="chopString" select="substring($chopString,1,$length - 1)"/>
128                                         <xsl:with-param name="punctuation" select="$punctuation"/>
129                                 </xsl:call-template>
130                         </xsl:when>
131                         <xsl:when test="not($chopString)"/>
132                         <xsl:otherwise>
133                                 <xsl:value-of select="$chopString"/>
134                         </xsl:otherwise>
135                 </xsl:choose>
136         </xsl:template>
137
138         <!-- nate added 12/14/2007 for lccn.loc.gov: url encode ampersand, etc. -->
139         <xsl:template name="url-encode">
140
141                 <xsl:param name="str"/>
142
143                 <xsl:if test="$str">
144                         <xsl:variable name="first-char" select="substring($str,1,1)"/>
145                         <xsl:choose>
146                                 <xsl:when test="contains($safe,$first-char)">
147                                         <xsl:value-of select="$first-char"/>
148                                 </xsl:when>
149                                 <xsl:otherwise>
150                                         <xsl:variable name="codepoint">
151                                                 <xsl:choose>
152                                                         <xsl:when test="contains($ascii,$first-char)">
153                                                                 <xsl:value-of
154                                                                         select="string-length(substring-before($ascii,$first-char)) + 32"
155                                                                 />
156                                                         </xsl:when>
157                                                         <xsl:when test="contains($latin1,$first-char)">
158                                                                 <xsl:value-of
159                                                                         select="string-length(substring-before($latin1,$first-char)) + 160"/>
160                                                                 <!-- was 160 -->
161                                                         </xsl:when>
162                                                         <xsl:otherwise>
163                                                                 <xsl:message terminate="no">Warning: string contains a character
164                                                                         that is out of range! Substituting "?".</xsl:message>
165                                                                 <xsl:text>63</xsl:text>
166                                                         </xsl:otherwise>
167                                                 </xsl:choose>
168                                         </xsl:variable>
169                                         <xsl:variable name="hex-digit1"
170                                                 select="substring($hex,floor($codepoint div 16) + 1,1)"/>
171                                         <xsl:variable name="hex-digit2" select="substring($hex,$codepoint mod 16 + 1,1)"/>
172                                         <!-- <xsl:value-of select="concat('%',$hex-digit2)"/> -->
173                                         <xsl:value-of select="concat('%',$hex-digit1,$hex-digit2)"/>
174                                 </xsl:otherwise>
175                         </xsl:choose>
176                         <xsl:if test="string-length($str) &gt; 1">
177                                 <xsl:call-template name="url-encode">
178                                         <xsl:with-param name="str" select="substring($str,2)"/>
179                                 </xsl:call-template>
180                         </xsl:if>
181                 </xsl:if>
182         </xsl:template>
183 </xsl:stylesheet>
184 <!-- Stylus Studio meta-information - (c)1998-2002 eXcelon Corp.
185 <metaInformation>
186 <scenarios/><MapperInfo srcSchemaPath="" srcSchemaRoot="" srcSchemaPathIsRelative="yes" srcSchemaInterpretAsXML="no" destSchemaPath="" destSchemaRoot="" destSchemaPathIsRelative="yes" destSchemaInterpretAsXML="no"/>
187 </metaInformation>
188 -->