Forward Port 3.8.1 Upgrade Script
[evergreen-equinox.git] / Open-ILS / src / sql / Pg / version-upgrade / 3.8.0-3.8.1-upgrade-db.sql
1 --Upgrade Script for 3.8.0 to 3.8.1
2 \set eg_version '''3.8.1'''
3 BEGIN;
4 INSERT INTO config.upgrade_log (version, applied_to) VALUES ('3.8.1', :eg_version);
5 \r
6 SELECT evergreen.upgrade_deps_block_check('1309', :eg_version);\r
7 \r
8 ALTER TABLE asset.course_module_term\r
9         DROP CONSTRAINT course_module_term_name_key;\r
10 \r
11 ALTER TABLE asset.course_module_term\r
12         ADD CONSTRAINT cmt_once_per_owning_lib UNIQUE (owning_lib, name);\r
13 \r
14
15 SELECT evergreen.upgrade_deps_block_check('1311', :eg_version);
16
17 CREATE OR REPLACE FUNCTION biblio.extract_located_uris( bib_id BIGINT, marcxml TEXT, editor_id INT ) RETURNS VOID AS $func$
18 DECLARE
19     uris            TEXT[];
20     uri_xml         TEXT;
21     uri_label       TEXT;
22     uri_href        TEXT;
23     uri_use         TEXT;
24     uri_owner_list  TEXT[];
25     uri_owner       TEXT;
26     uri_owner_id    INT;
27     uri_id          INT;
28     uri_cn_id       INT;
29     uri_map_id      INT;
30     current_uri     INT;
31     current_map     INT;
32     uri_map_count   INT;
33     current_uri_map_list    INT[];
34     current_map_owner_list  INT[];
35
36 BEGIN
37
38     uris := oils_xpath('//*[@tag="856" and (@ind1="4" or @ind1="1") and (@ind2="0" or @ind2="1")]',marcxml);
39     IF ARRAY_UPPER(uris,1) > 0 THEN
40         FOR i IN 1 .. ARRAY_UPPER(uris, 1) LOOP
41             -- First we pull info out of the 856
42             uri_xml     := uris[i];
43
44             uri_href    := (oils_xpath('//*[@code="u"]/text()',uri_xml))[1];
45             uri_label   := (oils_xpath('//*[@code="y"]/text()|//*[@code="3"]/text()',uri_xml))[1];
46             uri_use     := (oils_xpath('//*[@code="z"]/text()|//*[@code="2"]/text()|//*[@code="n"]/text()',uri_xml))[1];
47
48             IF uri_label IS NULL THEN
49                 uri_label := uri_href;
50             END IF;
51             CONTINUE WHEN uri_href IS NULL;
52
53             -- Get the distinct list of libraries wanting to use 
54             SELECT  ARRAY_AGG(
55                         DISTINCT REGEXP_REPLACE(
56                             x,
57                             $re$^.*?\((\w+)\).*$$re$,
58                             E'\\1'
59                         )
60                     ) INTO uri_owner_list
61               FROM  UNNEST(
62                         oils_xpath(
63                             '//*[@code="9"]/text()|//*[@code="w"]/text()|//*[@code="n"]/text()',
64                             uri_xml
65                         )
66                     )x;
67
68             IF ARRAY_UPPER(uri_owner_list,1) > 0 THEN
69
70                 -- look for a matching uri
71                 IF uri_use IS NULL THEN
72                     SELECT id INTO uri_id
73                         FROM asset.uri
74                         WHERE label = uri_label AND href = uri_href AND use_restriction IS NULL AND active
75                         ORDER BY id LIMIT 1;
76                     IF NOT FOUND THEN -- create one
77                         INSERT INTO asset.uri (label, href, use_restriction) VALUES (uri_label, uri_href, uri_use);
78                         SELECT id INTO uri_id
79                             FROM asset.uri
80                             WHERE label = uri_label AND href = uri_href AND use_restriction IS NULL AND active;
81                     END IF;
82                 ELSE
83                     SELECT id INTO uri_id
84                         FROM asset.uri
85                         WHERE label = uri_label AND href = uri_href AND use_restriction = uri_use AND active
86                         ORDER BY id LIMIT 1;
87                     IF NOT FOUND THEN -- create one
88                         INSERT INTO asset.uri (label, href, use_restriction) VALUES (uri_label, uri_href, uri_use);
89                         SELECT id INTO uri_id
90                             FROM asset.uri
91                             WHERE label = uri_label AND href = uri_href AND use_restriction = uri_use AND active;
92                     END IF;
93                 END IF;
94
95                 FOR j IN 1 .. ARRAY_UPPER(uri_owner_list, 1) LOOP
96                     uri_owner := uri_owner_list[j];
97
98                     SELECT id INTO uri_owner_id FROM actor.org_unit WHERE shortname = BTRIM(REPLACE(uri_owner,chr(160),''));
99                     CONTINUE WHEN NOT FOUND;
100
101                     -- we need a call number to link through
102                     SELECT id INTO uri_cn_id FROM asset.call_number WHERE owning_lib = uri_owner_id AND record = bib_id AND label = '##URI##' AND NOT deleted;
103                     IF NOT FOUND THEN
104                         INSERT INTO asset.call_number (owning_lib, record, create_date, edit_date, creator, editor, label)
105                             VALUES (uri_owner_id, bib_id, 'now', 'now', editor_id, editor_id, '##URI##');
106                         SELECT id INTO uri_cn_id FROM asset.call_number WHERE owning_lib = uri_owner_id AND record = bib_id AND label = '##URI##' AND NOT deleted;
107                     END IF;
108
109                     -- now, link them if they're not already
110                     SELECT id INTO uri_map_id FROM asset.uri_call_number_map WHERE call_number = uri_cn_id AND uri = uri_id;
111                     IF NOT FOUND THEN
112                         INSERT INTO asset.uri_call_number_map (call_number, uri) VALUES (uri_cn_id, uri_id);
113                         SELECT id INTO uri_map_id FROM asset.uri_call_number_map WHERE call_number = uri_cn_id AND uri = uri_id;
114                     END IF;
115
116                     current_uri_map_list := current_uri_map_list || uri_map_id;
117                     current_map_owner_list := current_map_owner_list || uri_cn_id;
118
119                 END LOOP;
120
121             END IF;
122
123         END LOOP;
124     END IF;
125
126     -- Clear any orphaned URIs, URI mappings and call
127     -- numbers for this bib that weren't mapped above.
128     FOR current_map IN
129         SELECT  m.id
130           FROM  asset.uri_call_number_map m
131                 LEFT JOIN asset.call_number cn ON (cn.id = m.call_number)
132           WHERE cn.record = bib_id
133                 AND cn.label = '##URI##'
134                 AND (NOT (m.id = ANY (current_uri_map_list))
135                      OR current_uri_map_list is NULL)
136     LOOP
137         SELECT uri INTO current_uri FROM asset.uri_call_number_map WHERE id = current_map;
138         DELETE FROM asset.uri_call_number_map WHERE id = current_map;
139
140         SELECT COUNT(*) INTO uri_map_count FROM asset.uri_call_number_map WHERE uri = current_uri;
141         IF uri_map_count = 0 THEN
142             DELETE FROM asset.uri WHERE id = current_uri;
143         END IF;
144     END LOOP;
145
146     UPDATE asset.call_number
147     SET deleted = TRUE, edit_date = now(), editor = editor_id
148     WHERE id IN (
149         SELECT  id
150           FROM  asset.call_number
151           WHERE record = bib_id
152                 AND label = '##URI##'
153                 AND NOT deleted
154                 AND (NOT (id = ANY (current_map_owner_list))
155                      OR current_map_owner_list is NULL)
156     );
157
158     RETURN;
159 END;
160 $func$ LANGUAGE PLPGSQL;
161
162 -- Remove existing orphaned URIs from the database.
163 DELETE FROM asset.uri
164 WHERE id IN
165 (
166 SELECT uri.id
167 FROM asset.uri
168 LEFT JOIN asset.uri_call_number_map
169 ON uri_call_number_map.uri = uri.id
170 LEFT JOIN serial.item
171 ON item.uri = uri.id
172 WHERE uri_call_number_map IS NULL
173 AND item IS NULL
174 );
175
176
177
178 SELECT evergreen.upgrade_deps_block_check('1312', :eg_version);
179
180 CREATE INDEX aum_editor ON actor.usr_message (editor);
181
182
183 SELECT evergreen.upgrade_deps_block_check('1313', :eg_version); -- alynn26
184
185 INSERT INTO config.workstation_setting_type (name, grp, datatype, label)
186 VALUES (
187     'eg.grid.cat.bucket.batch_hold.view', 'gui', 'object',
188     oils_i18n_gettext(
189         'eg.grid.cat.bucket.batch_hold.view',
190         'Grid Config: eg.grid.cat.bucket.batch_hold.view',
191         'cwst', 'label'
192     )
193 ), (
194     'eg.grid.cat.bucket.batch_hold.pending', 'gui', 'object',
195     oils_i18n_gettext(
196         'eg.grid.cat.bucket.batch_hold.pending',
197         'Grid Config: eg.grid.cat.bucket.batch_hold.pending',
198         'cwst', 'label'
199     )
200 ), (
201     'eg.grid.cat.bucket.batch_hold.events', 'gui', 'object',
202     oils_i18n_gettext(
203         'eg.grid.cat.bucket.batch_hold.events',
204         'Grid Config: eg.grid.cat.bucket.batch_hold.events',
205         'cwst', 'label'
206     )
207 ), (
208     'eg.grid.cat.bucket.batch_hold.list', 'gui', 'object',
209     oils_i18n_gettext(
210         'eg.grid.cat.bucket.batch_hold.list',
211         'Grid Config: eg.grid.cat.bucket.batch_hold.list',
212         'cwst', 'label'
213     )
214 );
215
216
217 SELECT evergreen.upgrade_deps_block_check('1319', :eg_version);
218
219 DO $SQL$
220 BEGIN
221     
222     PERFORM TRUE FROM config.usr_setting_type WHERE name = 'cat.copy.templates';
223
224     IF NOT FOUND THEN -- no matching user setting
225
226         PERFORM TRUE FROM config.workstation_setting_type WHERE name = 'cat.copy.templates';
227
228         IF NOT FOUND THEN
229             -- no matching workstation setting
230             -- Migrate the existing user setting and its data to the new name.
231
232             UPDATE config.usr_setting_type 
233             SET name = 'cat.copy.templates' 
234             WHERE name = 'webstaff.cat.copy.templates';
235
236             UPDATE actor.usr_setting
237             SET name = 'cat.copy.templates' 
238             WHERE name = 'webstaff.cat.copy.templates';
239
240         END IF;
241     END IF;
242
243 END; 
244 $SQL$;
245
246
247
248 SELECT evergreen.upgrade_deps_block_check('1325', :eg_version);
249
250 UPDATE config.xml_transform SET xslt=$XSLT$<?xml version="1.0" encoding="UTF-8"?>
251 <xsl:stylesheet version="1.0" xmlns:mads="http://www.loc.gov/mads/v2"
252         xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:marc="http://www.loc.gov/MARC21/slim"
253         xmlns:xsl="http://www.w3.org/1999/XSL/Transform" exclude-result-prefixes="marc">
254         <xsl:output method="xml" indent="yes" encoding="UTF-8"/>
255         <xsl:strip-space elements="*"/>
256
257         <xsl:variable name="ascii">
258                 <xsl:text> !"#$%&amp;'()*+,-./0123456789:;&lt;=&gt;?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~</xsl:text>
259         </xsl:variable>
260
261         <xsl:variable name="latin1">
262                 <xsl:text> ¡¢£¤¥¦§¨©ª«¬\ad®¯°±²³´µ¶·¸¹º»¼½¾¿ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖ×ØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþÿ</xsl:text>
263         </xsl:variable>
264         <!-- Characters that usually don't need to be escaped -->
265         <xsl:variable name="safe">
266                 <xsl:text>!'()*-.0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz~</xsl:text>
267         </xsl:variable>
268
269         <xsl:variable name="hex">0123456789ABCDEF</xsl:variable>
270
271
272         <xsl:template name="datafield">
273                 <xsl:param name="tag"/>
274                 <xsl:param name="ind1">
275                         <xsl:text> </xsl:text>
276                 </xsl:param>
277                 <xsl:param name="ind2">
278                         <xsl:text> </xsl:text>
279                 </xsl:param>
280                 <xsl:param name="subfields"/>
281                 <xsl:element name="marc:datafield">
282                         <xsl:attribute name="tag">
283                                 <xsl:value-of select="$tag"/>
284                         </xsl:attribute>
285                         <xsl:attribute name="ind1">
286                                 <xsl:value-of select="$ind1"/>
287                         </xsl:attribute>
288                         <xsl:attribute name="ind2">
289                                 <xsl:value-of select="$ind2"/>
290                         </xsl:attribute>
291                         <xsl:copy-of select="$subfields"/>
292                 </xsl:element>
293         </xsl:template>
294
295         <xsl:template name="subfieldSelect">
296                 <xsl:param name="codes">abcdefghijklmnopqrstuvwxyz</xsl:param>
297                 <xsl:param name="delimeter">
298                         <xsl:text> </xsl:text>
299                 </xsl:param>
300                 <xsl:variable name="str">
301                         <xsl:for-each select="marc:subfield">
302                                 <xsl:if test="contains($codes, @code)">
303                                         <xsl:value-of select="text()"/>
304                                         <xsl:value-of select="$delimeter"/>
305                                 </xsl:if>
306                         </xsl:for-each>
307                 </xsl:variable>
308                 <xsl:value-of select="substring($str,1,string-length($str)-string-length($delimeter))"/>
309         </xsl:template>
310
311         <xsl:template name="buildSpaces">
312                 <xsl:param name="spaces"/>
313                 <xsl:param name="char">
314                         <xsl:text> </xsl:text>
315                 </xsl:param>
316                 <xsl:if test="$spaces>0">
317                         <xsl:value-of select="$char"/>
318                         <xsl:call-template name="buildSpaces">
319                                 <xsl:with-param name="spaces" select="$spaces - 1"/>
320                                 <xsl:with-param name="char" select="$char"/>
321                         </xsl:call-template>
322                 </xsl:if>
323         </xsl:template>
324
325         <xsl:template name="chopPunctuation">
326                 <xsl:param name="chopString"/>
327                 <xsl:param name="punctuation">
328                         <xsl:text>.:,;/ </xsl:text>
329                 </xsl:param>
330                 <xsl:variable name="length" select="string-length($chopString)"/>
331                 <xsl:choose>
332                         <xsl:when test="$length=0"/>
333                         <xsl:when test="contains($punctuation, substring($chopString,$length,1))">
334                                 <xsl:call-template name="chopPunctuation">
335                                         <xsl:with-param name="chopString" select="substring($chopString,1,$length - 1)"/>
336                                         <xsl:with-param name="punctuation" select="$punctuation"/>
337                                 </xsl:call-template>
338                         </xsl:when>
339                         <xsl:when test="not($chopString)"/>
340                         <xsl:otherwise>
341                                 <xsl:value-of select="$chopString"/>
342                         </xsl:otherwise>
343                 </xsl:choose>
344         </xsl:template>
345
346         <xsl:template name="chopPunctuationFront">
347                 <xsl:param name="chopString"/>
348                 <xsl:variable name="length" select="string-length($chopString)"/>
349                 <xsl:choose>
350                         <xsl:when test="$length=0"/>
351                         <xsl:when test="contains('.:,;/[ ', substring($chopString,1,1))">
352                                 <xsl:call-template name="chopPunctuationFront">
353                                         <xsl:with-param name="chopString" select="substring($chopString,2,$length - 1)"
354                                         />
355                                 </xsl:call-template>
356                         </xsl:when>
357                         <xsl:when test="not($chopString)"/>
358                         <xsl:otherwise>
359                                 <xsl:value-of select="$chopString"/>
360                         </xsl:otherwise>
361                 </xsl:choose>
362         </xsl:template>
363
364         <xsl:template name="chopPunctuationBack">
365                 <xsl:param name="chopString"/>
366                 <xsl:param name="punctuation">
367                         <xsl:text>.:,;/] </xsl:text>
368                 </xsl:param>
369                 <xsl:variable name="length" select="string-length($chopString)"/>
370                 <xsl:choose>
371                         <xsl:when test="$length=0"/>
372                         <xsl:when test="contains($punctuation, substring($chopString,$length,1))">
373                                 <xsl:call-template name="chopPunctuation">
374                                         <xsl:with-param name="chopString" select="substring($chopString,1,$length - 1)"/>
375                                         <xsl:with-param name="punctuation" select="$punctuation"/>
376                                 </xsl:call-template>
377                         </xsl:when>
378                         <xsl:when test="not($chopString)"/>
379                         <xsl:otherwise>
380                                 <xsl:value-of select="$chopString"/>
381                         </xsl:otherwise>
382                 </xsl:choose>
383         </xsl:template>
384
385         <!-- nate added 12/14/2007 for lccn.loc.gov: url encode ampersand, etc. -->
386         <xsl:template name="url-encode">
387
388                 <xsl:param name="str"/>
389
390                 <xsl:if test="$str">
391                         <xsl:variable name="first-char" select="substring($str,1,1)"/>
392                         <xsl:choose>
393                                 <xsl:when test="contains($safe,$first-char)">
394                                         <xsl:value-of select="$first-char"/>
395                                 </xsl:when>
396                                 <xsl:otherwise>
397                                         <xsl:variable name="codepoint">
398                                                 <xsl:choose>
399                                                         <xsl:when test="contains($ascii,$first-char)">
400                                                                 <xsl:value-of
401                                                                         select="string-length(substring-before($ascii,$first-char)) + 32"
402                                                                 />
403                                                         </xsl:when>
404                                                         <xsl:when test="contains($latin1,$first-char)">
405                                                                 <xsl:value-of
406                                                                         select="string-length(substring-before($latin1,$first-char)) + 160"/>
407                                                                 <!-- was 160 -->
408                                                         </xsl:when>
409                                                         <xsl:otherwise>
410                                                                 <xsl:message terminate="no">Warning: string contains a character
411                                                                         that is out of range! Substituting "?".</xsl:message>
412                                                                 <xsl:text>63</xsl:text>
413                                                         </xsl:otherwise>
414                                                 </xsl:choose>
415                                         </xsl:variable>
416                                         <xsl:variable name="hex-digit1"
417                                                 select="substring($hex,floor($codepoint div 16) + 1,1)"/>
418                                         <xsl:variable name="hex-digit2" select="substring($hex,$codepoint mod 16 + 1,1)"/>
419                                         <!-- <xsl:value-of select="concat('%',$hex-digit2)"/> -->
420                                         <xsl:value-of select="concat('%',$hex-digit1,$hex-digit2)"/>
421                                 </xsl:otherwise>
422                         </xsl:choose>
423                         <xsl:if test="string-length($str) &gt; 1">
424                                 <xsl:call-template name="url-encode">
425                                         <xsl:with-param name="str" select="substring($str,2)"/>
426                                 </xsl:call-template>
427                         </xsl:if>
428                 </xsl:if>
429         </xsl:template>
430
431
432 <!--
433 2.15  reversed genre and setAuthority template order under relatedTypeAttribute                       tmee 11/13/2018
434 2.14    Fixed bug in mads:geographic attributes syntax                                      ws   05/04/2016             
435 2.13    fixed repeating <geographic>                                                                                                            tmee 01/31/2014
436 2.12    added $2 authority for <classification>                                                                                         tmee 09/18/2012
437 2.11    added delimiters between <classification> subfields                                                                     tmee 09/18/2012
438 2.10    fixed type="other" and type="otherType" for mads:related                                                        tmee 09/16/2011
439 2.09    fixed professionTerm and genreTerm empty tag error                                                                      tmee 09/16/2011
440 2.08    fixed marc:subfield @code='i' matching error                                                                            tmee 09/16/2011
441 2.07    fixed 555 duplication error                                                                                                                     tmee 08/10/2011 
442 2.06    fixed topic subfield error                                                                                                                      tmee 08/10/2011 
443 2.05    fixed title subfield error                                                                                                                      tmee 06/20/2011 
444 2.04    fixed geographicSubdivision mapping for authority element                                                       tmee 06/16/2011
445 2.03    added classification for 053, 055, 060, 065, 070, 080, 082, 083, 086, 087                       tmee 06/03/2011         
446 2.02    added descriptionStandard for 008/10                                                                                            tmee 04/27/2011
447 2.01    added extensions for 046, 336, 370, 374, 375, 376                                                                       tmee 04/08/2011
448 2.00    redefined imported MODS elements in version 1.0 to MADS elements in version 2.0         tmee 02/08/2011
449 1.08    added 372 subfields $a $s $t for <fieldOfActivity>                                                                      tmee 06/24/2010
450 1.07    removed role/roleTerm 100, 110, 111, 400, 410, 411, 500, 510, 511, 700, 710, 711        tmee 06/24/2010
451 1.06    added strip-space                                                                                                                                       tmee 06/24/2010
452 1.05    added subfield $a for 130, 430, 530                                                                                                     tmee 06/21/2010
453 1.04    fixed 550 z omission                                                                                                                            ntra 08/11/2008
454 1.03    removed duplication of 550 $a text                                                                                                      tmee 11/01/2006
455 1.02    fixed namespace references between mads and mods                                                                        ntra 10/06/2006
456 1.01    revised                                                                                                                                                         rgue/jrad 11/29/05
457 1.00    adapted from MARC21Slim2MODS3.xsl                                                                                                       ntra 07/06/05
458 -->
459
460         <!-- authority attribute defaults to 'naf' if not set using this authority parameter, for <authority> descriptors: name, titleInfo, geographic -->
461         <xsl:param name="authority"/>
462         <xsl:variable name="auth">
463                 <xsl:choose>
464                         <xsl:when test="$authority">
465                                 <xsl:value-of select="$authority"/>
466                         </xsl:when>
467                         <xsl:otherwise>naf</xsl:otherwise>
468                 </xsl:choose>
469         </xsl:variable>
470         <xsl:variable name="controlField008" select="marc:controlfield[@tag='008']"/>
471         <xsl:variable name="controlField008-06"
472                 select="substring(descendant-or-self::marc:controlfield[@tag=008],7,1)"/>
473         <xsl:variable name="controlField008-11"
474                 select="substring(descendant-or-self::marc:controlfield[@tag=008],12,1)"/>
475         <xsl:variable name="controlField008-14"
476                 select="substring(descendant-or-self::marc:controlfield[@tag=008],15,1)"/>
477         <xsl:template match="/">
478                 <xsl:choose>
479                         <xsl:when test="descendant-or-self::marc:collection">
480                                 <mads:madsCollection xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
481                                         xsi:schemaLocation="http://www.loc.gov/mads/v2 http://www.loc.gov/standards/mads/v2/mads-2-0.xsd">
482                                         <xsl:for-each select="descendant-or-self::marc:collection/marc:record">
483                                                 <mads:mads version="2.0">
484                                                         <xsl:call-template name="marcRecord"/>
485                                                 </mads:mads>
486                                         </xsl:for-each>
487                                 </mads:madsCollection>
488                         </xsl:when>
489                         <xsl:otherwise>
490                                 <mads:mads version="2.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
491                                         xsi:schemaLocation="http://www.loc.gov/mads/v2 http://www.loc.gov/standards/mads/mads-2-0.xsd">
492                                         <xsl:for-each select="descendant-or-self::marc:record">
493                                                 <xsl:call-template name="marcRecord"/>
494                                         </xsl:for-each>
495                                 </mads:mads>
496                         </xsl:otherwise>
497                 </xsl:choose>
498         </xsl:template>
499
500         <xsl:template name="marcRecord">
501                 <mads:authority>
502                         <!-- 2.04 -->
503                         <xsl:choose>
504                                 <xsl:when test="$controlField008-06='d'">
505                                         <xsl:attribute name="geographicSubdivision">
506                                                 <xsl:text>direct</xsl:text>
507                                         </xsl:attribute>
508                                 </xsl:when>
509                                 <xsl:when test="$controlField008-06='i'">
510                                         <xsl:attribute name="geographicSubdivision">
511                                                 <xsl:text>indirect</xsl:text>
512                                         </xsl:attribute>
513                                 </xsl:when>
514                                 <xsl:when test="$controlField008-06='n'">
515                                         <xsl:attribute name="geographicSubdivision">
516                                                 <xsl:text>not applicable</xsl:text>
517                                         </xsl:attribute>
518                                 </xsl:when>
519                         </xsl:choose>
520                         
521                         <xsl:apply-templates select="marc:datafield[100 &lt;= @tag  and @tag &lt; 200]"/>               
522                 </mads:authority>
523
524                 <!-- related -->
525                 <xsl:apply-templates
526                         select="marc:datafield[500 &lt;= @tag and @tag &lt;= 585]|marc:datafield[700 &lt;= @tag and @tag &lt;= 785]"/>
527
528                 <!-- variant -->
529                 <xsl:apply-templates select="marc:datafield[400 &lt;= @tag and @tag &lt;= 485]"/>
530
531                 <!-- notes -->
532                 <xsl:apply-templates select="marc:datafield[667 &lt;= @tag and @tag &lt;= 688]"/>
533
534                 <!-- url -->
535                 <xsl:apply-templates select="marc:datafield[@tag=856]"/>
536                 <xsl:apply-templates select="marc:datafield[@tag=010]"/>
537                 <xsl:apply-templates select="marc:datafield[@tag=024]"/>
538                 <xsl:apply-templates select="marc:datafield[@tag=372]"/>
539                 
540                 <!-- classification -->
541                 <xsl:apply-templates select="marc:datafield[@tag=053]"/>
542                 <xsl:apply-templates select="marc:datafield[@tag=055]"/>
543                 <xsl:apply-templates select="marc:datafield[@tag=060]"/>
544                 <xsl:apply-templates select="marc:datafield[@tag=065]"/>
545                 <xsl:apply-templates select="marc:datafield[@tag=070]"/>
546                 <xsl:apply-templates select="marc:datafield[@tag=080]"/>
547                 <xsl:apply-templates select="marc:datafield[@tag=082]"/>
548                 <xsl:apply-templates select="marc:datafield[@tag=083]"/>
549                 <xsl:apply-templates select="marc:datafield[@tag=086]"/>
550                 <xsl:apply-templates select="marc:datafield[@tag=087]"/>
551
552                 <!-- affiliation-->
553                 <xsl:for-each select="marc:datafield[@tag=373]">
554                         <mads:affiliation>
555                                 <mads:position>
556                                         <xsl:value-of select="marc:subfield[@code='a']"/>
557                                 </mads:position>
558                                 <mads:dateValid point="start">
559                                         <xsl:value-of select="marc:subfield[@code='s']"/>
560                                 </mads:dateValid>
561                                 <mads:dateValid point="end">
562                                         <xsl:value-of select="marc:subfield[@code='t']"/>
563                                 </mads:dateValid>
564                         </mads:affiliation>
565                 </xsl:for-each>
566                 <xsl:for-each select="marc:datafield[@tag=371]">
567                         <mads:affiliation>
568                                 <mads:address>
569                                         <mads:street>
570                                                 <xsl:value-of select="marc:subfield[@code='a']"/>
571                                         </mads:street>
572                                         <mads:city>
573                                                 <xsl:value-of select="marc:subfield[@code='b']"/>
574                                         </mads:city>
575                                         <mads:state>
576                                                 <xsl:value-of select="marc:subfield[@code='c']"/>
577                                         </mads:state>
578                                         <mads:country>
579                                                 <xsl:value-of select="marc:subfield[@code='d']"/>
580                                         </mads:country>
581                                         <mads:postcode>
582                                                 <xsl:value-of select="marc:subfield[@code='e']"/>
583                                         </mads:postcode>
584                                 </mads:address>
585                                 <mads:email>
586                                         <xsl:value-of select="marc:subfield[@code='m']"/>
587                                 </mads:email>
588                         </mads:affiliation>
589                 </xsl:for-each>
590
591                 <!-- extension-->
592                 <xsl:for-each select="marc:datafield[@tag=336]">
593                         <mads:extension>
594                                 <mads:contentType>
595                                         <mads:contentType type="text">
596                                                 <xsl:value-of select="marc:subfield[@code='a']"/>
597                                         </mads:contentType>
598                                         <mads:contentType type="code">
599                                                 <xsl:value-of select="marc:subfield[@code='b']"/>
600                                         </mads:contentType>
601                                 </mads:contentType>
602                         </mads:extension>
603                 </xsl:for-each>
604
605                 <xsl:for-each select="marc:datafield[@tag=374]">
606                         <mads:extension>
607                                 <mads:profession>
608                                         <xsl:choose>
609                                                 <xsl:when test="marc:subfield[@code='a']">
610                                                         <mads:professionTerm>
611                                                                 <xsl:value-of select="marc:subfield[@code='a']"/>
612                                                         </mads:professionTerm>
613                                                 </xsl:when>
614                                                 <xsl:when test="marc:subfield[@code='s']">
615                                                         <mads:dateValid point="start">
616                                                                 <xsl:value-of select="marc:subfield[@code='s']"/>
617                                                         </mads:dateValid>
618                                                 </xsl:when>
619                                                 <xsl:when test="marc:subfield[@code='t']">
620                                                         <mads:dateValid point="end">
621                                                                 <xsl:value-of select="marc:subfield[@code='t']"/>
622                                                         </mads:dateValid>
623                                                 </xsl:when>
624                                         </xsl:choose>
625                                 </mads:profession>
626                         </mads:extension>
627                 </xsl:for-each>
628                 
629                 <xsl:for-each select="marc:datafield[@tag=375]">
630                         <mads:extension>
631                                 <mads:gender>
632                                         <xsl:choose>
633                                                 <xsl:when test="marc:subfield[@code='a']">
634                                                         <mads:genderTerm>
635                                                                 <xsl:value-of select="marc:subfield[@code='a']"/>
636                                                         </mads:genderTerm>
637                                                 </xsl:when>
638                                                 <xsl:when test="marc:subfield[@code='s']">
639                                                         <mads:dateValid point="start">
640                                                                 <xsl:value-of select="marc:subfield[@code='s']"/>
641                                                         </mads:dateValid>
642                                                 </xsl:when>
643                                                 <xsl:when test="marc:subfield[@code='t']">
644                                                         <mads:dateValid point="end">
645                                                                 <xsl:value-of select="marc:subfield[@code='t']"/>
646                                                         </mads:dateValid>
647                                                 </xsl:when>
648                                         </xsl:choose>
649                                 </mads:gender>
650                         </mads:extension>
651                 </xsl:for-each>
652
653                 <xsl:for-each select="marc:datafield[@tag=376]">
654                         <mads:extension>
655                                 <mads:familyInformation>
656                                         <mads:typeOfFamily>
657                                                 <xsl:value-of select="marc:subfield[@code='a']"/>
658                                         </mads:typeOfFamily>
659                                         <mads:nameOfProminentMember>
660                                                 <xsl:value-of select="marc:subfield[@code='b']"/>
661                                         </mads:nameOfProminentMember>
662                                         <mads:hereditaryTitle>
663                                                 <xsl:value-of select="marc:subfield[@code='c']"/>
664                                         </mads:hereditaryTitle>
665                                         <mads:dateValid point="start">
666                                                 <xsl:value-of select="marc:subfield[@code='s']"/>
667                                         </mads:dateValid>
668                                         <mads:dateValid point="end">
669                                                 <xsl:value-of select="marc:subfield[@code='t']"/>
670                                         </mads:dateValid>
671                                 </mads:familyInformation>
672                         </mads:extension>
673                 </xsl:for-each>
674
675                 <mads:recordInfo>
676                         <mads:recordOrigin>Converted from MARCXML to MADS version 2.0 (Revision 2.13)</mads:recordOrigin>
677                         <!-- <xsl:apply-templates select="marc:datafield[@tag=024]"/> -->
678
679                         <xsl:apply-templates select="marc:datafield[@tag=040]/marc:subfield[@code='a']"/>
680                         <xsl:apply-templates select="marc:controlfield[@tag=005]"/>
681                         <xsl:apply-templates select="marc:controlfield[@tag=001]"/>
682                         <xsl:apply-templates select="marc:datafield[@tag=040]/marc:subfield[@code='b']"/>
683                         <xsl:apply-templates select="marc:datafield[@tag=040]/marc:subfield[@code='e']"/>
684                         <xsl:for-each select="marc:controlfield[@tag=008]">
685                                 <xsl:if test="substring(.,11,1)='a'">
686                                         <mads:descriptionStandard>
687                                                 <xsl:text>earlier rules</xsl:text>
688                                         </mads:descriptionStandard>
689                                 </xsl:if>
690                                 <xsl:if test="substring(.,11,1)='b'">
691                                         <mads:descriptionStandard>
692                                                 <xsl:text>aacr1</xsl:text>
693                                         </mads:descriptionStandard>
694                                 </xsl:if>
695                                 <xsl:if test="substring(.,11,1)='c'">
696                                         <mads:descriptionStandard>
697                                                 <xsl:text>aacr2</xsl:text>
698                                         </mads:descriptionStandard>
699                                 </xsl:if>
700                                 <xsl:if test="substring(.,11,1)='d'">
701                                         <mads:descriptionStandard>
702                                                 <xsl:text>aacr2 compatible</xsl:text>
703                                         </mads:descriptionStandard>
704                                 </xsl:if>
705                                 <xsl:if test="substring(.,11,1)='z'">
706                                         <mads:descriptionStandard>
707                                                 <xsl:text>other rules</xsl:text>
708                                         </mads:descriptionStandard>
709                                 </xsl:if>
710                         </xsl:for-each>
711                 </mads:recordInfo>
712         </xsl:template>
713
714         <!-- start of secondary templates -->
715
716         <!-- ======== xlink ======== -->
717
718         <!-- <xsl:template name="uri"> 
719     <xsl:for-each select="marc:subfield[@code='0']">
720       <xsl:attribute name="xlink:href">
721         <xsl:value-of select="."/>
722       </xsl:attribute>
723     </xsl:for-each>
724      </xsl:template> 
725    -->
726         <xsl:template match="marc:subfield[@code='i']">
727                 <xsl:attribute name="otherType">
728                         <xsl:value-of select="."/>
729                 </xsl:attribute>
730         </xsl:template>
731
732         <!-- No role/roleTerm mapped in MADS 06/24/2010
733         <xsl:template name="role">
734                 <xsl:for-each select="marc:subfield[@code='e']">
735                         <mads:role>
736                                 <mads:roleTerm type="text">
737                                         <xsl:value-of select="."/>
738                                 </mads:roleTerm>
739                         </mads:role>
740                 </xsl:for-each>
741         </xsl:template>
742 -->
743
744         <xsl:template name="part">
745                 <xsl:variable name="partNumber">
746                         <xsl:call-template name="specialSubfieldSelect">
747                                 <xsl:with-param name="axis">n</xsl:with-param>
748                                 <xsl:with-param name="anyCodes">n</xsl:with-param>
749                                 <xsl:with-param name="afterCodes">fghkdlmor</xsl:with-param>
750                         </xsl:call-template>
751                 </xsl:variable>
752                 <xsl:variable name="partName">
753                         <xsl:call-template name="specialSubfieldSelect">
754                                 <xsl:with-param name="axis">p</xsl:with-param>
755                                 <xsl:with-param name="anyCodes">p</xsl:with-param>
756                                 <xsl:with-param name="afterCodes">fghkdlmor</xsl:with-param>
757                         </xsl:call-template>
758                 </xsl:variable>
759                 <xsl:if test="string-length(normalize-space($partNumber))">
760                         <mads:partNumber>
761                                 <xsl:call-template name="chopPunctuation">
762                                         <xsl:with-param name="chopString" select="$partNumber"/>
763                                 </xsl:call-template>
764                         </mads:partNumber>
765                 </xsl:if>
766                 <xsl:if test="string-length(normalize-space($partName))">
767                         <mads:partName>
768                                 <xsl:call-template name="chopPunctuation">
769                                         <xsl:with-param name="chopString" select="$partName"/>
770                                 </xsl:call-template>
771                         </mads:partName>
772                 </xsl:if>
773         </xsl:template>
774
775         <xsl:template name="nameABCDN">
776                 <xsl:for-each select="marc:subfield[@code='a']">
777                         <mads:namePart>
778                                 <xsl:call-template name="chopPunctuation">
779                                         <xsl:with-param name="chopString" select="."/>
780                                 </xsl:call-template>
781                         </mads:namePart>
782                 </xsl:for-each>
783                 <xsl:for-each select="marc:subfield[@code='b']">
784                         <mads:namePart>
785                                 <xsl:value-of select="."/>
786                         </mads:namePart>
787                 </xsl:for-each>
788                 <xsl:if
789                         test="marc:subfield[@code='c'] or marc:subfield[@code='d'] or marc:subfield[@code='n']">
790                         <mads:namePart>
791                                 <xsl:call-template name="subfieldSelect">
792                                         <xsl:with-param name="codes">cdn</xsl:with-param>
793                                 </xsl:call-template>
794                         </mads:namePart>
795                 </xsl:if>
796         </xsl:template>
797
798         <xsl:template name="nameABCDQ">
799                 <mads:namePart>
800                         <xsl:call-template name="chopPunctuation">
801                                 <xsl:with-param name="chopString">
802                                         <xsl:call-template name="subfieldSelect">
803                                                 <xsl:with-param name="codes">aq</xsl:with-param>
804                                         </xsl:call-template>
805                                 </xsl:with-param>
806                         </xsl:call-template>
807                 </mads:namePart>
808                 <xsl:call-template name="termsOfAddress"/>
809                 <xsl:call-template name="nameDate"/>
810         </xsl:template>
811
812         <xsl:template name="nameACDENQ">
813                 <mads:namePart>
814                         <xsl:call-template name="subfieldSelect">
815                                 <xsl:with-param name="codes">acdenq</xsl:with-param>
816                         </xsl:call-template>
817                 </mads:namePart>
818         </xsl:template>
819
820         <xsl:template name="nameDate">
821                 <xsl:for-each select="marc:subfield[@code='d']">
822                         <mads:namePart type="date">
823                                 <xsl:call-template name="chopPunctuation">
824                                         <xsl:with-param name="chopString" select="."/>
825                                 </xsl:call-template>
826                         </mads:namePart>
827                 </xsl:for-each>
828         </xsl:template>
829
830         <xsl:template name="specialSubfieldSelect">
831                 <xsl:param name="anyCodes"/>
832                 <xsl:param name="axis"/>
833                 <xsl:param name="beforeCodes"/>
834                 <xsl:param name="afterCodes"/>
835                 <xsl:variable name="str">
836                         <xsl:for-each select="marc:subfield">
837                                 <xsl:if
838                                         test="contains($anyCodes, @code) or (contains($beforeCodes,@code) and following-sibling::marc:subfield[@code=$axis]) or (contains($afterCodes,@code) and preceding-sibling::marc:subfield[@code=$axis])">
839                                         <xsl:value-of select="text()"/>
840                                         <xsl:text> </xsl:text>
841                                 </xsl:if>
842                         </xsl:for-each>
843                 </xsl:variable>
844                 <xsl:value-of select="substring($str,1,string-length($str)-1)"/>
845         </xsl:template>
846
847         <xsl:template name="termsOfAddress">
848                 <xsl:if test="marc:subfield[@code='b' or @code='c']">
849                         <mads:namePart type="termsOfAddress">
850                                 <xsl:call-template name="chopPunctuation">
851                                         <xsl:with-param name="chopString">
852                                                 <xsl:call-template name="subfieldSelect">
853                                                         <xsl:with-param name="codes">bc</xsl:with-param>
854                                                 </xsl:call-template>
855                                         </xsl:with-param>
856                                 </xsl:call-template>
857                         </mads:namePart>
858                 </xsl:if>
859         </xsl:template>
860
861         <xsl:template name="displayLabel">
862                 <xsl:if test="marc:subfield[@code='z']">
863                         <xsl:attribute name="displayLabel">
864                                 <xsl:value-of select="marc:subfield[@code='z']"/>
865                         </xsl:attribute>
866                 </xsl:if>
867                 <xsl:if test="marc:subfield[@code='3']">
868                         <xsl:attribute name="displayLabel">
869                                 <xsl:value-of select="marc:subfield[@code='3']"/>
870                         </xsl:attribute>
871                 </xsl:if>
872         </xsl:template>
873
874         <xsl:template name="isInvalid">
875                 <xsl:if test="@code='z'">
876                         <xsl:attribute name="invalid">yes</xsl:attribute>
877                 </xsl:if>
878         </xsl:template>
879
880         <xsl:template name="sub2Attribute">
881                 <!-- 024 -->
882                 <xsl:if test="../marc:subfield[@code='2']">
883                         <xsl:attribute name="type">
884                                 <xsl:value-of select="../marc:subfield[@code='2']"/>
885                         </xsl:attribute>
886                 </xsl:if>
887         </xsl:template>
888
889         <xsl:template match="marc:controlfield[@tag=001]">
890                 <mads:recordIdentifier>
891                         <xsl:if test="../marc:controlfield[@tag=003]">
892                                 <xsl:attribute name="source">
893                                         <xsl:value-of select="../marc:controlfield[@tag=003]"/>
894                                 </xsl:attribute>
895                         </xsl:if>
896                         <xsl:value-of select="."/>
897                 </mads:recordIdentifier>
898         </xsl:template>
899
900         <xsl:template match="marc:controlfield[@tag=005]">
901                 <mads:recordChangeDate encoding="iso8601">
902                         <xsl:value-of select="."/>
903                 </mads:recordChangeDate>
904         </xsl:template>
905
906         <xsl:template match="marc:controlfield[@tag=008]">
907                 <mads:recordCreationDate encoding="marc">
908                         <xsl:value-of select="substring(.,1,6)"/>
909                 </mads:recordCreationDate>
910         </xsl:template>
911
912         <xsl:template match="marc:datafield[@tag=010]">
913                 <xsl:for-each select="marc:subfield">
914                         <mads:identifier type="lccn">
915                                 <xsl:call-template name="isInvalid"/>
916                                 <xsl:value-of select="."/>
917                         </mads:identifier>
918                 </xsl:for-each>
919         </xsl:template>
920
921         <xsl:template match="marc:datafield[@tag=024]">
922                 <xsl:for-each select="marc:subfield[not(@code=2)]">
923                         <mads:identifier>
924                                 <xsl:call-template name="isInvalid"/>
925                                 <xsl:call-template name="sub2Attribute"/>
926                                 <xsl:value-of select="."/>
927                         </mads:identifier>
928                 </xsl:for-each>
929         </xsl:template>
930
931         <!-- ========== 372 ========== -->
932         <xsl:template match="marc:datafield[@tag=372]">
933                 <mads:fieldOfActivity>
934                         <xsl:call-template name="subfieldSelect">
935                                 <xsl:with-param name="codes">a</xsl:with-param>
936                         </xsl:call-template>
937                         <xsl:text>-</xsl:text>
938                         <xsl:call-template name="subfieldSelect">
939                                 <xsl:with-param name="codes">st</xsl:with-param>
940                         </xsl:call-template>
941                 </mads:fieldOfActivity>
942         </xsl:template>
943
944
945         <!-- ========== 040 ========== -->
946         <xsl:template match="marc:datafield[@tag=040]/marc:subfield[@code='a']">
947                 <mads:recordContentSource authority="marcorg">
948                         <xsl:value-of select="."/>
949                 </mads:recordContentSource>
950         </xsl:template>
951
952         <xsl:template match="marc:datafield[@tag=040]/marc:subfield[@code='b']">
953                 <mads:languageOfCataloging>
954                         <mads:languageTerm authority="iso639-2b" type="code">
955                                 <xsl:value-of select="."/>
956                         </mads:languageTerm>
957                 </mads:languageOfCataloging>
958         </xsl:template>
959
960         <xsl:template match="marc:datafield[@tag=040]/marc:subfield[@code='e']">
961                 <mads:descriptionStandard>
962                         <xsl:value-of select="."/>
963                 </mads:descriptionStandard>
964         </xsl:template>
965         
966         <!-- ========== classification 2.03 ========== -->
967         
968         <xsl:template match="marc:datafield[@tag=053]">
969                 <mads:classification>
970                         <xsl:call-template name="subfieldSelect">
971                                 <xsl:with-param name="codes">abcdxyz</xsl:with-param>
972                                 <xsl:with-param name="delimeter">-</xsl:with-param>
973                         </xsl:call-template>
974                 </mads:classification>
975         </xsl:template>
976         
977         <xsl:template match="marc:datafield[@tag=055]">
978                 <mads:classification>
979                         <xsl:call-template name="subfieldSelect">
980                                 <xsl:with-param name="codes">abcdxyz</xsl:with-param>
981                                 <xsl:with-param name="delimeter">-</xsl:with-param>
982                         </xsl:call-template>
983                 </mads:classification>
984         </xsl:template>
985         
986         <xsl:template match="marc:datafield[@tag=060]">
987                 <mads:classification>
988                         <xsl:call-template name="subfieldSelect">
989                                 <xsl:with-param name="codes">abcdxyz</xsl:with-param>
990                                 <xsl:with-param name="delimeter">-</xsl:with-param>
991                         </xsl:call-template>
992                 </mads:classification>
993         </xsl:template>
994         <xsl:template match="marc:datafield[@tag=065]">
995                 <mads:classification>
996                         <xsl:attribute name="authority">
997                                 <xsl:value-of select="marc:subfield[@code='2']"/>
998                         </xsl:attribute>
999                         <xsl:call-template name="subfieldSelect">
1000                                 <xsl:with-param name="codes">abcdxyz</xsl:with-param>
1001                                 <xsl:with-param name="delimeter">-</xsl:with-param>
1002                         </xsl:call-template>
1003                 </mads:classification>
1004         </xsl:template>
1005         <xsl:template match="marc:datafield[@tag=070]">
1006                 <mads:classification>
1007                         <xsl:call-template name="subfieldSelect">
1008                                 <xsl:with-param name="codes">abcdxyz5</xsl:with-param>
1009                                 <xsl:with-param name="delimeter">-</xsl:with-param>
1010                         </xsl:call-template>
1011                 </mads:classification>
1012         </xsl:template>
1013         <xsl:template match="marc:datafield[@tag=080]">
1014                 <mads:classification>
1015                         <xsl:attribute name="authority">
1016                                 <xsl:value-of select="marc:subfield[@code='2']"/>
1017                         </xsl:attribute>
1018                         <xsl:call-template name="subfieldSelect">
1019                                 <xsl:with-param name="codes">abcdxyz5</xsl:with-param>
1020                                 <xsl:with-param name="delimeter">-</xsl:with-param>
1021                         </xsl:call-template>
1022                 </mads:classification>
1023         </xsl:template>
1024         <xsl:template match="marc:datafield[@tag=082]">
1025                 <mads:classification>
1026                         <xsl:attribute name="authority">
1027                                 <xsl:value-of select="marc:subfield[@code='2']"/>
1028                         </xsl:attribute>
1029                         <xsl:call-template name="subfieldSelect">
1030                                 <xsl:with-param name="codes">abcdxyz5</xsl:with-param>
1031                                 <xsl:with-param name="delimeter">-</xsl:with-param>
1032                         </xsl:call-template>
1033                 </mads:classification>
1034         </xsl:template>
1035         <xsl:template match="marc:datafield[@tag=083]">
1036                 <mads:classification>
1037                         <xsl:attribute name="authority">
1038                                 <xsl:value-of select="marc:subfield[@code='2']"/>
1039                         </xsl:attribute>
1040                         <xsl:call-template name="subfieldSelect">
1041                                 <xsl:with-param name="codes">abcdxyz5</xsl:with-param>
1042                                 <xsl:with-param name="delimeter">-</xsl:with-param>
1043                         </xsl:call-template>
1044                 </mads:classification>
1045         </xsl:template>
1046         <xsl:template match="marc:datafield[@tag=086]">
1047                 <mads:classification>
1048                         <xsl:attribute name="authority">
1049                                 <xsl:value-of select="marc:subfield[@code='2']"/>
1050                         </xsl:attribute>
1051                         <xsl:call-template name="subfieldSelect">
1052                                 <xsl:with-param name="codes">abcdxyz5</xsl:with-param>
1053                                 <xsl:with-param name="delimeter">-</xsl:with-param>
1054                         </xsl:call-template>
1055                 </mads:classification>
1056         </xsl:template>
1057         <xsl:template match="marc:datafield[@tag=087]">
1058                 <mads:classification>
1059                         <xsl:attribute name="authority">
1060                                 <xsl:value-of select="marc:subfield[@code='2']"/>
1061                         </xsl:attribute>
1062                         <xsl:call-template name="subfieldSelect">
1063                                 <xsl:with-param name="codes">abcdxyz5</xsl:with-param>
1064                                 <xsl:with-param name="delimeter">-</xsl:with-param>
1065                         </xsl:call-template>
1066                 </mads:classification>
1067         </xsl:template>
1068         
1069
1070         <!-- ========== names  ========== -->
1071         <xsl:template match="marc:datafield[@tag=100]">
1072                 <mads:name type="personal">
1073                         <xsl:call-template name="setAuthority"/>
1074                         <xsl:call-template name="nameABCDQ"/>
1075                 </mads:name>
1076                 <xsl:apply-templates select="*[marc:subfield[not(contains('abcdeq',@code))]]"/>
1077                 <xsl:call-template name="title"/>
1078                 <xsl:apply-templates select="marc:subfield[@code!='i']"/>
1079         </xsl:template>
1080
1081         <xsl:template match="marc:datafield[@tag=110]">
1082                 <mads:name type="corporate">
1083                         <xsl:call-template name="setAuthority"/>
1084                         <xsl:call-template name="nameABCDN"/>
1085                 </mads:name>
1086                 <xsl:apply-templates select="marc:subfield[@code!='i']"/>
1087         </xsl:template>
1088
1089         <xsl:template match="marc:datafield[@tag=111]">
1090                 <mads:name type="conference">
1091                         <xsl:call-template name="setAuthority"/>
1092                         <xsl:call-template name="nameACDENQ"/>
1093                 </mads:name>
1094                 <xsl:apply-templates select="marc:subfield[@code!='i']"/>
1095         </xsl:template>
1096
1097         <xsl:template match="marc:datafield[@tag=400]">
1098                 <mads:variant>
1099                         <xsl:call-template name="variantTypeAttribute"/>
1100                         <mads:name type="personal">
1101                                 <xsl:call-template name="nameABCDQ"/>
1102                         </mads:name>
1103                         <xsl:apply-templates select="marc:subfield[@code!='i']"/>
1104                         <xsl:call-template name="title"/>
1105                 </mads:variant>
1106         </xsl:template>
1107
1108         <xsl:template match="marc:datafield[@tag=410]">
1109                 <mads:variant>
1110                         <xsl:call-template name="variantTypeAttribute"/>
1111                         <mads:name type="corporate">
1112                                 <xsl:call-template name="nameABCDN"/>
1113                         </mads:name>
1114                         <xsl:apply-templates select="marc:subfield[@code!='i']"/>
1115                 </mads:variant>
1116         </xsl:template>
1117
1118         <xsl:template match="marc:datafield[@tag=411]">
1119                 <mads:variant>
1120                         <xsl:call-template name="variantTypeAttribute"/>
1121                         <mads:name type="conference">
1122                                 <xsl:call-template name="nameACDENQ"/>
1123                         </mads:name>
1124                         <xsl:apply-templates select="marc:subfield[@code!='i']"/>
1125                 </mads:variant>
1126         </xsl:template>
1127
1128         <xsl:template match="marc:datafield[@tag=500]|marc:datafield[@tag=700]">
1129                 <mads:related>
1130                         <xsl:call-template name="relatedTypeAttribute"/>
1131                         <!-- <xsl:call-template name="uri"/> -->
1132                         <mads:name type="personal">
1133                                 <xsl:call-template name="setAuthority"/>
1134                                 <xsl:call-template name="nameABCDQ"/>
1135                         </mads:name>
1136                         <xsl:call-template name="title"/>
1137                         <xsl:apply-templates select="marc:subfield[@code!='i']"/>
1138                 </mads:related>
1139         </xsl:template>
1140
1141         <xsl:template match="marc:datafield[@tag=510]|marc:datafield[@tag=710]">
1142                 <mads:related>
1143                         <xsl:call-template name="relatedTypeAttribute"/>
1144                         <!-- <xsl:call-template name="uri"/> -->
1145                         <mads:name type="corporate">
1146                                 <xsl:call-template name="setAuthority"/>
1147                                 <xsl:call-template name="nameABCDN"/>
1148                         </mads:name>
1149                         <xsl:apply-templates select="marc:subfield[@code!='i']"/>
1150                 </mads:related>
1151         </xsl:template>
1152
1153         <xsl:template match="marc:datafield[@tag=511]|marc:datafield[@tag=711]">
1154                 <mads:related>
1155                         <xsl:call-template name="relatedTypeAttribute"/>
1156                         <!-- <xsl:call-template name="uri"/> -->
1157                         <mads:name type="conference">
1158                                 <xsl:call-template name="setAuthority"/>
1159                                 <xsl:call-template name="nameACDENQ"/>
1160                         </mads:name>
1161                         <xsl:apply-templates select="marc:subfield[@code!='i']"/>
1162                 </mads:related>
1163         </xsl:template>
1164
1165         <!-- ========== titles  ========== -->
1166         <xsl:template match="marc:datafield[@tag=130]">
1167                 <xsl:call-template name="uniform-title"/>
1168                 <xsl:apply-templates select="marc:subfield[@code!='i']"/>
1169         </xsl:template>
1170
1171         <xsl:template match="marc:datafield[@tag=430]">
1172                 <mads:variant>
1173                         <xsl:call-template name="variantTypeAttribute"/>
1174                         <xsl:call-template name="uniform-title"/>
1175                         <xsl:apply-templates select="marc:subfield[@code!='i']"/>
1176                 </mads:variant>
1177         </xsl:template>
1178
1179         <xsl:template match="marc:datafield[@tag=530]|marc:datafield[@tag=730]">
1180                 <mads:related>
1181                         <xsl:call-template name="relatedTypeAttribute"/>
1182                         <xsl:call-template name="uniform-title"/>
1183                         <xsl:apply-templates select="marc:subfield[@code!='i']"/>
1184                 </mads:related>
1185         </xsl:template>
1186
1187         <xsl:template name="title">
1188                 <xsl:variable name="hasTitle">
1189                         <xsl:for-each select="marc:subfield">
1190                                 <xsl:if test="(contains('tfghklmors',@code) )">
1191                                         <xsl:value-of select="@code"/>
1192                                 </xsl:if>
1193                         </xsl:for-each>
1194                 </xsl:variable>
1195                 <xsl:if test="string-length($hasTitle) &gt; 0 ">
1196                         <mads:titleInfo>
1197                                 <xsl:call-template name="setAuthority"/>
1198                                 <mads:title>
1199                                         <xsl:variable name="str">
1200                                                 <xsl:for-each select="marc:subfield">
1201                                                         <xsl:if test="(contains('atfghklmors',@code) )">
1202                                                                 <xsl:value-of select="text()"/>
1203                                                                 <xsl:text> </xsl:text>
1204                                                         </xsl:if>
1205                                                 </xsl:for-each>
1206                                         </xsl:variable>
1207                                         <xsl:call-template name="chopPunctuation">
1208                                                 <xsl:with-param name="chopString">
1209                                                         <xsl:value-of select="substring($str,1,string-length($str)-1)"/>
1210                                                 </xsl:with-param>
1211                                         </xsl:call-template>
1212                                 </mads:title>
1213                                 <xsl:call-template name="part"/>
1214                                 <!-- <xsl:call-template name="uri"/> -->
1215                         </mads:titleInfo>
1216                 </xsl:if>
1217         </xsl:template>
1218
1219         <xsl:template name="uniform-title">
1220                 <xsl:variable name="hasTitle">
1221                         <xsl:for-each select="marc:subfield">
1222                                 <xsl:if test="(contains('atfghklmors',@code) )">
1223                                         <xsl:value-of select="@code"/>
1224                                 </xsl:if>
1225                         </xsl:for-each>
1226                 </xsl:variable>
1227                 <xsl:if test="string-length($hasTitle) &gt; 0 ">
1228                         <mads:titleInfo>
1229                                 <xsl:call-template name="setAuthority"/>
1230                                 <mads:title>
1231                                         <xsl:variable name="str">
1232                                                 <xsl:for-each select="marc:subfield">
1233                                                         <xsl:if test="(contains('adfghklmors',@code) )">
1234                                                                 <xsl:value-of select="text()"/>
1235                                                                 <xsl:text> </xsl:text>
1236                                                         </xsl:if>
1237                                                 </xsl:for-each>
1238                                         </xsl:variable>
1239                                         <xsl:call-template name="chopPunctuation">
1240                                                 <xsl:with-param name="chopString">
1241                                                         <xsl:value-of select="substring($str,1,string-length($str)-1)"/>
1242                                                 </xsl:with-param>
1243                                         </xsl:call-template>
1244                                 </mads:title>
1245                                 <xsl:call-template name="part"/>
1246                                 <!-- <xsl:call-template name="uri"/> -->
1247                         </mads:titleInfo>
1248                 </xsl:if>
1249         </xsl:template>
1250
1251
1252         <!-- ========== topics  ========== -->
1253         <xsl:template match="marc:subfield[@code='x']">
1254                 <mads:topic>
1255                         <xsl:call-template name="chopPunctuation">
1256                                 <xsl:with-param name="chopString">
1257                                         <xsl:value-of select="."/>
1258                                 </xsl:with-param>
1259                         </xsl:call-template>
1260                 </mads:topic>
1261         </xsl:template>
1262         
1263         <!-- 2.06 fix -->
1264         <xsl:template
1265                 match="marc:datafield[@tag=150][marc:subfield[@code='a' or @code='b']]|marc:datafield[@tag=180][marc:subfield[@code='x']]">
1266                 <xsl:call-template name="topic"/>
1267                 <xsl:apply-templates select="marc:subfield[@code!='i']"/>
1268         </xsl:template>
1269         <xsl:template
1270                 match="marc:datafield[@tag=450][marc:subfield[@code='a' or @code='b']]|marc:datafield[@tag=480][marc:subfield[@code='x']]">
1271                 <mads:variant>
1272                         <xsl:call-template name="variantTypeAttribute"/>
1273                         <xsl:call-template name="topic"/>
1274                 </mads:variant>
1275         </xsl:template>
1276         <xsl:template
1277                 match="marc:datafield[@tag=550 or @tag=750][marc:subfield[@code='a' or @code='b']]">
1278                 <mads:related>
1279                         <xsl:call-template name="relatedTypeAttribute"/>
1280                         <!-- <xsl:call-template name="uri"/> -->
1281                         <xsl:call-template name="topic"/>
1282                         <xsl:apply-templates select="marc:subfield[@code='z']"/>
1283                 </mads:related>
1284         </xsl:template>
1285         <xsl:template name="topic">
1286                 <mads:topic>
1287                         <xsl:call-template name="setAuthority"/>
1288                         <!-- tmee2006 dedupe 550a
1289                         <xsl:if test="@tag=550 or @tag=750">
1290                                 <xsl:call-template name="subfieldSelect">
1291                                         <xsl:with-param name="codes">ab</xsl:with-param>
1292                                 </xsl:call-template>
1293                         </xsl:if>       
1294                         -->
1295                         <xsl:choose>
1296                                 <xsl:when test="@tag=180 or @tag=480 or @tag=580 or @tag=780">
1297                                         <xsl:call-template name="chopPunctuation">
1298                                                 <xsl:with-param name="chopString">
1299                                                         <xsl:apply-templates select="marc:subfield[@code='x']"/>
1300                                                 </xsl:with-param>
1301                                         </xsl:call-template>
1302                                 </xsl:when>
1303                         </xsl:choose>
1304                         <xsl:call-template name="chopPunctuation">
1305                                 <xsl:with-param name="chopString">
1306                                         <xsl:choose>
1307                                                 <xsl:when test="@tag=180 or @tag=480 or @tag=580 or @tag=780">
1308                                                         <xsl:apply-templates select="marc:subfield[@code='x']"/>
1309                                                 </xsl:when>
1310                                                 <xsl:otherwise>
1311                                                         <xsl:call-template name="subfieldSelect">
1312                                                                 <xsl:with-param name="codes">ab</xsl:with-param>
1313                                                         </xsl:call-template>
1314                                                 </xsl:otherwise>
1315                                         </xsl:choose>
1316                                 </xsl:with-param>
1317                         </xsl:call-template>
1318                 </mads:topic>
1319         </xsl:template>
1320
1321         <!-- ========= temporals  ========== -->
1322         <xsl:template match="marc:subfield[@code='y']">
1323                 <mads:temporal>
1324                         <xsl:call-template name="chopPunctuation">
1325                                 <xsl:with-param name="chopString">
1326                                         <xsl:value-of select="."/>
1327                                 </xsl:with-param>
1328                         </xsl:call-template>
1329                 </mads:temporal>
1330         </xsl:template>
1331         <xsl:template
1332                 match="marc:datafield[@tag=148][marc:subfield[@code='a']]|marc:datafield[@tag=182 ][marc:subfield[@code='y']]">
1333                 <xsl:call-template name="temporal"/>
1334         </xsl:template>
1335         <xsl:template
1336                 match="marc:datafield[@tag=448][marc:subfield[@code='a']]|marc:datafield[@tag=482][marc:subfield[@code='y']]">
1337                 <mads:variant>
1338                         <xsl:call-template name="variantTypeAttribute"/>
1339                         <xsl:call-template name="temporal"/>
1340                 </mads:variant>
1341         </xsl:template>
1342         <xsl:template
1343                 match="marc:datafield[@tag=548 or @tag=748][marc:subfield[@code='a']]|marc:datafield[@tag=582 or @tag=782][marc:subfield[@code='y']]">
1344                 <mads:related>
1345                         <xsl:call-template name="relatedTypeAttribute"/>
1346                         <!-- <xsl:call-template name="uri"/> -->
1347                         <xsl:call-template name="temporal"/>
1348                 </mads:related>
1349         </xsl:template>
1350         <xsl:template name="temporal">
1351                 <mads:temporal>
1352                         <xsl:call-template name="setAuthority"/>
1353                         <xsl:if test="@tag=548 or @tag=748">
1354                                 <xsl:value-of select="marc:subfield[@code='a']"/>
1355                         </xsl:if>
1356                         <xsl:call-template name="chopPunctuation">
1357                                 <xsl:with-param name="chopString">
1358                                         <xsl:choose>
1359                                                 <xsl:when test="@tag=182 or @tag=482 or @tag=582 or @tag=782">
1360                                                         <xsl:apply-templates select="marc:subfield[@code='y']"/>
1361                                                 </xsl:when>
1362                                                 <xsl:otherwise>
1363                                                         <xsl:value-of select="marc:subfield[@code='a']"/>
1364                                                 </xsl:otherwise>
1365                                         </xsl:choose>
1366                                 </xsl:with-param>
1367                         </xsl:call-template>
1368                 </mads:temporal>
1369                 <xsl:apply-templates select="marc:subfield[@code!='i']"/>
1370         </xsl:template>
1371
1372         <!-- ========== genre  ========== -->
1373         <xsl:template match="marc:subfield[@code='v']">
1374                 <mads:genre>
1375                         <xsl:call-template name="chopPunctuation">
1376                                 <xsl:with-param name="chopString">
1377                                         <xsl:value-of select="."/>
1378                                 </xsl:with-param>
1379                         </xsl:call-template>
1380                 </mads:genre>
1381         </xsl:template>
1382         <xsl:template
1383                 match="marc:datafield[@tag=155][marc:subfield[@code='a']]|marc:datafield[@tag=185][marc:subfield[@code='v']]">
1384                 <xsl:call-template name="genre"/>
1385         </xsl:template>
1386         <xsl:template
1387                 match="marc:datafield[@tag=455][marc:subfield[@code='a']]|marc:datafield[@tag=485 ][marc:subfield[@code='v']]">
1388                 <mads:variant>
1389                         <xsl:call-template name="variantTypeAttribute"/>
1390                         <xsl:call-template name="genre"/>
1391                 </mads:variant>
1392         </xsl:template>
1393         <!--
1394         <xsl:template match="marc:datafield[@tag=555]">
1395                 <mads:related>
1396                         <xsl:call-template name="relatedTypeAttribute"/>
1397                         <xsl:call-template name="uri"/>
1398                         <xsl:call-template name="genre"/>
1399                 </mads:related>
1400         </xsl:template>
1401         -->
1402         <xsl:template
1403                 match="marc:datafield[@tag=555 or @tag=755][marc:subfield[@code='a']]|marc:datafield[@tag=585][marc:subfield[@code='v']]">
1404                 <mads:related>
1405                         <xsl:call-template name="relatedTypeAttribute"/>
1406                         <xsl:call-template name="genre"/>
1407                 </mads:related>
1408         </xsl:template>
1409         <xsl:template name="genre">
1410                 <mads:genre>
1411                         <xsl:if test="@tag=555">
1412                                 <xsl:value-of select="marc:subfield[@code='a']"/>
1413                         </xsl:if>
1414                         <xsl:call-template name="setAuthority"/>
1415                         <xsl:call-template name="chopPunctuation">
1416                                 <xsl:with-param name="chopString">
1417                                         <xsl:choose>
1418                                                 <!-- 2.07 fix -->
1419                                                 <xsl:when test="@tag='555'"/>
1420                                                 <xsl:when test="@tag=185 or @tag=485 or @tag=585">
1421                                                         <xsl:apply-templates select="marc:subfield[@code='v']"/>
1422                                                 </xsl:when>
1423                                                 <xsl:otherwise>
1424                                                         <xsl:value-of select="marc:subfield[@code='a']"/>
1425                                                 </xsl:otherwise>
1426                                         </xsl:choose>
1427                                 </xsl:with-param>
1428                         </xsl:call-template>
1429                 </mads:genre>
1430                 <xsl:apply-templates/>
1431         </xsl:template>
1432
1433         <!-- ========= geographic  ========== -->
1434         <xsl:template match="marc:subfield[@code='z']">
1435                 <mads:geographic>
1436                         <xsl:call-template name="chopPunctuation">
1437                                 <xsl:with-param name="chopString">
1438                                         <xsl:value-of select="."/>
1439                                 </xsl:with-param>
1440                         </xsl:call-template>
1441                 </mads:geographic>
1442         </xsl:template>
1443         <xsl:template name="geographic">
1444                 <mads:geographic>
1445                         <!-- 2.14 -->
1446                         <xsl:call-template name="setAuthority"/>
1447                         <!-- 2.13 -->
1448                         <xsl:if test="@tag=151 or @tag=551">
1449                                 <xsl:value-of select="marc:subfield[@code='a']"/>
1450                         </xsl:if>
1451                         <xsl:call-template name="chopPunctuation">
1452                                 <xsl:with-param name="chopString">
1453                                                 <xsl:if test="@tag=181 or @tag=481 or @tag=581">
1454                                                                 <xsl:apply-templates select="marc:subfield[@code='z']"/>
1455                                                 </xsl:if>
1456                                                 <!-- 2.13
1457                                                         <xsl:choose>
1458                                                 <xsl:when test="@tag=181 or @tag=481 or @tag=581">
1459                                                         <xsl:apply-templates select="marc:subfield[@code='z']"/>
1460                                                 </xsl:when>
1461                                         
1462                                                 <xsl:otherwise>
1463                                                         <xsl:value-of select="marc:subfield[@code='a']"/>
1464                                                 </xsl:otherwise>
1465                                                 </xsl:choose>
1466                                                 -->
1467                                 </xsl:with-param>
1468                         </xsl:call-template>
1469                 </mads:geographic>
1470                 <xsl:apply-templates select="marc:subfield[@code!='i']"/>
1471         </xsl:template>
1472         <xsl:template
1473                 match="marc:datafield[@tag=151][marc:subfield[@code='a']]|marc:datafield[@tag=181][marc:subfield[@code='z']]">
1474                 <xsl:call-template name="geographic"/>
1475         </xsl:template>
1476         <xsl:template
1477                 match="marc:datafield[@tag=451][marc:subfield[@code='a']]|marc:datafield[@tag=481][marc:subfield[@code='z']]">
1478                 <mads:variant>
1479                         <xsl:call-template name="variantTypeAttribute"/>
1480                         <xsl:call-template name="geographic"/>
1481                 </mads:variant>
1482         </xsl:template>
1483         <xsl:template
1484                 match="marc:datafield[@tag=551]|marc:datafield[@tag=581][marc:subfield[@code='z']]">
1485                 <mads:related>
1486                         <xsl:call-template name="relatedTypeAttribute"/>
1487                         <!-- <xsl:call-template name="uri"/> -->
1488                         <xsl:call-template name="geographic"/>
1489                 </mads:related>
1490         </xsl:template>
1491         <xsl:template match="marc:datafield[@tag=580]">
1492                 <mads:related>
1493                         <xsl:call-template name="relatedTypeAttribute"/>
1494                         <xsl:apply-templates select="marc:subfield[@code!='i']"/>
1495                 </mads:related>
1496         </xsl:template>
1497         <xsl:template
1498                 match="marc:datafield[@tag=751][marc:subfield[@code='z']]|marc:datafield[@tag=781][marc:subfield[@code='z']]">
1499                 <mads:related>
1500                         <xsl:call-template name="relatedTypeAttribute"/>
1501                         <xsl:call-template name="geographic"/>
1502                 </mads:related>
1503         </xsl:template>
1504         <xsl:template match="marc:datafield[@tag=755]">
1505                 <mads:related>
1506                         <xsl:call-template name="relatedTypeAttribute"/>
1507                         <xsl:call-template name="setAuthority"/>
1508                         <xsl:call-template name="genre"/>
1509                         <xsl:apply-templates select="marc:subfield[@code!='i']"/>
1510                 </mads:related>
1511         </xsl:template>
1512         <xsl:template match="marc:datafield[@tag=780]">
1513                 <mads:related>
1514                         <xsl:call-template name="relatedTypeAttribute"/>
1515                         <xsl:apply-templates select="marc:subfield[@code!='i']"/>
1516                 </mads:related>
1517         </xsl:template>
1518         <xsl:template match="marc:datafield[@tag=785]">
1519                 <mads:related>
1520                         <xsl:call-template name="relatedTypeAttribute"/>
1521                         <xsl:apply-templates select="marc:subfield[@code!='i']"/>
1522                 </mads:related>
1523         </xsl:template>
1524
1525         <!-- ========== notes  ========== -->
1526         <xsl:template match="marc:datafield[667 &lt;= @tag and @tag &lt;= 688]">
1527                 <mads:note>
1528                         <xsl:choose>
1529                                 <xsl:when test="@tag=667">
1530                                         <xsl:attribute name="type">nonpublic</xsl:attribute>
1531                                 </xsl:when>
1532                                 <xsl:when test="@tag=670">
1533                                         <xsl:attribute name="type">source</xsl:attribute>
1534                                 </xsl:when>
1535                                 <xsl:when test="@tag=675">
1536                                         <xsl:attribute name="type">notFound</xsl:attribute>
1537                                 </xsl:when>
1538                                 <xsl:when test="@tag=678">
1539                                         <xsl:attribute name="type">history</xsl:attribute>
1540                                 </xsl:when>
1541                                 <xsl:when test="@tag=681">
1542                                         <xsl:attribute name="type">subject example</xsl:attribute>
1543                                 </xsl:when>
1544                                 <xsl:when test="@tag=682">
1545                                         <xsl:attribute name="type">deleted heading information</xsl:attribute>
1546                                 </xsl:when>
1547                                 <xsl:when test="@tag=688">
1548                                         <xsl:attribute name="type">application history</xsl:attribute>
1549                                 </xsl:when>
1550                         </xsl:choose>
1551                         <xsl:call-template name="chopPunctuation">
1552                                 <xsl:with-param name="chopString">
1553                                         <xsl:choose>
1554                                                 <xsl:when test="@tag=667 or @tag=675">
1555                                                         <xsl:value-of select="marc:subfield[@code='a']"/>
1556                                                 </xsl:when>
1557                                                 <xsl:when test="@tag=670 or @tag=678">
1558                                                         <xsl:call-template name="subfieldSelect">
1559                                                                 <xsl:with-param name="codes">ab</xsl:with-param>
1560                                                         </xsl:call-template>
1561                                                 </xsl:when>
1562                                                 <xsl:when test="680 &lt;= @tag and @tag &lt;=688">
1563                                                         <xsl:call-template name="subfieldSelect">
1564                                                                 <xsl:with-param name="codes">ai</xsl:with-param>
1565                                                         </xsl:call-template>
1566                                                 </xsl:when>
1567                                         </xsl:choose>
1568                                 </xsl:with-param>
1569                         </xsl:call-template>
1570                 </mads:note>
1571         </xsl:template>
1572
1573         <!-- ========== url  ========== -->
1574         <xsl:template match="marc:datafield[@tag=856][marc:subfield[@code='u']]">
1575                 <mads:url>
1576                         <xsl:if test="marc:subfield[@code='z' or @code='3']">
1577                                 <xsl:attribute name="displayLabel">
1578                                         <xsl:call-template name="subfieldSelect">
1579                                                 <xsl:with-param name="codes">z3</xsl:with-param>
1580                                         </xsl:call-template>
1581                                 </xsl:attribute>
1582                         </xsl:if>
1583                         <xsl:value-of select="marc:subfield[@code='u']"/>
1584                 </mads:url>
1585         </xsl:template>
1586
1587         <xsl:template name="relatedTypeAttribute">
1588                 <xsl:choose>
1589                         <xsl:when
1590                                 test="@tag=500 or @tag=510 or @tag=511 or @tag=548 or @tag=550 or @tag=551 or @tag=555 or @tag=580 or @tag=581 or @tag=582 or @tag=585">
1591                                 <xsl:if test="substring(marc:subfield[@code='w'],1,1)='a'">
1592                                         <xsl:attribute name="type">earlier</xsl:attribute>
1593                                 </xsl:if>
1594                                 <xsl:if test="substring(marc:subfield[@code='w'],1,1)='b'">
1595                                         <xsl:attribute name="type">later</xsl:attribute>
1596                                 </xsl:if>
1597                                 <xsl:if test="substring(marc:subfield[@code='w'],1,1)='t'">
1598                                         <xsl:attribute name="type">parentOrg</xsl:attribute>
1599                                 </xsl:if>
1600                                 <xsl:if test="substring(marc:subfield[@code='w'],1,1)='g'">
1601                                         <xsl:attribute name="type">broader</xsl:attribute>
1602                                 </xsl:if>
1603                                 <xsl:if test="substring(marc:subfield[@code='w'],1,1)='h'">
1604                                         <xsl:attribute name="type">narrower</xsl:attribute>
1605                                 </xsl:if>
1606                                 <xsl:if test="substring(marc:subfield[@code='w'],1,1)='r'">
1607                                         <xsl:attribute name="type">other</xsl:attribute>
1608                                 </xsl:if>
1609                                 <xsl:if test="contains('fin|', substring(marc:subfield[@code='w'],1,1))">
1610                                         <xsl:attribute name="type">other</xsl:attribute>
1611                                 </xsl:if>
1612                         </xsl:when>
1613                         <xsl:when test="@tag=530 or @tag=730">
1614                                 <xsl:attribute name="type">other</xsl:attribute>
1615                         </xsl:when>
1616                         <xsl:otherwise>
1617                                 <!-- 7xx -->
1618                                 <xsl:attribute name="type">equivalent</xsl:attribute>
1619                         </xsl:otherwise>
1620                 </xsl:choose>
1621                 <xsl:apply-templates select="marc:subfield[@code='i']"/>
1622         </xsl:template>
1623         
1624
1625
1626         <xsl:template name="variantTypeAttribute">
1627                 <xsl:choose>
1628                         <xsl:when
1629                                 test="@tag=400 or @tag=410 or @tag=411 or @tag=451 or @tag=455 or @tag=480 or @tag=481 or @tag=482 or @tag=485">
1630                                 <xsl:if test="substring(marc:subfield[@code='w'],1,1)='d'">
1631                                         <xsl:attribute name="type">acronym</xsl:attribute>
1632                                 </xsl:if>
1633                                 <xsl:if test="substring(marc:subfield[@code='w'],1,1)='n'">
1634                                         <xsl:attribute name="type">other</xsl:attribute>
1635                                 </xsl:if>
1636                                 <xsl:if test="contains('fit', substring(marc:subfield[@code='w'],1,1))">
1637                                         <xsl:attribute name="type">other</xsl:attribute>
1638                                 </xsl:if>
1639                         </xsl:when>
1640                         <xsl:otherwise>
1641                                 <!-- 430  -->
1642                                 <xsl:attribute name="type">other</xsl:attribute>
1643                         </xsl:otherwise>
1644                 </xsl:choose>
1645                 <xsl:apply-templates select="marc:subfield[@code='i']"/>
1646         </xsl:template>
1647
1648         <xsl:template name="setAuthority">
1649                 <xsl:choose>
1650                         <!-- can be called from the datafield or subfield level, so "..//@tag" means
1651                         the tag can be at the subfield's parent level or at the datafields own level -->
1652
1653                         <xsl:when
1654                                 test="ancestor-or-self::marc:datafield/@tag=100 and (@ind1=0 or @ind1=1) and $controlField008-11='a' and $controlField008-14='a'">
1655                                 <xsl:attribute name="authority">
1656                                         <xsl:text>naf</xsl:text>
1657                                 </xsl:attribute>
1658                         </xsl:when>
1659                         <xsl:when
1660                                 test="ancestor-or-self::marc:datafield/@tag=100 and (@ind1=0 or @ind1=1) and $controlField008-11='a' and $controlField008-14='b'">
1661                                 <xsl:attribute name="authority">
1662                                         <xsl:text>lcsh</xsl:text>
1663                                 </xsl:attribute>
1664                         </xsl:when>
1665                         <xsl:when
1666                                 test="ancestor-or-self::marc:datafield/@tag=100 and (@ind1=0 or @ind1=1) and $controlField008-11='k'">
1667                                 <xsl:attribute name="authority">
1668                                         <xsl:text>lacnaf</xsl:text>
1669                                 </xsl:attribute>
1670                         </xsl:when>
1671                         <xsl:when
1672                                 test="ancestor-or-self::marc:datafield/@tag=100 and @ind1=3 and $controlField008-11='a' and $controlField008-14='b'">
1673                                 <xsl:attribute name="authority">
1674                                         <xsl:text>lcsh</xsl:text>
1675                                 </xsl:attribute>
1676                         </xsl:when>
1677                         <xsl:when
1678                                 test="ancestor-or-self::marc:datafield/@tag=100 and @ind1=3 and $controlField008-11='k' and $controlField008-14='b'">
1679                                 <xsl:attribute name="authority">cash</xsl:attribute>
1680                         </xsl:when>
1681                         <xsl:when
1682                                 test="ancestor-or-self::marc:datafield/@tag=110 and $controlField008-11='a' and $controlField008-14='a'">
1683                                 <xsl:attribute name="authority">naf</xsl:attribute>
1684                         </xsl:when>
1685                         <xsl:when
1686                                 test="ancestor-or-self::marc:datafield/@tag=110 and $controlField008-11='a' and $controlField008-14='b'">
1687                                 <xsl:attribute name="authority">lcsh</xsl:attribute>
1688                         </xsl:when>
1689                         <xsl:when
1690                                 test="ancestor-or-self::marc:datafield/@tag=110 and $controlField008-11='k' and $controlField008-14='a'">
1691                                 <xsl:attribute name="authority">
1692                                         <xsl:text>lacnaf</xsl:text>
1693                                 </xsl:attribute>
1694                         </xsl:when>
1695                         <xsl:when
1696                                 test="ancestor-or-self::marc:datafield/@tag=110 and $controlField008-11='k' and $controlField008-14='b'">
1697                                 <xsl:attribute name="authority">
1698                                         <xsl:text>cash</xsl:text>
1699                                 </xsl:attribute>
1700                         </xsl:when>
1701                         <xsl:when
1702                                 test="100 &lt;= ancestor-or-self::marc:datafield/@tag and ancestor-or-self::marc:datafield/@tag &lt;= 155 and $controlField008-11='b'">
1703                                 <xsl:attribute name="authority">
1704                                         <xsl:text>lcshcl</xsl:text>
1705                                 </xsl:attribute>
1706                         </xsl:when>
1707                         <xsl:when
1708                                 test="(ancestor-or-self::marc:datafield/@tag=100 or ancestor-or-self::marc:datafield/@tag=110 or ancestor-or-self::marc:datafield/@tag=111 or ancestor-or-self::marc:datafield/@tag=130 or ancestor-or-self::marc:datafield/@tag=151) and $controlField008-11='c'">
1709                                 <xsl:attribute name="authority">
1710                                         <xsl:text>nlmnaf</xsl:text>
1711                                 </xsl:attribute>
1712                         </xsl:when>
1713                         <xsl:when
1714                                 test="(ancestor-or-self::marc:datafield/@tag=100 or ancestor-or-self::marc:datafield/@tag=110 or ancestor-or-self::marc:datafield/@tag=111 or ancestor-or-self::marc:datafield/@tag=130 or ancestor-or-self::marc:datafield/@tag=151) and $controlField008-11='d'">
1715                                 <xsl:attribute name="authority">
1716                                         <xsl:text>nalnaf</xsl:text>
1717                                 </xsl:attribute>
1718                         </xsl:when>
1719                         <xsl:when
1720                                 test="100 &lt;= ancestor-or-self::marc:datafield/@tag and ancestor-or-self::marc:datafield/@tag &lt;= 155 and $controlField008-11='r'">
1721                                 <xsl:attribute name="authority">
1722                                         <xsl:text>aat</xsl:text>
1723                                 </xsl:attribute>
1724                         </xsl:when>
1725                         <xsl:when
1726                                 test="100 &lt;= ancestor-or-self::marc:datafield/@tag and ancestor-or-self::marc:datafield/@tag &lt;= 155 and $controlField008-11='s'">
1727                                 <xsl:attribute name="authority">sears</xsl:attribute>
1728                         </xsl:when>
1729                         <xsl:when
1730                                 test="100 &lt;= ancestor-or-self::marc:datafield/@tag and ancestor-or-self::marc:datafield/@tag &lt;= 155 and $controlField008-11='v'">
1731                                 <xsl:attribute name="authority">rvm</xsl:attribute>
1732                         </xsl:when>
1733                         <xsl:when
1734                                 test="100 &lt;= ancestor-or-self::marc:datafield/@tag and ancestor-or-self::marc:datafield/@tag &lt;= 155 and $controlField008-11='z'">
1735                                 <xsl:attribute name="authority">
1736                                         <xsl:value-of
1737                                                 select="../marc:datafield[ancestor-or-self::marc:datafield/@tag=040]/marc:subfield[@code='f']"
1738                                         />
1739                                 </xsl:attribute>
1740                         </xsl:when>
1741                         <xsl:when
1742                                 test="(ancestor-or-self::marc:datafield/@tag=111 or ancestor-or-self::marc:datafield/@tag=130) and $controlField008-11='a' and $controlField008-14='a'">
1743                                 <xsl:attribute name="authority">
1744                                         <xsl:text>naf</xsl:text>
1745                                 </xsl:attribute>
1746                         </xsl:when>
1747                         <xsl:when
1748                                 test="(ancestor-or-self::marc:datafield/@tag=111 or ancestor-or-self::marc:datafield/@tag=130) and $controlField008-11='a' and $controlField008-14='b'">
1749                                 <xsl:attribute name="authority">
1750                                         <xsl:text>lcsh</xsl:text>
1751                                 </xsl:attribute>
1752                         </xsl:when>
1753                         <xsl:when
1754                                 test="(ancestor-or-self::marc:datafield/@tag=111 or ancestor-or-self::marc:datafield/@tag=130) and $controlField008-11='k' ">
1755                                 <xsl:attribute name="authority">
1756                                         <xsl:text>lacnaf</xsl:text>
1757                                 </xsl:attribute>
1758                         </xsl:when>
1759                         <xsl:when
1760                                 test="(ancestor-or-self::marc:datafield/@tag=148 or ancestor-or-self::marc:datafield/@tag=150  or ancestor-or-self::marc:datafield/@tag=155) and $controlField008-11='a' ">
1761                                 <xsl:attribute name="authority">
1762                                         <xsl:text>lcsh</xsl:text>
1763                                 </xsl:attribute>
1764                         </xsl:when>
1765                         <xsl:when
1766                                 test="(ancestor-or-self::marc:datafield/@tag=148 or ancestor-or-self::marc:datafield/@tag=150  or ancestor-or-self::marc:datafield/@tag=155) and $controlField008-11='a' ">
1767                                 <xsl:attribute name="authority">
1768                                         <xsl:text>lcsh</xsl:text>
1769                                 </xsl:attribute>
1770                         </xsl:when>
1771                         <xsl:when
1772                                 test="(ancestor-or-self::marc:datafield/@tag=148 or ancestor-or-self::marc:datafield/@tag=150  or ancestor-or-self::marc:datafield/@tag=155) and $controlField008-11='c' ">
1773                                 <xsl:attribute name="authority">
1774                                         <xsl:text>mesh</xsl:text>
1775                                 </xsl:attribute>
1776                         </xsl:when>
1777                         <xsl:when
1778                                 test="(ancestor-or-self::marc:datafield/@tag=148 or ancestor-or-self::marc:datafield/@tag=150  or ancestor-or-self::marc:datafield/@tag=155) and $controlField008-11='d' ">
1779                                 <xsl:attribute name="authority">
1780                                         <xsl:text>nal</xsl:text>
1781                                 </xsl:attribute>
1782                         </xsl:when>
1783                         <xsl:when
1784                                 test="(ancestor-or-self::marc:datafield/@tag=148 or ancestor-or-self::marc:datafield/@tag=150  or ancestor-or-self::marc:datafield/@tag=155) and $controlField008-11='k' ">
1785                                 <xsl:attribute name="authority">
1786                                         <xsl:text>cash</xsl:text>
1787                                 </xsl:attribute>
1788                         </xsl:when>
1789                         <xsl:when
1790                                 test="ancestor-or-self::marc:datafield/@tag=151 and $controlField008-11='a' and $controlField008-14='a'">
1791                                 <xsl:attribute name="authority">
1792                                         <xsl:text>naf</xsl:text>
1793                                 </xsl:attribute>
1794                         </xsl:when>
1795                         <xsl:when
1796                                 test="ancestor-or-self::marc:datafield/@tag=151 and $controlField008-11='a' and $controlField008-14='b'">
1797                                 <xsl:attribute name="authority">lcsh</xsl:attribute>
1798                         </xsl:when>
1799                         <xsl:when
1800                                 test="ancestor-or-self::marc:datafield/@tag=151 and $controlField008-11='k' and $controlField008-14='a'">
1801                                 <xsl:attribute name="authority">lacnaf</xsl:attribute>
1802                         </xsl:when>
1803                         <xsl:when
1804                                 test="ancestor-or-self::marc:datafield/@tag=151 and $controlField008-11='k' and $controlField008-14='b'">
1805                                 <xsl:attribute name="authority">cash</xsl:attribute>
1806                         </xsl:when>
1807                         <xsl:when
1808                                 test="(..//ancestor-or-self::marc:datafield/@tag=180 or ..//ancestor-or-self::marc:datafield/@tag=181 or ..//ancestor-or-self::marc:datafield/@tag=182 or ..//ancestor-or-self::marc:datafield/@tag=185) and $controlField008-11='a'">
1809                                 <xsl:attribute name="authority">lcsh</xsl:attribute>
1810                         </xsl:when>
1811                         <xsl:when
1812                                 test="ancestor-or-self::marc:datafield/@tag=700 and (@ind1='0' or @ind1='1') and @ind2='0'">
1813                                 <xsl:attribute name="authority">naf</xsl:attribute>
1814                         </xsl:when>
1815                         <xsl:when
1816                                 test="ancestor-or-self::marc:datafield/@tag=700 and (@ind1='0' or @ind1='1') and @ind2='5'">
1817                                 <xsl:attribute name="authority">lacnaf</xsl:attribute>
1818                         </xsl:when>
1819                         <xsl:when test="ancestor-or-self::marc:datafield/@tag=700 and @ind1='3' and @ind2='0'">
1820                                 <xsl:attribute name="authority">lcsh</xsl:attribute>
1821                         </xsl:when>
1822                         <xsl:when test="ancestor-or-self::marc:datafield/@tag=700 and @ind1='3' and @ind2='5'">
1823                                 <xsl:attribute name="authority">cash</xsl:attribute>
1824                         </xsl:when>
1825                         <xsl:when
1826                                 test="(700 &lt;= ancestor-or-self::marc:datafield/@tag and ancestor-or-self::marc:datafield/@tag &lt;= 755 ) and @ind2='1'">
1827                                 <xsl:attribute name="authority">lcshcl</xsl:attribute>
1828                         </xsl:when>
1829                         <xsl:when
1830                                 test="(ancestor-or-self::marc:datafield/@tag=700 or ancestor-or-self::marc:datafield/@tag=710 or ancestor-or-self::marc:datafield/@tag=711 or ancestor-or-self::marc:datafield/@tag=730 or ancestor-or-self::marc:datafield/@tag=751)  and @ind2='2'">
1831                                 <xsl:attribute name="authority">nlmnaf</xsl:attribute>
1832                         </xsl:when>
1833                         <xsl:when
1834                                 test="(ancestor-or-self::marc:datafield/@tag=700 or ancestor-or-self::marc:datafield/@tag=710 or ancestor-or-self::marc:datafield/@tag=711 or ancestor-or-self::marc:datafield/@tag=730 or ancestor-or-self::marc:datafield/@tag=751)  and @ind2='3'">
1835                                 <xsl:attribute name="authority">nalnaf</xsl:attribute>
1836                         </xsl:when>
1837                         <xsl:when
1838                                 test="(700 &lt;= ancestor-or-self::marc:datafield/@tag and ancestor-or-self::marc:datafield/@tag &lt;= 755 ) and @ind2='6'">
1839                                 <xsl:attribute name="authority">rvm</xsl:attribute>
1840                         </xsl:when>
1841                         <xsl:when
1842                                 test="(700 &lt;= ancestor-or-self::marc:datafield/@tag and ancestor-or-self::marc:datafield/@tag &lt;= 755 ) and @ind2='7'">
1843                                 <xsl:attribute name="authority">
1844                                         <xsl:value-of select="marc:subfield[@code='2']"/>
1845                                 </xsl:attribute>
1846                         </xsl:when>
1847                         <xsl:when
1848                                 test="(ancestor-or-self::marc:datafield/@tag=710 or ancestor-or-self::marc:datafield/@tag=711 or ancestor-or-self::marc:datafield/@tag=730 or ancestor-or-self::marc:datafield/@tag=751)  and @ind2='5'">
1849                                 <xsl:attribute name="authority">lacnaf</xsl:attribute>
1850                         </xsl:when>
1851                         <xsl:when
1852                                 test="(ancestor-or-self::marc:datafield/@tag=710 or ancestor-or-self::marc:datafield/@tag=711 or ancestor-or-self::marc:datafield/@tag=730 or ancestor-or-self::marc:datafield/@tag=751)  and @ind2='0'">
1853                                 <xsl:attribute name="authority">naf</xsl:attribute>
1854                         </xsl:when>
1855                         <xsl:when
1856                                 test="(ancestor-or-self::marc:datafield/@tag=748 or ancestor-or-self::marc:datafield/@tag=750 or ancestor-or-self::marc:datafield/@tag=755)  and @ind2='0'">
1857                                 <xsl:attribute name="authority">lcsh</xsl:attribute>
1858                         </xsl:when>
1859                         <xsl:when
1860                                 test="(ancestor-or-self::marc:datafield/@tag=748 or ancestor-or-self::marc:datafield/@tag=750 or ancestor-or-self::marc:datafield/@tag=755)  and @ind2='2'">
1861                                 <xsl:attribute name="authority">mesh</xsl:attribute>
1862                         </xsl:when>
1863                         <xsl:when
1864                                 test="(ancestor-or-self::marc:datafield/@tag=748 or ancestor-or-self::marc:datafield/@tag=750 or ancestor-or-self::marc:datafield/@tag=755)  and @ind2='3'">
1865                                 <xsl:attribute name="authority">nal</xsl:attribute>
1866                         </xsl:when>
1867                         <xsl:when
1868                                 test="(ancestor-or-self::marc:datafield/@tag=748 or ancestor-or-self::marc:datafield/@tag=750 or ancestor-or-self::marc:datafield/@tag=755)  and @ind2='5'">
1869                                 <xsl:attribute name="authority">cash</xsl:attribute>
1870                         </xsl:when>
1871                 </xsl:choose>
1872         </xsl:template>
1873         <xsl:template match="*"/>
1874 </xsl:stylesheet>$XSLT$ WHERE name = 'mads21';
1875
1876 COMMIT;
1877
1878 -- Update auditor tables to catch changes to source tables.
1879 --   Can be removed/skipped if there were no schema changes.
1880 SELECT auditor.update_auditors();