]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/sql/Pg/version-upgrade/3.0.6-3.1.0-upgrade-db.sql
LP#1772028 Add some FK violation functions just in case they are missing
[Evergreen.git] / Open-ILS / src / sql / Pg / version-upgrade / 3.0.6-3.1.0-upgrade-db.sql
1 --Upgrade Script for 3.0.6 to 3.1.0
2 \set eg_version '''3.1.0'''
3 BEGIN;
4 INSERT INTO config.upgrade_log (version, applied_to) VALUES ('3.1.0', :eg_version);
5
6 SELECT evergreen.upgrade_deps_block_check('1089', :eg_version);
7
8 -- Add the circ.holds.max_duplicate_holds org. unit setting type.
9 INSERT into config.org_unit_setting_type
10 ( name, grp, label, description, datatype, fm_class )
11 VALUES
12 ( 'circ.holds.max_duplicate_holds', 'holds',
13    oils_i18n_gettext(
14      'circ.holds.max_duplicate_holds',
15      'Maximum number of duplicate holds allowed.',
16      'coust', 'label'),
17    oils_i18n_gettext(
18      'circ.holds.max_duplicate_holds',
19      'Maximum number of duplicate title or metarecord holds allowed per patron.',
20      'coust', 'description'),
21    'integer', null );
22
23
24
25 SELECT evergreen.upgrade_deps_block_check('1090', :eg_version);
26
27 ALTER TABLE biblio.record_entry
28     ADD COLUMN merge_date TIMESTAMP WITH TIME ZONE,
29     ADD COLUMN merged_to BIGINT REFERENCES biblio.record_entry(id);
30
31 CREATE OR REPLACE FUNCTION asset.merge_record_assets( target_record BIGINT, source_record BIGINT ) RETURNS INT AS $func$
32 DECLARE
33     moved_objects INT := 0;
34     source_cn     asset.call_number%ROWTYPE;
35     target_cn     asset.call_number%ROWTYPE;
36     metarec       metabib.metarecord%ROWTYPE;
37     hold          action.hold_request%ROWTYPE;
38     ser_rec       serial.record_entry%ROWTYPE;
39     ser_sub       serial.subscription%ROWTYPE;
40     acq_lineitem  acq.lineitem%ROWTYPE;
41     acq_request   acq.user_request%ROWTYPE;
42     booking       booking.resource_type%ROWTYPE;
43     source_part   biblio.monograph_part%ROWTYPE;
44     target_part   biblio.monograph_part%ROWTYPE;
45     multi_home    biblio.peer_bib_copy_map%ROWTYPE;
46     uri_count     INT := 0;
47     counter       INT := 0;
48     uri_datafield TEXT;
49     uri_text      TEXT := '';
50 BEGIN
51
52     -- move any 856 entries on records that have at least one MARC-mapped URI entry
53     SELECT  INTO uri_count COUNT(*)
54       FROM  asset.uri_call_number_map m
55             JOIN asset.call_number cn ON (m.call_number = cn.id)
56       WHERE cn.record = source_record;
57
58     IF uri_count > 0 THEN
59         
60         -- This returns more nodes than you might expect:
61         -- 7 instead of 1 for an 856 with $u $y $9
62         SELECT  COUNT(*) INTO counter
63           FROM  oils_xpath_table(
64                     'id',
65                     'marc',
66                     'biblio.record_entry',
67                     '//*[@tag="856"]',
68                     'id=' || source_record
69                 ) as t(i int,c text);
70     
71         FOR i IN 1 .. counter LOOP
72             SELECT  '<datafield xmlns="http://www.loc.gov/MARC21/slim"' || 
73                         ' tag="856"' ||
74                         ' ind1="' || FIRST(ind1) || '"'  ||
75                         ' ind2="' || FIRST(ind2) || '">' ||
76                         STRING_AGG(
77                             '<subfield code="' || subfield || '">' ||
78                             regexp_replace(
79                                 regexp_replace(
80                                     regexp_replace(data,'&','&amp;','g'),
81                                     '>', '&gt;', 'g'
82                                 ),
83                                 '<', '&lt;', 'g'
84                             ) || '</subfield>', ''
85                         ) || '</datafield>' INTO uri_datafield
86               FROM  oils_xpath_table(
87                         'id',
88                         'marc',
89                         'biblio.record_entry',
90                         '//*[@tag="856"][position()=' || i || ']/@ind1|' ||
91                         '//*[@tag="856"][position()=' || i || ']/@ind2|' ||
92                         '//*[@tag="856"][position()=' || i || ']/*/@code|' ||
93                         '//*[@tag="856"][position()=' || i || ']/*[@code]',
94                         'id=' || source_record
95                     ) as t(id int,ind1 text, ind2 text,subfield text,data text);
96
97             -- As most of the results will be NULL, protect against NULLifying
98             -- the valid content that we do generate
99             uri_text := uri_text || COALESCE(uri_datafield, '');
100         END LOOP;
101
102         IF uri_text <> '' THEN
103             UPDATE  biblio.record_entry
104               SET   marc = regexp_replace(marc,'(</[^>]*record>)', uri_text || E'\\1')
105               WHERE id = target_record;
106         END IF;
107
108     END IF;
109
110         -- Find and move metarecords to the target record
111         SELECT  INTO metarec *
112           FROM  metabib.metarecord
113           WHERE master_record = source_record;
114
115         IF FOUND THEN
116                 UPDATE  metabib.metarecord
117                   SET   master_record = target_record,
118                         mods = NULL
119                   WHERE id = metarec.id;
120
121                 moved_objects := moved_objects + 1;
122         END IF;
123
124         -- Find call numbers attached to the source ...
125         FOR source_cn IN SELECT * FROM asset.call_number WHERE record = source_record LOOP
126
127                 SELECT  INTO target_cn *
128                   FROM  asset.call_number
129                   WHERE label = source_cn.label
130             AND prefix = source_cn.prefix
131             AND suffix = source_cn.suffix
132                         AND owning_lib = source_cn.owning_lib
133                         AND record = target_record
134                         AND NOT deleted;
135
136                 -- ... and if there's a conflicting one on the target ...
137                 IF FOUND THEN
138
139                         -- ... move the copies to that, and ...
140                         UPDATE  asset.copy
141                           SET   call_number = target_cn.id
142                           WHERE call_number = source_cn.id;
143
144                         -- ... move V holds to the move-target call number
145                         FOR hold IN SELECT * FROM action.hold_request WHERE target = source_cn.id AND hold_type = 'V' LOOP
146                 
147                                 UPDATE  action.hold_request
148                                   SET   target = target_cn.id
149                                   WHERE id = hold.id;
150                 
151                                 moved_objects := moved_objects + 1;
152                         END LOOP;
153         
154             UPDATE asset.call_number SET deleted = TRUE WHERE id = source_cn.id;
155
156                 -- ... if not ...
157                 ELSE
158                         -- ... just move the call number to the target record
159                         UPDATE  asset.call_number
160                           SET   record = target_record
161                           WHERE id = source_cn.id;
162                 END IF;
163
164                 moved_objects := moved_objects + 1;
165         END LOOP;
166
167         -- Find T holds targeting the source record ...
168         FOR hold IN SELECT * FROM action.hold_request WHERE target = source_record AND hold_type = 'T' LOOP
169
170                 -- ... and move them to the target record
171                 UPDATE  action.hold_request
172                   SET   target = target_record
173                   WHERE id = hold.id;
174
175                 moved_objects := moved_objects + 1;
176         END LOOP;
177
178         -- Find serial records targeting the source record ...
179         FOR ser_rec IN SELECT * FROM serial.record_entry WHERE record = source_record LOOP
180                 -- ... and move them to the target record
181                 UPDATE  serial.record_entry
182                   SET   record = target_record
183                   WHERE id = ser_rec.id;
184
185                 moved_objects := moved_objects + 1;
186         END LOOP;
187
188         -- Find serial subscriptions targeting the source record ...
189         FOR ser_sub IN SELECT * FROM serial.subscription WHERE record_entry = source_record LOOP
190                 -- ... and move them to the target record
191                 UPDATE  serial.subscription
192                   SET   record_entry = target_record
193                   WHERE id = ser_sub.id;
194
195                 moved_objects := moved_objects + 1;
196         END LOOP;
197
198         -- Find booking resource types targeting the source record ...
199         FOR booking IN SELECT * FROM booking.resource_type WHERE record = source_record LOOP
200                 -- ... and move them to the target record
201                 UPDATE  booking.resource_type
202                   SET   record = target_record
203                   WHERE id = booking.id;
204
205                 moved_objects := moved_objects + 1;
206         END LOOP;
207
208         -- Find acq lineitems targeting the source record ...
209         FOR acq_lineitem IN SELECT * FROM acq.lineitem WHERE eg_bib_id = source_record LOOP
210                 -- ... and move them to the target record
211                 UPDATE  acq.lineitem
212                   SET   eg_bib_id = target_record
213                   WHERE id = acq_lineitem.id;
214
215                 moved_objects := moved_objects + 1;
216         END LOOP;
217
218         -- Find acq user purchase requests targeting the source record ...
219         FOR acq_request IN SELECT * FROM acq.user_request WHERE eg_bib = source_record LOOP
220                 -- ... and move them to the target record
221                 UPDATE  acq.user_request
222                   SET   eg_bib = target_record
223                   WHERE id = acq_request.id;
224
225                 moved_objects := moved_objects + 1;
226         END LOOP;
227
228         -- Find parts attached to the source ...
229         FOR source_part IN SELECT * FROM biblio.monograph_part WHERE record = source_record LOOP
230
231                 SELECT  INTO target_part *
232                   FROM  biblio.monograph_part
233                   WHERE label = source_part.label
234                         AND record = target_record;
235
236                 -- ... and if there's a conflicting one on the target ...
237                 IF FOUND THEN
238
239                         -- ... move the copy-part maps to that, and ...
240                         UPDATE  asset.copy_part_map
241                           SET   part = target_part.id
242                           WHERE part = source_part.id;
243
244                         -- ... move P holds to the move-target part
245                         FOR hold IN SELECT * FROM action.hold_request WHERE target = source_part.id AND hold_type = 'P' LOOP
246                 
247                                 UPDATE  action.hold_request
248                                   SET   target = target_part.id
249                                   WHERE id = hold.id;
250                 
251                                 moved_objects := moved_objects + 1;
252                         END LOOP;
253
254                 -- ... if not ...
255                 ELSE
256                         -- ... just move the part to the target record
257                         UPDATE  biblio.monograph_part
258                           SET   record = target_record
259                           WHERE id = source_part.id;
260                 END IF;
261
262                 moved_objects := moved_objects + 1;
263         END LOOP;
264
265         -- Find multi_home items attached to the source ...
266         FOR multi_home IN SELECT * FROM biblio.peer_bib_copy_map WHERE peer_record = source_record LOOP
267                 -- ... and move them to the target record
268                 UPDATE  biblio.peer_bib_copy_map
269                   SET   peer_record = target_record
270                   WHERE id = multi_home.id;
271
272                 moved_objects := moved_objects + 1;
273         END LOOP;
274
275         -- And delete mappings where the item's home bib was merged with the peer bib
276         DELETE FROM biblio.peer_bib_copy_map WHERE peer_record = (
277                 SELECT (SELECT record FROM asset.call_number WHERE id = call_number)
278                 FROM asset.copy WHERE id = target_copy
279         );
280
281     -- Apply merge tracking
282     UPDATE biblio.record_entry 
283         SET merge_date = NOW() WHERE id = target_record;
284
285     UPDATE biblio.record_entry
286         SET merge_date = NOW(), merged_to = target_record
287         WHERE id = source_record;
288
289     -- Finally, "delete" the source record
290     DELETE FROM biblio.record_entry WHERE id = source_record;
291
292         -- That's all, folks!
293         RETURN moved_objects;
294 END;
295 $func$ LANGUAGE plpgsql;
296
297
298
299 SELECT evergreen.upgrade_deps_block_check('1091', :eg_version);
300
301 ALTER TABLE acq.funding_source DROP CONSTRAINT funding_source_code_key;
302 ALTER TABLE acq.funding_source ALTER COLUMN code SET NOT NULL;
303 ALTER TABLE acq.funding_source ADD CONSTRAINT funding_source_code_once_per_owner UNIQUE (code,owner);
304
305
306 SELECT evergreen.upgrade_deps_block_check('1092', :eg_version);
307
308 CREATE OR REPLACE FUNCTION metabib.reingest_record_attributes (rid BIGINT, pattr_list TEXT[] DEFAULT NULL, prmarc TEXT DEFAULT NULL, rdeleted BOOL DEFAULT TRUE) RETURNS VOID AS $func$
309 DECLARE
310     transformed_xml TEXT;
311     rmarc           TEXT := prmarc;
312     tmp_val         TEXT;
313     prev_xfrm       TEXT;
314     normalizer      RECORD;
315     xfrm            config.xml_transform%ROWTYPE;
316     attr_vector     INT[] := '{}'::INT[];
317     attr_vector_tmp INT[];
318     attr_list       TEXT[] := pattr_list;
319     attr_value      TEXT[];
320     norm_attr_value TEXT[];
321     tmp_xml         TEXT;
322     tmp_array       TEXT[];
323     attr_def        config.record_attr_definition%ROWTYPE;
324     ccvm_row        config.coded_value_map%ROWTYPE;
325     jump_past       BOOL;
326 BEGIN
327
328     IF attr_list IS NULL OR rdeleted THEN -- need to do the full dance on INSERT or undelete
329         SELECT ARRAY_AGG(name) INTO attr_list FROM config.record_attr_definition
330         WHERE (
331             tag IS NOT NULL OR
332             fixed_field IS NOT NULL OR
333             xpath IS NOT NULL OR
334             phys_char_sf IS NOT NULL OR
335             composite
336         ) AND (
337             filter OR sorter
338         );
339     END IF;
340
341     IF rmarc IS NULL THEN
342         SELECT marc INTO rmarc FROM biblio.record_entry WHERE id = rid;
343     END IF;
344
345     FOR attr_def IN SELECT * FROM config.record_attr_definition WHERE NOT composite AND name = ANY( attr_list ) ORDER BY format LOOP
346
347         jump_past := FALSE; -- This gets set when we are non-multi and have found something
348         attr_value := '{}'::TEXT[];
349         norm_attr_value := '{}'::TEXT[];
350         attr_vector_tmp := '{}'::INT[];
351
352         SELECT * INTO ccvm_row FROM config.coded_value_map c WHERE c.ctype = attr_def.name LIMIT 1; 
353
354         IF attr_def.tag IS NOT NULL THEN -- tag (and optional subfield list) selection
355             SELECT  ARRAY_AGG(value) INTO attr_value
356               FROM  (SELECT * FROM metabib.full_rec ORDER BY tag, subfield) AS x
357               WHERE record = rid
358                     AND tag LIKE attr_def.tag
359                     AND CASE
360                         WHEN attr_def.sf_list IS NOT NULL 
361                             THEN POSITION(subfield IN attr_def.sf_list) > 0
362                         ELSE TRUE
363                     END
364               GROUP BY tag
365               ORDER BY tag;
366
367             IF NOT attr_def.multi THEN
368                 attr_value := ARRAY[ARRAY_TO_STRING(attr_value, COALESCE(attr_def.joiner,' '))];
369                 jump_past := TRUE;
370             END IF;
371         END IF;
372
373         IF NOT jump_past AND attr_def.fixed_field IS NOT NULL THEN -- a named fixed field, see config.marc21_ff_pos_map.fixed_field
374             attr_value := attr_value || vandelay.marc21_extract_fixed_field_list(rmarc, attr_def.fixed_field);
375
376             IF NOT attr_def.multi THEN
377                 attr_value := ARRAY[attr_value[1]];
378                 jump_past := TRUE;
379             END IF;
380         END IF;
381
382         IF NOT jump_past AND attr_def.xpath IS NOT NULL THEN -- and xpath expression
383
384             SELECT INTO xfrm * FROM config.xml_transform WHERE name = attr_def.format;
385         
386             -- See if we can skip the XSLT ... it's expensive
387             IF prev_xfrm IS NULL OR prev_xfrm <> xfrm.name THEN
388                 -- Can't skip the transform
389                 IF xfrm.xslt <> '---' THEN
390                     transformed_xml := oils_xslt_process(rmarc,xfrm.xslt);
391                 ELSE
392                     transformed_xml := rmarc;
393                 END IF;
394     
395                 prev_xfrm := xfrm.name;
396             END IF;
397
398             IF xfrm.name IS NULL THEN
399                 -- just grab the marcxml (empty) transform
400                 SELECT INTO xfrm * FROM config.xml_transform WHERE xslt = '---' LIMIT 1;
401                 prev_xfrm := xfrm.name;
402             END IF;
403
404             FOR tmp_xml IN SELECT UNNEST(oils_xpath(attr_def.xpath, transformed_xml, ARRAY[ARRAY[xfrm.prefix, xfrm.namespace_uri]])) LOOP
405                 tmp_val := oils_xpath_string(
406                                 '//*',
407                                 tmp_xml,
408                                 COALESCE(attr_def.joiner,' '),
409                                 ARRAY[ARRAY[xfrm.prefix, xfrm.namespace_uri]]
410                             );
411                 IF tmp_val IS NOT NULL AND BTRIM(tmp_val) <> '' THEN
412                     attr_value := attr_value || tmp_val;
413                     EXIT WHEN NOT attr_def.multi;
414                 END IF;
415             END LOOP;
416         END IF;
417
418         IF NOT jump_past AND attr_def.phys_char_sf IS NOT NULL THEN -- a named Physical Characteristic, see config.marc21_physical_characteristic_*_map
419             SELECT  ARRAY_AGG(m.value) INTO tmp_array
420               FROM  vandelay.marc21_physical_characteristics(rmarc) v
421                     LEFT JOIN config.marc21_physical_characteristic_value_map m ON (m.id = v.value)
422               WHERE v.subfield = attr_def.phys_char_sf AND (m.value IS NOT NULL AND BTRIM(m.value) <> '')
423                     AND ( ccvm_row.id IS NULL OR ( ccvm_row.id IS NOT NULL AND v.id IS NOT NULL) );
424
425             attr_value := attr_value || tmp_array;
426
427             IF NOT attr_def.multi THEN
428                 attr_value := ARRAY[attr_value[1]];
429             END IF;
430
431         END IF;
432
433                 -- apply index normalizers to attr_value
434         FOR tmp_val IN SELECT value FROM UNNEST(attr_value) x(value) LOOP
435             FOR normalizer IN
436                 SELECT  n.func AS func,
437                         n.param_count AS param_count,
438                         m.params AS params
439                   FROM  config.index_normalizer n
440                         JOIN config.record_attr_index_norm_map m ON (m.norm = n.id)
441                   WHERE attr = attr_def.name
442                   ORDER BY m.pos LOOP
443                     EXECUTE 'SELECT ' || normalizer.func || '(' ||
444                     COALESCE( quote_literal( tmp_val ), 'NULL' ) ||
445                         CASE
446                             WHEN normalizer.param_count > 0
447                                 THEN ',' || REPLACE(REPLACE(BTRIM(normalizer.params,'[]'),E'\'',E'\\\''),E'"',E'\'')
448                                 ELSE ''
449                             END ||
450                     ')' INTO tmp_val;
451
452             END LOOP;
453             IF tmp_val IS NOT NULL AND tmp_val <> '' THEN
454                 -- note that a string that contains only blanks
455                 -- is a valid value for some attributes
456                 norm_attr_value := norm_attr_value || tmp_val;
457             END IF;
458         END LOOP;
459         
460         IF attr_def.filter THEN
461             -- Create unknown uncontrolled values and find the IDs of the values
462             IF ccvm_row.id IS NULL THEN
463                 FOR tmp_val IN SELECT value FROM UNNEST(norm_attr_value) x(value) LOOP
464                     IF tmp_val IS NOT NULL AND BTRIM(tmp_val) <> '' THEN
465                         BEGIN -- use subtransaction to isolate unique constraint violations
466                             INSERT INTO metabib.uncontrolled_record_attr_value ( attr, value ) VALUES ( attr_def.name, tmp_val );
467                         EXCEPTION WHEN unique_violation THEN END;
468                     END IF;
469                 END LOOP;
470
471                 SELECT ARRAY_AGG(id) INTO attr_vector_tmp FROM metabib.uncontrolled_record_attr_value WHERE attr = attr_def.name AND value = ANY( norm_attr_value );
472             ELSE
473                 SELECT ARRAY_AGG(id) INTO attr_vector_tmp FROM config.coded_value_map WHERE ctype = attr_def.name AND code = ANY( norm_attr_value );
474             END IF;
475
476             -- Add the new value to the vector
477             attr_vector := attr_vector || attr_vector_tmp;
478         END IF;
479
480         IF attr_def.sorter THEN
481             DELETE FROM metabib.record_sorter WHERE source = rid AND attr = attr_def.name;
482             IF norm_attr_value[1] IS NOT NULL THEN
483                 INSERT INTO metabib.record_sorter (source, attr, value) VALUES (rid, attr_def.name, norm_attr_value[1]);
484             END IF;
485         END IF;
486
487     END LOOP;
488
489 /* We may need to rewrite the vlist to contain
490    the intersection of new values for requested
491    attrs and old values for ignored attrs. To
492    do this, we take the old attr vlist and
493    subtract any values that are valid for the
494    requested attrs, and then add back the new
495    set of attr values. */
496
497     IF ARRAY_LENGTH(pattr_list, 1) > 0 THEN 
498         SELECT vlist INTO attr_vector_tmp FROM metabib.record_attr_vector_list WHERE source = rid;
499         SELECT attr_vector_tmp - ARRAY_AGG(id::INT) INTO attr_vector_tmp FROM metabib.full_attr_id_map WHERE attr = ANY (pattr_list);
500         attr_vector := attr_vector || attr_vector_tmp;
501     END IF;
502
503     -- On to composite attributes, now that the record attrs have been pulled.  Processed in name order, so later composite
504     -- attributes can depend on earlier ones.
505     PERFORM metabib.compile_composite_attr_cache_init();
506     FOR attr_def IN SELECT * FROM config.record_attr_definition WHERE composite AND name = ANY( attr_list ) ORDER BY name LOOP
507
508         FOR ccvm_row IN SELECT * FROM config.coded_value_map c WHERE c.ctype = attr_def.name ORDER BY value LOOP
509
510             tmp_val := metabib.compile_composite_attr( ccvm_row.id );
511             CONTINUE WHEN tmp_val IS NULL OR tmp_val = ''; -- nothing to do
512
513             IF attr_def.filter THEN
514                 IF attr_vector @@ tmp_val::query_int THEN
515                     attr_vector = attr_vector + intset(ccvm_row.id);
516                     EXIT WHEN NOT attr_def.multi;
517                 END IF;
518             END IF;
519
520             IF attr_def.sorter THEN
521                 IF attr_vector @@ tmp_val THEN
522                     DELETE FROM metabib.record_sorter WHERE source = rid AND attr = attr_def.name;
523                     INSERT INTO metabib.record_sorter (source, attr, value) VALUES (rid, attr_def.name, ccvm_row.code);
524                 END IF;
525             END IF;
526
527         END LOOP;
528
529     END LOOP;
530
531     IF ARRAY_LENGTH(attr_vector, 1) > 0 THEN
532         IF rdeleted THEN -- initial insert OR revivication
533             DELETE FROM metabib.record_attr_vector_list WHERE source = rid;
534             INSERT INTO metabib.record_attr_vector_list (source, vlist) VALUES (rid, attr_vector);
535         ELSE
536             UPDATE metabib.record_attr_vector_list SET vlist = attr_vector WHERE source = rid;
537         END IF;
538     END IF;
539
540 END;
541
542 $func$ LANGUAGE PLPGSQL;
543
544
545
546 SELECT evergreen.upgrade_deps_block_check('1093', :eg_version);
547
548 UPDATE config.record_attr_definition SET tag = '041', sf_list = 'abdefgm' where name = 'item_lang';
549
550
551
552 SELECT evergreen.upgrade_deps_block_check('1094', :eg_version);
553
554 SELECT metabib.reingest_record_attributes (record, '{item_lang}'::TEXT[])
555   FROM (SELECT  DISTINCT record
556           FROM  metabib.real_full_rec
557            WHERE tag = '041'
558                   AND subfield IN ('a','b','d','e','f','g','m')
559        ) x;
560
561
562
563 SELECT evergreen.upgrade_deps_block_check('1095', :eg_version);
564
565 CREATE OR REPLACE FUNCTION asset.copy_state (cid BIGINT) RETURNS TEXT AS $$
566 DECLARE
567     last_circ_stop      TEXT;
568     the_copy        asset.copy%ROWTYPE;
569 BEGIN
570
571     SELECT * INTO the_copy FROM asset.copy WHERE id = cid;
572     IF NOT FOUND THEN RETURN NULL; END IF;
573
574     IF the_copy.status = 3 THEN -- Lost
575         RETURN 'LOST';
576     ELSIF the_copy.status = 4 THEN -- Missing
577         RETURN 'MISSING';
578     ELSIF the_copy.status = 14 THEN -- Damaged
579         RETURN 'DAMAGED';
580     ELSIF the_copy.status = 17 THEN -- Lost and paid
581         RETURN 'LOST_AND_PAID';
582     END IF;
583
584     SELECT stop_fines INTO last_circ_stop
585       FROM  action.circulation
586       WHERE target_copy = cid
587       ORDER BY xact_start DESC LIMIT 1;
588
589     IF FOUND THEN
590         IF last_circ_stop IN (
591             'CLAIMSNEVERCHECKEDOUT',
592             'CLAIMSRETURNED',
593             'LONGOVERDUE'
594         ) THEN
595             RETURN last_circ_stop;
596         END IF;
597     END IF;
598
599     RETURN 'NORMAL';
600 END;
601 $$ LANGUAGE PLPGSQL;
602
603 CREATE TYPE config.copy_alert_type_state AS ENUM (
604     'NORMAL',
605     'LOST',
606     'LOST_AND_PAID',
607     'MISSING',
608     'DAMAGED',
609     'CLAIMSRETURNED',
610     'LONGOVERDUE',
611     'CLAIMSNEVERCHECKEDOUT'
612 );
613
614 CREATE TYPE config.copy_alert_type_event AS ENUM (
615     'CHECKIN',
616     'CHECKOUT'
617 );
618
619 CREATE TABLE config.copy_alert_type (
620     id          serial  primary key, -- reserve 1-100 for system
621     scope_org   int not null references actor.org_unit (id) on delete cascade,
622     active      bool    not null default true,
623     name        text    not null unique,
624     state       config.copy_alert_type_state,
625     event       config.copy_alert_type_event,
626     in_renew    bool,
627     invert_location bool    not null default false,
628     at_circ     bool,
629     at_owning   bool,
630     next_status int[]
631 );
632 SELECT SETVAL('config.copy_alert_type_id_seq'::TEXT, 100);
633
634 CREATE OR REPLACE FUNCTION evergreen.asset_copy_alert_copy_inh_fkey() RETURNS TRIGGER AS $f$
635 BEGIN
636         PERFORM 1 FROM asset.copy WHERE id = NEW.copy;
637         IF NOT FOUND THEN
638                 RAISE foreign_key_violation USING MESSAGE = FORMAT(
639                         $$Referenced asset.copy id not found, copy:%s$$, NEW.copy
640                 );
641         END IF;
642         RETURN NEW;
643 END;
644 $f$ LANGUAGE PLPGSQL VOLATILE COST 50;
645
646 CREATE TABLE actor.copy_alert_suppress (
647     id          serial primary key,
648     org         int not null references actor.org_unit (id) on delete cascade,
649     alert_type  int not null references config.copy_alert_type (id) on delete cascade
650 );
651
652 CREATE TABLE asset.copy_alert (
653     id      bigserial   primary key,
654     alert_type  int     not null references config.copy_alert_type (id) on delete cascade,
655     copy        bigint  not null,
656     temp        bool    not null default false,
657     create_time timestamptz not null default now(),
658     create_staff    bigint  not null references actor.usr (id) on delete set null,
659     note        text,
660     ack_time    timestamptz,
661     ack_staff   bigint references actor.usr (id) on delete set null
662 );
663
664 CREATE CONSTRAINT TRIGGER inherit_asset_copy_alert_copy_fkey
665         AFTER UPDATE OR INSERT ON asset.copy_alert
666         DEFERRABLE FOR EACH ROW EXECUTE PROCEDURE evergreen.asset_copy_alert_copy_inh_fkey();
667
668 CREATE VIEW asset.active_copy_alert AS
669     SELECT  *
670       FROM  asset.copy_alert
671       WHERE ack_time IS NULL;
672
673
674
675 SELECT evergreen.upgrade_deps_block_check('1096', :eg_version);
676
677 -- staff-usable alert types with no location awareness
678 INSERT INTO config.copy_alert_type (id, scope_org, active, name, state, event, in_renew)
679 VALUES (1, 1, TRUE, 'Normal checkout', 'NORMAL', 'CHECKOUT', FALSE);
680 INSERT INTO config.copy_alert_type (id, scope_org, active, name, state, event, in_renew)
681 VALUES (2, 1, TRUE, 'Normal checkin', 'NORMAL', 'CHECKIN', FALSE);
682 INSERT INTO config.copy_alert_type (id, scope_org, active, name, state, event, in_renew)
683 VALUES (3, 1, FALSE, 'Normal renewal', 'NORMAL', 'CHECKIN', TRUE);
684
685 -- copy alerts upon checkin or renewal of exceptional copy statuses are not active by
686 -- default; they're meant to be turned once a site is ready to fully
687 -- commit to using the webstaff client for circulation
688 INSERT INTO config.copy_alert_type (id, scope_org, active, name, state, event)
689 VALUES (4, 1, FALSE, 'Checkin of lost copy', 'LOST', 'CHECKIN');
690 INSERT INTO config.copy_alert_type (id, scope_org, active, name, state, event)
691 VALUES (5, 1, FALSE, 'Checkin of missing copy', 'MISSING', 'CHECKIN');
692 INSERT INTO config.copy_alert_type (id, scope_org, active, name, state, event)
693 VALUES (6, 1, FALSE, 'Checkin of lost-and-paid copy', 'LOST_AND_PAID', 'CHECKIN');
694 INSERT INTO config.copy_alert_type (id, scope_org, active, name, state, event)
695 VALUES (7, 1, FALSE, 'Checkin of damaged copy', 'DAMAGED', 'CHECKIN');
696 INSERT INTO config.copy_alert_type (id, scope_org, active, name, state, event)
697 VALUES (8, 1, FALSE, 'Checkin of claims-returned copy', 'CLAIMSRETURNED', 'CHECKIN');
698 INSERT INTO config.copy_alert_type (id, scope_org, active, name, state, event)
699 VALUES (9, 1, FALSE, 'Checkin of long overdue copy', 'LONGOVERDUE', 'CHECKIN');
700 INSERT INTO config.copy_alert_type (id, scope_org, active, name, state, event)
701 VALUES (10, 1, FALSE, 'Checkin of claims-never-checked-out copy', 'CLAIMSNEVERCHECKEDOUT', 'CHECKIN');
702
703 -- copy alerts upon checkout of exceptional copy statuses are not active by
704 -- default; they're meant to be turned once a site is ready to fully
705 -- commit to using the webstaff client for circulation
706 INSERT INTO config.copy_alert_type (id, scope_org, active, name, state, event)
707 VALUES (11, 1, FALSE, 'Checkout of lost copy', 'LOST', 'CHECKOUT');
708 INSERT INTO config.copy_alert_type (id, scope_org, active, name, state, event)
709 VALUES (12, 1, FALSE, 'Checkout of missing copy', 'MISSING', 'CHECKOUT');
710 INSERT INTO config.copy_alert_type (id, scope_org, active, name, state, event)
711 VALUES (13, 1, FALSE, 'Checkout of lost-and-paid copy', 'LOST_AND_PAID', 'CHECKOUT');
712 INSERT INTO config.copy_alert_type (id, scope_org, active, name, state, event)
713 VALUES (14, 1, FALSE, 'Checkout of damaged copy', 'DAMAGED', 'CHECKOUT');
714 INSERT INTO config.copy_alert_type (id, scope_org, active, name, state, event)
715 VALUES (15, 1, FALSE, 'Checkout of claims-returned copy', 'CLAIMSRETURNED', 'CHECKOUT');
716 INSERT INTO config.copy_alert_type (id, scope_org, active, name, state, event)
717 VALUES (16, 1, FALSE, 'Checkout of long overdue copy', 'LONGOVERDUE', 'CHECKOUT');
718 INSERT INTO config.copy_alert_type (id, scope_org, active, name, state, event)
719 VALUES (17, 1, FALSE, 'Checkout of claims-never-checked-out copy', 'CLAIMSNEVERCHECKEDOUT', 'CHECKOUT');
720
721 -- staff-usable alert types based on location
722 INSERT INTO config.copy_alert_type (id, scope_org, active, name, state, event, in_renew, at_circ)
723 VALUES (18, 1, FALSE, 'Normal checkout at circ lib', 'NORMAL', 'CHECKOUT', FALSE, TRUE);
724 INSERT INTO config.copy_alert_type (id, scope_org, active, name, state, event, in_renew, at_circ)
725 VALUES (19, 1, FALSE, 'Normal checkin at circ lib', 'NORMAL', 'CHECKIN', FALSE, TRUE);
726 INSERT INTO config.copy_alert_type (id, scope_org, active, name, state, event, in_renew, at_circ)
727 VALUES (20, 1, FALSE, 'Normal renewal at circ lib', 'NORMAL', 'CHECKIN', TRUE, TRUE);
728
729 INSERT INTO config.copy_alert_type (id, scope_org, active, name, state, event, in_renew, at_owning)
730 VALUES (21, 1, FALSE, 'Normal checkout at owning lib', 'NORMAL', 'CHECKOUT', FALSE, TRUE);
731 INSERT INTO config.copy_alert_type (id, scope_org, active, name, state, event, in_renew, at_owning)
732 VALUES (22, 1, FALSE, 'Normal checkin at owning lib', 'NORMAL', 'CHECKIN', FALSE, TRUE);
733 INSERT INTO config.copy_alert_type (id, scope_org, active, name, state, event, in_renew, at_owning)
734 VALUES (23, 1, FALSE, 'Normal renewal at owning lib', 'NORMAL', 'CHECKIN', TRUE, TRUE);
735
736
737 SELECT evergreen.upgrade_deps_block_check('1097', :eg_version);
738
739 INSERT INTO config.org_unit_setting_type
740     (name, grp, label, description, datatype)
741     VALUES
742         ('circ.copy_alerts.forgive_fines_on_lost_checkin',
743          'circ',
744          oils_i18n_gettext('circ.copy_alerts.forgive_fines_on_lost_checkin',
745             'Forgive fines when checking out a lost item and copy alert is suppressed?',
746             'coust', 'label'),
747          oils_i18n_gettext('circ.copy_alerts.forgive_fines_on_lost_checkin',
748             'Controls whether fines are automatically forgiven when checking out an '||
749             'item that has been marked as lost, and the corresponding copy alert has been '||
750             'suppressed.',
751             'coust', 'description'),
752         'bool');
753
754 INSERT INTO config.org_unit_setting_type
755     (name, grp, label, description, datatype)
756     VALUES
757         ('circ.copy_alerts.forgive_fines_on_long_overdue_checkin',
758          'circ',
759          oils_i18n_gettext('circ.copy_alerts.forgive_fines_on_long_overdue_checkin',
760             'Forgive fines when checking out a long-overdue item and copy alert is suppressed?',
761             'coust', 'label'),
762          oils_i18n_gettext('circ.copy_alerts.forgive_fines_on_lost_checkin',
763             'Controls whether fines are automatically forgiven when checking out an '||
764             'item that has been marked as lost, and the corresponding copy alert has been '||
765             'suppressed.',
766             'coust', 'description'),
767         'bool');
768
769
770 SELECT evergreen.upgrade_deps_block_check('1098', :eg_version);
771
772 \qecho Copying copy alert messages to normal checkout copy alerts...
773 INSERT INTO asset.copy_alert (alert_type, copy, note, create_staff)
774 SELECT 1, id, alert_message, 1
775 FROM asset.copy
776 WHERE alert_message IS NOT NULL
777 AND   alert_message <> '';
778
779 \qecho Copying copy alert messages to normal checkin copy alerts...
780 INSERT INTO asset.copy_alert (alert_type, copy, note, create_staff)
781 SELECT 2, id, alert_message, 1
782 FROM asset.copy
783 WHERE alert_message IS NOT NULL
784 AND   alert_message <> '';
785
786 \qecho Clearing legacy copy alert field; this may take a while
787 UPDATE asset.copy SET alert_message = NULL
788 WHERE alert_message IS NOT NULL;
789
790
791 SELECT evergreen.upgrade_deps_block_check('1099', :eg_version);
792
793 \qecho Making the following copy alert types active by default; if you
794 \qecho are not using the web staff client yet, you may want to disable
795 \qecho them.
796 \qecho  - Checkin of lost, missing, lost-and-paid, damaged, claims returned,
797 \qecho    long overdue, and claims never checked out items.
798 \qecho  - Checkout of lost, missing, lost-and-paid, damaged, claims returned,
799 \qecho    long overdue, and claims never checked out items.
800
801 UPDATE config.copy_alert_type
802 SET active = TRUE
803 WHERE id IN (4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17);
804
805
806 SELECT evergreen.upgrade_deps_block_check('1100', :eg_version);
807
808 -- NEW config.metabib_field entries
809
810 UPDATE config.metabib_field SET display_xpath = facet_xpath, display_field = TRUE WHERE id = 33;
811
812 INSERT INTO config.metabib_field (id, field_class, name, 
813     label, xpath, display_field, search_field, browse_field)
814 VALUES (
815     38, 'identifier', 'edition', 
816     oils_i18n_gettext(38, 'Edition', 'cmf', 'label'),
817     $$//mods33:mods/mods33:originInfo//mods33:edition[1]$$,
818     TRUE, TRUE, FALSE
819 );
820
821 INSERT INTO config.metabib_field (id, field_class, name, 
822     label, xpath, display_field, search_field, browse_field)
823 VALUES (
824     39, 'keyword', 'physical_description', 
825     oils_i18n_gettext(39, 'Physical Descrption', 'cmf', 'label'),
826     $$(//mods33:mods/mods33:physicalDescription/mods33:form|//mods33:mods/mods33:physicalDescription/mods33:extent|//mods33:mods/mods33:physicalDescription/mods33:reformattingQuality|//mods33:mods/mods33:physicalDescription/mods33:internetMediaType|//mods33:mods/mods33:physicalDescription/mods33:digitalOrigin)$$,
827     TRUE, TRUE, FALSE
828 );
829
830 INSERT INTO config.metabib_field (id, field_class, name, 
831     label, xpath, display_field, search_field, browse_field)
832 VALUES (
833     40, 'identifier', 'publisher', 
834     oils_i18n_gettext(40, 'Publisher', 'cmf', 'label'),
835     $$//mods33:mods/mods33:originInfo//mods33:publisher[1]$$,
836     TRUE, TRUE, FALSE
837 );
838
839 INSERT INTO config.metabib_field (id, field_class, name, 
840     label, xpath, display_field, search_field, browse_field)
841 VALUES (
842     41, 'keyword', 'abstract', 
843     oils_i18n_gettext(41, 'Abstract', 'cmf', 'label'),
844     $$//mods33:mods/mods33:abstract$$,
845     TRUE, TRUE, FALSE
846 );
847
848 INSERT INTO config.metabib_field (id, field_class, name, 
849     label, xpath, display_field, search_field, browse_field)
850 VALUES (
851     42, 'keyword', 'toc', 
852     oils_i18n_gettext(42, 'Table of Contents', 'cmf', 'label'),
853     $$//mods33:tableOfContents$$,
854     TRUE, TRUE, FALSE
855 );
856
857 INSERT INTO config.metabib_field (id, field_class, name, 
858     label, xpath, display_field, search_field, browse_field)
859 VALUES (
860     43, 'identifier', 'type_of_resource', 
861     oils_i18n_gettext(43, 'Type of Resource', 'cmf', 'label'),
862     $$//mods33:mods/mods33:typeOfResource$$,
863     TRUE, FALSE, FALSE
864 );
865
866 INSERT INTO config.metabib_field (id, field_class, name, 
867     label, xpath, display_field, search_field, browse_field)
868 VALUES (
869     44, 'identifier', 'pubdate', 
870     oils_i18n_gettext(44, 'Publication Date', 'cmf', 'label'),
871     $$//mods33:mods/mods33:originInfo//mods33:dateIssued[@encoding="marc"]|//mods33:mods/mods33:originInfo//mods33:dateIssued[1]$$,
872     TRUE, FALSE, FALSE
873 );
874
875 INSERT INTO config.metabib_field (id, field_class, name, 
876     label, xpath, display_field, search_field, browse_field)
877 VALUES (
878     46, 'keyword', 'bibliography', 
879     oils_i18n_gettext(46, 'Bibliography', 'cmf', 'label'),
880     $$//mods33:note[@type='bibliography']$$,
881     TRUE, TRUE, FALSE
882 ),(
883     47, 'keyword', 'thesis', 
884     oils_i18n_gettext(47, 'Thesis', 'cmf', 'label'),
885     $$//mods33:note[@type='thesis']$$,
886     TRUE, TRUE, FALSE
887 ),(
888     48, 'keyword', 'production_credits', 
889     oils_i18n_gettext(48, 'Creation/Production Credits', 'cmf', 'label'),
890     $$//mods33:note[@type='creation/production credits']$$,
891     TRUE, TRUE, FALSE
892 ),(
893     49, 'keyword', 'performers', 
894     oils_i18n_gettext(49, 'Performers', 'cmf', 'label'),
895     $$//mods33:note[@type='performers']$$,
896     TRUE, TRUE, FALSE
897 ),(
898     50, 'keyword', 'general_note', 
899     oils_i18n_gettext(50, 'General Note', 'cmf', 'label'),
900     $$//mods33:note[not(@type)]$$,
901     TRUE, TRUE, FALSE
902 )
903 ;
904
905 INSERT INTO config.metabib_field (id, field_class, name, format,
906     label, xpath, display_xpath, display_field, search_field, browse_field)
907 VALUES (
908     51, 'author', 'first_author', 'mods32',
909     oils_i18n_gettext(51, 'Author', 'cmf', 'label'),
910     $$//mods32:mods/mods32:name[mods32:role/mods32:roleTerm[text()='creator']][1]$$,
911     $$//*[local-name()='namePart']$$,
912     TRUE, TRUE, FALSE
913 );
914
915 INSERT INTO config.metabib_field (id, field_class, name, format,
916     label, xpath, display_xpath, display_field, search_field, browse_field)
917 VALUES (
918     52, 'identifier', 'origin_info', 'marcxml',
919     oils_i18n_gettext(52, 'Origin Info', 'cmf', 'label'),
920     $$//*[@tag='260']$$,
921     $$//*[local-name()='subfield' and contains('abc',@code)]$$,
922     TRUE, FALSE, FALSE
923 );
924
925
926 -- Modify existing config.metabib_field entries
927
928 UPDATE config.metabib_field SET display_field = TRUE WHERE id IN (
929     1,  -- seriestitle
930     11, -- subject_geographic 
931     12, -- subject_name
932     13, -- subject_temporal
933     14, -- subject_topic
934     19, -- ISSN
935     20, -- UPC
936     26  -- TCN
937 );
938
939 -- Map display field names to config.metabib_field entries
940
941 INSERT INTO config.display_field_map (name, field, multi) VALUES 
942     ('series_title',         1, TRUE),
943     ('subject_geographic',  11, TRUE),
944     ('subject_name',        12, TRUE),
945     ('subject_temporal',    13, TRUE),
946     ('subject_topic',       14, TRUE),
947     ('issn',                19, TRUE),
948     ('upc',                 20, TRUE),
949     ('tcn',                 26, FALSE),
950     ('edition',             38, FALSE),
951     ('physical_description',39, TRUE),
952     ('genre',               33, TRUE),
953     ('bibliography',        46, TRUE),
954     ('thesis',              47, TRUE),
955     ('performers',          49, TRUE),
956     ('production_credits',  48, TRUE),
957     ('general_note',        50, TRUE),
958     ('publisher',           52, FALSE),
959     ('abstract',            41, FALSE),
960     ('toc',                 42, FALSE),
961     ('type_of_resource',    43, FALSE),
962     ('pubdate',             44, FALSE)
963 ;
964
965 UPDATE config.display_field_map SET field = 51 WHERE name = 'author';
966
967 -- Add a column to wide-display-entry per well-known field
968
969 DROP VIEW IF EXISTS metabib.wide_display_entry;
970 CREATE VIEW metabib.wide_display_entry AS
971     SELECT 
972         bre.id AS source,
973         COALESCE(mcde_title.value, 'null')::TEXT AS title,
974         COALESCE(mcde_author.value, 'null')::TEXT AS author,
975         COALESCE(mcde_subject_geographic.value, 'null')::TEXT AS subject_geographic,
976         COALESCE(mcde_subject_name.value, 'null')::TEXT AS subject_name,
977         COALESCE(mcde_subject_temporal.value, 'null')::TEXT AS subject_temporal,
978         COALESCE(mcde_subject_topic.value, 'null')::TEXT AS subject_topic,
979         COALESCE(mcde_creators.value, 'null')::TEXT AS creators,
980         COALESCE(mcde_isbn.value, 'null')::TEXT AS isbn,
981         COALESCE(mcde_issn.value, 'null')::TEXT AS issn,
982         COALESCE(mcde_upc.value, 'null')::TEXT AS upc,
983         COALESCE(mcde_tcn.value, 'null')::TEXT AS tcn,
984         COALESCE(mcde_edition.value, 'null')::TEXT AS edition,
985         COALESCE(mcde_physical_description.value, 'null')::TEXT AS physical_description,
986         COALESCE(mcde_publisher.value, 'null')::TEXT AS publisher,
987         COALESCE(mcde_series_title.value, 'null')::TEXT AS series_title,
988         COALESCE(mcde_abstract.value, 'null')::TEXT AS abstract,
989         COALESCE(mcde_toc.value, 'null')::TEXT AS toc,
990         COALESCE(mcde_pubdate.value, 'null')::TEXT AS pubdate,
991         COALESCE(mcde_type_of_resource.value, 'null')::TEXT AS type_of_resource
992     FROM biblio.record_entry bre 
993     LEFT JOIN metabib.compressed_display_entry mcde_title 
994         ON (bre.id = mcde_title.source AND mcde_title.name = 'title')
995     LEFT JOIN metabib.compressed_display_entry mcde_author 
996         ON (bre.id = mcde_author.source AND mcde_author.name = 'author')
997     LEFT JOIN metabib.compressed_display_entry mcde_subject 
998         ON (bre.id = mcde_subject.source AND mcde_subject.name = 'subject')
999     LEFT JOIN metabib.compressed_display_entry mcde_subject_geographic 
1000         ON (bre.id = mcde_subject_geographic.source 
1001             AND mcde_subject_geographic.name = 'subject_geographic')
1002     LEFT JOIN metabib.compressed_display_entry mcde_subject_name 
1003         ON (bre.id = mcde_subject_name.source 
1004             AND mcde_subject_name.name = 'subject_name')
1005     LEFT JOIN metabib.compressed_display_entry mcde_subject_temporal 
1006         ON (bre.id = mcde_subject_temporal.source 
1007             AND mcde_subject_temporal.name = 'subject_temporal')
1008     LEFT JOIN metabib.compressed_display_entry mcde_subject_topic 
1009         ON (bre.id = mcde_subject_topic.source 
1010             AND mcde_subject_topic.name = 'subject_topic')
1011     LEFT JOIN metabib.compressed_display_entry mcde_creators 
1012         ON (bre.id = mcde_creators.source AND mcde_creators.name = 'creators')
1013     LEFT JOIN metabib.compressed_display_entry mcde_isbn 
1014         ON (bre.id = mcde_isbn.source AND mcde_isbn.name = 'isbn')
1015     LEFT JOIN metabib.compressed_display_entry mcde_issn 
1016         ON (bre.id = mcde_issn.source AND mcde_issn.name = 'issn')
1017     LEFT JOIN metabib.compressed_display_entry mcde_upc 
1018         ON (bre.id = mcde_upc.source AND mcde_upc.name = 'upc')
1019     LEFT JOIN metabib.compressed_display_entry mcde_tcn 
1020         ON (bre.id = mcde_tcn.source AND mcde_tcn.name = 'tcn')
1021     LEFT JOIN metabib.compressed_display_entry mcde_edition 
1022         ON (bre.id = mcde_edition.source AND mcde_edition.name = 'edition')
1023     LEFT JOIN metabib.compressed_display_entry mcde_physical_description 
1024         ON (bre.id = mcde_physical_description.source 
1025             AND mcde_physical_description.name = 'physical_description')
1026     LEFT JOIN metabib.compressed_display_entry mcde_publisher 
1027         ON (bre.id = mcde_publisher.source AND mcde_publisher.name = 'publisher')
1028     LEFT JOIN metabib.compressed_display_entry mcde_series_title 
1029         ON (bre.id = mcde_series_title.source AND mcde_series_title.name = 'series_title')
1030     LEFT JOIN metabib.compressed_display_entry mcde_abstract 
1031         ON (bre.id = mcde_abstract.source AND mcde_abstract.name = 'abstract')
1032     LEFT JOIN metabib.compressed_display_entry mcde_toc 
1033         ON (bre.id = mcde_toc.source AND mcde_toc.name = 'toc')
1034     LEFT JOIN metabib.compressed_display_entry mcde_pubdate 
1035         ON (bre.id = mcde_pubdate.source AND mcde_pubdate.name = 'pubdate')
1036     LEFT JOIN metabib.compressed_display_entry mcde_type_of_resource 
1037         ON (bre.id = mcde_type_of_resource.source 
1038             AND mcde_type_of_resource.name = 'type_of_resource')
1039 ;
1040
1041 CREATE OR REPLACE VIEW reporter.old_super_simple_record AS
1042 SELECT  r.id,
1043     r.fingerprint,
1044     r.quality,
1045     r.tcn_source,
1046     r.tcn_value,
1047     evergreen.oils_json_to_text(d.title) AS title,
1048     evergreen.oils_json_to_text(d.author) AS author,
1049     evergreen.oils_json_to_text(d.publisher) AS publisher,
1050     evergreen.oils_json_to_text(d.pubdate) AS pubdate,
1051     CASE WHEN d.isbn = 'null'
1052         THEN NULL
1053         ELSE (SELECT ARRAY(SELECT json_array_elements_text(d.isbn::JSON)))
1054     END AS isbn,
1055     CASE WHEN d.issn = 'null'
1056         THEN NULL
1057         ELSE (SELECT ARRAY(SELECT json_array_elements_text(d.issn::JSON)))
1058     END AS issn
1059   FROM  biblio.record_entry r
1060         JOIN metabib.wide_display_entry d ON (r.id = d.source);
1061
1062
1063
1064 SELECT evergreen.upgrade_deps_block_check('1101', :eg_version);
1065
1066 ALTER TABLE config.metabib_field ALTER COLUMN xpath DROP NOT NULL;
1067
1068 CREATE TABLE config.metabib_field_virtual_map (
1069     id      SERIAL  PRIMARY KEY,
1070     real    INT NOT NULL REFERENCES config.metabib_field (id),
1071     virtual INT NOT NULL REFERENCES config.metabib_field (id),
1072     weight  INT NOT NULL DEFAULT 1
1073 );
1074 COMMENT ON TABLE config.metabib_field_virtual_map IS $$
1075 Maps between real (physically extracted) index definitions
1076 and virtual (target sync, no required extraction of its own)
1077 index definitions.
1078
1079 The virtual side may not extract any data of its own, but
1080 will collect data from all of the real fields.  This reduces
1081 extraction (ingest) overhead by eliminating duplcated extraction,
1082 and allows for searching across novel combinations of fields, such
1083 as names used as either subjects or authors.  By preserving this
1084 mapping rather than defining duplicate extractions, information
1085 about the originating, "real" index definitions can be used
1086 in interesting ways, such as highlighting in search results.
1087 $$;
1088
1089 CREATE OR REPLACE VIEW metabib.combined_all_field_entry AS
1090     SELECT * FROM metabib.combined_title_field_entry
1091         UNION ALL
1092     SELECT * FROM metabib.combined_author_field_entry
1093         UNION ALL
1094     SELECT * FROM metabib.combined_subject_field_entry
1095         UNION ALL
1096     SELECT * FROM metabib.combined_keyword_field_entry
1097         UNION ALL
1098     SELECT * FROM metabib.combined_identifier_field_entry
1099         UNION ALL
1100     SELECT * FROM metabib.combined_series_field_entry;
1101
1102
1103 CREATE OR REPLACE FUNCTION biblio.extract_metabib_field_entry (
1104     rid BIGINT,
1105     default_joiner TEXT,
1106     field_types TEXT[],
1107     only_fields INT[]
1108 ) RETURNS SETOF metabib.field_entry_template AS $func$
1109 DECLARE
1110     bib     biblio.record_entry%ROWTYPE;
1111     idx     config.metabib_field%ROWTYPE;
1112     xfrm        config.xml_transform%ROWTYPE;
1113     prev_xfrm   TEXT;
1114     transformed_xml TEXT;
1115     xml_node    TEXT;
1116     xml_node_list   TEXT[];
1117     facet_text  TEXT;
1118     display_text TEXT;
1119     browse_text TEXT;
1120     sort_value  TEXT;
1121     raw_text    TEXT;
1122     curr_text   TEXT;
1123     joiner      TEXT := default_joiner; -- XXX will index defs supply a joiner?
1124     authority_text TEXT;
1125     authority_link BIGINT;
1126     output_row  metabib.field_entry_template%ROWTYPE;
1127     process_idx BOOL;
1128 BEGIN
1129
1130     -- Start out with no field-use bools set
1131     output_row.browse_field = FALSE;
1132     output_row.facet_field = FALSE;
1133     output_row.display_field = FALSE;
1134     output_row.search_field = FALSE;
1135
1136     -- Get the record
1137     SELECT INTO bib * FROM biblio.record_entry WHERE id = rid;
1138
1139     -- Loop over the indexing entries
1140     FOR idx IN SELECT * FROM config.metabib_field WHERE id = ANY (only_fields) ORDER BY format LOOP
1141         CONTINUE WHEN idx.xpath IS NULL OR idx.xpath = ''; -- pure virtual field
1142
1143         process_idx := FALSE;
1144         IF idx.display_field AND 'display' = ANY (field_types) THEN process_idx = TRUE; END IF;
1145         IF idx.browse_field AND 'browse' = ANY (field_types) THEN process_idx = TRUE; END IF;
1146         IF idx.search_field AND 'search' = ANY (field_types) THEN process_idx = TRUE; END IF;
1147         IF idx.facet_field AND 'facet' = ANY (field_types) THEN process_idx = TRUE; END IF;
1148         CONTINUE WHEN process_idx = FALSE; -- disabled for all types
1149
1150         joiner := COALESCE(idx.joiner, default_joiner);
1151
1152         SELECT INTO xfrm * from config.xml_transform WHERE name = idx.format;
1153
1154         -- See if we can skip the XSLT ... it's expensive
1155         IF prev_xfrm IS NULL OR prev_xfrm <> xfrm.name THEN
1156             -- Can't skip the transform
1157             IF xfrm.xslt <> '---' THEN
1158                 transformed_xml := oils_xslt_process(bib.marc,xfrm.xslt);
1159             ELSE
1160                 transformed_xml := bib.marc;
1161             END IF;
1162
1163             prev_xfrm := xfrm.name;
1164         END IF;
1165
1166         xml_node_list := oils_xpath( idx.xpath, transformed_xml, ARRAY[ARRAY[xfrm.prefix, xfrm.namespace_uri]] );
1167
1168         raw_text := NULL;
1169         FOR xml_node IN SELECT x FROM unnest(xml_node_list) AS x LOOP
1170             CONTINUE WHEN xml_node !~ E'^\\s*<';
1171
1172             -- XXX much of this should be moved into oils_xpath_string...
1173             curr_text := ARRAY_TO_STRING(evergreen.array_remove_item_by_value(evergreen.array_remove_item_by_value(
1174                 oils_xpath( '//text()', -- get the content of all the nodes within the main selected node
1175                     REGEXP_REPLACE( xml_node, E'\\s+', ' ', 'g' ) -- Translate adjacent whitespace to a single space
1176                 ), ' '), ''),  -- throw away morally empty (bankrupt?) strings
1177                 joiner
1178             );
1179
1180             CONTINUE WHEN curr_text IS NULL OR curr_text = '';
1181
1182             IF raw_text IS NOT NULL THEN
1183                 raw_text := raw_text || joiner;
1184             END IF;
1185
1186             raw_text := COALESCE(raw_text,'') || curr_text;
1187
1188             -- autosuggest/metabib.browse_entry
1189             IF idx.browse_field THEN
1190
1191                 IF idx.browse_xpath IS NOT NULL AND idx.browse_xpath <> '' THEN
1192                     browse_text := oils_xpath_string( idx.browse_xpath, xml_node, joiner, ARRAY[ARRAY[xfrm.prefix, xfrm.namespace_uri]] );
1193                 ELSE
1194                     browse_text := curr_text;
1195                 END IF;
1196
1197                 IF idx.browse_sort_xpath IS NOT NULL AND
1198                     idx.browse_sort_xpath <> '' THEN
1199
1200                     sort_value := oils_xpath_string(
1201                         idx.browse_sort_xpath, xml_node, joiner,
1202                         ARRAY[ARRAY[xfrm.prefix, xfrm.namespace_uri]]
1203                     );
1204                 ELSE
1205                     sort_value := browse_text;
1206                 END IF;
1207
1208                 output_row.field_class = idx.field_class;
1209                 output_row.field = idx.id;
1210                 output_row.source = rid;
1211                 output_row.value = BTRIM(REGEXP_REPLACE(browse_text, E'\\s+', ' ', 'g'));
1212                 output_row.sort_value :=
1213                     public.naco_normalize(sort_value);
1214
1215                 output_row.authority := NULL;
1216
1217                 IF idx.authority_xpath IS NOT NULL AND idx.authority_xpath <> '' THEN
1218                     authority_text := oils_xpath_string(
1219                         idx.authority_xpath, xml_node, joiner,
1220                         ARRAY[
1221                             ARRAY[xfrm.prefix, xfrm.namespace_uri],
1222                             ARRAY['xlink','http://www.w3.org/1999/xlink']
1223                         ]
1224                     );
1225
1226                     IF authority_text ~ '^\d+$' THEN
1227                         authority_link := authority_text::BIGINT;
1228                         PERFORM * FROM authority.record_entry WHERE id = authority_link;
1229                         IF FOUND THEN
1230                             output_row.authority := authority_link;
1231                         END IF;
1232                     END IF;
1233
1234                 END IF;
1235
1236                 output_row.browse_field = TRUE;
1237                 -- Returning browse rows with search_field = true for search+browse
1238                 -- configs allows us to retain granularity of being able to search
1239                 -- browse fields with "starts with" type operators (for example, for
1240                 -- titles of songs in music albums)
1241                 IF idx.search_field THEN
1242                     output_row.search_field = TRUE;
1243                 END IF;
1244                 RETURN NEXT output_row;
1245                 output_row.browse_field = FALSE;
1246                 output_row.search_field = FALSE;
1247                 output_row.sort_value := NULL;
1248             END IF;
1249
1250             -- insert raw node text for faceting
1251             IF idx.facet_field THEN
1252
1253                 IF idx.facet_xpath IS NOT NULL AND idx.facet_xpath <> '' THEN
1254                     facet_text := oils_xpath_string( idx.facet_xpath, xml_node, joiner, ARRAY[ARRAY[xfrm.prefix, xfrm.namespace_uri]] );
1255                 ELSE
1256                     facet_text := curr_text;
1257                 END IF;
1258
1259                 output_row.field_class = idx.field_class;
1260                 output_row.field = -1 * idx.id;
1261                 output_row.source = rid;
1262                 output_row.value = BTRIM(REGEXP_REPLACE(facet_text, E'\\s+', ' ', 'g'));
1263
1264                 output_row.facet_field = TRUE;
1265                 RETURN NEXT output_row;
1266                 output_row.facet_field = FALSE;
1267             END IF;
1268
1269             -- insert raw node text for display
1270             IF idx.display_field THEN
1271
1272                 IF idx.display_xpath IS NOT NULL AND idx.display_xpath <> '' THEN
1273                     display_text := oils_xpath_string( idx.display_xpath, xml_node, joiner, ARRAY[ARRAY[xfrm.prefix, xfrm.namespace_uri]] );
1274                 ELSE
1275                     display_text := curr_text;
1276                 END IF;
1277
1278                 output_row.field_class = idx.field_class;
1279                 output_row.field = -1 * idx.id;
1280                 output_row.source = rid;
1281                 output_row.value = BTRIM(REGEXP_REPLACE(display_text, E'\\s+', ' ', 'g'));
1282
1283                 output_row.display_field = TRUE;
1284                 RETURN NEXT output_row;
1285                 output_row.display_field = FALSE;
1286             END IF;
1287
1288         END LOOP;
1289
1290         CONTINUE WHEN raw_text IS NULL OR raw_text = '';
1291
1292         -- insert combined node text for searching
1293         IF idx.search_field THEN
1294             output_row.field_class = idx.field_class;
1295             output_row.field = idx.id;
1296             output_row.source = rid;
1297             output_row.value = BTRIM(REGEXP_REPLACE(raw_text, E'\\s+', ' ', 'g'));
1298
1299             output_row.search_field = TRUE;
1300             RETURN NEXT output_row;
1301             output_row.search_field = FALSE;
1302         END IF;
1303
1304     END LOOP;
1305
1306 END;
1307 $func$ LANGUAGE PLPGSQL;
1308
1309 CREATE OR REPLACE FUNCTION metabib.update_combined_index_vectors(bib_id BIGINT) RETURNS VOID AS $func$
1310 DECLARE
1311     rdata       TSVECTOR;
1312     vclass      TEXT;
1313     vfield      INT;
1314     rfields     INT[];
1315 BEGIN
1316     DELETE FROM metabib.combined_keyword_field_entry WHERE record = bib_id;
1317     INSERT INTO metabib.combined_keyword_field_entry(record, metabib_field, index_vector)
1318         SELECT bib_id, field, strip(COALESCE(string_agg(index_vector::TEXT,' '),'')::tsvector)
1319         FROM metabib.keyword_field_entry WHERE source = bib_id GROUP BY field;
1320     INSERT INTO metabib.combined_keyword_field_entry(record, metabib_field, index_vector)
1321         SELECT bib_id, NULL, strip(COALESCE(string_agg(index_vector::TEXT,' '),'')::tsvector)
1322         FROM metabib.keyword_field_entry WHERE source = bib_id;
1323
1324     DELETE FROM metabib.combined_title_field_entry WHERE record = bib_id;
1325     INSERT INTO metabib.combined_title_field_entry(record, metabib_field, index_vector)
1326         SELECT bib_id, field, strip(COALESCE(string_agg(index_vector::TEXT,' '),'')::tsvector)
1327         FROM metabib.title_field_entry WHERE source = bib_id GROUP BY field;
1328     INSERT INTO metabib.combined_title_field_entry(record, metabib_field, index_vector)
1329         SELECT bib_id, NULL, strip(COALESCE(string_agg(index_vector::TEXT,' '),'')::tsvector)
1330         FROM metabib.title_field_entry WHERE source = bib_id;
1331
1332     DELETE FROM metabib.combined_author_field_entry WHERE record = bib_id;
1333     INSERT INTO metabib.combined_author_field_entry(record, metabib_field, index_vector)
1334         SELECT bib_id, field, strip(COALESCE(string_agg(index_vector::TEXT,' '),'')::tsvector)
1335         FROM metabib.author_field_entry WHERE source = bib_id GROUP BY field;
1336     INSERT INTO metabib.combined_author_field_entry(record, metabib_field, index_vector)
1337         SELECT bib_id, NULL, strip(COALESCE(string_agg(index_vector::TEXT,' '),'')::tsvector)
1338         FROM metabib.author_field_entry WHERE source = bib_id;
1339
1340     DELETE FROM metabib.combined_subject_field_entry WHERE record = bib_id;
1341     INSERT INTO metabib.combined_subject_field_entry(record, metabib_field, index_vector)
1342         SELECT bib_id, field, strip(COALESCE(string_agg(index_vector::TEXT,' '),'')::tsvector)
1343         FROM metabib.subject_field_entry WHERE source = bib_id GROUP BY field;
1344     INSERT INTO metabib.combined_subject_field_entry(record, metabib_field, index_vector)
1345         SELECT bib_id, NULL, strip(COALESCE(string_agg(index_vector::TEXT,' '),'')::tsvector)
1346         FROM metabib.subject_field_entry WHERE source = bib_id;
1347
1348     DELETE FROM metabib.combined_series_field_entry WHERE record = bib_id;
1349     INSERT INTO metabib.combined_series_field_entry(record, metabib_field, index_vector)
1350         SELECT bib_id, field, strip(COALESCE(string_agg(index_vector::TEXT,' '),'')::tsvector)
1351         FROM metabib.series_field_entry WHERE source = bib_id GROUP BY field;
1352     INSERT INTO metabib.combined_series_field_entry(record, metabib_field, index_vector)
1353         SELECT bib_id, NULL, strip(COALESCE(string_agg(index_vector::TEXT,' '),'')::tsvector)
1354         FROM metabib.series_field_entry WHERE source = bib_id;
1355
1356     DELETE FROM metabib.combined_identifier_field_entry WHERE record = bib_id;
1357     INSERT INTO metabib.combined_identifier_field_entry(record, metabib_field, index_vector)
1358         SELECT bib_id, field, strip(COALESCE(string_agg(index_vector::TEXT,' '),'')::tsvector)
1359         FROM metabib.identifier_field_entry WHERE source = bib_id GROUP BY field;
1360     INSERT INTO metabib.combined_identifier_field_entry(record, metabib_field, index_vector)
1361         SELECT bib_id, NULL, strip(COALESCE(string_agg(index_vector::TEXT,' '),'')::tsvector)
1362         FROM metabib.identifier_field_entry WHERE source = bib_id;
1363
1364     -- For each virtual def, gather the data from the combined real field
1365     -- entries and append it to the virtual combined entry.
1366     FOR vfield, rfields IN SELECT virtual, ARRAY_AGG(real)  FROM config.metabib_field_virtual_map GROUP BY virtual LOOP
1367         SELECT  field_class INTO vclass
1368           FROM  config.metabib_field
1369           WHERE id = vfield;
1370
1371         SELECT  string_agg(index_vector::TEXT,' ')::tsvector INTO rdata
1372           FROM  metabib.combined_all_field_entry
1373           WHERE record = bib_id
1374                 AND metabib_field = ANY (rfields);
1375
1376         BEGIN -- I cannot wait for INSERT ON CONFLICT ... 9.5, though
1377             EXECUTE $$
1378                 INSERT INTO metabib.combined_$$ || vclass || $$_field_entry
1379                     (record, metabib_field, index_vector) VALUES ($1, $2, $3)
1380             $$ USING bib_id, vfield, rdata;
1381         EXCEPTION WHEN unique_violation THEN
1382             EXECUTE $$
1383                 UPDATE  metabib.combined_$$ || vclass || $$_field_entry
1384                   SET   index_vector = index_vector || $3
1385                   WHERE record = $1
1386                         AND metabib_field = $2
1387             $$ USING bib_id, vfield, rdata;
1388         WHEN OTHERS THEN
1389             -- ignore and move on
1390         END;
1391     END LOOP;
1392 END;
1393 $func$ LANGUAGE PLPGSQL;
1394
1395 CREATE OR REPLACE VIEW search.best_tsconfig AS
1396     SELECT  m.id AS id,
1397             COALESCE(f.ts_config, c.ts_config, 'simple') AS ts_config
1398       FROM  config.metabib_field m
1399             LEFT JOIN config.metabib_class_ts_map c ON (c.field_class = m.field_class AND c.index_weight = 'C')
1400             LEFT JOIN config.metabib_field_ts_map f ON (f.metabib_field = m.id AND f.index_weight = 'C');
1401
1402 CREATE TYPE search.highlight_result AS ( id BIGINT, source BIGINT, field INT, value TEXT, highlight TEXT );
1403
1404 CREATE OR REPLACE FUNCTION search.highlight_display_fields_impl(
1405     rid         BIGINT,
1406     tsq         TEXT,
1407     field_list  INT[] DEFAULT '{}'::INT[],
1408     css_class   TEXT DEFAULT 'oils_SH',
1409     hl_all      BOOL DEFAULT TRUE,
1410     minwords    INT DEFAULT 5,
1411     maxwords    INT DEFAULT 25,
1412     shortwords  INT DEFAULT 0,
1413     maxfrags    INT DEFAULT 0,
1414     delimiter   TEXT DEFAULT ' ... '
1415 ) RETURNS SETOF search.highlight_result AS $f$
1416 DECLARE
1417     opts            TEXT := '';
1418     v_css_class     TEXT := css_class;
1419     v_delimiter     TEXT := delimiter;
1420     v_field_list    INT[] := field_list;
1421     hl_query        TEXT;
1422 BEGIN
1423     IF v_delimiter LIKE $$%'%$$ OR v_delimiter LIKE '%"%' THEN --"
1424         v_delimiter := ' ... ';
1425     END IF;
1426
1427     IF NOT hl_all THEN
1428         opts := opts || 'MinWords=' || minwords;
1429         opts := opts || ', MaxWords=' || maxwords;
1430         opts := opts || ', ShortWords=' || shortwords;
1431         opts := opts || ', MaxFragments=' || maxfrags;
1432         opts := opts || ', FragmentDelimiter="' || delimiter || '"';
1433     ELSE
1434         opts := opts || 'HighlightAll=TRUE';
1435     END IF;
1436
1437     IF v_css_class LIKE $$%'%$$ OR v_css_class LIKE '%"%' THEN -- "
1438         v_css_class := 'oils_SH';
1439     END IF;
1440
1441     opts := opts || $$, StopSel=</b>, StartSel="<b class='$$ || v_css_class; -- "
1442
1443     IF v_field_list = '{}'::INT[] THEN
1444         SELECT ARRAY_AGG(id) INTO v_field_list FROM config.metabib_field WHERE display_field;
1445     END IF;
1446
1447     hl_query := $$
1448         SELECT  de.id,
1449                 de.source,
1450                 de.field,
1451                 de.value AS value,
1452                 ts_headline(
1453                     ts_config::REGCONFIG,
1454                     evergreen.escape_for_html(de.value),
1455                     $$ || quote_literal(tsq) || $$,
1456                     $1 || ' ' || mf.field_class || ' ' || mf.name || $xx$'>"$xx$ -- "'
1457                 ) AS highlight
1458           FROM  metabib.display_entry de
1459                 JOIN config.metabib_field mf ON (mf.id = de.field)
1460                 JOIN search.best_tsconfig t ON (t.id = de.field)
1461           WHERE de.source = $2
1462                 AND field = ANY ($3)
1463           ORDER BY de.id;$$;
1464
1465     RETURN QUERY EXECUTE hl_query USING opts, rid, v_field_list;
1466 END;
1467 $f$ LANGUAGE PLPGSQL;
1468
1469 CREATE OR REPLACE FUNCTION evergreen.escape_for_html (TEXT) RETURNS TEXT AS $$
1470     SELECT  regexp_replace(
1471                 regexp_replace(
1472                     regexp_replace(
1473                         $1,
1474                         '&',
1475                         '&amp;',
1476                         'g'
1477                     ),
1478                     '<',
1479                     '&lt;',
1480                     'g'
1481                 ),
1482                 '>',
1483                 '&gt;',
1484                 'g'
1485             );
1486 $$ LANGUAGE SQL IMMUTABLE LEAKPROOF STRICT COST 10;
1487
1488 CREATE OR REPLACE FUNCTION search.highlight_display_fields(
1489     rid         BIGINT,
1490     tsq_map     TEXT, -- { '(a | b) & c' => '1,2,3,4', ...}
1491     css_class   TEXT DEFAULT 'oils_SH',
1492     hl_all      BOOL DEFAULT TRUE,
1493     minwords    INT DEFAULT 5,
1494     maxwords    INT DEFAULT 25,
1495     shortwords  INT DEFAULT 0,
1496     maxfrags    INT DEFAULT 0,
1497     delimiter   TEXT DEFAULT ' ... '
1498 ) RETURNS SETOF search.highlight_result AS $f$
1499 DECLARE
1500     tsq_hstore  HSTORE;
1501     tsq         TEXT;
1502     fields      TEXT;
1503     afields     INT[];
1504     seen        INT[];
1505 BEGIN
1506
1507     IF (tsq_map ILIKE 'hstore%') THEN
1508         EXECUTE 'SELECT ' || tsq_map INTO tsq_hstore;
1509     ELSE
1510         tsq_hstore := tsq_map::HSTORE;
1511     END IF;
1512     
1513     FOR tsq, fields IN SELECT key, value FROM each(tsq_hstore::HSTORE) LOOP
1514         SELECT  ARRAY_AGG(unnest::INT) INTO afields
1515           FROM  unnest(regexp_split_to_array(fields,','));
1516         seen := seen || afields;
1517
1518         RETURN QUERY
1519             SELECT * FROM search.highlight_display_fields_impl(
1520                 rid, tsq, afields, css_class, hl_all,minwords,
1521                 maxwords, shortwords, maxfrags, delimiter
1522             );
1523     END LOOP;
1524
1525     RETURN QUERY
1526         SELECT  id,
1527                 source,
1528                 field,
1529                 value,
1530                 value AS highlight
1531           FROM  metabib.display_entry
1532           WHERE source = rid
1533                 AND NOT (field = ANY (seen));
1534 END;
1535 $f$ LANGUAGE PLPGSQL ROWS 10;
1536  
1537 CREATE OR REPLACE FUNCTION metabib.remap_metarecord_for_bib(
1538     bib_id bigint,
1539     fp text,
1540     bib_is_deleted boolean DEFAULT false,
1541     retain_deleted boolean DEFAULT false
1542 ) RETURNS bigint AS $function$
1543 DECLARE
1544     new_mapping     BOOL := TRUE;
1545     source_count    INT;
1546     old_mr          BIGINT;
1547     tmp_mr          metabib.metarecord%ROWTYPE;
1548     deleted_mrs     BIGINT[];
1549 BEGIN
1550
1551     -- We need to make sure we're not a deleted master record of an MR
1552     IF bib_is_deleted THEN
1553         IF NOT retain_deleted THEN -- Go away for any MR that we're master of, unless retained
1554             DELETE FROM metabib.metarecord_source_map WHERE source = bib_id;
1555         END IF;
1556
1557         FOR old_mr IN SELECT id FROM metabib.metarecord WHERE master_record = bib_id LOOP
1558
1559             -- Now, are there any more sources on this MR?
1560             SELECT COUNT(*) INTO source_count FROM metabib.metarecord_source_map WHERE metarecord = old_mr;
1561
1562             IF source_count = 0 AND NOT retain_deleted THEN -- No other records
1563                 deleted_mrs := ARRAY_APPEND(deleted_mrs, old_mr); -- Just in case...
1564                 DELETE FROM metabib.metarecord WHERE id = old_mr;
1565
1566             ELSE -- indeed there are. Update it with a null cache and recalcualated master record
1567                 UPDATE  metabib.metarecord
1568                   SET   mods = NULL,
1569                         master_record = ( SELECT id FROM biblio.record_entry WHERE fingerprint = fp AND NOT deleted ORDER BY quality DESC LIMIT 1)
1570                   WHERE id = old_mr;
1571             END IF;
1572         END LOOP;
1573
1574     ELSE -- insert or update
1575
1576         FOR tmp_mr IN SELECT m.* FROM metabib.metarecord m JOIN metabib.metarecord_source_map s ON (s.metarecord = m.id) WHERE s.source = bib_id LOOP
1577
1578             -- Find the first fingerprint-matching
1579             IF old_mr IS NULL AND fp = tmp_mr.fingerprint THEN
1580                 old_mr := tmp_mr.id;
1581                 new_mapping := FALSE;
1582
1583             ELSE -- Our fingerprint changed ... maybe remove the old MR
1584                 DELETE FROM metabib.metarecord_source_map WHERE metarecord = tmp_mr.id AND source = bib_id; -- remove the old source mapping
1585                 SELECT COUNT(*) INTO source_count FROM metabib.metarecord_source_map WHERE metarecord = tmp_mr.id;
1586                 IF source_count = 0 THEN -- No other records
1587                     deleted_mrs := ARRAY_APPEND(deleted_mrs, tmp_mr.id);
1588                     DELETE FROM metabib.metarecord WHERE id = tmp_mr.id;
1589                 END IF;
1590             END IF;
1591
1592         END LOOP;
1593
1594         -- we found no suitable, preexisting MR based on old source maps
1595         IF old_mr IS NULL THEN
1596             SELECT id INTO old_mr FROM metabib.metarecord WHERE fingerprint = fp; -- is there one for our current fingerprint?
1597
1598             IF old_mr IS NULL THEN -- nope, create one and grab its id
1599                 INSERT INTO metabib.metarecord ( fingerprint, master_record ) VALUES ( fp, bib_id );
1600                 SELECT id INTO old_mr FROM metabib.metarecord WHERE fingerprint = fp;
1601
1602             ELSE -- indeed there is. update it with a null cache and recalcualated master record
1603                 UPDATE  metabib.metarecord
1604                   SET   mods = NULL,
1605                         master_record = ( SELECT id FROM biblio.record_entry WHERE fingerprint = fp AND NOT deleted ORDER BY quality DESC LIMIT 1)
1606                   WHERE id = old_mr;
1607             END IF;
1608
1609         ELSE -- there was one we already attached to, update its mods cache and master_record
1610             UPDATE  metabib.metarecord
1611               SET   mods = NULL,
1612                     master_record = ( SELECT id FROM biblio.record_entry WHERE fingerprint = fp AND NOT deleted ORDER BY quality DESC LIMIT 1)
1613               WHERE id = old_mr;
1614         END IF;
1615
1616         IF new_mapping THEN
1617             INSERT INTO metabib.metarecord_source_map (metarecord, source) VALUES (old_mr, bib_id); -- new source mapping
1618         END IF;
1619
1620     END IF;
1621
1622     IF ARRAY_UPPER(deleted_mrs,1) > 0 THEN
1623         UPDATE action.hold_request SET target = old_mr WHERE target IN ( SELECT unnest(deleted_mrs) ) AND hold_type = 'M'; -- if we had to delete any MRs above, make sure their holds are moved
1624     END IF;
1625
1626     RETURN old_mr;
1627
1628 END;
1629 $function$ LANGUAGE plpgsql;
1630
1631 CREATE OR REPLACE FUNCTION evergreen.marc_to (marc text, xfrm text) RETURNS TEXT AS $$
1632     SELECT evergreen.xml_pretty_print(xslt_process($1,xslt)::XML)::TEXT FROM config.xml_transform WHERE name = $2;
1633 $$ LANGUAGE SQL;
1634
1635
1636
1637 SELECT evergreen.upgrade_deps_block_check('1102', :eg_version);
1638
1639 update config.xml_transform set xslt = $XXXX$<?xml version="1.0" encoding="UTF-8"?>
1640 <xsl:stylesheet xmlns="http://www.loc.gov/mods/v3" xmlns:marc="http://www.loc.gov/MARC21/slim" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" exclude-result-prefixes="xlink marc" version="1.0">
1641         <xsl:output encoding="UTF-8" indent="yes" method="xml"/>
1642 <!--
1643 Revision 1.14 - Fixed template isValid and fields 010, 020, 022, 024, 028, and 037 to output additional identifier elements
1644   with corresponding @type and @invalid eq 'yes' when subfields z or y (in the case of 022) exist in the MARCXML ::: 2007/01/04 17:35:20 cred
1645
1646 Revision 1.13 - Changed order of output under cartographics to reflect schema  2006/11/28 tmee
1647
1648 Revision 1.12 - Updated to reflect MODS 3.2 Mapping  2006/10/11 tmee
1649
1650 Revision 1.11 - The attribute objectPart moved from <languageTerm> to <language>
1651       2006/04/08  jrad
1652
1653 Revision 1.10 MODS 3.1 revisions to language and classification elements
1654                                 (plus ability to find marc:collection embedded in wrapper elements such as SRU zs: wrappers)
1655                                 2006/02/06  ggar
1656
1657 Revision 1.9 subfield $y was added to field 242 2004/09/02 10:57 jrad
1658
1659 Revision 1.8 Subject chopPunctuation expanded and attribute fixes 2004/08/12 jrad
1660
1661 Revision 1.7 2004/03/25 08:29 jrad
1662
1663 Revision 1.6 various validation fixes 2004/02/20 ntra
1664
1665 Revision 1.5  2003/10/02 16:18:58  ntra
1666 MODS2 to MODS3 updates, language unstacking and
1667 de-duping, chopPunctuation expanded
1668
1669 Revision 1.3  2003/04/03 00:07:19  ntra
1670 Revision 1.3 Additional Changes not related to MODS Version 2.0 by ntra
1671
1672 Revision 1.2  2003/03/24 19:37:42  ckeith
1673 Added Log Comment
1674
1675 -->
1676         <xsl:template match="/">
1677                 <xsl:choose>
1678                         <xsl:when test="//marc:collection">
1679                                 <modsCollection xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.loc.gov/mods/v3 http://www.loc.gov/standards/mods/v3/mods-3-2.xsd">
1680                                         <xsl:for-each select="//marc:collection/marc:record">
1681                                                 <mods version="3.2">
1682                                                         <xsl:call-template name="marcRecord"/>
1683                                                 </mods>
1684                                         </xsl:for-each>
1685                                 </modsCollection>
1686                         </xsl:when>
1687                         <xsl:otherwise>
1688                                 <mods xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="3.2" xsi:schemaLocation="http://www.loc.gov/mods/v3 http://www.loc.gov/standards/mods/v3/mods-3-2.xsd">
1689                                         <xsl:for-each select="//marc:record">
1690                                                 <xsl:call-template name="marcRecord"/>
1691                                         </xsl:for-each>
1692                                 </mods>
1693                         </xsl:otherwise>
1694                 </xsl:choose>
1695         </xsl:template>
1696         <xsl:template name="marcRecord">
1697                 <xsl:variable name="leader" select="marc:leader"/>
1698                 <xsl:variable name="leader6" select="substring($leader,7,1)"/>
1699                 <xsl:variable name="leader7" select="substring($leader,8,1)"/>
1700                 <xsl:variable name="controlField008" select="marc:controlfield[@tag='008']"/>
1701                 <xsl:variable name="typeOf008">
1702                         <xsl:choose>
1703                                 <xsl:when test="$leader6='a'">
1704                                         <xsl:choose>
1705                                                 <xsl:when test="$leader7='a' or $leader7='c' or $leader7='d' or $leader7='m'">BK</xsl:when>
1706                                                 <xsl:when test="$leader7='b' or $leader7='i' or $leader7='s'">SE</xsl:when>
1707                                         </xsl:choose>
1708                                 </xsl:when>
1709                                 <xsl:when test="$leader6='t'">BK</xsl:when>
1710                                 <xsl:when test="$leader6='p'">MM</xsl:when>
1711                                 <xsl:when test="$leader6='m'">CF</xsl:when>
1712                                 <xsl:when test="$leader6='e' or $leader6='f'">MP</xsl:when>
1713                                 <xsl:when test="$leader6='g' or $leader6='k' or $leader6='o' or $leader6='r'">VM</xsl:when>
1714                                 <xsl:when test="$leader6='c' or $leader6='d' or $leader6='i' or $leader6='j'">MU</xsl:when>
1715                         </xsl:choose>
1716                 </xsl:variable>
1717                 <xsl:for-each select="marc:datafield[@tag='245']">
1718                         <titleInfo>
1719                                 <xsl:variable name="title">
1720                                         <xsl:choose>
1721                                                 <xsl:when test="marc:subfield[@code='b']">
1722                                                         <xsl:call-template name="specialSubfieldSelect">
1723                                                                 <xsl:with-param name="axis">b</xsl:with-param>
1724                                                                 <xsl:with-param name="beforeCodes">afgk</xsl:with-param>
1725                                                         </xsl:call-template>
1726                                                 </xsl:when>
1727                                                 <xsl:otherwise>
1728                                                         <xsl:call-template name="subfieldSelect">
1729                                                                 <xsl:with-param name="codes">abfgk</xsl:with-param>
1730                                                         </xsl:call-template>
1731                                                 </xsl:otherwise>
1732                                         </xsl:choose>
1733                                 </xsl:variable>
1734                                 <xsl:variable name="titleChop">
1735                                         <xsl:call-template name="chopPunctuation">
1736                                                 <xsl:with-param name="chopString">
1737                                                         <xsl:value-of select="$title"/>
1738                                                 </xsl:with-param>
1739                                                 <xsl:with-param name="punctuation">
1740                                                     <xsl:text>,;/ </xsl:text>
1741                                                 </xsl:with-param>
1742                                         </xsl:call-template>
1743                                 </xsl:variable>
1744                                 <xsl:choose>
1745                                         <xsl:when test="@ind2>0">
1746                                                 <nonSort>
1747                                                         <xsl:value-of select="substring($titleChop,1,@ind2)"/>
1748                                                 </nonSort>
1749                                                 <title>
1750                                                         <xsl:value-of select="substring($titleChop,@ind2+1)"/>
1751                                                 </title>
1752                                         </xsl:when>
1753                                         <xsl:otherwise>
1754                                                 <title>
1755                                                         <xsl:value-of select="$titleChop"/>
1756                                                 </title>
1757                                         </xsl:otherwise>
1758                                 </xsl:choose>
1759                                 <xsl:if test="marc:subfield[@code='b']">
1760                                         <subTitle>
1761                                                 <xsl:call-template name="chopPunctuation">
1762                                                         <xsl:with-param name="chopString">
1763                                                                 <xsl:call-template name="specialSubfieldSelect">
1764                                                                         <xsl:with-param name="axis">b</xsl:with-param>
1765                                                                         <xsl:with-param name="anyCodes">b</xsl:with-param>
1766                                                                         <xsl:with-param name="afterCodes">afgk</xsl:with-param>
1767                                                                 </xsl:call-template>
1768                                                         </xsl:with-param>
1769                                                 </xsl:call-template>
1770                                         </subTitle>
1771                                 </xsl:if>
1772                                 <xsl:call-template name="part"></xsl:call-template>
1773                         </titleInfo>
1774                         <!-- A form of title that ignores non-filing characters; useful
1775                                  for not converting "L'Oreal" into "L' Oreal" at index time -->
1776                         <titleNonfiling>
1777                                 <title>
1778                                         <xsl:call-template name="chopPunctuation">
1779                                                 <xsl:with-param name="chopString">
1780                                                         <xsl:call-template name="subfieldSelect">
1781                                                                 <xsl:with-param name="codes">abfgk</xsl:with-param>
1782                                                         </xsl:call-template>
1783                                                 </xsl:with-param>
1784                                         </xsl:call-template>
1785                                 </title>
1786                                 <xsl:call-template name="part"></xsl:call-template>
1787                         </titleNonfiling>
1788                         <!-- hybrid of titleInfo and titleNonfiling which will give us a preformatted string (for punctuation)
1789                                  but also keep the nonSort stuff in a separate field (for sorting) -->
1790                         <titleBrowse>
1791                                 <xsl:variable name="titleBrowseChop">
1792                                         <xsl:call-template name="chopPunctuation">
1793                                                 <xsl:with-param name="chopString">
1794                                                         <xsl:call-template name="subfieldSelect">
1795                                                                 <xsl:with-param name="codes">abfgk</xsl:with-param>
1796                                                         </xsl:call-template>
1797                                                 </xsl:with-param>
1798                                         </xsl:call-template>
1799                                 </xsl:variable>
1800                                 <xsl:choose>
1801                                         <xsl:when test="@ind2>0">
1802                                                 <nonSort>
1803                                                         <xsl:value-of select="substring($titleBrowseChop,1,@ind2)"/>
1804                                                 </nonSort>
1805                                                 <title>
1806                                                         <xsl:value-of select="substring($titleBrowseChop,@ind2+1)"/>
1807                                                 </title>
1808                                         </xsl:when>
1809                                         <xsl:otherwise>
1810                                                 <title>
1811                                                         <xsl:value-of select="$titleBrowseChop"/>
1812                                                 </title>
1813                                         </xsl:otherwise>
1814                                 </xsl:choose>
1815                                 <xsl:call-template name="part"></xsl:call-template>
1816                         </titleBrowse>
1817                 </xsl:for-each>
1818                 <xsl:for-each select="marc:datafield[@tag='210']">
1819                         <titleInfo type="abbreviated">
1820                                 <title>
1821                                         <xsl:call-template name="chopPunctuation">
1822                                                 <xsl:with-param name="chopString">
1823                                                         <xsl:call-template name="subfieldSelect">
1824                                                                 <xsl:with-param name="codes">a</xsl:with-param>
1825                                                         </xsl:call-template>
1826                                                 </xsl:with-param>
1827                                         </xsl:call-template>
1828                                 </title>
1829                                 <xsl:call-template name="subtitle"/>
1830                         </titleInfo>
1831                 </xsl:for-each>
1832                 <xsl:for-each select="marc:datafield[@tag='242']">
1833                         <xsl:variable name="titleChop">
1834                                 <xsl:call-template name="chopPunctuation">
1835                                         <xsl:with-param name="chopString">
1836                                                 <xsl:call-template name="subfieldSelect">
1837                                                         <!-- 1/04 removed $h, b -->
1838                                                         <xsl:with-param name="codes">a</xsl:with-param>
1839                                                 </xsl:call-template>
1840                                         </xsl:with-param>
1841                                 </xsl:call-template>
1842                         </xsl:variable>
1843                         <titleInfo type="translated">
1844                                 <!--09/01/04 Added subfield $y-->
1845                                 <xsl:for-each select="marc:subfield[@code='y']">
1846                                         <xsl:attribute name="lang">
1847                                                 <xsl:value-of select="text()"/>
1848                                         </xsl:attribute>
1849                                 </xsl:for-each>
1850                                 <title>
1851                                         <xsl:value-of select="$titleChop" />
1852                                 </title>
1853                                 <!-- 1/04 fix -->
1854                                 <xsl:call-template name="subtitle"/>
1855                                 <xsl:call-template name="part"/>
1856                         </titleInfo>
1857                         <titleInfo type="translated-nfi">
1858                                 <xsl:for-each select="marc:subfield[@code='y']">
1859                                         <xsl:attribute name="lang">
1860                                                 <xsl:value-of select="text()"/>
1861                                         </xsl:attribute>
1862                                 </xsl:for-each>
1863                                 <xsl:choose>
1864                                         <xsl:when test="@ind2>0">
1865                                                 <nonSort>
1866                                                         <xsl:value-of select="substring($titleChop,1,@ind2)"/>
1867                                                 </nonSort>
1868                                                 <title>
1869                                                         <xsl:value-of select="substring($titleChop,@ind2+1)"/>
1870                                                 </title>
1871                                         </xsl:when>
1872                                         <xsl:otherwise>
1873                                                 <title>
1874                                                         <xsl:value-of select="$titleChop" />
1875                                                 </title>
1876                                         </xsl:otherwise>
1877                                 </xsl:choose>
1878                                 <xsl:call-template name="subtitle"/>
1879                                 <xsl:call-template name="part"/>
1880                         </titleInfo>
1881                 </xsl:for-each>
1882                 <xsl:for-each select="marc:datafield[@tag='246']">
1883                         <titleInfo type="alternative">
1884                                 <xsl:for-each select="marc:subfield[@code='i']">
1885                                         <xsl:attribute name="displayLabel">
1886                                                 <xsl:value-of select="text()"/>
1887                                         </xsl:attribute>
1888                                 </xsl:for-each>
1889                                 <title>
1890                                         <xsl:call-template name="chopPunctuation">
1891                                                 <xsl:with-param name="chopString">
1892                                                         <xsl:call-template name="subfieldSelect">
1893                                                                 <!-- 1/04 removed $h, $b -->
1894                                                                 <xsl:with-param name="codes">af</xsl:with-param>
1895                                                         </xsl:call-template>
1896                                                 </xsl:with-param>
1897                                         </xsl:call-template>
1898                                 </title>
1899                                 <xsl:call-template name="subtitle"/>
1900                                 <xsl:call-template name="part"/>
1901                         </titleInfo>
1902                 </xsl:for-each>
1903                 <xsl:for-each select="marc:datafield[@tag='130']|marc:datafield[@tag='240']|marc:datafield[@tag='730'][@ind2!='2']">
1904                         <xsl:variable name="nfi">
1905                                 <xsl:choose>
1906                                         <xsl:when test="@tag='240'">
1907                                                 <xsl:value-of select="@ind2"/>
1908                                         </xsl:when>
1909                                         <xsl:otherwise>
1910                                                 <xsl:value-of select="@ind1"/>
1911                                         </xsl:otherwise>
1912                                 </xsl:choose>
1913                         </xsl:variable>
1914                         <xsl:variable name="titleChop">
1915                                 <xsl:call-template name="uri" />
1916                                 <xsl:variable name="str">
1917                                         <xsl:for-each select="marc:subfield">
1918                                                 <xsl:if test="(contains('adfklmor',@code) and (not(../marc:subfield[@code='n' or @code='p']) or (following-sibling::marc:subfield[@code='n' or @code='p'])))">
1919                                                         <xsl:value-of select="text()"/>
1920                                                         <xsl:text> </xsl:text>
1921                                                 </xsl:if>
1922                                         </xsl:for-each>
1923                                 </xsl:variable>
1924                                 <xsl:call-template name="chopPunctuation">
1925                                         <xsl:with-param name="chopString">
1926                                                 <xsl:value-of select="substring($str,1,string-length($str)-1)"/>
1927                                         </xsl:with-param>
1928                                 </xsl:call-template>
1929                         </xsl:variable>
1930                         <titleInfo type="uniform">
1931                                 <title>
1932                                         <xsl:value-of select="$titleChop"/>
1933                                 </title>
1934                                 <xsl:call-template name="part"/>
1935                         </titleInfo>
1936                         <titleInfo type="uniform-nfi">
1937                                 <xsl:choose>
1938                                         <xsl:when test="$nfi>0">
1939                                                 <nonSort>
1940                                                         <xsl:value-of select="substring($titleChop,1,$nfi)"/>
1941                                                 </nonSort>
1942                                                 <title>
1943                                                         <xsl:value-of select="substring($titleChop,$nfi+1)"/>
1944                                                 </title>
1945                                         </xsl:when>
1946                                         <xsl:otherwise>
1947                                                 <title>
1948                                                         <xsl:value-of select="$titleChop"/>
1949                                                 </title>
1950                                         </xsl:otherwise>
1951                                 </xsl:choose>
1952                                 <xsl:call-template name="part"/>
1953                         </titleInfo>
1954                 </xsl:for-each>
1955                 <xsl:for-each select="marc:datafield[@tag='740'][@ind2!='2']">
1956                         <xsl:variable name="titleChop">
1957                                 <xsl:call-template name="chopPunctuation">
1958                                         <xsl:with-param name="chopString">
1959                                                 <xsl:call-template name="subfieldSelect">
1960                                                         <xsl:with-param name="codes">ah</xsl:with-param>
1961                                                 </xsl:call-template>
1962                                         </xsl:with-param>
1963                                 </xsl:call-template>
1964                         </xsl:variable>
1965                         <titleInfo type="alternative">
1966                                 <title>
1967                                         <xsl:value-of select="$titleChop" />
1968                                 </title>
1969                                 <xsl:call-template name="part"/>
1970                         </titleInfo>
1971                         <titleInfo type="alternative-nfi">
1972                                 <xsl:choose>
1973                                         <xsl:when test="@ind1>0">
1974                                                 <nonSort>
1975                                                         <xsl:value-of select="substring($titleChop,1,@ind1)"/>
1976                                                 </nonSort>
1977                                                 <title>
1978                                                         <xsl:value-of select="substring($titleChop,@ind1+1)"/>
1979                                                 </title>
1980                                         </xsl:when>
1981                                         <xsl:otherwise>
1982                                                 <title>
1983                                                         <xsl:value-of select="$titleChop" />
1984                                                 </title>
1985                                         </xsl:otherwise>
1986                                 </xsl:choose>
1987                                 <xsl:call-template name="part"/>
1988                         </titleInfo>
1989                 </xsl:for-each>
1990                 <xsl:for-each select="marc:datafield[@tag='100']">
1991                         <name type="personal">
1992                                 <xsl:call-template name="uri" />
1993                                 <xsl:call-template name="nameABCDQ"/>
1994                                 <xsl:call-template name="affiliation"/>
1995                                 <role>
1996                                         <roleTerm authority="marcrelator" type="text">creator</roleTerm>
1997                                 </role>
1998                                 <xsl:call-template name="role"/>
1999                         </name>
2000                 </xsl:for-each>
2001                 <xsl:for-each select="marc:datafield[@tag='110']">
2002                         <name type="corporate">
2003                                 <xsl:call-template name="uri" />
2004                                 <xsl:call-template name="nameABCDN"/>
2005                                 <role>
2006                                         <roleTerm authority="marcrelator" type="text">creator</roleTerm>
2007                                 </role>
2008                                 <xsl:call-template name="role"/>
2009                         </name>
2010                 </xsl:for-each>
2011                 <xsl:for-each select="marc:datafield[@tag='111']">
2012                         <name type="conference">
2013                                 <xsl:call-template name="uri" />
2014                                 <xsl:call-template name="nameACDEQ"/>
2015                                 <role>
2016                                         <roleTerm authority="marcrelator" type="text">creator</roleTerm>
2017                                 </role>
2018                                 <xsl:call-template name="role"/>
2019                         </name>
2020                 </xsl:for-each>
2021                 <xsl:for-each select="marc:datafield[@tag='700'][not(marc:subfield[@code='t'])]">
2022                         <name type="personal">
2023                                 <xsl:call-template name="uri" />
2024                                 <xsl:call-template name="nameABCDQ"/>
2025                                 <xsl:call-template name="affiliation"/>
2026                                 <xsl:call-template name="role"/>
2027                         </name>
2028                 </xsl:for-each>
2029                 <xsl:for-each select="marc:datafield[@tag='710'][not(marc:subfield[@code='t'])]">
2030                         <name type="corporate">
2031                                 <xsl:call-template name="uri" />
2032                                 <xsl:call-template name="nameABCDN"/>
2033                                 <xsl:call-template name="role"/>
2034                         </name>
2035                 </xsl:for-each>
2036                 <xsl:for-each select="marc:datafield[@tag='711'][not(marc:subfield[@code='t'])]">
2037                         <name type="conference">
2038                                 <xsl:call-template name="uri" />
2039                                 <xsl:call-template name="nameACDEQ"/>
2040                                 <xsl:call-template name="role"/>
2041                         </name>
2042                 </xsl:for-each>
2043                 <xsl:for-each select="marc:datafield[@tag='720'][not(marc:subfield[@code='t'])]">
2044                         <name>
2045                                 <xsl:if test="@ind1=1">
2046                                         <xsl:attribute name="type">
2047                                                 <xsl:text>personal</xsl:text>
2048                                         </xsl:attribute>
2049                                 </xsl:if>
2050                                 <namePart>
2051                                         <xsl:value-of select="marc:subfield[@code='a']"/>
2052                                 </namePart>
2053                                 <xsl:call-template name="role"/>
2054                         </name>
2055                 </xsl:for-each>
2056                 <typeOfResource>
2057                         <xsl:if test="$leader7='c'">
2058                                 <xsl:attribute name="collection">yes</xsl:attribute>
2059                         </xsl:if>
2060                         <xsl:if test="$leader6='d' or $leader6='f' or $leader6='p' or $leader6='t'">
2061                                 <xsl:attribute name="manuscript">yes</xsl:attribute>
2062                         </xsl:if>
2063                         <xsl:choose>
2064                                 <xsl:when test="$leader6='a' or $leader6='t'">text</xsl:when>
2065                                 <xsl:when test="$leader6='e' or $leader6='f'">cartographic</xsl:when>
2066                                 <xsl:when test="$leader6='c' or $leader6='d'">notated music</xsl:when>
2067                                 <xsl:when test="$leader6='i'">sound recording-nonmusical</xsl:when>
2068                                 <xsl:when test="$leader6='j'">sound recording-musical</xsl:when>
2069                                 <xsl:when test="$leader6='k'">still image</xsl:when>
2070                                 <xsl:when test="$leader6='g'">moving image</xsl:when>
2071                                 <xsl:when test="$leader6='r'">three dimensional object</xsl:when>
2072                                 <xsl:when test="$leader6='m'">software, multimedia</xsl:when>
2073                                 <xsl:when test="$leader6='p'">mixed material</xsl:when>
2074                         </xsl:choose>
2075                 </typeOfResource>
2076                 <xsl:if test="substring($controlField008,26,1)='d'">
2077                         <genre authority="marc">globe</genre>
2078                 </xsl:if>
2079                 <xsl:if test="marc:controlfield[@tag='007'][substring(text(),1,1)='a'][substring(text(),2,1)='r']">
2080                         <genre authority="marc">remote sensing image</genre>
2081                 </xsl:if>
2082                 <xsl:if test="$typeOf008='MP'">
2083                         <xsl:variable name="controlField008-25" select="substring($controlField008,26,1)"></xsl:variable>
2084                         <xsl:choose>
2085                                 <xsl:when test="$controlField008-25='a' or $controlField008-25='b' or $controlField008-25='c' or marc:controlfield[@tag=007][substring(text(),1,1)='a'][substring(text(),2,1)='j']">
2086                                         <genre authority="marc">map</genre>
2087                                 </xsl:when>
2088                                 <xsl:when test="$controlField008-25='e' or marc:controlfield[@tag=007][substring(text(),1,1)='a'][substring(text(),2,1)='d']">
2089                                         <genre authority="marc">atlas</genre>
2090                                 </xsl:when>
2091                         </xsl:choose>
2092                 </xsl:if>
2093                 <xsl:if test="$typeOf008='SE'">
2094                         <xsl:variable name="controlField008-21" select="substring($controlField008,22,1)"></xsl:variable>
2095                         <xsl:choose>
2096                                 <xsl:when test="$controlField008-21='d'">
2097                                         <genre authority="marc">database</genre>
2098                                 </xsl:when>
2099                                 <xsl:when test="$controlField008-21='l'">
2100                                         <genre authority="marc">loose-leaf</genre>
2101                                 </xsl:when>
2102                                 <xsl:when test="$controlField008-21='m'">
2103                                         <genre authority="marc">series</genre>
2104                                 </xsl:when>
2105                                 <xsl:when test="$controlField008-21='n'">
2106                                         <genre authority="marc">newspaper</genre>
2107                                 </xsl:when>
2108                                 <xsl:when test="$controlField008-21='p'">
2109                                         <genre authority="marc">periodical</genre>
2110                                 </xsl:when>
2111                                 <xsl:when test="$controlField008-21='w'">
2112                                         <genre authority="marc">web site</genre>
2113                                 </xsl:when>
2114                         </xsl:choose>
2115                 </xsl:if>
2116                 <xsl:if test="$typeOf008='BK' or $typeOf008='SE'">
2117                         <xsl:variable name="controlField008-24" select="substring($controlField008,25,4)"></xsl:variable>
2118                         <xsl:choose>
2119                                 <xsl:when test="contains($controlField008-24,'a')">
2120                                         <genre authority="marc">abstract or summary</genre>
2121                                 </xsl:when>
2122                                 <xsl:when test="contains($controlField008-24,'b')">
2123                                         <genre authority="marc">bibliography</genre>
2124                                 </xsl:when>
2125                                 <xsl:when test="contains($controlField008-24,'c')">
2126                                         <genre authority="marc">catalog</genre>
2127                                 </xsl:when>
2128                                 <xsl:when test="contains($controlField008-24,'d')">
2129                                         <genre authority="marc">dictionary</genre>
2130                                 </xsl:when>
2131                                 <xsl:when test="contains($controlField008-24,'e')">
2132                                         <genre authority="marc">encyclopedia</genre>
2133                                 </xsl:when>
2134                                 <xsl:when test="contains($controlField008-24,'f')">
2135                                         <genre authority="marc">handbook</genre>
2136                                 </xsl:when>
2137                                 <xsl:when test="contains($controlField008-24,'g')">
2138                                         <genre authority="marc">legal article</genre>
2139                                 </xsl:when>
2140                                 <xsl:when test="contains($controlField008-24,'i')">
2141                                         <genre authority="marc">index</genre>
2142                                 </xsl:when>
2143                                 <xsl:when test="contains($controlField008-24,'k')">
2144                                         <genre authority="marc">discography</genre>
2145                                 </xsl:when>
2146                                 <xsl:when test="contains($controlField008-24,'l')">
2147                                         <genre authority="marc">legislation</genre>
2148                                 </xsl:when>
2149                                 <xsl:when test="contains($controlField008-24,'m')">
2150                                         <genre authority="marc">theses</genre>
2151                                 </xsl:when>
2152                                 <xsl:when test="contains($controlField008-24,'n')">
2153                                         <genre authority="marc">survey of literature</genre>
2154                                 </xsl:when>
2155                                 <xsl:when test="contains($controlField008-24,'o')">
2156                                         <genre authority="marc">review</genre>
2157                                 </xsl:when>
2158                                 <xsl:when test="contains($controlField008-24,'p')">
2159                                         <genre authority="marc">programmed text</genre>
2160                                 </xsl:when>
2161                                 <xsl:when test="contains($controlField008-24,'q')">
2162                                         <genre authority="marc">filmography</genre>
2163                                 </xsl:when>
2164                                 <xsl:when test="contains($controlField008-24,'r')">
2165                                         <genre authority="marc">directory</genre>
2166                                 </xsl:when>
2167                                 <xsl:when test="contains($controlField008-24,'s')">
2168                                         <genre authority="marc">statistics</genre>
2169                                 </xsl:when>
2170                                 <xsl:when test="contains($controlField008-24,'t')">
2171                                         <genre authority="marc">technical report</genre>
2172                                 </xsl:when>
2173                                 <xsl:when test="contains($controlField008-24,'v')">
2174                                         <genre authority="marc">legal case and case notes</genre>
2175                                 </xsl:when>
2176                                 <xsl:when test="contains($controlField008-24,'w')">
2177                                         <genre authority="marc">law report or digest</genre>
2178                                 </xsl:when>
2179                                 <xsl:when test="contains($controlField008-24,'z')">
2180                                         <genre authority="marc">treaty</genre>
2181                                 </xsl:when>
2182                         </xsl:choose>
2183                         <xsl:variable name="controlField008-29" select="substring($controlField008,30,1)"></xsl:variable>
2184                         <xsl:choose>
2185                                 <xsl:when test="$controlField008-29='1'">
2186                                         <genre authority="marc">conference publication</genre>
2187                                 </xsl:when>
2188                         </xsl:choose>
2189                 </xsl:if>
2190                 <xsl:if test="$typeOf008='CF'">
2191                         <xsl:variable name="controlField008-26" select="substring($controlField008,27,1)"></xsl:variable>
2192                         <xsl:choose>
2193                                 <xsl:when test="$controlField008-26='a'">
2194                                         <genre authority="marc">numeric data</genre>
2195                                 </xsl:when>
2196                                 <xsl:when test="$controlField008-26='e'">
2197                                         <genre authority="marc">database</genre>
2198                                 </xsl:when>
2199                                 <xsl:when test="$controlField008-26='f'">
2200                                         <genre authority="marc">font</genre>
2201                                 </xsl:when>
2202                                 <xsl:when test="$controlField008-26='g'">
2203                                         <genre authority="marc">game</genre>
2204                                 </xsl:when>
2205                         </xsl:choose>
2206                 </xsl:if>
2207                 <xsl:if test="$typeOf008='BK'">
2208                         <xsl:if test="substring($controlField008,25,1)='j'">
2209                                 <genre authority="marc">patent</genre>
2210                         </xsl:if>
2211                         <xsl:if test="substring($controlField008,31,1)='1'">
2212                                 <genre authority="marc">festschrift</genre>
2213                         </xsl:if>
2214                         <xsl:variable name="controlField008-34" select="substring($controlField008,35,1)"></xsl:variable>
2215                         <xsl:if test="$controlField008-34='a' or $controlField008-34='b' or $controlField008-34='c' or $controlField008-34='d'">
2216                                 <genre authority="marc">biography</genre>
2217                         </xsl:if>
2218                         <xsl:variable name="controlField008-33" select="substring($controlField008,34,1)"></xsl:variable>
2219                         <xsl:choose>
2220                                 <xsl:when test="$controlField008-33='e'">
2221                                         <genre authority="marc">essay</genre>
2222                                 </xsl:when>
2223                                 <xsl:when test="$controlField008-33='d'">
2224                                         <genre authority="marc">drama</genre>
2225                                 </xsl:when>
2226                                 <xsl:when test="$controlField008-33='c'">
2227                                         <genre authority="marc">comic strip</genre>
2228                                 </xsl:when>
2229                                 <xsl:when test="$controlField008-33='l'">
2230                                         <genre authority="marc">fiction</genre>
2231                                 </xsl:when>
2232                                 <xsl:when test="$controlField008-33='h'">
2233                                         <genre authority="marc">humor, satire</genre>
2234                                 </xsl:when>
2235                                 <xsl:when test="$controlField008-33='i'">
2236                                         <genre authority="marc">letter</genre>
2237                                 </xsl:when>
2238                                 <xsl:when test="$controlField008-33='f'">
2239                                         <genre authority="marc">novel</genre>
2240                                 </xsl:when>
2241                                 <xsl:when test="$controlField008-33='j'">
2242                                         <genre authority="marc">short story</genre>
2243                                 </xsl:when>
2244                                 <xsl:when test="$controlField008-33='s'">
2245                                         <genre authority="marc">speech</genre>
2246                                 </xsl:when>
2247                         </xsl:choose>
2248                 </xsl:if>
2249                 <xsl:if test="$typeOf008='MU'">
2250                         <xsl:variable name="controlField008-30-31" select="substring($controlField008,31,2)"></xsl:variable>
2251                         <xsl:if test="contains($controlField008-30-31,'b')">
2252                                 <genre authority="marc">biography</genre>
2253                         </xsl:if>
2254                         <xsl:if test="contains($controlField008-30-31,'c')">
2255                                 <genre authority="marc">conference publication</genre>
2256                         </xsl:if>
2257                         <xsl:if test="contains($controlField008-30-31,'d')">
2258                                 <genre authority="marc">drama</genre>
2259                         </xsl:if>
2260                         <xsl:if test="contains($controlField008-30-31,'e')">
2261                                 <genre authority="marc">essay</genre>
2262                         </xsl:if>
2263                         <xsl:if test="contains($controlField008-30-31,'f')">
2264                                 <genre authority="marc">fiction</genre>
2265                         </xsl:if>
2266                         <xsl:if test="contains($controlField008-30-31,'o')">
2267                                 <genre authority="marc">folktale</genre>
2268                         </xsl:if>
2269                         <xsl:if test="contains($controlField008-30-31,'h')">
2270                                 <genre authority="marc">history</genre>
2271                         </xsl:if>
2272                         <xsl:if test="contains($controlField008-30-31,'k')">
2273                                 <genre authority="marc">humor, satire</genre>
2274                         </xsl:if>
2275                         <xsl:if test="contains($controlField008-30-31,'m')">
2276                                 <genre authority="marc">memoir</genre>
2277                         </xsl:if>
2278                         <xsl:if test="contains($controlField008-30-31,'p')">
2279                                 <genre authority="marc">poetry</genre>
2280                         </xsl:if>
2281                         <xsl:if test="contains($controlField008-30-31,'r')">
2282                                 <genre authority="marc">rehearsal</genre>
2283                         </xsl:if>
2284                         <xsl:if test="contains($controlField008-30-31,'g')">
2285                                 <genre authority="marc">reporting</genre>
2286                         </xsl:if>
2287                         <xsl:if test="contains($controlField008-30-31,'s')">
2288                                 <genre authority="marc">sound</genre>
2289                         </xsl:if>
2290                         <xsl:if test="contains($controlField008-30-31,'l')">
2291                                 <genre authority="marc">speech</genre>
2292                         </xsl:if>
2293                 </xsl:if>
2294                 <xsl:if test="$typeOf008='VM'">
2295                         <xsl:variable name="controlField008-33" select="substring($controlField008,34,1)"></xsl:variable>
2296                         <xsl:choose>
2297                                 <xsl:when test="$controlField008-33='a'">
2298                                         <genre authority="marc">art original</genre>
2299                                 </xsl:when>
2300                                 <xsl:when test="$controlField008-33='b'">
2301                                         <genre authority="marc">kit</genre>
2302                                 </xsl:when>
2303                                 <xsl:when test="$controlField008-33='c'">
2304                                         <genre authority="marc">art reproduction</genre>
2305                                 </xsl:when>
2306                                 <xsl:when test="$controlField008-33='d'">
2307                                         <genre authority="marc">diorama</genre>
2308                                 </xsl:when>
2309                                 <xsl:when test="$controlField008-33='f'">
2310                                         <genre authority="marc">filmstrip</genre>
2311                                 </xsl:when>
2312                                 <xsl:when test="$controlField008-33='g'">
2313                                         <genre authority="marc">legal article</genre>
2314                                 </xsl:when>
2315                                 <xsl:when test="$controlField008-33='i'">
2316                                         <genre authority="marc">picture</genre>
2317                                 </xsl:when>
2318                                 <xsl:when test="$controlField008-33='k'">
2319                                         <genre authority="marc">graphic</genre>
2320                                 </xsl:when>
2321                                 <xsl:when test="$controlField008-33='l'">
2322                                         <genre authority="marc">technical drawing</genre>
2323                                 </xsl:when>
2324                                 <xsl:when test="$controlField008-33='m'">
2325                                         <genre authority="marc">motion picture</genre>
2326                                 </xsl:when>
2327                                 <xsl:when test="$controlField008-33='n'">
2328                                         <genre authority="marc">chart</genre>
2329                                 </xsl:when>
2330                                 <xsl:when test="$controlField008-33='o'">
2331                                         <genre authority="marc">flash card</genre>
2332                                 </xsl:when>
2333                                 <xsl:when test="$controlField008-33='p'">
2334                                         <genre authority="marc">microscope slide</genre>
2335                                 </xsl:when>
2336                                 <xsl:when test="$controlField008-33='q' or marc:controlfield[@tag=007][substring(text(),1,1)='a'][substring(text(),2,1)='q']">
2337                                         <genre authority="marc">model</genre>
2338                                 </xsl:when>
2339                                 <xsl:when test="$controlField008-33='r'">
2340                                         <genre authority="marc">realia</genre>
2341                                 </xsl:when>
2342                                 <xsl:when test="$controlField008-33='s'">
2343                                         <genre authority="marc">slide</genre>
2344                                 </xsl:when>
2345                                 <xsl:when test="$controlField008-33='t'">
2346                                         <genre authority="marc">transparency</genre>
2347                                 </xsl:when>
2348                                 <xsl:when test="$controlField008-33='v'">
2349                                         <genre authority="marc">videorecording</genre>
2350                                 </xsl:when>
2351                                 <xsl:when test="$controlField008-33='w'">
2352                                         <genre authority="marc">toy</genre>
2353                                 </xsl:when>
2354                         </xsl:choose>
2355                 </xsl:if>
2356                 <xsl:for-each select="marc:datafield[@tag=655]">
2357                         <genre authority="marc">
2358                                 <xsl:attribute name="authority">
2359                                         <xsl:value-of select="marc:subfield[@code='2']"/>
2360                                 </xsl:attribute>
2361                                 <xsl:call-template name="subfieldSelect">
2362                                         <xsl:with-param name="codes">abvxyz</xsl:with-param>
2363                                         <xsl:with-param name="delimeter">-</xsl:with-param>
2364                                 </xsl:call-template>
2365                         </genre>
2366                 </xsl:for-each>
2367                 <originInfo>
2368                         <xsl:variable name="MARCpublicationCode" select="normalize-space(substring($controlField008,16,3))"></xsl:variable>
2369                         <xsl:if test="translate($MARCpublicationCode,'|','')">
2370                                 <place>
2371                                         <placeTerm>
2372                                                 <xsl:attribute name="type">code</xsl:attribute>
2373                                                 <xsl:attribute name="authority">marccountry</xsl:attribute>
2374                                                 <xsl:value-of select="$MARCpublicationCode"/>
2375                                         </placeTerm>
2376                                 </place>
2377                         </xsl:if>
2378                         <xsl:for-each select="marc:datafield[@tag=044]/marc:subfield[@code='c']">
2379                                 <place>
2380                                         <placeTerm>
2381                                                 <xsl:attribute name="type">code</xsl:attribute>
2382                                                 <xsl:attribute name="authority">iso3166</xsl:attribute>
2383                                                 <xsl:value-of select="."/>
2384                                         </placeTerm>
2385                                 </place>
2386                         </xsl:for-each>
2387                         <xsl:for-each select="marc:datafield[@tag=260]/marc:subfield[@code='a']">
2388                                 <place>
2389                                         <placeTerm>
2390                                                 <xsl:attribute name="type">text</xsl:attribute>
2391                                                 <xsl:call-template name="chopPunctuationFront">
2392                                                         <xsl:with-param name="chopString">
2393                                                                 <xsl:call-template name="chopPunctuation">
2394                                                                         <xsl:with-param name="chopString" select="."/>
2395                                                                 </xsl:call-template>
2396                                                         </xsl:with-param>
2397                                                 </xsl:call-template>
2398                                         </placeTerm>
2399                                 </place>
2400                         </xsl:for-each>
2401                         <xsl:for-each select="marc:datafield[@tag=046]/marc:subfield[@code='m']">
2402                                 <dateValid point="start">
2403                                         <xsl:value-of select="."/>
2404                                 </dateValid>
2405                         </xsl:for-each>
2406                         <xsl:for-each select="marc:datafield[@tag=046]/marc:subfield[@code='n']">
2407                                 <dateValid point="end">
2408                                         <xsl:value-of select="."/>
2409                                 </dateValid>
2410                         </xsl:for-each>
2411                         <xsl:for-each select="marc:datafield[@tag=046]/marc:subfield[@code='j']">
2412                                 <dateModified>
2413                                         <xsl:value-of select="."/>
2414                                 </dateModified>
2415                         </xsl:for-each>
2416                         <xsl:for-each select="marc:datafield[@tag=260]/marc:subfield[@code='b' or @code='c' or @code='g']">
2417                                 <xsl:choose>
2418                                         <xsl:when test="@code='b'">
2419                                                 <publisher>
2420                                                         <xsl:call-template name="chopPunctuation">
2421                                                                 <xsl:with-param name="chopString" select="."/>
2422                                                                 <xsl:with-param name="punctuation">
2423                                                                         <xsl:text>:,;/ </xsl:text>
2424                                                                 </xsl:with-param>
2425                                                         </xsl:call-template>
2426                                                 </publisher>
2427                                         </xsl:when>
2428                                         <xsl:when test="@code='c'">
2429                                                 <dateIssued>
2430                                                         <xsl:call-template name="chopPunctuation">
2431                                                                 <xsl:with-param name="chopString" select="."/>
2432                                                         </xsl:call-template>
2433                                                 </dateIssued>
2434                                         </xsl:when>
2435                                         <xsl:when test="@code='g'">
2436                                                 <dateCreated>
2437                                                         <xsl:value-of select="."/>
2438                                                 </dateCreated>
2439                                         </xsl:when>
2440                                 </xsl:choose>
2441                         </xsl:for-each>
2442                         <xsl:variable name="dataField260c">
2443                                 <xsl:call-template name="chopPunctuation">
2444                                         <xsl:with-param name="chopString" select="marc:datafield[@tag=260]/marc:subfield[@code='c']"></xsl:with-param>
2445                                 </xsl:call-template>
2446                         </xsl:variable>
2447                         <xsl:variable name="controlField008-7-10" select="normalize-space(substring($controlField008, 8, 4))"></xsl:variable>
2448                         <xsl:variable name="controlField008-11-14" select="normalize-space(substring($controlField008, 12, 4))"></xsl:variable>
2449                         <xsl:variable name="controlField008-6" select="normalize-space(substring($controlField008, 7, 1))"></xsl:variable>
2450                         <xsl:if test="$controlField008-6='e' or $controlField008-6='p' or $controlField008-6='r' or $controlField008-6='t' or $controlField008-6='s'">
2451                                 <xsl:if test="$controlField008-7-10 and ($controlField008-7-10 != $dataField260c)">
2452                                         <dateIssued encoding="marc">
2453                                                 <xsl:value-of select="$controlField008-7-10"/>
2454                                         </dateIssued>
2455                                 </xsl:if>
2456                         </xsl:if>
2457                         <xsl:if test="$controlField008-6='c' or $controlField008-6='d' or $controlField008-6='i' or $controlField008-6='k' or $controlField008-6='m' or $controlField008-6='q' or $controlField008-6='u'">
2458                                 <xsl:if test="$controlField008-7-10">
2459                                         <dateIssued encoding="marc" point="start">
2460                                                 <xsl:value-of select="$controlField008-7-10"/>
2461                                         </dateIssued>
2462                                 </xsl:if>
2463                         </xsl:if>
2464                         <xsl:if test="$controlField008-6='c' or $controlField008-6='d' or $controlField008-6='i' or $controlField008-6='k' or $controlField008-6='m' or $controlField008-6='q' or $controlField008-6='u'">
2465                                 <xsl:if test="$controlField008-11-14">
2466                                         <dateIssued encoding="marc" point="end">
2467                                                 <xsl:value-of select="$controlField008-11-14"/>
2468                                         </dateIssued>
2469                                 </xsl:if>
2470                         </xsl:if>
2471                         <xsl:if test="$controlField008-6='q'">
2472                                 <xsl:if test="$controlField008-7-10">
2473                                         <dateIssued encoding="marc" point="start" qualifier="questionable">
2474                                                 <xsl:value-of select="$controlField008-7-10"/>
2475                                         </dateIssued>
2476                                 </xsl:if>
2477                         </xsl:if>
2478                         <xsl:if test="$controlField008-6='q'">
2479                                 <xsl:if test="$controlField008-11-14">
2480                                         <dateIssued encoding="marc" point="end" qualifier="questionable">
2481                                                 <xsl:value-of select="$controlField008-11-14"/>
2482                                         </dateIssued>
2483                                 </xsl:if>
2484                         </xsl:if>
2485                         <xsl:if test="$controlField008-6='t'">
2486                                 <xsl:if test="$controlField008-11-14">
2487                                         <copyrightDate encoding="marc">
2488                                                 <xsl:value-of select="$controlField008-11-14"/>
2489                                         </copyrightDate>
2490                                 </xsl:if>
2491                         </xsl:if>
2492                         <xsl:for-each select="marc:datafield[@tag=033][@ind1=0 or @ind1=1]/marc:subfield[@code='a']">
2493                                 <dateCaptured encoding="iso8601">
2494                                         <xsl:value-of select="."/>
2495                                 </dateCaptured>
2496                         </xsl:for-each>
2497                         <xsl:for-each select="marc:datafield[@tag=033][@ind1=2]/marc:subfield[@code='a'][1]">
2498                                 <dateCaptured encoding="iso8601" point="start">
2499                                         <xsl:value-of select="."/>
2500                                 </dateCaptured>
2501                         </xsl:for-each>
2502                         <xsl:for-each select="marc:datafield[@tag=033][@ind1=2]/marc:subfield[@code='a'][2]">
2503                                 <dateCaptured encoding="iso8601" point="end">
2504                                         <xsl:value-of select="."/>
2505                                 </dateCaptured>
2506                         </xsl:for-each>
2507                         <xsl:for-each select="marc:datafield[@tag=250]/marc:subfield[@code='a']">
2508                                 <edition>
2509                                         <xsl:value-of select="."/>
2510                                 </edition>
2511                         </xsl:for-each>
2512                         <xsl:for-each select="marc:leader">
2513                                 <issuance>
2514                                         <xsl:choose>
2515                                                 <xsl:when test="$leader7='a' or $leader7='c' or $leader7='d' or $leader7='m'">monographic</xsl:when>
2516                                                 <xsl:when test="$leader7='b' or $leader7='i' or $leader7='s'">continuing</xsl:when>
2517                                         </xsl:choose>
2518                                 </issuance>
2519                         </xsl:for-each>
2520                         <xsl:for-each select="marc:datafield[@tag=310]|marc:datafield[@tag=321]">
2521                                 <frequency>
2522                                         <xsl:call-template name="subfieldSelect">
2523                                                 <xsl:with-param name="codes">ab</xsl:with-param>
2524                                         </xsl:call-template>
2525                                 </frequency>
2526                         </xsl:for-each>
2527                 </originInfo>
2528                 <xsl:variable name="controlField008-35-37" select="normalize-space(translate(substring($controlField008,36,3),'|#',''))"></xsl:variable>
2529                 <xsl:if test="$controlField008-35-37">
2530                         <language>
2531                                 <languageTerm authority="iso639-2b" type="code">
2532                                         <xsl:value-of select="substring($controlField008,36,3)"/>
2533                                 </languageTerm>
2534                         </language>
2535                 </xsl:if>
2536                 <xsl:for-each select="marc:datafield[@tag=041]">
2537                         <xsl:for-each select="marc:subfield[@code='a' or @code='b' or @code='d' or @code='e' or @code='f' or @code='g' or @code='h']">
2538                                 <xsl:variable name="langCodes" select="."/>
2539                                 <xsl:choose>
2540                                         <xsl:when test="../marc:subfield[@code='2']='rfc3066'">
2541                                                 <!-- not stacked but could be repeated -->
2542                                                 <xsl:call-template name="rfcLanguages">
2543                                                         <xsl:with-param name="nodeNum">
2544                                                                 <xsl:value-of select="1"/>
2545                                                         </xsl:with-param>
2546                                                         <xsl:with-param name="usedLanguages">
2547                                                                 <xsl:text></xsl:text>
2548                                                         </xsl:with-param>
2549                                                         <xsl:with-param name="controlField008-35-37">
2550                                                                 <xsl:value-of select="$controlField008-35-37"></xsl:value-of>
2551                                                         </xsl:with-param>
2552                                                 </xsl:call-template>
2553                                         </xsl:when>
2554                                         <xsl:otherwise>
2555                                                 <!-- iso -->
2556                                                 <xsl:variable name="allLanguages">
2557                                                         <xsl:copy-of select="$langCodes"></xsl:copy-of>
2558                                                 </xsl:variable>
2559                                                 <xsl:variable name="currentLanguage">
2560                                                         <xsl:value-of select="substring($allLanguages,1,3)"></xsl:value-of>
2561                                                 </xsl:variable>
2562                                                 <xsl:call-template name="isoLanguage">
2563                                                         <xsl:with-param name="currentLanguage">
2564                                                                 <xsl:value-of select="substring($allLanguages,1,3)"></xsl:value-of>
2565                                                         </xsl:with-param>
2566                                                         <xsl:with-param name="remainingLanguages">
2567                                                                 <xsl:value-of select="substring($allLanguages,4,string-length($allLanguages)-3)"></xsl:value-of>
2568                                                         </xsl:with-param>
2569                                                         <xsl:with-param name="usedLanguages">
2570                                                                 <xsl:if test="$controlField008-35-37">
2571                                                                         <xsl:value-of select="$controlField008-35-37"></xsl:value-of>
2572                                                                 </xsl:if>
2573                                                         </xsl:with-param>
2574                                                 </xsl:call-template>
2575                                         </xsl:otherwise>
2576                                 </xsl:choose>
2577                         </xsl:for-each>
2578                 </xsl:for-each>
2579                 <xsl:variable name="physicalDescription">
2580                         <!--3.2 change tmee 007/11 -->
2581                         <xsl:if test="$typeOf008='CF' and marc:controlfield[@tag=007][substring(.,12,1)='a']">
2582                                 <digitalOrigin>reformatted digital</digitalOrigin>
2583                         </xsl:if>
2584                         <xsl:if test="$typeOf008='CF' and marc:controlfield[@tag=007][substring(.,12,1)='b']">
2585                                 <digitalOrigin>digitized microfilm</digitalOrigin>
2586                         </xsl:if>
2587                         <xsl:if test="$typeOf008='CF' and marc:controlfield[@tag=007][substring(.,12,1)='d']">
2588                                 <digitalOrigin>digitized other analog</digitalOrigin>
2589                         </xsl:if>
2590                         <xsl:variable name="controlField008-23" select="substring($controlField008,24,1)"></xsl:variable>
2591                         <xsl:variable name="controlField008-29" select="substring($controlField008,30,1)"></xsl:variable>
2592                         <xsl:variable name="check008-23">
2593                                 <xsl:if test="$typeOf008='BK' or $typeOf008='MU' or $typeOf008='SE' or $typeOf008='MM'">
2594                                         <xsl:value-of select="true()"></xsl:value-of>
2595                                 </xsl:if>
2596                         </xsl:variable>
2597                         <xsl:variable name="check008-29">
2598                                 <xsl:if test="$typeOf008='MP' or $typeOf008='VM'">
2599                                         <xsl:value-of select="true()"></xsl:value-of>
2600                                 </xsl:if>
2601                         </xsl:variable>
2602                         <xsl:choose>
2603                                 <xsl:when test="($check008-23 and $controlField008-23='f') or ($check008-29 and $controlField008-29='f')">
2604                                         <form authority="marcform">braille</form>
2605                                 </xsl:when>
2606                                 <xsl:when test="($controlField008-23=' ' and ($leader6='c' or $leader6='d')) or (($typeOf008='BK' or $typeOf008='SE') and ($controlField008-23=' ' or $controlField008='r'))">
2607                                         <form authority="marcform">print</form>
2608                                 </xsl:when>
2609                                 <xsl:when test="$leader6 = 'm' or ($check008-23 and $controlField008-23='s') or ($check008-29 and $controlField008-29='s')">
2610                                         <form authority="marcform">electronic</form>
2611                                 </xsl:when>
2612                                 <xsl:when test="($check008-23 and $controlField008-23='b') or ($check008-29 and $controlField008-29='b')">
2613                                         <form authority="marcform">microfiche</form>
2614                                 </xsl:when>
2615                                 <xsl:when test="($check008-23 and $controlField008-23='a') or ($check008-29 and $controlField008-29='a')">
2616                                         <form authority="marcform">microfilm</form>
2617                                 </xsl:when>
2618                         </xsl:choose>
2619                         <!-- 1/04 fix -->
2620                         <xsl:if test="marc:datafield[@tag=130]/marc:subfield[@code='h']">
2621                                 <form authority="gmd">
2622                                         <xsl:call-template name="chopBrackets">
2623                                                 <xsl:with-param name="chopString">
2624                                                         <xsl:value-of select="marc:datafield[@tag=130]/marc:subfield[@code='h']"></xsl:value-of>
2625                                                 </xsl:with-param>
2626                                         </xsl:call-template>
2627                                 </form>
2628                         </xsl:if>
2629                         <xsl:if test="marc:datafield[@tag=240]/marc:subfield[@code='h']">
2630                                 <form authority="gmd">
2631                                         <xsl:call-template name="chopBrackets">
2632                                                 <xsl:with-param name="chopString">
2633                                                         <xsl:value-of select="marc:datafield[@tag=240]/marc:subfield[@code='h']"></xsl:value-of>
2634                                                 </xsl:with-param>
2635                                         </xsl:call-template>
2636                                 </form>
2637                         </xsl:if>
2638                         <xsl:if test="marc:datafield[@tag=242]/marc:subfield[@code='h']">
2639                                 <form authority="gmd">
2640                                         <xsl:call-template name="chopBrackets">
2641                                                 <xsl:with-param name="chopString">
2642                                                         <xsl:value-of select="marc:datafield[@tag=242]/marc:subfield[@code='h']"></xsl:value-of>
2643                                                 </xsl:with-param>
2644                                         </xsl:call-template>
2645                                 </form>
2646                         </xsl:if>
2647                         <xsl:if test="marc:datafield[@tag=245]/marc:subfield[@code='h']">
2648                                 <form authority="gmd">
2649                                         <xsl:call-template name="chopBrackets">
2650                                                 <xsl:with-param name="chopString">
2651                                                         <xsl:value-of select="marc:datafield[@tag=245]/marc:subfield[@code='h']"></xsl:value-of>
2652                                                 </xsl:with-param>
2653                                         </xsl:call-template>
2654                                 </form>
2655                         </xsl:if>
2656                         <xsl:if test="marc:datafield[@tag=246]/marc:subfield[@code='h']">
2657                                 <form authority="gmd">
2658                                         <xsl:call-template name="chopBrackets">
2659                                                 <xsl:with-param name="chopString">
2660                                                         <xsl:value-of select="marc:datafield[@tag=246]/marc:subfield[@code='h']"></xsl:value-of>
2661                                                 </xsl:with-param>
2662                                         </xsl:call-template>
2663                                 </form>
2664                         </xsl:if>
2665                         <xsl:if test="marc:datafield[@tag=730]/marc:subfield[@code='h']">
2666                                 <form authority="gmd">
2667                                         <xsl:call-template name="chopBrackets">
2668                                                 <xsl:with-param name="chopString">
2669                                                         <xsl:value-of select="marc:datafield[@tag=730]/marc:subfield[@code='h']"></xsl:value-of>
2670                                                 </xsl:with-param>
2671                                         </xsl:call-template>
2672                                 </form>
2673                         </xsl:if>
2674                         <xsl:for-each select="marc:datafield[@tag=256]/marc:subfield[@code='a']">
2675                                 <form>
2676                                         <xsl:value-of select="."></xsl:value-of>
2677                                 </form>
2678                         </xsl:for-each>
2679                         <xsl:for-each select="marc:controlfield[@tag=007][substring(text(),1,1)='c']">
2680                                 <xsl:choose>
2681                                         <xsl:when test="substring(text(),14,1)='a'">
2682                                                 <reformattingQuality>access</reformattingQuality>
2683                                         </xsl:when>
2684                                         <xsl:when test="substring(text(),14,1)='p'">
2685                                                 <reformattingQuality>preservation</reformattingQuality>
2686                                         </xsl:when>
2687                                         <xsl:when test="substring(text(),14,1)='r'">
2688                                                 <reformattingQuality>replacement</reformattingQuality>
2689                                         </xsl:when>
2690                                 </xsl:choose>
2691                         </xsl:for-each>
2692                         <!--3.2 change tmee 007/01 -->
2693                         <xsl:if test="marc:controlfield[@tag=007][substring(text(),1,1)='c'][substring(text(),2,1)='b']">
2694                                 <form authority="smd">chip cartridge</form>
2695                         </xsl:if>
2696                         <xsl:if test="marc:controlfield[@tag=007][substring(text(),1,1)='c'][substring(text(),2,1)='c']">
2697                                 <form authority="smd">computer optical disc cartridge</form>
2698                         </xsl:if>
2699                         <xsl:if test="marc:controlfield[@tag=007][substring(text(),1,1)='c'][substring(text(),2,1)='j']">
2700                                 <form authority="smd">magnetic disc</form>
2701                         </xsl:if>
2702                         <xsl:if test="marc:controlfield[@tag=007][substring(text(),1,1)='c'][substring(text(),2,1)='m']">
2703                                 <form authority="smd">magneto-optical disc</form>
2704                         </xsl:if>
2705                         <xsl:if test="marc:controlfield[@tag=007][substring(text(),1,1)='c'][substring(text(),2,1)='o']">
2706                                 <form authority="smd">optical disc</form>
2707                         </xsl:if>
2708                         <xsl:if test="marc:controlfield[@tag=007][substring(text(),1,1)='c'][substring(text(),2,1)='r']">
2709                                 <form authority="smd">remote</form>
2710                         </xsl:if>
2711                         <xsl:if test="marc:controlfield[@tag=007][substring(text(),1,1)='c'][substring(text(),2,1)='a']">
2712                                 <form authority="smd">tape cartridge</form>
2713                         </xsl:if>
2714                         <xsl:if test="marc:controlfield[@tag=007][substring(text(),1,1)='c'][substring(text(),2,1)='f']">
2715                                 <form authority="smd">tape cassette</form>
2716                         </xsl:if>
2717                         <xsl:if test="marc:controlfield[@tag=007][substring(text(),1,1)='c'][substring(text(),2,1)='h']">
2718                                 <form authority="smd">tape reel</form>
2719                         </xsl:if>
2720
2721                         <xsl:if test="marc:controlfield[@tag=007][substring(text(),1,1)='d'][substring(text(),2,1)='a']">
2722                                 <form authority="smd">celestial globe</form>
2723                         </xsl:if>
2724                         <xsl:if test="marc:controlfield[@tag=007][substring(text(),1,1)='d'][substring(text(),2,1)='e']">
2725                                 <form authority="smd">earth moon globe</form>
2726                         </xsl:if>
2727                         <xsl:if test="marc:controlfield[@tag=007][substring(text(),1,1)='d'][substring(text(),2,1)='b']">
2728                                 <form authority="smd">planetary or lunar globe</form>
2729                         </xsl:if>
2730                         <xsl:if test="marc:controlfield[@tag=007][substring(text(),1,1)='d'][substring(text(),2,1)='c']">
2731                                 <form authority="smd">terrestrial globe</form>
2732                         </xsl:if>
2733
2734                         <xsl:if test="marc:controlfield[@tag=007][substring(text(),1,1)='o'][substring(text(),2,1)='o']">
2735                                 <form authority="smd">kit</form>
2736                         </xsl:if>
2737
2738                         <xsl:if test="marc:controlfield[@tag=007][substring(text(),1,1)='a'][substring(text(),2,1)='d']">
2739                                 <form authority="smd">atlas</form>
2740                         </xsl:if>
2741                         <xsl:if test="marc:controlfield[@tag=007][substring(text(),1,1)='a'][substring(text(),2,1)='g']">
2742                                 <form authority="smd">diagram</form>
2743                         </xsl:if>
2744                         <xsl:if test="marc:controlfield[@tag=007][substring(text(),1,1)='a'][substring(text(),2,1)='j']">
2745                                 <form authority="smd">map</form>
2746                         </xsl:if>
2747                         <xsl:if test="marc:controlfield[@tag=007][substring(text(),1,1)='a'][substring(text(),2,1)='q']">
2748                                 <form authority="smd">model</form>
2749                         </xsl:if>
2750                         <xsl:if test="marc:controlfield[@tag=007][substring(text(),1,1)='a'][substring(text(),2,1)='k']">
2751                                 <form authority="smd">profile</form>
2752                         </xsl:if>
2753                         <xsl:if test="marc:controlfield[@tag=007][substring(text(),1,1)='a'][substring(text(),2,1)='r']">
2754                                 <form authority="smd">remote-sensing image</form>
2755                         </xsl:if>
2756                         <xsl:if test="marc:controlfield[@tag=007][substring(text(),1,1)='a'][substring(text(),2,1)='s']">
2757                                 <form authority="smd">section</form>
2758                         </xsl:if>
2759                         <xsl:if test="marc:controlfield[@tag=007][substring(text(),1,1)='a'][substring(text(),2,1)='y']">
2760                                 <form authority="smd">view</form>
2761                         </xsl:if>
2762
2763                         <xsl:if test="marc:controlfield[@tag=007][substring(text(),1,1)='h'][substring(text(),2,1)='a']">
2764                                 <form authority="smd">aperture card</form>
2765                         </xsl:if>
2766                         <xsl:if test="marc:controlfield[@tag=007][substring(text(),1,1)='h'][substring(text(),2,1)='e']">
2767                                 <form authority="smd">microfiche</form>
2768                         </xsl:if>
2769                         <xsl:if test="marc:controlfield[@tag=007][substring(text(),1,1)='h'][substring(text(),2,1)='f']">
2770                                 <form authority="smd">microfiche cassette</form>
2771                         </xsl:if>
2772                         <xsl:if test="marc:controlfield[@tag=007][substring(text(),1,1)='h'][substring(text(),2,1)='b']">
2773                                 <form authority="smd">microfilm cartridge</form>
2774                         </xsl:if>
2775                         <xsl:if test="marc:controlfield[@tag=007][substring(text(),1,1)='h'][substring(text(),2,1)='c']">
2776                                 <form authority="smd">microfilm cassette</form>
2777                         </xsl:if>
2778                         <xsl:if test="marc:controlfield[@tag=007][substring(text(),1,1)='h'][substring(text(),2,1)='d']">
2779                                 <form authority="smd">microfilm reel</form>
2780                         </xsl:if>
2781                         <xsl:if test="marc:controlfield[@tag=007][substring(text(),1,1)='h'][substring(text(),2,1)='g']">
2782                                 <form authority="smd">microopaque</form>
2783                         </xsl:if>
2784
2785                         <xsl:if test="marc:controlfield[@tag=007][substring(text(),1,1)='m'][substring(text(),2,1)='c']">
2786                                 <form authority="smd">film cartridge</form>
2787                         </xsl:if>
2788                         <xsl:if test="marc:controlfield[@tag=007][substring(text(),1,1)='m'][substring(text(),2,1)='f']">
2789                                 <form authority="smd">film cassette</form>
2790                         </xsl:if>
2791                         <xsl:if test="marc:controlfield[@tag=007][substring(text(),1,1)='m'][substring(text(),2,1)='r']">
2792                                 <form authority="smd">film reel</form>
2793                         </xsl:if>
2794
2795                         <xsl:if test="marc:controlfield[@tag=007][substring(text(),1,1)='k'][substring(text(),2,1)='n']">
2796                                 <form authority="smd">chart</form>
2797                         </xsl:if>
2798                         <xsl:if test="marc:controlfield[@tag=007][substring(text(),1,1)='k'][substring(text(),2,1)='c']">
2799                                 <form authority="smd">collage</form>
2800                         </xsl:if>
2801                         <xsl:if test="marc:controlfield[@tag=007][substring(text(),1,1)='k'][substring(text(),2,1)='d']">
2802                                 <form authority="smd">drawing</form>
2803                         </xsl:if>
2804                         <xsl:if test="marc:controlfield[@tag=007][substring(text(),1,1)='k'][substring(text(),2,1)='o']">
2805                                 <form authority="smd">flash card</form>
2806                         </xsl:if>
2807                         <xsl:if test="marc:controlfield[@tag=007][substring(text(),1,1)='k'][substring(text(),2,1)='e']">
2808                                 <form authority="smd">painting</form>
2809                         </xsl:if>
2810                         <xsl:if test="marc:controlfield[@tag=007][substring(text(),1,1)='k'][substring(text(),2,1)='f']">
2811                                 <form authority="smd">photomechanical print</form>
2812                         </xsl:if>
2813                         <xsl:if test="marc:controlfield[@tag=007][substring(text(),1,1)='k'][substring(text(),2,1)='g']">
2814                                 <form authority="smd">photonegative</form>
2815                         </xsl:if>
2816                         <xsl:if test="marc:controlfield[@tag=007][substring(text(),1,1)='k'][substring(text(),2,1)='h']">
2817                                 <form authority="smd">photoprint</form>
2818                         </xsl:if>
2819                         <xsl:if test="marc:controlfield[@tag=007][substring(text(),1,1)='k'][substring(text(),2,1)='i']">
2820                                 <form authority="smd">picture</form>
2821                         </xsl:if>
2822                         <xsl:if test="marc:controlfield[@tag=007][substring(text(),1,1)='k'][substring(text(),2,1)='j']">
2823                                 <form authority="smd">print</form>
2824                         </xsl:if>
2825                         <xsl:if test="marc:controlfield[@tag=007][substring(text(),1,1)='k'][substring(text(),2,1)='l']">
2826                                 <form authority="smd">technical drawing</form>
2827                         </xsl:if>
2828
2829                         <xsl:if test="marc:controlfield[@tag=007][substring(text(),1,1)='q'][substring(text(),2,1)='q']">
2830                                 <form authority="smd">notated music</form>
2831                         </xsl:if>
2832
2833                         <xsl:if test="marc:controlfield[@tag=007][substring(text(),1,1)='g'][substring(text(),2,1)='d']">
2834                                 <form authority="smd">filmslip</form>
2835                         </xsl:if>
2836                         <xsl:if test="marc:controlfield[@tag=007][substring(text(),1,1)='g'][substring(text(),2,1)='c']">
2837                                 <form authority="smd">filmstrip cartridge</form>
2838                         </xsl:if>
2839                         <xsl:if test="marc:controlfield[@tag=007][substring(text(),1,1)='g'][substring(text(),2,1)='o']">
2840                                 <form authority="smd">filmstrip roll</form>
2841                         </xsl:if>
2842                         <xsl:if test="marc:controlfield[@tag=007][substring(text(),1,1)='g'][substring(text(),2,1)='f']">
2843                                 <form authority="smd">other filmstrip type</form>
2844                         </xsl:if>
2845                         <xsl:if test="marc:controlfield[@tag=007][substring(text(),1,1)='g'][substring(text(),2,1)='s']">
2846                                 <form authority="smd">slide</form>
2847                         </xsl:if>
2848                         <xsl:if test="marc:controlfield[@tag=007][substring(text(),1,1)='g'][substring(text(),2,1)='t']">
2849                                 <form authority="smd">transparency</form>
2850                         </xsl:if>
2851                         <xsl:if test="marc:controlfield[@tag=007][substring(text(),1,1)='r'][substring(text(),2,1)='r']">
2852                                 <form authority="smd">remote-sensing image</form>
2853                         </xsl:if>
2854                         <xsl:if test="marc:controlfield[@tag=007][substring(text(),1,1)='s'][substring(text(),2,1)='e']">
2855                                 <form authority="smd">cylinder</form>
2856                         </xsl:if>
2857                         <xsl:if test="marc:controlfield[@tag=007][substring(text(),1,1)='s'][substring(text(),2,1)='q']">
2858                                 <form authority="smd">roll</form>
2859                         </xsl:if>
2860                         <xsl:if test="marc:controlfield[@tag=007][substring(text(),1,1)='s'][substring(text(),2,1)='g']">
2861                                 <form authority="smd">sound cartridge</form>
2862                         </xsl:if>
2863                         <xsl:if test="marc:controlfield[@tag=007][substring(text(),1,1)='s'][substring(text(),2,1)='s']">
2864                                 <form authority="smd">sound cassette</form>
2865                         </xsl:if>
2866                         <xsl:if test="marc:controlfield[@tag=007][substring(text(),1,1)='s'][substring(text(),2,1)='d']">
2867                                 <form authority="smd">sound disc</form>
2868                         </xsl:if>
2869                         <xsl:if test="marc:controlfield[@tag=007][substring(text(),1,1)='s'][substring(text(),2,1)='t']">
2870                                 <form authority="smd">sound-tape reel</form>
2871                         </xsl:if>
2872                         <xsl:if test="marc:controlfield[@tag=007][substring(text(),1,1)='s'][substring(text(),2,1)='i']">
2873                                 <form authority="smd">sound-track film</form>
2874                         </xsl:if>
2875                         <xsl:if test="marc:controlfield[@tag=007][substring(text(),1,1)='s'][substring(text(),2,1)='w']">
2876                                 <form authority="smd">wire recording</form>
2877                         </xsl:if>
2878
2879                         <xsl:if test="marc:controlfield[@tag=007][substring(text(),1,1)='f'][substring(text(),2,1)='c']">
2880                                 <form authority="smd">braille</form>
2881                         </xsl:if>
2882                         <xsl:if test="marc:controlfield[@tag=007][substring(text(),1,1)='f'][substring(text(),2,1)='b']">
2883                                 <form authority="smd">combination</form>
2884                         </xsl:if>
2885                         <xsl:if test="marc:controlfield[@tag=007][substring(text(),1,1)='f'][substring(text(),2,1)='a']">
2886                                 <form authority="smd">moon</form>
2887                         </xsl:if>
2888                         <xsl:if test="marc:controlfield[@tag=007][substring(text(),1,1)='f'][substring(text(),2,1)='d']">
2889                                 <form authority="smd">tactile, with no writing system</form>
2890                         </xsl:if>
2891
2892                         <xsl:if test="marc:controlfield[@tag=007][substring(text(),1,1)='t'][substring(text(),2,1)='c']">
2893                                 <form authority="smd">braille</form>
2894                         </xsl:if>
2895                         <xsl:if test="marc:controlfield[@tag=007][substring(text(),1,1)='t'][substring(text(),2,1)='b']">
2896                                 <form authority="smd">large print</form>
2897                         </xsl:if>
2898                         <xsl:if test="marc:controlfield[@tag=007][substring(text(),1,1)='t'][substring(text(),2,1)='a']">
2899                                 <form authority="smd">regular print</form>
2900                         </xsl:if>
2901                         <xsl:if test="marc:controlfield[@tag=007][substring(text(),1,1)='t'][substring(text(),2,1)='d']">
2902                                 <form authority="smd">text in looseleaf binder</form>
2903                         </xsl:if>
2904
2905                         <xsl:if test="marc:controlfield[@tag=007][substring(text(),1,1)='v'][substring(text(),2,1)='c']">
2906                                 <form authority="smd">videocartridge</form>
2907                         </xsl:if>
2908                         <xsl:if test="marc:controlfield[@tag=007][substring(text(),1,1)='v'][substring(text(),2,1)='f']">
2909                                 <form authority="smd">videocassette</form>
2910                         </xsl:if>
2911                         <xsl:if test="marc:controlfield[@tag=007][substring(text(),1,1)='v'][substring(text(),2,1)='d']">
2912                                 <form authority="smd">videodisc</form>
2913                         </xsl:if>
2914                         <xsl:if test="marc:controlfield[@tag=007][substring(text(),1,1)='v'][substring(text(),2,1)='r']">
2915                                 <form authority="smd">videoreel</form>
2916                         </xsl:if>
2917
2918                         <xsl:for-each select="marc:datafield[@tag=856]/marc:subfield[@code='q'][string-length(.)>1]">
2919                                 <internetMediaType>
2920                                         <xsl:value-of select="."></xsl:value-of>
2921                                 </internetMediaType>
2922                         </xsl:for-each>
2923                         <xsl:for-each select="marc:datafield[@tag=300]">
2924                                 <extent>
2925                                         <xsl:call-template name="subfieldSelect">
2926                                                 <xsl:with-param name="codes">abce</xsl:with-param>
2927                                         </xsl:call-template>
2928                                 </extent>
2929                         </xsl:for-each>
2930                 </xsl:variable>
2931                 <xsl:if test="string-length(normalize-space($physicalDescription))">
2932                         <physicalDescription>
2933                                 <xsl:copy-of select="$physicalDescription"></xsl:copy-of>
2934                         </physicalDescription>
2935                 </xsl:if>
2936                 <xsl:for-each select="marc:datafield[@tag=520]">
2937                         <abstract>
2938                                 <xsl:call-template name="uri"></xsl:call-template>
2939                                 <xsl:call-template name="subfieldSelect">
2940                                         <xsl:with-param name="codes">ab</xsl:with-param>
2941                                 </xsl:call-template>
2942                         </abstract>
2943                 </xsl:for-each>
2944                 <xsl:for-each select="marc:datafield[@tag=505]">
2945                         <tableOfContents>
2946                                 <xsl:call-template name="uri"></xsl:call-template>
2947                                 <xsl:call-template name="subfieldSelect">
2948                                         <xsl:with-param name="codes">agrt</xsl:with-param>
2949                                 </xsl:call-template>
2950                         </tableOfContents>
2951                 </xsl:for-each>
2952                 <xsl:for-each select="marc:datafield[@tag=521]">
2953                         <targetAudience>
2954                                 <xsl:call-template name="subfieldSelect">
2955                                         <xsl:with-param name="codes">ab</xsl:with-param>
2956                                 </xsl:call-template>
2957                         </targetAudience>
2958                 </xsl:for-each>
2959                 <xsl:if test="$typeOf008='BK' or $typeOf008='CF' or $typeOf008='MU' or $typeOf008='VM'">
2960                         <xsl:variable name="controlField008-22" select="substring($controlField008,23,1)"></xsl:variable>
2961                         <xsl:choose>
2962                                 <!-- 01/04 fix -->
2963                                 <xsl:when test="$controlField008-22='d'">
2964                                         <targetAudience authority="marctarget">adolescent</targetAudience>
2965                                 </xsl:when>
2966                                 <xsl:when test="$controlField008-22='e'">
2967                                         <targetAudience authority="marctarget">adult</targetAudience>
2968                                 </xsl:when>
2969                                 <xsl:when test="$controlField008-22='g'">
2970                                         <targetAudience authority="marctarget">general</targetAudience>
2971                                 </xsl:when>
2972                                 <xsl:when test="$controlField008-22='b' or $controlField008-22='c' or $controlField008-22='j'">
2973                                         <targetAudience authority="marctarget">juvenile</targetAudience>
2974                                 </xsl:when>
2975                                 <xsl:when test="$controlField008-22='a'">
2976                                         <targetAudience authority="marctarget">preschool</targetAudience>
2977                                 </xsl:when>
2978                                 <xsl:when test="$controlField008-22='f'">
2979                                         <targetAudience authority="marctarget">specialized</targetAudience>
2980                                 </xsl:when>
2981                         </xsl:choose>
2982                 </xsl:if>
2983                 <xsl:for-each select="marc:datafield[@tag=245]/marc:subfield[@code='c']">
2984                         <note type="statement of responsibility">
2985                                 <xsl:value-of select="."></xsl:value-of>
2986                         </note>
2987                 </xsl:for-each>
2988                 <xsl:for-each select="marc:datafield[@tag=500]">
2989                         <note>
2990                                 <xsl:value-of select="marc:subfield[@code='a']"></xsl:value-of>
2991                                 <xsl:call-template name="uri"></xsl:call-template>
2992                         </note>
2993                 </xsl:for-each>
2994
2995                 <!--3.2 change tmee additional note fields-->
2996
2997                 <xsl:for-each select="marc:datafield[@tag=502]">
2998                         <note type="thesis">
2999                                 <xsl:call-template name="uri"/>
3000                                 <xsl:variable name="str">
3001                                         <xsl:for-each select="marc:subfield[@code!='6' or @code!='8']">
3002                                                 <xsl:value-of select="."/>
3003                                                 <xsl:text> </xsl:text>
3004                                         </xsl:for-each>
3005                                 </xsl:variable>
3006                                 <xsl:value-of select="substring($str,1,string-length($str)-1)"/>
3007                         </note>
3008                 </xsl:for-each>
3009
3010                 <xsl:for-each select="marc:datafield[@tag=504]">
3011                         <note type="bibliography">
3012                                 <xsl:call-template name="uri"/>
3013                                 <xsl:variable name="str">
3014                                         <xsl:for-each select="marc:subfield[@code!='6' or @code!='8']">
3015                                                 <xsl:value-of select="."/>
3016                                                 <xsl:text> </xsl:text>
3017                                         </xsl:for-each>
3018                                 </xsl:variable>
3019                                 <xsl:value-of select="substring($str,1,string-length($str)-1)"/>
3020                         </note>
3021                 </xsl:for-each>
3022
3023                 <xsl:for-each select="marc:datafield[@tag=508]">
3024                         <note type="creation/production credits">
3025                                 <xsl:call-template name="uri"/>
3026                                 <xsl:variable name="str">
3027                                         <xsl:for-each select="marc:subfield[@code!='6' or @code!='8']">
3028                                                 <xsl:value-of select="."/>
3029                                                 <xsl:text> </xsl:text>
3030                                         </xsl:for-each>
3031                                 </xsl:variable>
3032                                 <xsl:value-of select="substring($str,1,string-length($str)-1)"/>
3033                         </note>
3034                 </xsl:for-each>
3035
3036                 <xsl:for-each select="marc:datafield[@tag=506]">
3037                         <note type="restrictions">
3038                                 <xsl:call-template name="uri"></xsl:call-template>
3039                                 <xsl:variable name="str">
3040                                         <xsl:for-each select="marc:subfield[@code!='6' or @code!='8']">
3041                                                 <xsl:value-of select="."></xsl:value-of>
3042                                                 <xsl:text> </xsl:text>
3043                                         </xsl:for-each>
3044                                 </xsl:variable>
3045                                 <xsl:value-of select="substring($str,1,string-length($str)-1)"></xsl:value-of>
3046                         </note>
3047                 </xsl:for-each>
3048
3049                 <xsl:for-each select="marc:datafield[@tag=510]">
3050                         <note  type="citation/reference">
3051                                 <xsl:call-template name="uri"></xsl:call-template>
3052                                 <xsl:variable name="str">
3053                                         <xsl:for-each select="marc:subfield[@code!='6' or @code!='8']">
3054                                                 <xsl:value-of select="."></xsl:value-of>
3055                                                 <xsl:text> </xsl:text>
3056                                         </xsl:for-each>
3057                                 </xsl:variable>
3058                                 <xsl:value-of select="substring($str,1,string-length($str)-1)"></xsl:value-of>
3059                         </note>
3060                 </xsl:for-each>
3061
3062
3063                 <xsl:for-each select="marc:datafield[@tag=511]">
3064                         <note type="performers">
3065                                 <xsl:call-template name="uri"></xsl:call-template>
3066                                 <xsl:value-of select="marc:subfield[@code='a']"></xsl:value-of>
3067                         </note>
3068                 </xsl:for-each>
3069                 <xsl:for-each select="marc:datafield[@tag=518]">
3070                         <note type="venue">
3071                                 <xsl:call-template name="uri"></xsl:call-template>
3072                                 <xsl:value-of select="marc:subfield[@code='a']"></xsl:value-of>
3073                         </note>
3074                 </xsl:for-each>
3075
3076                 <xsl:for-each select="marc:datafield[@tag=530]">
3077                         <note  type="additional physical form">
3078                                 <xsl:call-template name="uri"></xsl:call-template>
3079                                 <xsl:variable name="str">
3080                                         <xsl:for-each select="marc:subfield[@code!='6' or @code!='8']">
3081                                                 <xsl:value-of select="."></xsl:value-of>
3082                                                 <xsl:text> </xsl:text>
3083                                         </xsl:for-each>
3084                                 </xsl:variable>
3085                                 <xsl:value-of select="substring($str,1,string-length($str)-1)"></xsl:value-of>
3086                         </note>
3087                 </xsl:for-each>
3088
3089                 <xsl:for-each select="marc:datafield[@tag=533]">
3090                         <note  type="reproduction">
3091                                 <xsl:call-template name="uri"></xsl:call-template>
3092                                 <xsl:variable name="str">
3093                                         <xsl:for-each select="marc:subfield[@code!='6' or @code!='8']">
3094                                                 <xsl:value-of select="."></xsl:value-of>
3095                                                 <xsl:text> </xsl:text>
3096                                         </xsl:for-each>
3097                                 </xsl:variable>
3098                                 <xsl:value-of select="substring($str,1,string-length($str)-1)"></xsl:value-of>
3099                         </note>
3100                 </xsl:for-each>
3101
3102                 <xsl:for-each select="marc:datafield[@tag=534]">
3103                         <note  type="original version">
3104                                 <xsl:call-template name="uri"></xsl:call-template>
3105                                 <xsl:variable name="str">
3106                                         <xsl:for-each select="marc:subfield[@code!='6' or @code!='8']">
3107                                                 <xsl:value-of select="."></xsl:value-of>
3108                                                 <xsl:text> </xsl:text>
3109                                         </xsl:for-each>
3110                                 </xsl:variable>
3111                                 <xsl:value-of select="substring($str,1,string-length($str)-1)"></xsl:value-of>
3112                         </note>
3113                 </xsl:for-each>
3114
3115                 <xsl:for-each select="marc:datafield[@tag=538]">
3116                         <note  type="system details">
3117                                 <xsl:call-template name="uri"></xsl:call-template>
3118                                 <xsl:variable name="str">
3119                                         <xsl:for-each select="marc:subfield[@code!='6' or @code!='8']">
3120                                                 <xsl:value-of select="."></xsl:value-of>
3121                                                 <xsl:text> </xsl:text>
3122                                         </xsl:for-each>
3123                                 </xsl:variable>
3124                                 <xsl:value-of select="substring($str,1,string-length($str)-1)"></xsl:value-of>
3125                         </note>
3126                 </xsl:for-each>
3127
3128                 <xsl:for-each select="marc:datafield[@tag=583]">
3129                         <note type="action">
3130                                 <xsl:call-template name="uri"></xsl:call-template>
3131                                 <xsl:variable name="str">
3132                                         <xsl:for-each select="marc:subfield[@code!='6' or @code!='8']">
3133                                                 <xsl:value-of select="."></xsl:value-of>
3134                                                 <xsl:text> </xsl:text>
3135                                         </xsl:for-each>
3136                                 </xsl:variable>
3137                                 <xsl:value-of select="substring($str,1,string-length($str)-1)"></xsl:value-of>
3138                         </note>
3139                 </xsl:for-each>
3140
3141
3142
3143
3144
3145                 <xsl:for-each select="marc:datafield[@tag=501 or @tag=507 or  @tag=513 or @tag=514 or @tag=515 or @tag=516 or @tag=522 or @tag=524 or @tag=525 or @tag=526 or @tag=535 or @tag=536 or @tag=540 or @tag=541 or @tag=544 or @tag=545 or @tag=546 or @tag=547 or @tag=550 or @tag=552 or @tag=555 or @tag=556 or @tag=561 or @tag=562 or @tag=565 or @tag=567 or @tag=580 or @tag=581 or @tag=584 or @tag=585 or @tag=586]">
3146                         <note>
3147                                 <xsl:call-template name="uri"></xsl:call-template>
3148                                 <xsl:variable name="str">
3149                                         <xsl:for-each select="marc:subfield[@code!='6' or @code!='8']">
3150                                                 <xsl:value-of select="."></xsl:value-of>
3151                                                 <xsl:text> </xsl:text>
3152                                         </xsl:for-each>
3153                                 </xsl:variable>
3154                                 <xsl:value-of select="substring($str,1,string-length($str)-1)"></xsl:value-of>
3155                         </note>
3156                 </xsl:for-each>
3157                 <xsl:for-each select="marc:datafield[@tag=034][marc:subfield[@code='d' or @code='e' or @code='f' or @code='g']]">
3158                         <subject>
3159                                 <cartographics>
3160                                         <coordinates>
3161                                                 <xsl:call-template name="subfieldSelect">
3162                                                         <xsl:with-param name="codes">defg</xsl:with-param>
3163                                                 </xsl:call-template>
3164                                         </coordinates>
3165                                 </cartographics>
3166                         </subject>
3167                 </xsl:for-each>
3168                 <xsl:for-each select="marc:datafield[@tag=043]">
3169                         <subject>
3170                                 <xsl:for-each select="marc:subfield[@code='a' or @code='b' or @code='c']">
3171                                         <geographicCode>
3172                                                 <xsl:attribute name="authority">
3173                                                         <xsl:if test="@code='a'">
3174                                                                 <xsl:text>marcgac</xsl:text>
3175                                                         </xsl:if>
3176                                                         <xsl:if test="@code='b'">
3177                                                                 <xsl:value-of select="following-sibling::marc:subfield[@code=2]"></xsl:value-of>
3178                                                         </xsl:if>
3179                                                         <xsl:if test="@code='c'">
3180                                                                 <xsl:text>iso3166</xsl:text>
3181                                                         </xsl:if>
3182                                                 </xsl:attribute>
3183                                                 <xsl:value-of select="self::marc:subfield"></xsl:value-of>
3184                                         </geographicCode>
3185                                 </xsl:for-each>
3186                         </subject>
3187                 </xsl:for-each>
3188                 <!-- tmee 2006/11/27 -->
3189                 <xsl:for-each select="marc:datafield[@tag=255]">
3190                         <subject>
3191                                 <xsl:for-each select="marc:subfield[@code='a' or @code='b' or @code='c']">
3192                                 <cartographics>
3193                                         <xsl:if test="@code='a'">
3194                                                 <scale>
3195                                                         <xsl:value-of select="."></xsl:value-of>
3196                                                 </scale>
3197                                         </xsl:if>
3198                                         <xsl:if test="@code='b'">
3199                                                 <projection>
3200                                                         <xsl:value-of select="."></xsl:value-of>
3201                                                 </projection>
3202                                         </xsl:if>
3203                                         <xsl:if test="@code='c'">
3204                                                 <coordinates>
3205                                                         <xsl:value-of select="."></xsl:value-of>
3206                                                 </coordinates>
3207                                         </xsl:if>
3208                                 </cartographics>
3209                                 </xsl:for-each>
3210                         </subject>
3211                 </xsl:for-each>
3212
3213                 <xsl:apply-templates select="marc:datafield[653 >= @tag and @tag >= 600]"></xsl:apply-templates>
3214                 <xsl:apply-templates select="marc:datafield[@tag=656]"></xsl:apply-templates>
3215                 <xsl:for-each select="marc:datafield[@tag=752]">
3216                         <subject>
3217                                 <hierarchicalGeographic>
3218                                         <xsl:for-each select="marc:subfield[@code='a']">
3219                                                 <country>
3220                                                         <xsl:call-template name="chopPunctuation">
3221                                                                 <xsl:with-param name="chopString" select="."></xsl:with-param>
3222                                                         </xsl:call-template>
3223                                                 </country>
3224                                         </xsl:for-each>
3225                                         <xsl:for-each select="marc:subfield[@code='b']">
3226                                                 <state>
3227                                                         <xsl:call-template name="chopPunctuation">
3228                                                                 <xsl:with-param name="chopString" select="."></xsl:with-param>
3229                                                         </xsl:call-template>
3230                                                 </state>
3231                                         </xsl:for-each>
3232                                         <xsl:for-each select="marc:subfield[@code='c']">
3233                                                 <county>
3234                                                         <xsl:call-template name="chopPunctuation">
3235                                                                 <xsl:with-param name="chopString" select="."></xsl:with-param>
3236                                                         </xsl:call-template>
3237                                                 </county>
3238                                         </xsl:for-each>
3239                                         <xsl:for-each select="marc:subfield[@code='d']">
3240                                                 <city>
3241                                                         <xsl:call-template name="chopPunctuation">
3242                                                                 <xsl:with-param name="chopString" select="."></xsl:with-param>
3243                                                         </xsl:call-template>
3244                                                 </city>
3245                                         </xsl:for-each>
3246                                 </hierarchicalGeographic>
3247                         </subject>
3248                 </xsl:for-each>
3249                 <xsl:for-each select="marc:datafield[@tag=045][marc:subfield[@code='b']]">
3250                         <subject>
3251                                 <xsl:choose>
3252                                         <xsl:when test="@ind1=2">
3253                                                 <temporal encoding="iso8601" point="start">
3254                                                         <xsl:call-template name="chopPunctuation">
3255                                                                 <xsl:with-param name="chopString">
3256                                                                         <xsl:value-of select="marc:subfield[@code='b'][1]"></xsl:value-of>
3257                                                                 </xsl:with-param>
3258                                                         </xsl:call-template>
3259                                                 </temporal>
3260                                                 <temporal encoding="iso8601" point="end">
3261                                                         <xsl:call-template name="chopPunctuation">
3262                                                                 <xsl:with-param name="chopString">
3263                                                                         <xsl:value-of select="marc:subfield[@code='b'][2]"></xsl:value-of>
3264                                                                 </xsl:with-param>
3265                                                         </xsl:call-template>
3266                                                 </temporal>
3267                                         </xsl:when>
3268                                         <xsl:otherwise>
3269                                                 <xsl:for-each select="marc:subfield[@code='b']">
3270                                                         <temporal encoding="iso8601">
3271                                                                 <xsl:call-template name="chopPunctuation">
3272                                                                         <xsl:with-param name="chopString" select="."></xsl:with-param>
3273                                                                 </xsl:call-template>
3274                                                         </temporal>
3275                                                 </xsl:for-each>
3276                                         </xsl:otherwise>
3277                                 </xsl:choose>
3278                         </subject>
3279                 </xsl:for-each>
3280                 <xsl:for-each select="marc:datafield[@tag=050]">
3281                         <xsl:for-each select="marc:subfield[@code='b']">
3282                                 <classification authority="lcc">
3283                                         <xsl:if test="../marc:subfield[@code='3']">
3284                                                 <xsl:attribute name="displayLabel">
3285                                                         <xsl:value-of select="../marc:subfield[@code='3']"></xsl:value-of>
3286                                                 </xsl:attribute>
3287                                         </xsl:if>
3288                                         <xsl:value-of select="preceding-sibling::marc:subfield[@code='a'][1]"></xsl:value-of>
3289                                         <xsl:text> </xsl:text>
3290                                         <xsl:value-of select="text()"></xsl:value-of>
3291                                 </classification>
3292                         </xsl:for-each>
3293                         <xsl:for-each select="marc:subfield[@code='a'][not(following-sibling::marc:subfield[@code='b'])]">
3294                                 <classification authority="lcc">
3295                                         <xsl:if test="../marc:subfield[@code='3']">
3296                                                 <xsl:attribute name="displayLabel">
3297                                                         <xsl:value-of select="../marc:subfield[@code='3']"></xsl:value-of>
3298                                                 </xsl:attribute>
3299                                         </xsl:if>
3300                                         <xsl:value-of select="text()"></xsl:value-of>
3301                                 </classification>
3302                         </xsl:for-each>
3303                 </xsl:for-each>
3304                 <xsl:for-each select="marc:datafield[@tag=082]">
3305                         <classification authority="ddc">
3306                                 <xsl:if test="marc:subfield[@code='2']">
3307                                         <xsl:attribute name="edition">
3308                                                 <xsl:value-of select="marc:subfield[@code='2']"></xsl:value-of>
3309                                         </xsl:attribute>
3310                                 </xsl:if>
3311                                 <xsl:call-template name="subfieldSelect">
3312                                         <xsl:with-param name="codes">ab</xsl:with-param>
3313                                 </xsl:call-template>
3314                         </classification>
3315                 </xsl:for-each>
3316                 <xsl:for-each select="marc:datafield[@tag=080]">
3317                         <classification authority="udc">
3318                                 <xsl:call-template name="subfieldSelect">
3319                                         <xsl:with-param name="codes">abx</xsl:with-param>
3320                                 </xsl:call-template>
3321                         </classification>
3322                 </xsl:for-each>
3323                 <xsl:for-each select="marc:datafield[@tag=060]">
3324                         <classification authority="nlm">
3325                                 <xsl:call-template name="subfieldSelect">
3326                                         <xsl:with-param name="codes">ab</xsl:with-param>
3327                                 </xsl:call-template>
3328                         </classification>
3329                 </xsl:for-each>
3330                 <xsl:for-each select="marc:datafield[@tag=086][@ind1=0]">
3331                         <classification authority="sudocs">
3332                                 <xsl:value-of select="marc:subfield[@code='a']"></xsl:value-of>
3333                         </classification>
3334                 </xsl:for-each>
3335                 <xsl:for-each select="marc:datafield[@tag=086][@ind1=1]">
3336                         <classification authority="candoc">
3337                                 <xsl:value-of select="marc:subfield[@code='a']"></xsl:value-of>
3338                         </classification>
3339                 </xsl:for-each>
3340                 <xsl:for-each select="marc:datafield[@tag=086]">
3341                         <classification>
3342                                 <xsl:attribute name="authority">
3343                                         <xsl:value-of select="marc:subfield[@code='2']"></xsl:value-of>
3344                                 </xsl:attribute>
3345                                 <xsl:value-of select="marc:subfield[@code='a']"></xsl:value-of>
3346                         </classification>
3347                 </xsl:for-each>
3348                 <xsl:for-each select="marc:datafield[@tag=084]">
3349                         <classification>
3350                                 <xsl:attribute name="authority">
3351                                         <xsl:value-of select="marc:subfield[@code='2']"></xsl:value-of>
3352                                 </xsl:attribute>
3353                                 <xsl:call-template name="subfieldSelect">
3354                                         <xsl:with-param name="codes">ab</xsl:with-param>
3355                                 </xsl:call-template>
3356                         </classification>
3357                 </xsl:for-each>
3358                 <xsl:for-each select="marc:datafield[@tag=440]">
3359                         <relatedItem type="series">
3360                                 <xsl:variable name="titleChop">
3361                                         <xsl:call-template name="chopPunctuation">
3362                                                 <xsl:with-param name="chopString">
3363                                                         <xsl:call-template name="subfieldSelect">
3364                                                                 <xsl:with-param name="codes">av</xsl:with-param>
3365                                                         </xsl:call-template>
3366                                                 </xsl:with-param>
3367                                         </xsl:call-template>
3368                                 </xsl:variable>
3369                                 <titleInfo>
3370                                         <title>
3371                                                 <xsl:value-of select="$titleChop" />
3372                                         </title>
3373                                         <xsl:call-template name="part"></xsl:call-template>
3374                                 </titleInfo>
3375                                 <titleInfo type="nfi">
3376                                         <xsl:choose>
3377                                                 <xsl:when test="@ind2>0">
3378                                                         <nonSort>
3379                                                                 <xsl:value-of select="substring($titleChop,1,@ind2)"/>
3380                                                         </nonSort>
3381                                                         <title>
3382                                                                 <xsl:value-of select="substring($titleChop,@ind2+1)"/>
3383                                                         </title>
3384                                                         <xsl:call-template name="part"/>
3385                                                 </xsl:when>
3386                                                 <xsl:otherwise>
3387                                                         <title>
3388                                                                 <xsl:value-of select="$titleChop" />
3389                                                         </title>
3390                                                 </xsl:otherwise>
3391                                         </xsl:choose>
3392                                         <xsl:call-template name="part"></xsl:call-template>
3393                                 </titleInfo>
3394                         </relatedItem>
3395                 </xsl:for-each>
3396                 <xsl:for-each select="marc:datafield[@tag=490][@ind1=0]">
3397                         <relatedItem type="series">
3398                                 <titleInfo>
3399                                         <title>
3400                                                 <xsl:call-template name="chopPunctuation">
3401                                                         <xsl:with-param name="chopString">
3402                                                                 <xsl:call-template name="subfieldSelect">
3403                                                                         <xsl:with-param name="codes">av</xsl:with-param>
3404                                                                 </xsl:call-template>
3405                                                         </xsl:with-param>
3406                                                 </xsl:call-template>
3407                                         </title>
3408                                         <xsl:call-template name="part"></xsl:call-template>
3409                                 </titleInfo>
3410                         </relatedItem>
3411                 </xsl:for-each>
3412                 <xsl:for-each select="marc:datafield[@tag=510]">
3413                         <relatedItem type="isReferencedBy">
3414                                 <note>
3415                                         <xsl:call-template name="subfieldSelect">
3416                                                 <xsl:with-param name="codes">abcx3</xsl:with-param>
3417                                         </xsl:call-template>
3418                                 </note>
3419                         </relatedItem>
3420                 </xsl:for-each>
3421                 <xsl:for-each select="marc:datafield[@tag=534]">
3422                         <relatedItem type="original">
3423                                 <xsl:call-template name="relatedTitle"></xsl:call-template>
3424                                 <xsl:call-template name="relatedName"></xsl:call-template>
3425                                 <xsl:if test="marc:subfield[@code='b' or @code='c']">
3426                                         <originInfo>
3427                                                 <xsl:for-each select="marc:subfield[@code='c']">
3428                                                         <publisher>
3429                                                                 <xsl:value-of select="."></xsl:value-of>
3430                                                         </publisher>
3431                                                 </xsl:for-each>
3432                                                 <xsl:for-each select="marc:subfield[@code='b']">
3433                                                         <edition>
3434                                                                 <xsl:value-of select="."></xsl:value-of>
3435                                                         </edition>
3436                                                 </xsl:for-each>
3437                                         </originInfo>
3438                                 </xsl:if>
3439                                 <xsl:call-template name="relatedIdentifierISSN"></xsl:call-template>
3440                                 <xsl:for-each select="marc:subfield[@code='z']">
3441                                         <identifier type="isbn">
3442                                                 <xsl:value-of select="."></xsl:value-of>
3443                                         </identifier>
3444                                 </xsl:for-each>
3445                                 <xsl:call-template name="relatedNote"></xsl:call-template>
3446                         </relatedItem>
3447                 </xsl:for-each>
3448                 <xsl:for-each select="marc:datafield[@tag=700][marc:subfield[@code='t']]">
3449                         <relatedItem>
3450                                 <xsl:call-template name="constituentOrRelatedType"></xsl:call-template>
3451                                 <titleInfo>
3452                                         <title>
3453                                                 <xsl:call-template name="chopPunctuation">
3454                                                         <xsl:with-param name="chopString">
3455                                                                 <xsl:call-template name="specialSubfieldSelect">
3456                                                                         <xsl:with-param name="anyCodes">tfklmorsv</xsl:with-param>
3457                                                                         <xsl:with-param name="axis">t</xsl:with-param>
3458                                                                         <xsl:with-param name="afterCodes">g</xsl:with-param>
3459                                                                 </xsl:call-template>
3460                                                         </xsl:with-param>
3461                                                 </xsl:call-template>
3462                                         </title>
3463                                         <xsl:call-template name="part"></xsl:call-template>
3464                                 </titleInfo>
3465                                 <name type="personal">
3466                                         <namePart>
3467                                                 <xsl:call-template name="specialSubfieldSelect">
3468                                                         <xsl:with-param name="anyCodes">aq</xsl:with-param>
3469                                                         <xsl:with-param name="axis">t</xsl:with-param>
3470                                                         <xsl:with-param name="beforeCodes">g</xsl:with-param>
3471                                                 </xsl:call-template>
3472                                         </namePart>
3473                                         <xsl:call-template name="termsOfAddress"></xsl:call-template>
3474                                         <xsl:call-template name="nameDate"></xsl:call-template>
3475                                         <xsl:call-template name="role"></xsl:call-template>
3476                                 </name>
3477                                 <xsl:call-template name="relatedForm"></xsl:call-template>
3478                                 <xsl:call-template name="relatedIdentifierISSN"></xsl:call-template>
3479                         </relatedItem>
3480                 </xsl:for-each>
3481                 <xsl:for-each select="marc:datafield[@tag=710][marc:subfield[@code='t']]">
3482                         <relatedItem>
3483                                 <xsl:call-template name="constituentOrRelatedType"></xsl:call-template>
3484                                 <titleInfo>
3485                                         <title>
3486                                                 <xsl:call-template name="chopPunctuation">
3487                                                         <xsl:with-param name="chopString">
3488                                                                 <xsl:call-template name="specialSubfieldSelect">
3489                                                                         <xsl:with-param name="anyCodes">tfklmorsv</xsl:with-param>
3490                                                                         <xsl:with-param name="axis">t</xsl:with-param>
3491                                                                         <xsl:with-param name="afterCodes">dg</xsl:with-param>
3492                                                                 </xsl:call-template>
3493                                                         </xsl:with-param>
3494                                                 </xsl:call-template>
3495                                         </title>
3496                                         <xsl:call-template name="relatedPartNumName"></xsl:call-template>
3497                                 </titleInfo>
3498                                 <name type="corporate">
3499                                         <xsl:for-each select="marc:subfield[@code='a']">
3500                                                 <namePart>
3501                                                         <xsl:value-of select="."></xsl:value-of>
3502                                                 </namePart>
3503                                         </xsl:for-each>
3504                                         <xsl:for-each select="marc:subfield[@code='b']">
3505                                                 <namePart>
3506                                                         <xsl:value-of select="."></xsl:value-of>
3507                                                 </namePart>
3508                                         </xsl:for-each>
3509                                         <xsl:variable name="tempNamePart">
3510                                                 <xsl:call-template name="specialSubfieldSelect">
3511                                                         <xsl:with-param name="anyCodes">c</xsl:with-param>
3512                                                         <xsl:with-param name="axis">t</xsl:with-param>
3513                                                         <xsl:with-param name="beforeCodes">dgn</xsl:with-param>
3514                                                 </xsl:call-template>
3515                                         </xsl:variable>
3516                                         <xsl:if test="normalize-space($tempNamePart)">
3517                                                 <namePart>
3518                                                         <xsl:value-of select="$tempNamePart"></xsl:value-of>
3519                                                 </namePart>
3520                                         </xsl:if>
3521                                         <xsl:call-template name="role"></xsl:call-template>
3522                                 </name>
3523                                 <xsl:call-template name="relatedForm"></xsl:call-template>
3524                                 <xsl:call-template name="relatedIdentifierISSN"></xsl:call-template>
3525                         </relatedItem>
3526                 </xsl:for-each>
3527                 <xsl:for-each select="marc:datafield[@tag=711][marc:subfield[@code='t']]">
3528                         <relatedItem>
3529                                 <xsl:call-template name="constituentOrRelatedType"></xsl:call-template>
3530                                 <titleInfo>
3531                                         <title>
3532                                                 <xsl:call-template name="chopPunctuation">
3533                                                         <xsl:with-param name="chopString">
3534                                                                 <xsl:call-template name="specialSubfieldSelect">
3535                                                                         <xsl:with-param name="anyCodes">tfklsv</xsl:with-param>
3536                                                                         <xsl:with-param name="axis">t</xsl:with-param>
3537                                                                         <xsl:with-param name="afterCodes">g</xsl:with-param>
3538                                                                 </xsl:call-template>
3539                                                         </xsl:with-param>
3540                                                 </xsl:call-template>
3541                                         </title>
3542                                         <xsl:call-template name="relatedPartNumName"></xsl:call-template>
3543                                 </titleInfo>
3544                                 <name type="conference">
3545                                         <namePart>
3546                                                 <xsl:call-template name="specialSubfieldSelect">
3547                                                         <xsl:with-param name="anyCodes">aqdc</xsl:with-param>
3548                                                         <xsl:with-param name="axis">t</xsl:with-param>
3549                                                         <xsl:with-param name="beforeCodes">gn</xsl:with-param>
3550                                                 </xsl:call-template>
3551                                         </namePart>
3552                                 </name>
3553                                 <xsl:call-template name="relatedForm"></xsl:call-template>
3554                                 <xsl:call-template name="relatedIdentifierISSN"></xsl:call-template>
3555                         </relatedItem>
3556                 </xsl:for-each>
3557                 <xsl:for-each select="marc:datafield[@tag=730][@ind2=2]">
3558                         <relatedItem>
3559                                 <xsl:call-template name="constituentOrRelatedType"></xsl:call-template>
3560                                 <titleInfo>
3561                                         <title>
3562                                                 <xsl:call-template name="chopPunctuation">
3563                                                         <xsl:with-param name="chopString">
3564                                                                 <xsl:call-template name="subfieldSelect">
3565                                                                         <xsl:with-param name="codes">adfgklmorsv</xsl:with-param>
3566                                                                 </xsl:call-template>
3567                                                         </xsl:with-param>
3568                                                 </xsl:call-template>
3569                                         </title>
3570                                         <xsl:call-template name="part"></xsl:call-template>
3571                                 </titleInfo>
3572                                 <xsl:call-template name="relatedForm"></xsl:call-template>
3573                                 <xsl:call-template name="relatedIdentifierISSN"></xsl:call-template>
3574                         </relatedItem>
3575                 </xsl:for-each>
3576                 <xsl:for-each select="marc:datafield[@tag=740][@ind2=2]">
3577                         <relatedItem>
3578                                 <xsl:call-template name="constituentOrRelatedType"></xsl:call-template>
3579                                 <xsl:variable name="titleChop">
3580                                         <xsl:call-template name="chopPunctuation">
3581                                                 <xsl:with-param name="chopString">
3582                                                         <xsl:value-of select="marc:subfield[@code='a']"></xsl:value-of>
3583                                                 </xsl:with-param>
3584                                         </xsl:call-template>
3585                                 </xsl:variable>
3586                                 <titleInfo>
3587                                         <title>
3588                                                 <xsl:value-of select="$titleChop" />
3589                                         </title>
3590                                         <xsl:call-template name="part"></xsl:call-template>
3591                                 </titleInfo>
3592                                 <titleInfo type="nfi">
3593                                         <xsl:choose>
3594                                                 <xsl:when test="@ind1>0">
3595                                                         <nonSort>
3596                                                                 <xsl:value-of select="substring($titleChop,1,@ind1)"/>
3597                                                         </nonSort>
3598                                                         <title>
3599                                                                 <xsl:value-of select="substring($titleChop,@ind1+1)"/>
3600                                                         </title>
3601                                                 </xsl:when>
3602                                                 <xsl:otherwise>
3603                                                         <title>
3604                                                                 <xsl:value-of select="$titleChop" />
3605                                                         </title>
3606                                                 </xsl:otherwise>
3607                                         </xsl:choose>
3608                                         <xsl:call-template name="part"></xsl:call-template>
3609                                 </titleInfo>
3610                                 <xsl:call-template name="relatedForm"></xsl:call-template>
3611                         </relatedItem>
3612                 </xsl:for-each>
3613                 <xsl:for-each select="marc:datafield[@tag=760]|marc:datafield[@tag=762]">
3614                         <relatedItem type="series">
3615                                 <xsl:call-template name="relatedItem76X-78X"></xsl:call-template>
3616                         </relatedItem>
3617                 </xsl:for-each>
3618                 <xsl:for-each select="marc:datafield[@tag=765]|marc:datafield[@tag=767]|marc:datafield[@tag=777]|marc:datafield[@tag=787]">
3619                         <relatedItem>
3620                                 <xsl:call-template name="relatedItem76X-78X"></xsl:call-template>
3621                         </relatedItem>
3622                 </xsl:for-each>
3623                 <xsl:for-each select="marc:datafield[@tag=775]">
3624                         <relatedItem type="otherVersion">
3625                                 <xsl:call-template name="relatedItem76X-78X"></xsl:call-template>
3626                         </relatedItem>
3627                 </xsl:for-each>
3628                 <xsl:for-each select="marc:datafield[@tag=770]|marc:datafield[@tag=774]">
3629                         <relatedItem type="constituent">
3630                                 <xsl:call-template name="relatedItem76X-78X"></xsl:call-template>
3631                         </relatedItem>
3632                 </xsl:for-each>
3633                 <xsl:for-each select="marc:datafield[@tag=772]|marc:datafield[@tag=773]">
3634                         <relatedItem type="host">
3635                                 <xsl:call-template name="relatedItem76X-78X"></xsl:call-template>
3636                         </relatedItem>
3637                 </xsl:for-each>
3638                 <xsl:for-each select="marc:datafield[@tag=776]">
3639                         <relatedItem type="otherFormat">
3640                                 <xsl:call-template name="relatedItem76X-78X"></xsl:call-template>
3641                         </relatedItem>
3642                 </xsl:for-each>
3643                 <xsl:for-each select="marc:datafield[@tag=780]">
3644                         <relatedItem type="preceding">
3645                                 <xsl:call-template name="relatedItem76X-78X"></xsl:call-template>
3646                         </relatedItem>
3647                 </xsl:for-each>
3648                 <xsl:for-each select="marc:datafield[@tag=785]">
3649                         <relatedItem type="succeeding">
3650                                 <xsl:call-template name="relatedItem76X-78X"></xsl:call-template>
3651                         </relatedItem>
3652                 </xsl:for-each>
3653                 <xsl:for-each select="marc:datafield[@tag=786]">
3654                         <relatedItem type="original">
3655                                 <xsl:call-template name="relatedItem76X-78X"></xsl:call-template>
3656                         </relatedItem>
3657                 </xsl:for-each>
3658                 <xsl:for-each select="marc:datafield[@tag=800]">
3659                         <relatedItem type="series">
3660                                 <titleInfo>
3661                                         <title>
3662                                                 <xsl:call-template name="chopPunctuation">
3663                                                         <xsl:with-param name="chopString">
3664                                                                 <xsl:call-template name="specialSubfieldSelect">
3665                                                                         <xsl:with-param name="anyCodes">tfklmorsv</xsl:with-param>
3666                                                                         <xsl:with-param name="axis">t</xsl:with-param>
3667                                                                         <xsl:with-param name="afterCodes">g</xsl:with-param>
3668                                                                 </xsl:call-template>
3669                                                         </xsl:with-param>
3670                                                 </xsl:call-template>
3671                                         </title>
3672                                         <xsl:call-template name="part"></xsl:call-template>
3673                                 </titleInfo>
3674                                 <name type="personal">
3675                                         <namePart>
3676                                                 <xsl:call-template name="chopPunctuation">
3677                                                         <xsl:with-param name="chopString">
3678                                                                 <xsl:call-template name="specialSubfieldSelect">
3679                                                                         <xsl:with-param name="anyCodes">aq</xsl:with-param>
3680                                                                         <xsl:with-param name="axis">t</xsl:with-param>
3681                                                                         <xsl:with-param name="beforeCodes">g</xsl:with-param>
3682                                                                 </xsl:call-template>
3683                                                         </xsl:with-param>
3684                                                 </xsl:call-template>
3685                                         </namePart>
3686                                         <xsl:call-template name="termsOfAddress"></xsl:call-template>
3687                                         <xsl:call-template name="nameDate"></xsl:call-template>
3688                                         <xsl:call-template name="role"></xsl:call-template>
3689                                 </name>
3690                                 <xsl:call-template name="relatedForm"></xsl:call-template>
3691                         </relatedItem>
3692                 </xsl:for-each>
3693                 <xsl:for-each select="marc:datafield[@tag=810]">
3694                         <relatedItem type="series">
3695                                 <titleInfo>
3696                                         <title>
3697                                                 <xsl:call-template name="chopPunctuation">
3698                                                         <xsl:with-param name="chopString">
3699                                                                 <xsl:call-template name="specialSubfieldSelect">
3700                                                                         <xsl:with-param name="anyCodes">tfklmorsv</xsl:with-param>
3701                                                                         <xsl:with-param name="axis">t</xsl:with-param>
3702                                                                         <xsl:with-param name="afterCodes">dg</xsl:with-param>
3703                                                                 </xsl:call-template>
3704                                                         </xsl:with-param>
3705                                                 </xsl:call-template>
3706                                         </title>
3707                                         <xsl:call-template name="relatedPartNumName"></xsl:call-template>
3708                                 </titleInfo>
3709                                 <name type="corporate">
3710                                         <xsl:for-each select="marc:subfield[@code='a']">
3711                                                 <namePart>
3712                                                         <xsl:value-of select="."></xsl:value-of>
3713                                                 </namePart>
3714                                         </xsl:for-each>
3715                                         <xsl:for-each select="marc:subfield[@code='b']">
3716                                                 <namePart>
3717                                                         <xsl:value-of select="."></xsl:value-of>
3718                                                 </namePart>
3719                                         </xsl:for-each>
3720                                         <namePart>
3721                                                 <xsl:call-template name="specialSubfieldSelect">
3722                                                         <xsl:with-param name="anyCodes">c</xsl:with-param>
3723                                                         <xsl:with-param name="axis">t</xsl:with-param>
3724                                                         <xsl:with-param name="beforeCodes">dgn</xsl:with-param>
3725                                                 </xsl:call-template>
3726                                         </namePart>
3727                                         <xsl:call-template name="role"></xsl:call-template>
3728                                 </name>
3729                                 <xsl:call-template name="relatedForm"></xsl:call-template>
3730                         </relatedItem>
3731                 </xsl:for-each>
3732                 <xsl:for-each select="marc:datafield[@tag=811]">
3733                         <relatedItem type="series">
3734                                 <titleInfo>
3735                                         <title>
3736                                                 <xsl:call-template name="chopPunctuation">
3737                                                         <xsl:with-param name="chopString">
3738                                                                 <xsl:call-template name="specialSubfieldSelect">
3739                                                                         <xsl:with-param name="anyCodes">tfklsv</xsl:with-param>
3740                                                                         <xsl:with-param name="axis">t</xsl:with-param>
3741                                                                         <xsl:with-param name="afterCodes">g</xsl:with-param>
3742                                                                 </xsl:call-template>
3743                                                         </xsl:with-param>
3744                                                 </xsl:call-template>
3745                                         </title>
3746                                         <xsl:call-template name="relatedPartNumName"/>
3747                                 </titleInfo>
3748                                 <name type="conference">
3749                                         <namePart>
3750                                                 <xsl:call-template name="specialSubfieldSelect">
3751                                                         <xsl:with-param name="anyCodes">aqdc</xsl:with-param>
3752                                                         <xsl:with-param name="axis">t</xsl:with-param>
3753                                                         <xsl:with-param name="beforeCodes">gn</xsl:with-param>
3754                                                 </xsl:call-template>
3755                                         </namePart>
3756                                         <xsl:call-template name="role"/>
3757                                 </name>
3758                                 <xsl:call-template name="relatedForm"/>
3759                         </relatedItem>
3760                 </xsl:for-each>
3761                 <xsl:for-each select="marc:datafield[@tag='830']">
3762                         <relatedItem type="series">
3763                                 <xsl:variable name="titleChop">
3764                                         <xsl:call-template name="chopPunctuation">
3765                                                 <xsl:with-param name="chopString">
3766                                                         <xsl:call-template name="subfieldSelect">
3767                                                                 <xsl:with-param name="codes">adfgklmorsv</xsl:with-param>
3768                                                         </xsl:call-template>
3769                                                 </xsl:with-param>
3770                                         </xsl:call-template>
3771                                 </xsl:variable>
3772                                 <titleInfo>
3773                                         <title>
3774                                                 <xsl:value-of select="$titleChop" />
3775                                         </title>
3776                                         <xsl:call-template name="part"/>
3777                                 </titleInfo>
3778                                 <titleInfo type="nfi">
3779                                         <xsl:choose>
3780                                                 <xsl:when test="@ind2>0">
3781                                                         <nonSort>
3782                                                                 <xsl:value-of select="substring($titleChop,1,@ind2)"/>
3783                                                         </nonSort>
3784                                                         <title>
3785                                                                 <xsl:value-of select="substring($titleChop,@ind2+1)"/>
3786                                                         </title>
3787                                                 </xsl:when>
3788                                                 <xsl:otherwise>
3789                                                         <title>
3790                                                                 <xsl:value-of select="$titleChop" />
3791                                                         </title>
3792                                                 </xsl:otherwise>
3793                                         </xsl:choose>
3794                                         <xsl:call-template name="part"/>
3795                                 </titleInfo>
3796                                 <xsl:call-template name="relatedForm"/>
3797                         </relatedItem>
3798                 </xsl:for-each>
3799                 <xsl:for-each select="marc:datafield[@tag='856'][@ind2='2']/marc:subfield[@code='q']">
3800                         <relatedItem>
3801                                 <internetMediaType>
3802                                         <xsl:value-of select="."/>
3803                                 </internetMediaType>
3804                         </relatedItem>
3805                 </xsl:for-each>
3806                 <xsl:for-each select="marc:datafield[@tag='020']">
3807                         <xsl:call-template name="isInvalid">
3808                                 <xsl:with-param name="type">isbn</xsl:with-param>
3809                         </xsl:call-template>
3810                         <xsl:if test="marc:subfield[@code='a']">
3811                                 <identifier type="isbn">
3812                                         <xsl:value-of select="marc:subfield[@code='a']"/>
3813                                 </identifier>
3814                         </xsl:if>
3815                 </xsl:for-each>
3816                 <xsl:for-each select="marc:datafield[@tag='024'][@ind1='0']">
3817                         <xsl:call-template name="isInvalid">
3818                                 <xsl:with-param name="type">isrc</xsl:with-param>
3819                         </xsl:call-template>
3820                         <xsl:if test="marc:subfield[@code='a']">
3821                                 <identifier type="isrc">
3822                                         <xsl:value-of select="marc:subfield[@code='a']"/>
3823                                 </identifier>
3824                         </xsl:if>
3825                 </xsl:for-each>
3826                 <xsl:for-each select="marc:datafield[@tag='024'][@ind1='2']">
3827                         <xsl:call-template name="isInvalid">
3828                                 <xsl:with-param name="type">ismn</xsl:with-param>
3829                         </xsl:call-template>
3830                         <xsl:if test="marc:subfield[@code='a']">
3831                                 <identifier type="ismn">
3832                                         <xsl:value-of select="marc:subfield[@code='a']"/>
3833                                 </identifier>
3834                         </xsl:if>
3835                 </xsl:for-each>
3836                 <xsl:for-each select="marc:datafield[@tag='024'][@ind1='4']">
3837                         <xsl:call-template name="isInvalid">
3838                                 <xsl:with-param name="type">sici</xsl:with-param>
3839                         </xsl:call-template>
3840                         <identifier type="sici">
3841                                 <xsl:call-template name="subfieldSelect">
3842                                         <xsl:with-param name="codes">ab</xsl:with-param>
3843                                 </xsl:call-template>
3844                         </identifier>
3845                 </xsl:for-each>
3846                 <xsl:for-each select="marc:datafield[@tag='022']">
3847                         <xsl:call-template name="isInvalid">
3848                                 <xsl:with-param name="type">issn</xsl:with-param>
3849                         </xsl:call-template>
3850                         <identifier type="issn">
3851                                 <xsl:value-of select="marc:subfield[@code='a']"/>
3852                         </identifier>
3853                 </xsl:for-each>
3854                 <xsl:for-each select="marc:datafield[@tag='010']">
3855                         <xsl:call-template name="isInvalid">
3856                                 <xsl:with-param name="type">lccn</xsl:with-param>
3857                         </xsl:call-template>
3858                         <identifier type="lccn">
3859                                 <xsl:value-of select="normalize-space(marc:subfield[@code='a'])"/>
3860                         </identifier>
3861                 </xsl:for-each>
3862                 <xsl:for-each select="marc:datafield[@tag='028']">
3863                         <identifier>
3864                                 <xsl:attribute name="type">
3865                                         <xsl:choose>
3866                                                 <xsl:when test="@ind1='0'">issue number</xsl:when>
3867                                                 <xsl:when test="@ind1='1'">matrix number</xsl:when>
3868                                                 <xsl:when test="@ind1='2'">music plate</xsl:when>
3869                                                 <xsl:when test="@ind1='3'">music publisher</xsl:when>
3870                                                 <xsl:when test="@ind1='4'">videorecording identifier</xsl:when>
3871                                         </xsl:choose>
3872                                 </xsl:attribute>
3873                                 <!--<xsl:call-template name="isInvalid"/>--> <!-- no $z in 028 -->
3874                                 <xsl:call-template name="subfieldSelect">
3875                                         <xsl:with-param name="codes">
3876                                                 <xsl:choose>
3877                                                         <xsl:when test="@ind1='0'">ba</xsl:when>
3878                                                         <xsl:otherwise>ab</xsl:otherwise>
3879                                                 </xsl:choose>
3880                                         </xsl:with-param>
3881                                 </xsl:call-template>
3882                         </identifier>
3883                 </xsl:for-each>
3884                 <xsl:for-each select="marc:datafield[@tag='037']">
3885                         <identifier type="stock number">
3886                                 <!--<xsl:call-template name="isInvalid"/>--> <!-- no $z in 037 -->
3887                                 <xsl:call-template name="subfieldSelect">
3888                                         <xsl:with-param name="codes">ab</xsl:with-param>
3889                                 </xsl:call-template>
3890                         </identifier>
3891                 </xsl:for-each>
3892                 <xsl:for-each select="marc:datafield[@tag='856'][marc:subfield[@code='u']]">
3893                         <identifier>
3894                                 <xsl:attribute name="type">
3895                                         <xsl:choose>
3896                                                 <xsl:when test="starts-with(marc:subfield[@code='u'],'urn:doi') or starts-with(marc:subfield[@code='u'],'doi')">doi</xsl:when>
3897                                                 <xsl:when test="starts-with(marc:subfield[@code='u'],'urn:hdl') or starts-with(marc:subfield[@code='u'],'hdl') or starts-with(marc:subfield[@code='u'],'http://hdl.loc.gov')">hdl</xsl:when>
3898                                                 <xsl:otherwise>uri</xsl:otherwise>
3899                                         </xsl:choose>
3900                                 </xsl:attribute>
3901                                 <xsl:choose>
3902                                         <xsl:when test="starts-with(marc:subfield[@code='u'],'urn:hdl') or starts-with(marc:subfield[@code='u'],'hdl') or starts-with(marc:subfield[@code='u'],'http://hdl.loc.gov') ">
3903                                                 <xsl:value-of select="concat('hdl:',substring-after(marc:subfield[@code='u'],'http://hdl.loc.gov/'))"></xsl:value-of>
3904                                         </xsl:when>
3905                                         <xsl:otherwise>
3906                                                 <xsl:value-of select="marc:subfield[@code='u']"></xsl:value-of>
3907                                         </xsl:otherwise>
3908                                 </xsl:choose>
3909                         </identifier>
3910                         <xsl:if test="starts-with(marc:subfield[@code='u'],'urn:hdl') or starts-with(marc:subfield[@code='u'],'hdl')">
3911                                 <identifier type="hdl">
3912                                         <xsl:if test="marc:subfield[@code='y' or @code='3' or @code='z']">
3913                                                 <xsl:attribute name="displayLabel">
3914                                                         <xsl:call-template name="subfieldSelect">
3915                                                                 <xsl:with-param name="codes">y3z</xsl:with-param>
3916                                                         </xsl:call-template>
3917                                                 </xsl:attribute>
3918                                         </xsl:if>
3919                                         <xsl:value-of select="concat('hdl:',substring-after(marc:subfield[@code='u'],'http://hdl.loc.gov/'))"></xsl:value-of>
3920                                 </identifier>
3921                         </xsl:if>
3922                 </xsl:for-each>
3923                 <xsl:for-each select="marc:datafield[@tag=024][@ind1=1]">
3924                         <identifier type="upc">
3925                                 <xsl:call-template name="isInvalid"/>
3926                                 <xsl:value-of select="marc:subfield[@code='a']"/>
3927                         </identifier>
3928                 </xsl:for-each>
3929                 <!-- 1/04 fix added $y -->
3930                 <xsl:for-each select="marc:datafield[@tag=856][marc:subfield[@code='u']]">
3931                         <location>
3932                                 <url>
3933                                         <xsl:if test="marc:subfield[@code='y' or @code='3']">
3934                                                 <xsl:attribute name="displayLabel">
3935                                                         <xsl:call-template name="subfieldSelect">
3936                                                                 <xsl:with-param name="codes">y3</xsl:with-param>
3937                                                         </xsl:call-template>
3938                                                 </xsl:attribute>
3939                                         </xsl:if>
3940                                         <xsl:if test="marc:subfield[@code='z' ]">
3941                                                 <xsl:attribute name="note">
3942                                                         <xsl:call-template name="subfieldSelect">
3943                                                                 <xsl:with-param name="codes">z</xsl:with-param>
3944                                                         </xsl:call-template>
3945                                                 </xsl:attribute>
3946                                         </xsl:if>
3947                                         <xsl:value-of select="marc:subfield[@code='u']"></xsl:value-of>
3948
3949                                 </url>
3950                         </location>
3951                 </xsl:for-each>
3952
3953                         <!-- 3.2 change tmee 856z  -->
3954
3955
3956                 <xsl:for-each select="marc:datafield[@tag=852]">
3957                         <location>
3958                                 <physicalLocation>
3959                                         <xsl:call-template name="displayLabel"></xsl:call-template>
3960                                         <xsl:call-template name="subfieldSelect">
3961                                                 <xsl:with-param name="codes">abje</xsl:with-param>
3962                                         </xsl:call-template>
3963                                 </physicalLocation>
3964                         </location>
3965                 </xsl:for-each>
3966                 <xsl:for-each select="marc:datafield[@tag=506]">
3967                         <accessCondition type="restrictionOnAccess">
3968                                 <xsl:call-template name="subfieldSelect">
3969                                         <xsl:with-param name="codes">abcd35</xsl:with-param>
3970                                 </xsl:call-template>
3971                         </accessCondition>
3972                 </xsl:for-each>
3973                 <xsl:for-each select="marc:datafield[@tag=540]">
3974                         <accessCondition type="useAndReproduction">
3975                                 <xsl:call-template name="subfieldSelect">
3976                                         <xsl:with-param name="codes">abcde35</xsl:with-param>
3977                                 </xsl:call-template>
3978                         </accessCondition>
3979                 </xsl:for-each>
3980                 <recordInfo>
3981                         <xsl:for-each select="marc:datafield[@tag=040]">
3982                                 <recordContentSource authority="marcorg">
3983                                         <xsl:value-of select="marc:subfield[@code='a']"></xsl:value-of>
3984                                 </recordContentSource>
3985                         </xsl:for-each>
3986                         <xsl:for-each select="marc:controlfield[@tag=008]">
3987                                 <recordCreationDate encoding="marc">
3988                                         <xsl:value-of select="substring(.,1,6)"></xsl:value-of>
3989                                 </recordCreationDate>
3990                         </xsl:for-each>
3991                         <xsl:for-each select="marc:controlfield[@tag=005]">
3992                                 <recordChangeDate encoding="iso8601">
3993                                         <xsl:value-of select="."></xsl:value-of>
3994                                 </recordChangeDate>
3995                         </xsl:for-each>
3996                         <xsl:for-each select="marc:controlfield[@tag=001]">
3997                                 <recordIdentifier>
3998                                         <xsl:if test="../marc:controlfield[@tag=003]">
3999                                                 <xsl:attribute name="source">
4000                                                         <xsl:value-of select="../marc:controlfield[@tag=003]"></xsl:value-of>
4001                                                 </xsl:attribute>
4002                                         </xsl:if>
4003                                         <xsl:value-of select="."></xsl:value-of>
4004                                 </recordIdentifier>
4005                         </xsl:for-each>
4006                         <xsl:for-each select="marc:datafield[@tag=040]/marc:subfield[@code='b']">
4007                                 <languageOfCataloging>
4008                                         <languageTerm authority="iso639-2b" type="code">
4009                                                 <xsl:value-of select="."></xsl:value-of>
4010                                         </languageTerm>
4011                                 </languageOfCataloging>
4012                         </xsl:for-each>
4013                 </recordInfo>
4014         </xsl:template>
4015         <xsl:template name="displayForm">
4016                 <xsl:for-each select="marc:subfield[@code='c']">
4017                         <displayForm>
4018                                 <xsl:value-of select="."></xsl:value-of>
4019                         </displayForm>
4020                 </xsl:for-each>
4021         </xsl:template>
4022         <xsl:template name="affiliation">
4023                 <xsl:for-each select="marc:subfield[@code='u']">
4024                         <affiliation>
4025                                 <xsl:value-of select="."></xsl:value-of>
4026                         </affiliation>
4027                 </xsl:for-each>
4028         </xsl:template>
4029         <xsl:template name="uri">
4030                 <xsl:for-each select="marc:subfield[@code='u']">
4031                         <xsl:attribute name="xlink:href">
4032                                 <xsl:value-of select="."></xsl:value-of>
4033                         </xsl:attribute>
4034                 </xsl:for-each>
4035                 <xsl:for-each select="marc:subfield[@code='0']">
4036                         <xsl:choose>
4037                                 <xsl:when test="contains(text(), ')')">
4038                                         <xsl:attribute name="xlink:href">
4039                                                 <xsl:value-of select="substring-after(text(), ')')"></xsl:value-of>
4040                                         </xsl:attribute>
4041                                 </xsl:when>
4042                                 <xsl:otherwise>
4043                                         <xsl:attribute name="xlink:href">
4044                                                 <xsl:value-of select="."></xsl:value-of>
4045                                         </xsl:attribute>
4046                                 </xsl:otherwise>
4047                         </xsl:choose>
4048                 </xsl:for-each>
4049         </xsl:template>
4050         <xsl:template name="role">
4051                 <xsl:for-each select="marc:subfield[@code='e']">
4052                         <role>
4053                                 <roleTerm type="text">
4054                                         <xsl:value-of select="."></xsl:value-of>
4055                                 </roleTerm>
4056                         </role>
4057                 </xsl:for-each>
4058                 <xsl:for-each select="marc:subfield[@code='4']">
4059                         <role>
4060                                 <roleTerm authority="marcrelator" type="code">
4061                                         <xsl:value-of select="."></xsl:value-of>
4062                                 </roleTerm>
4063                         </role>
4064                 </xsl:for-each>
4065         </xsl:template>
4066         <xsl:template name="part">
4067                 <xsl:variable name="partNumber">
4068                         <xsl:call-template name="specialSubfieldSelect">
4069                                 <xsl:with-param name="axis">n</xsl:with-param>
4070                                 <xsl:with-param name="anyCodes">n</xsl:with-param>
4071                                 <xsl:with-param name="afterCodes">fgkdlmor</xsl:with-param>
4072                         </xsl:call-template>
4073                 </xsl:variable>
4074                 <xsl:variable name="partName">
4075                         <xsl:call-template name="specialSubfieldSelect">
4076                                 <xsl:with-param name="axis">p</xsl:with-param>
4077                                 <xsl:with-param name="anyCodes">p</xsl:with-param>
4078                                 <xsl:with-param name="afterCodes">fgkdlmor</xsl:with-param>
4079                         </xsl:call-template>
4080                 </xsl:variable>
4081                 <xsl:if test="string-length(normalize-space($partNumber))">
4082                         <partNumber>
4083                                 <xsl:call-template name="chopPunctuation">
4084                                         <xsl:with-param name="chopString" select="$partNumber"></xsl:with-param>
4085                                 </xsl:call-template>
4086                         </partNumber>
4087                 </xsl:if>
4088                 <xsl:if test="string-length(normalize-space($partName))">
4089                         <partName>
4090                                 <xsl:call-template name="chopPunctuation">
4091                                         <xsl:with-param name="chopString" select="$partName"></xsl:with-param>
4092                                 </xsl:call-template>
4093                         </partName>
4094                 </xsl:if>
4095         </xsl:template>
4096         <xsl:template name="relatedPart">
4097                 <xsl:if test="@tag=773">
4098                         <xsl:for-each select="marc:subfield[@code='g']">
4099                                 <part>
4100                                         <text>
4101                                                 <xsl:value-of select="."></xsl:value-of>
4102                                         </text>
4103                                 </part>
4104                         </xsl:for-each>
4105                         <xsl:for-each select="marc:subfield[@code='q']">
4106                                 <part>
4107                                         <xsl:call-template name="parsePart"></xsl:call-template>
4108                                 </part>
4109                         </xsl:for-each>
4110                 </xsl:if>
4111         </xsl:template>
4112         <xsl:template name="relatedPartNumName">
4113                 <xsl:variable name="partNumber">
4114                         <xsl:call-template name="specialSubfieldSelect">
4115                                 <xsl:with-param name="axis">g</xsl:with-param>
4116                                 <xsl:with-param name="anyCodes">g</xsl:with-param>
4117                                 <xsl:with-param name="afterCodes">pst</xsl:with-param>
4118                         </xsl:call-template>
4119                 </xsl:variable>
4120                 <xsl:variable name="partName">
4121                         <xsl:call-template name="specialSubfieldSelect">
4122                                 <xsl:with-param name="axis">p</xsl:with-param>
4123                                 <xsl:with-param name="anyCodes">p</xsl:with-param>
4124                                 <xsl:with-param name="afterCodes">fgkdlmor</xsl:with-param>
4125                         </xsl:call-template>
4126                 </xsl:variable>
4127                 <xsl:if test="string-length(normalize-space($partNumber))">
4128                         <partNumber>
4129                                 <xsl:value-of select="$partNumber"></xsl:value-of>
4130                         </partNumber>
4131                 </xsl:if>
4132                 <xsl:if test="string-length(normalize-space($partName))">
4133                         <partName>
4134                                 <xsl:value-of select="$partName"></xsl:value-of>
4135                         </partName>
4136                 </xsl:if>
4137         </xsl:template>
4138         <xsl:template name="relatedName">
4139                 <xsl:for-each select="marc:subfield[@code='a']">
4140                         <name>
4141                                 <namePart>
4142                                         <xsl:value-of select="."></xsl:value-of>
4143                                 </namePart>
4144                         </name>
4145                 </xsl:for-each>
4146         </xsl:template>
4147         <xsl:template name="relatedForm">
4148                 <xsl:for-each select="marc:subfield[@code='h']">
4149                         <physicalDescription>
4150                                 <form>
4151                                         <xsl:value-of select="."></xsl:value-of>
4152                                 </form>
4153                         </physicalDescription>
4154                 </xsl:for-each>
4155         </xsl:template>
4156         <xsl:template name="relatedExtent">
4157                 <xsl:for-each select="marc:subfield[@code='h']">
4158                         <physicalDescription>
4159                                 <extent>
4160                                         <xsl:value-of select="."></xsl:value-of>
4161                                 </extent>
4162                         </physicalDescription>
4163                 </xsl:for-each>
4164         </xsl:template>
4165         <xsl:template name="relatedNote">
4166                 <xsl:for-each select="marc:subfield[@code='n']">
4167                         <note>
4168                                 <xsl:value-of select="."></xsl:value-of>
4169                         </note>
4170                 </xsl:for-each>
4171         </xsl:template>
4172         <xsl:template name="relatedSubject">
4173                 <xsl:for-each select="marc:subfield[@code='j']">
4174                         <subject>
4175                                 <temporal encoding="iso8601">
4176                                         <xsl:call-template name="chopPunctuation">
4177                                                 <xsl:with-param name="chopString" select="."></xsl:with-param>
4178                                         </xsl:call-template>
4179                                 </temporal>
4180                         </subject>
4181                 </xsl:for-each>
4182         </xsl:template>
4183         <xsl:template name="relatedIdentifierISSN">
4184                 <xsl:for-each select="marc:subfield[@code='x']">
4185                         <identifier type="issn">
4186                                 <xsl:value-of select="."></xsl:value-of>
4187                         </identifier>
4188                 </xsl:for-each>
4189         </xsl:template>
4190         <xsl:template name="relatedIdentifierLocal">
4191                 <xsl:for-each select="marc:subfield[@code='w']">
4192                         <identifier type="local">
4193                                 <xsl:value-of select="."></xsl:value-of>
4194                         </identifier>
4195                 </xsl:for-each>
4196         </xsl:template>
4197         <xsl:template name="relatedIdentifier">
4198                 <xsl:for-each select="marc:subfield[@code='o']">
4199                         <identifier>
4200                                 <xsl:value-of select="."></xsl:value-of>
4201                         </identifier>
4202                 </xsl:for-each>
4203         </xsl:template>
4204         <xsl:template name="relatedItem76X-78X">
4205                 <xsl:call-template name="displayLabel"></xsl:call-template>
4206                 <xsl:call-template name="relatedTitle76X-78X"></xsl:call-template>
4207                 <xsl:call-template name="relatedName"></xsl:call-template>
4208                 <xsl:call-template name="relatedOriginInfo"></xsl:call-template>
4209                 <xsl:call-template name="relatedLanguage"></xsl:call-template>
4210                 <xsl:call-template name="relatedExtent"></xsl:call-template>
4211                 <xsl:call-template name="relatedNote"></xsl:call-template>
4212                 <xsl:call-template name="relatedSubject"></xsl:call-template>
4213                 <xsl:call-template name="relatedIdentifier"></xsl:call-template>
4214                 <xsl:call-template name="relatedIdentifierISSN"></xsl:call-template>
4215                 <xsl:call-template name="relatedIdentifierLocal"></xsl:call-template>
4216                 <xsl:call-template name="relatedPart"></xsl:call-template>
4217         </xsl:template>
4218         <xsl:template name="subjectGeographicZ">
4219                 <geographic>
4220                         <xsl:call-template name="chopPunctuation">
4221                                 <xsl:with-param name="chopString" select="."></xsl:with-param>
4222                         </xsl:call-template>
4223                 </geographic>
4224         </xsl:template>
4225         <xsl:template name="subjectTemporalY">
4226                 <temporal>
4227                         <xsl:call-template name="chopPunctuation">
4228                                 <xsl:with-param name="chopString" select="."></xsl:with-param>
4229                         </xsl:call-template>
4230                 </temporal>
4231         </xsl:template>
4232         <xsl:template name="subjectTopic">
4233                 <topic>
4234                         <xsl:call-template name="chopPunctuation">
4235                                 <xsl:with-param name="chopString" select="."></xsl:with-param>
4236                         </xsl:call-template>
4237                 </topic>
4238         </xsl:template>
4239         <!-- 3.2 change tmee 6xx $v genre -->
4240         <xsl:template name="subjectGenre">
4241                 <genre>
4242                         <xsl:call-template name="chopPunctuation">
4243                                 <xsl:with-param name="chopString" select="."></xsl:with-param>
4244                         </xsl:call-template>
4245                 </genre>
4246         </xsl:template>
4247
4248         <xsl:template name="nameABCDN">
4249                 <xsl:for-each select="marc:subfield[@code='a']">
4250                         <namePart>
4251                                 <xsl:call-template name="chopPunctuation">
4252                                         <xsl:with-param name="chopString" select="."></xsl:with-param>
4253                                 </xsl:call-template>
4254                         </namePart>
4255                 </xsl:for-each>
4256                 <xsl:for-each select="marc:subfield[@code='b']">
4257                         <namePart>
4258                                 <xsl:value-of select="."></xsl:value-of>
4259                         </namePart>
4260                 </xsl:for-each>
4261                 <xsl:if test="marc:subfield[@code='c'] or marc:subfield[@code='d'] or marc:subfield[@code='n']">
4262                         <namePart>
4263                                 <xsl:call-template name="subfieldSelect">
4264                                         <xsl:with-param name="codes">cdn</xsl:with-param>
4265                                 </xsl:call-template>
4266                         </namePart>
4267                 </xsl:if>
4268         </xsl:template>
4269         <xsl:template name="nameABCDQ">
4270                 <namePart>
4271                         <xsl:call-template name="chopPunctuation">
4272                                 <xsl:with-param name="chopString">
4273                                         <xsl:call-template name="subfieldSelect">
4274                                                 <xsl:with-param name="codes">aq</xsl:with-param>
4275                                         </xsl:call-template>
4276                                 </xsl:with-param>
4277                                 <xsl:with-param name="punctuation">
4278                                         <xsl:text>:,;/ </xsl:text>
4279                                 </xsl:with-param>
4280                         </xsl:call-template>
4281                 </namePart>
4282                 <xsl:call-template name="termsOfAddress"></xsl:call-template>
4283                 <xsl:call-template name="nameDate"></xsl:call-template>
4284         </xsl:template>
4285         <xsl:template name="nameACDEQ">
4286                 <namePart>
4287                         <xsl:call-template name="subfieldSelect">
4288                                 <xsl:with-param name="codes">acdeq</xsl:with-param>
4289                         </xsl:call-template>
4290                 </namePart>
4291         </xsl:template>
4292         <xsl:template name="constituentOrRelatedType">
4293                 <xsl:if test="@ind2=2">
4294                         <xsl:attribute name="type">constituent</xsl:attribute>
4295                 </xsl:if>
4296         </xsl:template>
4297         <xsl:template name="relatedTitle">
4298                 <xsl:for-each select="marc:subfield[@code='t']">
4299                         <titleInfo>
4300                                 <title>
4301                                         <xsl:call-template name="chopPunctuation">
4302                                                 <xsl:with-param name="chopString">
4303                                                         <xsl:value-of select="."></xsl:value-of>
4304                                                 </xsl:with-param>
4305                                         </xsl:call-template>
4306                                 </title>
4307                         </titleInfo>
4308                 </xsl:for-each>
4309         </xsl:template>
4310         <xsl:template name="relatedTitle76X-78X">
4311                 <xsl:for-each select="marc:subfield[@code='t']">
4312                         <titleInfo>
4313                                 <title>
4314                                         <xsl:call-template name="chopPunctuation">
4315                                                 <xsl:with-param name="chopString">
4316                                                         <xsl:value-of select="."></xsl:value-of>
4317                                                 </xsl:with-param>
4318                                         </xsl:call-template>
4319                                 </title>
4320                                 <xsl:if test="marc:datafield[@tag!=773]and marc:subfield[@code='g']">
4321                                         <xsl:call-template name="relatedPartNumName"></xsl:call-template>
4322                                 </xsl:if>
4323                         </titleInfo>
4324                 </xsl:for-each>
4325                 <xsl:for-each select="marc:subfield[@code='p']">
4326                         <titleInfo type="abbreviated">
4327                                 <title>
4328                                         <xsl:call-template name="chopPunctuation">
4329                                                 <xsl:with-param name="chopString">
4330                                                         <xsl:value-of select="."></xsl:value-of>
4331                                                 </xsl:with-param>
4332                                         </xsl:call-template>
4333                                 </title>
4334                                 <xsl:if test="marc:datafield[@tag!=773]and marc:subfield[@code='g']">
4335                                         <xsl:call-template name="relatedPartNumName"></xsl:call-template>
4336                                 </xsl:if>
4337                         </titleInfo>
4338                 </xsl:for-each>
4339                 <xsl:for-each select="marc:subfield[@code='s']">
4340                         <titleInfo type="uniform">
4341                                 <title>
4342                                         <xsl:call-template name="chopPunctuation">
4343                                                 <xsl:with-param name="chopString">
4344                                                         <xsl:value-of select="."></xsl:value-of>
4345                                                 </xsl:with-param>
4346                                         </xsl:call-template>
4347                                 </title>
4348                                 <xsl:if test="marc:datafield[@tag!=773]and marc:subfield[@code='g']">
4349                                         <xsl:call-template name="relatedPartNumName"></xsl:call-template>
4350                                 </xsl:if>
4351                         </titleInfo>
4352                 </xsl:for-each>
4353         </xsl:template>
4354         <xsl:template name="relatedOriginInfo">
4355                 <xsl:if test="marc:subfield[@code='b' or @code='d'] or marc:subfield[@code='f']">
4356                         <originInfo>
4357                                 <xsl:if test="@tag=775">
4358                                         <xsl:for-each select="marc:subfield[@code='f']">
4359                                                 <place>
4360                                                         <placeTerm>
4361                                                                 <xsl:attribute name="type">code</xsl:attribute>
4362                                                                 <xsl:attribute name="authority">marcgac</xsl:attribute>
4363                                                                 <xsl:value-of select="."></xsl:value-of>
4364                                                         </placeTerm>
4365                                                 </place>
4366                                         </xsl:for-each>
4367                                 </xsl:if>
4368                                 <xsl:for-each select="marc:subfield[@code='d']">
4369                                         <publisher>
4370                                                 <xsl:value-of select="."></xsl:value-of>
4371                                         </publisher>
4372                                 </xsl:for-each>
4373                                 <xsl:for-each select="marc:subfield[@code='b']">
4374                                         <edition>
4375                                                 <xsl:value-of select="."></xsl:value-of>
4376                                         </edition>
4377                                 </xsl:for-each>
4378                         </originInfo>
4379                 </xsl:if>
4380         </xsl:template>
4381         <xsl:template name="relatedLanguage">
4382                 <xsl:for-each select="marc:subfield[@code='e']">
4383                         <xsl:call-template name="getLanguage">
4384                                 <xsl:with-param name="langString">
4385                                         <xsl:value-of select="."></xsl:value-of>
4386                                 </xsl:with-param>
4387                         </xsl:call-template>
4388                 </xsl:for-each>
4389         </xsl:template>
4390         <xsl:template name="nameDate">
4391                 <xsl:for-each select="marc:subfield[@code='d']">
4392                         <namePart type="date">
4393                                 <xsl:call-template name="chopPunctuation">
4394                                         <xsl:with-param name="chopString" select="."></xsl:with-param>
4395                                 </xsl:call-template>
4396                         </namePart>
4397                 </xsl:for-each>
4398         </xsl:template>
4399         <xsl:template name="subjectAuthority">
4400                 <xsl:if test="@ind2!=4">
4401                         <xsl:if test="@ind2!=' '">
4402                                 <xsl:if test="@ind2!=8">
4403                                         <xsl:if test="@ind2!=9">
4404                                                 <xsl:attribute name="authority">
4405                                                         <xsl:choose>
4406                                                                 <xsl:when test="@ind2=0">lcsh</xsl:when>
4407                                                                 <xsl:when test="@ind2=1">lcshac</xsl:when>
4408                                                                 <xsl:when test="@ind2=2">mesh</xsl:when>
4409                                                                 <!-- 1/04 fix -->
4410                                                                 <xsl:when test="@ind2=3">nal</xsl:when>
4411                                                                 <xsl:when test="@ind2=5">csh</xsl:when>
4412                                                                 <xsl:when test="@ind2=6">rvm</xsl:when>
4413                                                                 <xsl:when test="@ind2=7">
4414                                                                         <xsl:value-of select="marc:subfield[@code='2']"></xsl:value-of>
4415                                                                 </xsl:when>
4416                                                         </xsl:choose>
4417                                                 </xsl:attribute>
4418                                         </xsl:if>
4419                                 </xsl:if>
4420                         </xsl:if>
4421                 </xsl:if>
4422         </xsl:template>
4423         <xsl:template name="subjectAnyOrder">
4424                 <xsl:for-each select="marc:subfield[@code='v' or @code='x' or @code='y' or @code='z']">
4425                         <xsl:choose>
4426                                 <xsl:when test="@code='v'">
4427                                         <xsl:call-template name="subjectGenre"></xsl:call-template>
4428                                 </xsl:when>
4429                                 <xsl:when test="@code='x'">
4430                                         <xsl:call-template name="subjectTopic"></xsl:call-template>
4431                                 </xsl:when>
4432                                 <xsl:when test="@code='y'">
4433                                         <xsl:call-template name="subjectTemporalY"></xsl:call-template>
4434                                 </xsl:when>
4435                                 <xsl:when test="@code='z'">
4436                                         <xsl:call-template name="subjectGeographicZ"></xsl:call-template>
4437                                 </xsl:when>
4438                         </xsl:choose>
4439                 </xsl:for-each>
4440         </xsl:template>
4441         <xsl:template name="specialSubfieldSelect">
4442                 <xsl:param name="anyCodes"></xsl:param>
4443                 <xsl:param name="axis"></xsl:param>
4444                 <xsl:param name="beforeCodes"></xsl:param>
4445                 <xsl:param name="afterCodes"></xsl:param>
4446                 <xsl:variable name="str">
4447                         <xsl:for-each select="marc:subfield">
4448                                 <xsl:if 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])">
4449                                         <xsl:value-of select="text()"></xsl:value-of>
4450                                         <xsl:text> </xsl:text>
4451                                 </xsl:if>
4452                         </xsl:for-each>
4453                 </xsl:variable>
4454                 <xsl:value-of select="substring($str,1,string-length($str)-1)"></xsl:value-of>
4455         </xsl:template>
4456
4457         <!-- 3.2 change tmee 6xx $v genre -->
4458         <xsl:template match="marc:datafield[@tag=600]">
4459                 <subject>
4460                         <xsl:call-template name="subjectAuthority"></xsl:call-template>
4461                         <name type="personal">
4462                                 <xsl:call-template name="uri" />
4463                                 <namePart>
4464                                         <xsl:call-template name="chopPunctuation">
4465                                                 <xsl:with-param name="chopString">
4466                                                         <xsl:call-template name="subfieldSelect">
4467                                                                 <xsl:with-param name="codes">aq</xsl:with-param>
4468                                                         </xsl:call-template>
4469                                                 </xsl:with-param>
4470                                         </xsl:call-template>
4471                                 </namePart>
4472                                 <xsl:call-template name="termsOfAddress"></xsl:call-template>
4473                                 <xsl:call-template name="nameDate"></xsl:call-template>
4474                                 <xsl:call-template name="affiliation"></xsl:call-template>
4475                                 <xsl:call-template name="role"></xsl:call-template>
4476                         </name>
4477                         <xsl:call-template name="subjectAnyOrder"></xsl:call-template>
4478                 </subject>
4479         </xsl:template>
4480         <xsl:template match="marc:datafield[@tag=610]">
4481                 <subject>
4482                         <xsl:call-template name="subjectAuthority"></xsl:call-template>
4483                         <name type="corporate">
4484                                 <xsl:call-template name="uri" />
4485                                 <xsl:for-each select="marc:subfield[@code='a']">
4486                                         <namePart>
4487                                                 <xsl:value-of select="."></xsl:value-of>
4488                                         </namePart>
4489                                 </xsl:for-each>
4490                                 <xsl:for-each select="marc:subfield[@code='b']">
4491                                         <namePart>
4492                                                 <xsl:value-of select="."></xsl:value-of>
4493                                         </namePart>
4494                                 </xsl:for-each>
4495                                 <xsl:if test="marc:subfield[@code='c' or @code='d' or @code='n' or @code='p']">
4496                                         <namePart>
4497                                                 <xsl:call-template name="subfieldSelect">
4498                                                         <xsl:with-param name="codes">cdnp</xsl:with-param>
4499                                                 </xsl:call-template>
4500                                         </namePart>
4501                                 </xsl:if>
4502                                 <xsl:call-template name="role"></xsl:call-template>
4503                         </name>
4504                         <xsl:call-template name="subjectAnyOrder"></xsl:call-template>
4505                 </subject>
4506         </xsl:template>
4507         <xsl:template match="marc:datafield[@tag=611]">
4508                 <subject>
4509                         <xsl:call-template name="subjectAuthority"></xsl:call-template>
4510                         <name type="conference">
4511                                 <xsl:call-template name="uri" />
4512                                 <namePart>
4513                                         <xsl:call-template name="subfieldSelect">
4514                                                 <xsl:with-param name="codes">abcdeqnp</xsl:with-param>
4515                                         </xsl:call-template>
4516                                 </namePart>
4517                                 <xsl:for-each select="marc:subfield[@code='4']">
4518                                         <role>
4519                                                 <roleTerm authority="marcrelator" type="code">
4520                                                         <xsl:value-of select="."></xsl:value-of>
4521                                                 </roleTerm>
4522                                         </role>
4523                                 </xsl:for-each>
4524                         </name>
4525                         <xsl:call-template name="subjectAnyOrder"></xsl:call-template>
4526                 </subject>
4527         </xsl:template>
4528         <xsl:template match="marc:datafield[@tag=630]">
4529                 <subject>
4530                         <xsl:call-template name="subjectAuthority"></xsl:call-template>
4531                         <xsl:variable name="titleChop">
4532                                 <xsl:call-template name="chopPunctuation">
4533                                         <xsl:with-param name="chopString">
4534                                                 <xsl:call-template name="subfieldSelect">
4535                                                         <xsl:with-param name="codes">adfhklor</xsl:with-param>
4536                                                 </xsl:call-template>
4537                                         </xsl:with-param>
4538                                 </xsl:call-template>
4539                         </xsl:variable>
4540                         <titleInfo>
4541                                 <title>
4542                                         <xsl:value-of select="$titleChop" />
4543                                 </title>
4544                                 <xsl:call-template name="part"></xsl:call-template>
4545                         </titleInfo>
4546                         <titleInfo type="nfi">
4547                                 <xsl:choose>
4548                                         <xsl:when test="@ind1>0">
4549                                                 <nonSort>
4550                                                         <xsl:value-of select="substring($titleChop,1,@ind1)"/>
4551                                                 </nonSort>
4552                                                 <title>
4553                                                         <xsl:value-of select="substring($titleChop,@ind1+1)"/>
4554                                                 </title>
4555                                                 <xsl:call-template name="part"/>
4556                                         </xsl:when>
4557                                         <xsl:otherwise>
4558                                                 <title>
4559                                                         <xsl:value-of select="$titleChop" />
4560                                                 </title>
4561                                         </xsl:otherwise>
4562                                 </xsl:choose>
4563                                 <xsl:call-template name="part"></xsl:call-template>
4564                         </titleInfo>
4565                         <xsl:call-template name="subjectAnyOrder"></xsl:call-template>
4566                 </subject>
4567         </xsl:template>
4568         <xsl:template match="marc:datafield[@tag=650]">
4569                 <subject>
4570                         <xsl:call-template name="subjectAuthority"></xsl:call-template>
4571                         <topic>
4572                                 <xsl:call-template name="uri" />
4573                                 <xsl:call-template name="chopPunctuation">
4574                                         <xsl:with-param name="chopString">
4575                                                 <xsl:call-template name="subfieldSelect">
4576                                                         <xsl:with-param name="codes">abcd</xsl:with-param>
4577                                                 </xsl:call-template>
4578                                         </xsl:with-param>
4579                                 </xsl:call-template>
4580                         </topic>
4581                         <xsl:call-template name="subjectAnyOrder"></xsl:call-template>
4582                 </subject>
4583         </xsl:template>
4584         <xsl:template match="marc:datafield[@tag=651]">
4585                 <subject>
4586                         <xsl:call-template name="subjectAuthority"></xsl:call-template>
4587                         <xsl:for-each select="marc:subfield[@code='a']">
4588                                 <geographic>
4589                                         <xsl:call-template name="uri" />
4590                                         <xsl:call-template name="chopPunctuation">
4591                                                 <xsl:with-param name="chopString" select="."></xsl:with-param>
4592                                         </xsl:call-template>
4593                                 </geographic>
4594                         </xsl:for-each>
4595                         <xsl:call-template name="subjectAnyOrder"></xsl:call-template>
4596                 </subject>
4597         </xsl:template>
4598         <xsl:template match="marc:datafield[@tag=653]">
4599                 <subject>
4600                         <xsl:for-each select="marc:subfield[@code='a']">
4601                                 <topic>
4602                                         <xsl:call-template name="uri" />
4603                                         <xsl:value-of select="."></xsl:value-of>
4604                                 </topic>
4605                         </xsl:for-each>
4606                 </subject>
4607         </xsl:template>
4608         <xsl:template match="marc:datafield[@tag=656]">
4609                 <subject>
4610                         <xsl:if test="marc:subfield[@code=2]">
4611                                 <xsl:attribute name="authority">
4612                                         <xsl:value-of select="marc:subfield[@code=2]"></xsl:value-of>
4613                                 </xsl:attribute>
4614                         </xsl:if>
4615                         <occupation>
4616                                 <xsl:call-template name="uri" />
4617                                 <xsl:call-template name="chopPunctuation">
4618                                         <xsl:with-param name="chopString">
4619                                                 <xsl:value-of select="marc:subfield[@code='a']"></xsl:value-of>
4620                                         </xsl:with-param>
4621                                 </xsl:call-template>
4622                         </occupation>
4623                 </subject>
4624         </xsl:template>
4625         <xsl:template name="termsOfAddress">
4626                 <xsl:if test="marc:subfield[@code='b' or @code='c']">
4627                         <namePart type="termsOfAddress">
4628                                 <xsl:call-template name="chopPunctuation">
4629                                         <xsl:with-param name="chopString">
4630                                                 <xsl:call-template name="subfieldSelect">
4631                                                         <xsl:with-param name="codes">bc</xsl:with-param>
4632                                                 </xsl:call-template>
4633                                         </xsl:with-param>
4634                                 </xsl:call-template>
4635                         </namePart>
4636                 </xsl:if>
4637         </xsl:template>
4638         <xsl:template name="displayLabel">
4639                 <xsl:if test="marc:subfield[@code='i']">
4640                         <xsl:attribute name="displayLabel">
4641                                 <xsl:value-of select="marc:subfield[@code='i']"></xsl:value-of>
4642                         </xsl:attribute>
4643                 </xsl:if>
4644                 <xsl:if test="marc:subfield[@code='3']">
4645                         <xsl:attribute name="displayLabel">
4646                                 <xsl:value-of select="marc:subfield[@code='3']"></xsl:value-of>
4647                         </xsl:attribute>
4648                 </xsl:if>
4649         </xsl:template>
4650         <xsl:template name="isInvalid">
4651                 <xsl:param name="type"/>
4652                 <xsl:if test="marc:subfield[@code='z'] or marc:subfield[@code='y']">
4653                         <identifier>
4654                                 <xsl:attribute name="type">
4655                                         <xsl:value-of select="$type"/>
4656                                 </xsl:attribute>
4657                                 <xsl:attribute name="invalid">
4658                                         <xsl:text>yes</xsl:text>
4659                                 </xsl:attribute>
4660                                 <xsl:if test="marc:subfield[@code='z']">
4661                                         <xsl:value-of select="marc:subfield[@code='z']"/>
4662                                 </xsl:if>
4663                                 <xsl:if test="marc:subfield[@code='y']">
4664                                         <xsl:value-of select="marc:subfield[@code='y']"/>
4665                                 </xsl:if>
4666                         </identifier>
4667                 </xsl:if>
4668         </xsl:template>
4669         <xsl:template name="subtitle">
4670                 <xsl:if test="marc:subfield[@code='b']">
4671                         <subTitle>
4672                                 <xsl:call-template name="chopPunctuation">
4673                                         <xsl:with-param name="chopString">
4674                                                 <xsl:value-of select="marc:subfield[@code='b']"/>
4675                                                 <!--<xsl:call-template name="subfieldSelect">
4676                                                         <xsl:with-param name="codes">b</xsl:with-param>
4677                                                 </xsl:call-template>-->
4678                                         </xsl:with-param>
4679                                 </xsl:call-template>
4680                         </subTitle>
4681                 </xsl:if>
4682         </xsl:template>
4683         <xsl:template name="script">
4684                 <xsl:param name="scriptCode"></xsl:param>
4685                 <xsl:attribute name="script">
4686                         <xsl:choose>
4687                                 <xsl:when test="$scriptCode='(3'">Arabic</xsl:when>
4688                                 <xsl:when test="$scriptCode='(B'">Latin</xsl:when>
4689                                 <xsl:when test="$scriptCode='$1'">Chinese, Japanese, Korean</xsl:when>
4690                                 <xsl:when test="$scriptCode='(N'">Cyrillic</xsl:when>
4691                                 <xsl:when test="$scriptCode='(2'">Hebrew</xsl:when>
4692                                 <xsl:when test="$scriptCode='(S'">Greek</xsl:when>
4693                         </xsl:choose>
4694                 </xsl:attribute>
4695         </xsl:template>
4696         <xsl:template name="parsePart">
4697                 <!-- assumes 773$q= 1:2:3<4
4698                      with up to 3 levels and one optional start page
4699                 -->
4700                 <xsl:variable name="level1">
4701                         <xsl:choose>
4702                                 <xsl:when test="contains(text(),':')">
4703                                         <!-- 1:2 -->
4704                                         <xsl:value-of select="substring-before(text(),':')"></xsl:value-of>
4705                                 </xsl:when>
4706                                 <xsl:when test="not(contains(text(),':'))">
4707                                         <!-- 1 or 1<3 -->
4708                                         <xsl:if test="contains(text(),'&lt;')">
4709                                                 <!-- 1<3 -->
4710                                                 <xsl:value-of select="substring-before(text(),'&lt;')"></xsl:value-of>
4711                                         </xsl:if>
4712                                         <xsl:if test="not(contains(text(),'&lt;'))">
4713                                                 <!-- 1 -->
4714                                                 <xsl:value-of select="text()"></xsl:value-of>
4715                                         </xsl:if>
4716                                 </xsl:when>
4717                         </xsl:choose>
4718                 </xsl:variable>
4719                 <xsl:variable name="sici2">
4720                         <xsl:choose>
4721                                 <xsl:when test="starts-with(substring-after(text(),$level1),':')">
4722                                         <xsl:value-of select="substring(substring-after(text(),$level1),2)"></xsl:value-of>
4723                                 </xsl:when>
4724                                 <xsl:otherwise>
4725                                         <xsl:value-of select="substring-after(text(),$level1)"></xsl:value-of>
4726                                 </xsl:otherwise>
4727                         </xsl:choose>
4728                 </xsl:variable>
4729                 <xsl:variable name="level2">
4730                         <xsl:choose>
4731                                 <xsl:when test="contains($sici2,':')">
4732                                         <!--  2:3<4  -->
4733                                         <xsl:value-of select="substring-before($sici2,':')"></xsl:value-of>
4734                                 </xsl:when>
4735                                 <xsl:when test="contains($sici2,'&lt;')">
4736                                         <!-- 1: 2<4 -->
4737                                         <xsl:value-of select="substring-before($sici2,'&lt;')"></xsl:value-of>
4738                                 </xsl:when>
4739                                 <xsl:otherwise>
4740                                         <xsl:value-of select="$sici2"></xsl:value-of>
4741                                         <!-- 1:2 -->
4742                                 </xsl:otherwise>
4743                         </xsl:choose>
4744                 </xsl:variable>
4745                 <xsl:variable name="sici3">
4746                         <xsl:choose>
4747                                 <xsl:when test="starts-with(substring-after($sici2,$level2),':')">
4748                                         <xsl:value-of select="substring(substring-after($sici2,$level2),2)"></xsl:value-of>
4749                                 </xsl:when>
4750                                 <xsl:otherwise>
4751                                         <xsl:value-of select="substring-after($sici2,$level2)"></xsl:value-of>
4752                                 </xsl:otherwise>
4753                         </xsl:choose>
4754                 </xsl:variable>
4755                 <xsl:variable name="level3">
4756                         <xsl:choose>
4757                                 <xsl:when test="contains($sici3,'&lt;')">
4758                                         <!-- 2<4 -->
4759                                         <xsl:value-of select="substring-before($sici3,'&lt;')"></xsl:value-of>
4760                                 </xsl:when>
4761                                 <xsl:otherwise>
4762                                         <xsl:value-of select="$sici3"></xsl:value-of>
4763                                         <!-- 3 -->
4764                                 </xsl:otherwise>
4765                         </xsl:choose>
4766                 </xsl:variable>
4767                 <xsl:variable name="page">
4768                         <xsl:if test="contains(text(),'&lt;')">
4769                                 <xsl:value-of select="substring-after(text(),'&lt;')"></xsl:value-of>
4770                         </xsl:if>
4771                 </xsl:variable>
4772                 <xsl:if test="$level1">
4773                         <detail level="1">
4774                                 <number>
4775                                         <xsl:value-of select="$level1"></xsl:value-of>
4776                                 </number>
4777                         </detail>
4778                 </xsl:if>
4779                 <xsl:if test="$level2">
4780                         <detail level="2">
4781                                 <number>
4782                                         <xsl:value-of select="$level2"></xsl:value-of>
4783                                 </number>
4784                         </detail>
4785                 </xsl:if>
4786                 <xsl:if test="$level3">
4787                         <detail level="3">
4788                                 <number>
4789                                         <xsl:value-of select="$level3"></xsl:value-of>
4790                                 </number>
4791                         </detail>
4792                 </xsl:if>
4793                 <xsl:if test="$page">
4794                         <extent unit="page">
4795                                 <start>
4796                                         <xsl:value-of select="$page"></xsl:value-of>
4797                                 </start>
4798                         </extent>
4799                 </xsl:if>
4800         </xsl:template>
4801         <xsl:template name="getLanguage">
4802                 <xsl:param name="langString"></xsl:param>
4803                 <xsl:param name="controlField008-35-37"></xsl:param>
4804                 <xsl:variable name="length" select="string-length($langString)"></xsl:variable>
4805                 <xsl:choose>
4806                         <xsl:when test="$length=0"></xsl:when>
4807                         <xsl:when test="$controlField008-35-37=substring($langString,1,3)">
4808                                 <xsl:call-template name="getLanguage">
4809                                         <xsl:with-param name="langString" select="substring($langString,4,$length)"></xsl:with-param>
4810                                         <xsl:with-param name="controlField008-35-37" select="$controlField008-35-37"></xsl:with-param>
4811                                 </xsl:call-template>
4812                         </xsl:when>
4813                         <xsl:otherwise>
4814                                 <language>
4815                                         <languageTerm authority="iso639-2b" type="code">
4816                                                 <xsl:value-of select="substring($langString,1,3)"></xsl:value-of>
4817                                         </languageTerm>
4818                                 </language>
4819                                 <xsl:call-template name="getLanguage">
4820                                         <xsl:with-param name="langString" select="substring($langString,4,$length)"></xsl:with-param>
4821                                         <xsl:with-param name="controlField008-35-37" select="$controlField008-35-37"></xsl:with-param>
4822                                 </xsl:call-template>
4823                         </xsl:otherwise>
4824                 </xsl:choose>
4825         </xsl:template>
4826         <xsl:template name="isoLanguage">
4827                 <xsl:param name="currentLanguage"></xsl:param>
4828                 <xsl:param name="usedLanguages"></xsl:param>
4829                 <xsl:param name="remainingLanguages"></xsl:param>
4830                 <xsl:choose>
4831                         <xsl:when test="string-length($currentLanguage)=0"></xsl:when>
4832                         <xsl:when test="not(contains($usedLanguages, $currentLanguage))">
4833                                 <language>
4834                                         <xsl:if test="@code!='a'">
4835                                                 <xsl:attribute name="objectPart">
4836                                                         <xsl:choose>
4837                                                                 <xsl:when test="@code='b'">summary or subtitle</xsl:when>
4838                                                                 <xsl:when test="@code='d'">sung or spoken text</xsl:when>
4839                                                                 <xsl:when test="@code='e'">libretto</xsl:when>
4840                                                                 <xsl:when test="@code='f'">table of contents</xsl:when>
4841                                                                 <xsl:when test="@code='g'">accompanying material</xsl:when>
4842                                                                 <xsl:when test="@code='h'">translation</xsl:when>
4843                                                         </xsl:choose>
4844                                                 </xsl:attribute>
4845                                         </xsl:if>
4846                                         <languageTerm authority="iso639-2b" type="code">
4847                                                 <xsl:value-of select="$currentLanguage"></xsl:value-of>
4848                                         </languageTerm>
4849                                 </language>
4850                                 <xsl:call-template name="isoLanguage">
4851                                         <xsl:with-param name="currentLanguage">
4852                                                 <xsl:value-of select="substring($remainingLanguages,1,3)"></xsl:value-of>
4853                                         </xsl:with-param>
4854                                         <xsl:with-param name="usedLanguages">
4855                                                 <xsl:value-of select="concat($usedLanguages,$currentLanguage)"></xsl:value-of>
4856                                         </xsl:with-param>
4857                                         <xsl:with-param name="remainingLanguages">
4858                                                 <xsl:value-of select="substring($remainingLanguages,4,string-length($remainingLanguages))"></xsl:value-of>
4859                                         </xsl:with-param>
4860                                 </xsl:call-template>
4861                         </xsl:when>
4862                         <xsl:otherwise>
4863                                 <xsl:call-template name="isoLanguage">
4864                                         <xsl:with-param name="currentLanguage">
4865                                                 <xsl:value-of select="substring($remainingLanguages,1,3)"></xsl:value-of>
4866                                         </xsl:with-param>
4867                                         <xsl:with-param name="usedLanguages">
4868                                                 <xsl:value-of select="concat($usedLanguages,$currentLanguage)"></xsl:value-of>
4869                                         </xsl:with-param>
4870                                         <xsl:with-param name="remainingLanguages">
4871                                                 <xsl:value-of select="substring($remainingLanguages,4,string-length($remainingLanguages))"></xsl:value-of>
4872                                         </xsl:with-param>
4873                                 </xsl:call-template>
4874                         </xsl:otherwise>
4875                 </xsl:choose>
4876         </xsl:template>
4877         <xsl:template name="chopBrackets">
4878                 <xsl:param name="chopString"></xsl:param>
4879                 <xsl:variable name="string">
4880                         <xsl:call-template name="chopPunctuation">
4881                                 <xsl:with-param name="chopString" select="$chopString"></xsl:with-param>
4882                         </xsl:call-template>
4883                 </xsl:variable>
4884                 <xsl:if test="substring($string, 1,1)='['">
4885                         <xsl:value-of select="substring($string,2, string-length($string)-2)"></xsl:value-of>
4886                 </xsl:if>
4887                 <xsl:if test="substring($string, 1,1)!='['">
4888                         <xsl:value-of select="$string"></xsl:value-of>
4889                 </xsl:if>
4890         </xsl:template>
4891         <xsl:template name="rfcLanguages">
4892                 <xsl:param name="nodeNum"></xsl:param>
4893                 <xsl:param name="usedLanguages"></xsl:param>
4894                 <xsl:param name="controlField008-35-37"></xsl:param>
4895                 <xsl:variable name="currentLanguage" select="."></xsl:variable>
4896                 <xsl:choose>
4897                         <xsl:when test="not($currentLanguage)"></xsl:when>
4898                         <xsl:when test="$currentLanguage!=$controlField008-35-37 and $currentLanguage!='rfc3066'">
4899                                 <xsl:if test="not(contains($usedLanguages,$currentLanguage))">
4900                                         <language>
4901                                                 <xsl:if test="@code!='a'">
4902                                                         <xsl:attribute name="objectPart">
4903                                                                 <xsl:choose>
4904                                                                         <xsl:when test="@code='b'">summary or subtitle</xsl:when>
4905                                                                         <xsl:when test="@code='d'">sung or spoken text</xsl:when>
4906                                                                         <xsl:when test="@code='e'">libretto</xsl:when>
4907                                                                         <xsl:when test="@code='f'">table of contents</xsl:when>
4908                                                                         <xsl:when test="@code='g'">accompanying material</xsl:when>
4909                                                                         <xsl:when test="@code='h'">translation</xsl:when>
4910                                                                 </xsl:choose>
4911                                                         </xsl:attribute>
4912                                                 </xsl:if>
4913                                                 <languageTerm authority="rfc3066" type="code">
4914                                                         <xsl:value-of select="$currentLanguage"/>
4915                                                 </languageTerm>
4916                                         </language>
4917                                 </xsl:if>
4918                         </xsl:when>
4919                         <xsl:otherwise>
4920                         </xsl:otherwise>
4921                 </xsl:choose>
4922         </xsl:template>
4923         <xsl:template name="datafield">
4924                 <xsl:param name="tag"/>
4925                 <xsl:param name="ind1"><xsl:text> </xsl:text></xsl:param>
4926                 <xsl:param name="ind2"><xsl:text> </xsl:text></xsl:param>
4927                 <xsl:param name="subfields"/>
4928                 <xsl:element name="marc:datafield">
4929                         <xsl:attribute name="tag">
4930                                 <xsl:value-of select="$tag"/>
4931                         </xsl:attribute>
4932                         <xsl:attribute name="ind1">
4933                                 <xsl:value-of select="$ind1"/>
4934                         </xsl:attribute>
4935                         <xsl:attribute name="ind2">
4936                                 <xsl:value-of select="$ind2"/>
4937                         </xsl:attribute>
4938                         <xsl:copy-of select="$subfields"/>
4939                 </xsl:element>
4940         </xsl:template>
4941
4942         <xsl:template name="subfieldSelect">
4943                 <xsl:param name="codes"/>
4944                 <xsl:param name="delimeter"><xsl:text> </xsl:text></xsl:param>
4945                 <xsl:variable name="str">
4946                         <xsl:for-each select="marc:subfield">
4947                                 <xsl:if test="contains($codes, @code)">
4948                                         <xsl:value-of select="text()"/><xsl:value-of select="$delimeter"/>
4949                                 </xsl:if>
4950                         </xsl:for-each>
4951                 </xsl:variable>
4952                 <xsl:value-of select="substring($str,1,string-length($str)-string-length($delimeter))"/>
4953         </xsl:template>
4954
4955         <xsl:template name="buildSpaces">
4956                 <xsl:param name="spaces"/>
4957                 <xsl:param name="char"><xsl:text> </xsl:text></xsl:param>
4958                 <xsl:if test="$spaces>0">
4959                         <xsl:value-of select="$char"/>
4960                         <xsl:call-template name="buildSpaces">
4961                                 <xsl:with-param name="spaces" select="$spaces - 1"/>
4962                                 <xsl:with-param name="char" select="$char"/>
4963                         </xsl:call-template>
4964                 </xsl:if>
4965         </xsl:template>
4966
4967         <xsl:template name="chopPunctuation">
4968                 <xsl:param name="chopString"/>
4969                 <xsl:param name="punctuation"><xsl:text>.:,;/ </xsl:text></xsl:param>
4970                 <xsl:variable name="length" select="string-length($chopString)"/>
4971                 <xsl:choose>
4972                         <xsl:when test="$length=0"/>
4973                         <xsl:when test="contains($punctuation, substring($chopString,$length,1))">
4974                                 <xsl:call-template name="chopPunctuation">
4975                                         <xsl:with-param name="chopString" select="substring($chopString,1,$length - 1)"/>
4976                                         <xsl:with-param name="punctuation" select="$punctuation"/>
4977                                 </xsl:call-template>
4978                         </xsl:when>
4979                         <xsl:when test="not($chopString)"/>
4980                         <xsl:otherwise><xsl:value-of select="$chopString"/></xsl:otherwise>
4981                 </xsl:choose>
4982         </xsl:template>
4983
4984         <xsl:template name="chopPunctuationFront">
4985                 <xsl:param name="chopString"/>
4986                 <xsl:variable name="length" select="string-length($chopString)"/>
4987                 <xsl:choose>
4988                         <xsl:when test="$length=0"/>
4989                         <xsl:when test="contains('.:,;/[ ', substring($chopString,1,1))">
4990                                 <xsl:call-template name="chopPunctuationFront">
4991                                         <xsl:with-param name="chopString" select="substring($chopString,2,$length - 1)"/>
4992                                 </xsl:call-template>
4993                         </xsl:when>
4994                         <xsl:when test="not($chopString)"/>
4995                         <xsl:otherwise><xsl:value-of select="$chopString"/></xsl:otherwise>
4996                 </xsl:choose>
4997         </xsl:template>
4998 </xsl:stylesheet>$XXXX$ where name = $$mods32$$;
4999
5000 update config.xml_transform set xslt = $XXXX$<xsl:stylesheet xmlns="http://www.loc.gov/mods/v3" xmlns:marc="http://www.loc.gov/MARC21/slim"
5001         xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
5002         exclude-result-prefixes="xlink marc" version="1.0">
5003         <xsl:output encoding="UTF-8" indent="yes" method="xml"/>
5004
5005         <xsl:variable name="ascii">
5006                 <xsl:text> !"#$%&amp;'()*+,-./0123456789:;&lt;=&gt;?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~</xsl:text>
5007         </xsl:variable>
5008
5009         <xsl:variable name="latin1">
5010                 <xsl:text> ¡¢£¤¥¦§¨©ª«¬­®¯°±²³´µ¶·¸¹º»¼½¾¿ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖ×ØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþÿ</xsl:text>
5011         </xsl:variable>
5012         <!-- Characters that usually don't need to be escaped -->
5013         <xsl:variable name="safe">
5014                 <xsl:text>!'()*-.0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz~</xsl:text>
5015         </xsl:variable>
5016
5017         <xsl:variable name="hex">0123456789ABCDEF</xsl:variable>
5018
5019     <!-- Evergreen specific: revert Revision 1.23, so we can have those authority xlink attributes back. -->
5020
5021         <!--MARC21slim2MODS3-3.xsl
5022 Revision 1.27 - Mapped 648 to <subject> 2009/03/13 tmee
5023 Revision 1.26 - Added subfield $s mapping for 130/240/730  2008/10/16 tmee
5024 Revision 1.25 - Mapped 040e to <descriptiveStandard> and Leader/18 to <descriptive standard>aacr2  2008/09/18 tmee
5025 Revision 1.24 - Mapped 852 subfields $h, $i, $j, $k, $l, $m, $t to <shelfLocation> and 852 subfield $u to <physicalLocation> with @xlink 2008/09/17 tmee
5026 Revision 1.23 - Commented out xlink/uri for subfield 0 for 130/240/730, 100/700, 110/710, 111/711 as these are currently unactionable  2008/09/17  tmee
5027 Revision 1.22 - Mapped 022 subfield $l to type "issn-l" subfield $m to output identifier element with corresponding @type and @invalid eq 'yes'2008/09/17  tmee
5028 Revision 1.21 - Mapped 856 ind2=1 or ind2=2 to <relatedItem><location><url>  2008/07/03  tmee
5029 Revision 1.20 - Added genre w/@auth="contents of 2" and type= "musical composition"  2008/07/01  tmee
5030 Revision 1.19 - Added genre offprint for 008/24+ BK code 2  2008/07/01  tmee
5031 Revision 1.18 - Added xlink/uri for subfield 0 for 130/240/730, 100/700, 110/710, 111/711  2008/06/26  tmee
5032 Revision 1.17 - Added mapping of 662 2008/05/14 tmee
5033 Revision 1.16 - Changed @authority from "marc" to "marcgt" for 007 and 008 codes mapped to a term in <genre> 2007/07/10  tmee
5034 Revision 1.15 - For field 630, moved call to part template outside title element  2007/07/10  tmee
5035 Revision 1.14 - Fixed template isValid and fields 010, 020, 022, 024, 028, and 037 to output additional identifier elements with corresponding @type and @invalid eq 'yes' when subfields z or y (in the case of 022) exist in the MARCXML ::: 2007/01/04 17:35:20 cred
5036 Revision 1.13 - Changed order of output under cartographics to reflect schema  2006/11/28  tmee
5037 Revision 1.12 - Updated to reflect MODS 3.2 Mapping  2006/10/11  tmee
5038 Revision 1.11 - The attribute objectPart moved from <languageTerm> to <language>  2006/04/08  jrad
5039 Revision 1.10 - MODS 3.1 revisions to language and classification elements  (plus ability to find marc:collection embedded in wrapper elements such as SRU zs: wrappers)  2006/02/06  ggar
5040 Revision 1.9 - Subfield $y was added to field 242 2004/09/02 10:57 jrad
5041 Revision 1.8 - Subject chopPunctuation expanded and attribute fixes 2004/08/12 jrad
5042 Revision 1.7 - 2004/03/25 08:29 jrad
5043 Revision 1.6 - Various validation fixes 2004/02/20 ntra
5044 Revision 1.5 - MODS2 to MODS3 updates, language unstacking and de-duping, chopPunctuation expanded  2003/10/02 16:18:58  ntra
5045 Revision 1.3 - Additional Changes not related to MODS Version 2.0 by ntra
5046 Revision 1.2 - Added Log Comment  2003/03/24 19:37:42  ckeith
5047 -->
5048         <xsl:template match="/">
5049                 <xsl:choose>
5050                         <xsl:when test="//marc:collection">
5051                                 <modsCollection xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5052                                         xsi:schemaLocation="http://www.loc.gov/mods/v3 http://www.loc.gov/standards/mods/v3/mods-3-2.xsd">
5053                                         <xsl:for-each select="//marc:collection/marc:record">
5054                                                 <mods version="3.3">
5055                                                         <xsl:call-template name="marcRecord"/>
5056                                                 </mods>
5057                                         </xsl:for-each>
5058                                 </modsCollection>
5059                         </xsl:when>
5060                         <xsl:otherwise>
5061                                 <mods xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="3.3"
5062                                         xsi:schemaLocation="http://www.loc.gov/mods/v3 http://www.loc.gov/standards/mods/v3/mods-3-2.xsd">
5063                                         <xsl:for-each select="//marc:record">
5064                                                 <xsl:call-template name="marcRecord"/>
5065                                         </xsl:for-each>
5066                                 </mods>
5067                         </xsl:otherwise>
5068                 </xsl:choose>
5069         </xsl:template>
5070         <xsl:template name="marcRecord">
5071                 <xsl:variable name="leader" select="marc:leader"/>
5072                 <xsl:variable name="leader6" select="substring($leader,7,1)"/>
5073                 <xsl:variable name="leader7" select="substring($leader,8,1)"/>
5074                 <xsl:variable name="controlField008" select="marc:controlfield[@tag='008']"/>
5075                 <xsl:variable name="typeOf008">
5076                         <xsl:choose>
5077                                 <xsl:when test="$leader6='a'">
5078                                         <xsl:choose>
5079                                                 <xsl:when
5080                                                         test="$leader7='a' or $leader7='c' or $leader7='d' or $leader7='m'">BK</xsl:when>
5081                                                 <xsl:when test="$leader7='b' or $leader7='i' or $leader7='s'">SE</xsl:when>
5082                                         </xsl:choose>
5083                                 </xsl:when>
5084                                 <xsl:when test="$leader6='t'">BK</xsl:when>
5085                                 <xsl:when test="$leader6='p'">MM</xsl:when>
5086                                 <xsl:when test="$leader6='m'">CF</xsl:when>
5087                                 <xsl:when test="$leader6='e' or $leader6='f'">MP</xsl:when>
5088                                 <xsl:when test="$leader6='g' or $leader6='k' or $leader6='o' or $leader6='r'">VM</xsl:when>
5089                                 <xsl:when test="$leader6='c' or $leader6='d' or $leader6='i' or $leader6='j'"
5090                                 >MU</xsl:when>
5091                         </xsl:choose>
5092                 </xsl:variable>
5093                 <xsl:for-each select="marc:datafield[@tag='245']">
5094                         <titleInfo>
5095                                 <xsl:variable name="title">
5096                                         <xsl:choose>
5097                                                 <xsl:when test="marc:subfield[@code='b']">
5098                                                         <xsl:call-template name="specialSubfieldSelect">
5099                                                                 <xsl:with-param name="axis">b</xsl:with-param>
5100                                                                 <xsl:with-param name="beforeCodes">afgk</xsl:with-param>
5101                                                         </xsl:call-template>
5102                                                 </xsl:when>
5103                                                 <xsl:otherwise>
5104                                                         <xsl:call-template name="subfieldSelect">
5105                                                                 <xsl:with-param name="codes">abfgk</xsl:with-param>
5106                                                         </xsl:call-template>
5107                                                 </xsl:otherwise>
5108                                         </xsl:choose>
5109                                 </xsl:variable>
5110                                 <xsl:variable name="titleChop">
5111                                         <xsl:call-template name="chopPunctuation">
5112                                                 <xsl:with-param name="chopString">
5113                                                         <xsl:value-of select="$title"/>
5114                                                 </xsl:with-param>
5115                                                 <xsl:with-param name="punctuation">
5116                                                     <xsl:text>,;/ </xsl:text>
5117                                                 </xsl:with-param>
5118                                         </xsl:call-template>
5119                                 </xsl:variable>
5120                                 <xsl:choose>
5121                                         <xsl:when test="@ind2&gt;0">
5122                                                 <nonSort>
5123                                                         <xsl:value-of select="substring($titleChop,1,@ind2)"/>
5124                                                 </nonSort>
5125                                                 <title>
5126                                                         <xsl:value-of select="substring($titleChop,@ind2+1)"/>
5127                                                 </title>
5128                                         </xsl:when>
5129                                         <xsl:otherwise>
5130                                                 <title>
5131                                                         <xsl:value-of select="$titleChop"/>
5132                                                 </title>
5133                                         </xsl:otherwise>
5134                                 </xsl:choose>
5135                                 <xsl:if test="marc:subfield[@code='b']">
5136                                         <subTitle>
5137                                                 <xsl:call-template name="chopPunctuation">
5138                                                         <xsl:with-param name="chopString">
5139                                                                 <xsl:call-template name="specialSubfieldSelect">
5140                                                                         <xsl:with-param name="axis">b</xsl:with-param>
5141                                                                         <xsl:with-param name="anyCodes">b</xsl:with-param>
5142                                                                         <xsl:with-param name="afterCodes">afgk</xsl:with-param>
5143                                                                 </xsl:call-template>
5144                                                         </xsl:with-param>
5145                                                 </xsl:call-template>
5146                                         </subTitle>
5147                                 </xsl:if>
5148                                 <xsl:call-template name="part"/>
5149                         </titleInfo>
5150                 </xsl:for-each>
5151                 <xsl:for-each select="marc:datafield[@tag='210']">
5152                         <titleInfo type="abbreviated">
5153                                 <title>
5154                                         <xsl:call-template name="chopPunctuation">
5155                                                 <xsl:with-param name="chopString">
5156                                                         <xsl:call-template name="subfieldSelect">
5157                                                                 <xsl:with-param name="codes">a</xsl:with-param>
5158                                                         </xsl:call-template>
5159                                                 </xsl:with-param>
5160                                         </xsl:call-template>
5161                                 </title>
5162                                 <xsl:call-template name="subtitle"/>
5163                         </titleInfo>
5164                 </xsl:for-each>
5165                 <xsl:for-each select="marc:datafield[@tag='242']">
5166                         <titleInfo type="translated">
5167                                 <!--09/01/04 Added subfield $y-->
5168                                 <xsl:for-each select="marc:subfield[@code='y']">
5169                                         <xsl:attribute name="lang">
5170                                                 <xsl:value-of select="text()"/>
5171                                         </xsl:attribute>
5172                                 </xsl:for-each>
5173                                 <xsl:for-each select="marc:subfield[@code='i']">
5174                                         <xsl:attribute name="displayLabel">
5175                                                 <xsl:value-of select="text()"/>
5176                                         </xsl:attribute>
5177                                 </xsl:for-each>
5178                                 <title>
5179                                         <xsl:call-template name="chopPunctuation">
5180                                                 <xsl:with-param name="chopString">
5181                                                         <xsl:call-template name="subfieldSelect">
5182                                                                 <!-- 1/04 removed $h, b -->
5183                                                                 <xsl:with-param name="codes">a</xsl:with-param>
5184                                                         </xsl:call-template>
5185                                                 </xsl:with-param>
5186                                         </xsl:call-template>
5187                                 </title>
5188                                 <!-- 1/04 fix -->
5189                                 <xsl:call-template name="subtitle"/>
5190                                 <xsl:call-template name="part"/>
5191                         </titleInfo>
5192                 </xsl:for-each>
5193                 <xsl:for-each select="marc:datafield[@tag='246']">
5194                         <titleInfo type="alternative">
5195                                 <xsl:for-each select="marc:subfield[@code='i']">
5196                                         <xsl:attribute name="displayLabel">
5197                                                 <xsl:value-of select="text()"/>
5198                                         </xsl:attribute>
5199                                 </xsl:for-each>
5200                                 <title>
5201                                         <xsl:call-template name="chopPunctuation">
5202                                                 <xsl:with-param name="chopString">
5203                                                         <xsl:call-template name="subfieldSelect">
5204                                                                 <!-- 1/04 removed $h, $b -->
5205                                                                 <xsl:with-param name="codes">af</xsl:with-param>
5206                                                         </xsl:call-template>
5207                                                 </xsl:with-param>
5208                                         </xsl:call-template>
5209                                 </title>
5210                                 <xsl:call-template name="subtitle"/>
5211                                 <xsl:call-template name="part"/>
5212                         </titleInfo>
5213                 </xsl:for-each>
5214                 <xsl:for-each
5215                         select="marc:datafield[@tag='130']|marc:datafield[@tag='240']|marc:datafield[@tag='730'][@ind2!='2']">
5216                         <titleInfo type="uniform">
5217                                 <title>
5218                                                 <xsl:call-template name="uri"/>
5219
5220                                         <xsl:variable name="str">
5221                                                 <xsl:for-each select="marc:subfield">
5222                                                         <xsl:if
5223                                                                 test="(contains('adfklmors',@code) and (not(../marc:subfield[@code='n' or @code='p']) or (following-sibling::marc:subfield[@code='n' or @code='p'])))">
5224                                                                 <xsl:value-of select="text()"/>
5225                                                                 <xsl:text> </xsl:text>
5226                                                         </xsl:if>
5227                                                 </xsl:for-each>
5228                                         </xsl:variable>
5229                                         <xsl:call-template name="chopPunctuation">
5230                                                 <xsl:with-param name="chopString">
5231                                                         <xsl:value-of select="substring($str,1,string-length($str)-1)"/>
5232                                                 </xsl:with-param>
5233                                         </xsl:call-template>
5234                                 </title>
5235                                 <xsl:call-template name="part"/>
5236                         </titleInfo>
5237                 </xsl:for-each>
5238                 <xsl:for-each select="marc:datafield[@tag='740'][@ind2!='2']">
5239                         <titleInfo type="alternative">
5240                                 <title>
5241                                         <xsl:call-template name="chopPunctuation">
5242                                                 <xsl:with-param name="chopString">
5243                                                         <xsl:call-template name="subfieldSelect">
5244                                                                 <xsl:with-param name="codes">ah</xsl:with-param>
5245                                                         </xsl:call-template>
5246                                                 </xsl:with-param>
5247                                         </xsl:call-template>
5248                                 </title>
5249                                 <xsl:call-template name="part"/>
5250                         </titleInfo>
5251                 </xsl:for-each>
5252                 <xsl:for-each select="marc:datafield[@tag='100']">
5253                         <name type="personal">
5254
5255                                 <xsl:call-template name="uri"/>
5256
5257                                 <xsl:call-template name="nameABCDQ"/>
5258                                 <xsl:call-template name="affiliation"/>
5259                                 <role>
5260                                         <roleTerm authority="marcrelator" type="text">creator</roleTerm>
5261                                 </role>
5262                                 <xsl:call-template name="role"/>
5263                         </name>
5264                 </xsl:for-each>
5265                 <xsl:for-each select="marc:datafield[@tag='110']">
5266                         <name type="corporate">
5267
5268                                         <xsl:call-template name="uri"/>
5269
5270                                 <xsl:call-template name="nameABCDN"/>
5271                                 <role>
5272                                         <roleTerm authority="marcrelator" type="text">creator</roleTerm>
5273                                 </role>
5274                                 <xsl:call-template name="role"/>
5275                         </name>
5276                 </xsl:for-each>
5277                 <xsl:for-each select="marc:datafield[@tag='111']">
5278                         <name type="conference">
5279
5280                                         <xsl:call-template name="uri"/>
5281
5282                                 <xsl:call-template name="nameACDEQ"/>
5283                                 <role>
5284                                         <roleTerm authority="marcrelator" type="text">creator</roleTerm>
5285                                 </role>
5286                                 <xsl:call-template name="role"/>
5287                         </name>
5288                 </xsl:for-each>
5289                 <xsl:for-each select="marc:datafield[@tag='700'][not(marc:subfield[@code='t'])]">
5290                         <name type="personal">
5291
5292                                         <xsl:call-template name="uri"/>
5293
5294                                 <xsl:call-template name="nameABCDQ"/>
5295                                 <xsl:call-template name="affiliation"/>
5296                                 <xsl:call-template name="role"/>
5297                         </name>
5298                 </xsl:for-each>
5299                 <xsl:for-each select="marc:datafield[@tag='710'][not(marc:subfield[@code='t'])]">
5300                         <name type="corporate">
5301
5302                                         <xsl:call-template name="uri"/>
5303
5304                                 <xsl:call-template name="nameABCDN"/>
5305                                 <xsl:call-template name="role"/>
5306                         </name>
5307                 </xsl:for-each>
5308                 <xsl:for-each select="marc:datafield[@tag='711'][not(marc:subfield[@code='t'])]">
5309                         <name type="conference">
5310
5311                                         <xsl:call-template name="uri"/>
5312
5313                                 <xsl:call-template name="nameACDEQ"/>
5314                                 <xsl:call-template name="role"/>
5315                         </name>
5316                 </xsl:for-each>
5317                 <xsl:for-each select="marc:datafield[@tag='720'][not(marc:subfield[@code='t'])]">
5318                         <name>
5319                                 <xsl:if test="@ind1=1">
5320                                         <xsl:attribute name="type">
5321                                                 <xsl:text>personal</xsl:text>
5322                                         </xsl:attribute>
5323                                 </xsl:if>
5324                                 <namePart>
5325                                         <xsl:value-of select="marc:subfield[@code='a']"/>
5326                                 </namePart>
5327                                 <xsl:call-template name="role"/>
5328                         </name>
5329                 </xsl:for-each>
5330                 <typeOfResource>
5331                         <xsl:if test="$leader7='c'">
5332                                 <xsl:attribute name="collection">yes</xsl:attribute>
5333                         </xsl:if>
5334                         <xsl:if test="$leader6='d' or $leader6='f' or $leader6='p' or $leader6='t'">
5335                                 <xsl:attribute name="manuscript">yes</xsl:attribute>
5336                         </xsl:if>
5337                         <xsl:choose>
5338                                 <xsl:when test="$leader6='a' or $leader6='t'">text</xsl:when>
5339                                 <xsl:when test="$leader6='e' or $leader6='f'">cartographic</xsl:when>
5340                                 <xsl:when test="$leader6='c' or $leader6='d'">notated music</xsl:when>
5341                                 <xsl:when test="$leader6='i'">sound recording-nonmusical</xsl:when>
5342                                 <xsl:when test="$leader6='j'">sound recording-musical</xsl:when>
5343                                 <xsl:when test="$leader6='k'">still image</xsl:when>
5344                                 <xsl:when test="$leader6='g'">moving image</xsl:when>
5345                                 <xsl:when test="$leader6='r'">three dimensional object</xsl:when>
5346                                 <xsl:when test="$leader6='m'">software, multimedia</xsl:when>
5347                                 <xsl:when test="$leader6='p'">mixed material</xsl:when>
5348                         </xsl:choose>
5349                 </typeOfResource>
5350                 <xsl:if test="substring($controlField008,26,1)='d'">
5351                         <genre authority="marcgt">globe</genre>
5352                 </xsl:if>
5353                 <xsl:if
5354                         test="marc:controlfield[@tag='007'][substring(text(),1,1)='a'][substring(text(),2,1)='r']">
5355                         <genre authority="marcgt">remote-sensing image</genre>
5356                 </xsl:if>
5357                 <xsl:if test="$typeOf008='MP'">
5358                         <xsl:variable name="controlField008-25" select="substring($controlField008,26,1)"/>
5359                         <xsl:choose>
5360                                 <xsl:when
5361                                         test="$controlField008-25='a' or $controlField008-25='b' or $controlField008-25='c' or marc:controlfield[@tag=007][substring(text(),1,1)='a'][substring(text(),2,1)='j']">
5362                                         <genre authority="marcgt">map</genre>
5363                                 </xsl:when>
5364                                 <xsl:when
5365                                         test="$controlField008-25='e' or marc:controlfield[@tag=007][substring(text(),1,1)='a'][substring(text(),2,1)='d']">
5366                                         <genre authority="marcgt">atlas</genre>
5367                                 </xsl:when>
5368                         </xsl:choose>
5369                 </xsl:if>
5370                 <xsl:if test="$typeOf008='SE'">
5371                         <xsl:variable name="controlField008-21" select="substring($controlField008,22,1)"/>
5372                         <xsl:choose>
5373                                 <xsl:when test="$controlField008-21='d'">
5374                                         <genre authority="marcgt">database</genre>
5375                                 </xsl:when>
5376                                 <xsl:when test="$controlField008-21='l'">
5377                                         <genre authority="marcgt">loose-leaf</genre>
5378                                 </xsl:when>
5379                                 <xsl:when test="$controlField008-21='m'">
5380                                         <genre authority="marcgt">series</genre>
5381                                 </xsl:when>
5382                                 <xsl:when test="$controlField008-21='n'">
5383                                         <genre authority="marcgt">newspaper</genre>
5384                                 </xsl:when>
5385                                 <xsl:when test="$controlField008-21='p'">
5386                                         <genre authority="marcgt">periodical</genre>
5387                                 </xsl:when>
5388                                 <xsl:when test="$controlField008-21='w'">
5389                                         <genre authority="marcgt">web site</genre>
5390                                 </xsl:when>
5391                         </xsl:choose>
5392                 </xsl:if>
5393                 <xsl:if test="$typeOf008='BK' or $typeOf008='SE'">
5394                         <xsl:variable name="controlField008-24" select="substring($controlField008,25,4)"/>
5395                         <xsl:choose>
5396                                 <xsl:when test="contains($controlField008-24,'a')">
5397                                         <genre authority="marcgt">abstract or summary</genre>
5398                                 </xsl:when>
5399                                 <xsl:when test="contains($controlField008-24,'b')">
5400                                         <genre authority="marcgt">bibliography</genre>
5401                                 </xsl:when>
5402                                 <xsl:when test="contains($controlField008-24,'c')">
5403                                         <genre authority="marcgt">catalog</genre>
5404                                 </xsl:when>
5405                                 <xsl:when test="contains($controlField008-24,'d')">
5406                                         <genre authority="marcgt">dictionary</genre>
5407                                 </xsl:when>
5408                                 <xsl:when test="contains($controlField008-24,'e')">
5409                                         <genre authority="marcgt">encyclopedia</genre>
5410                                 </xsl:when>
5411                                 <xsl:when test="contains($controlField008-24,'f')">
5412                                         <genre authority="marcgt">handbook</genre>
5413                                 </xsl:when>
5414                                 <xsl:when test="contains($controlField008-24,'g')">
5415                                         <genre authority="marcgt">legal article</genre>
5416                                 </xsl:when>
5417                                 <xsl:when test="contains($controlField008-24,'i')">
5418                                         <genre authority="marcgt">index</genre>
5419                                 </xsl:when>
5420                                 <xsl:when test="contains($controlField008-24,'k')">
5421                                         <genre authority="marcgt">discography</genre>
5422                                 </xsl:when>
5423                                 <xsl:when test="contains($controlField008-24,'l')">
5424                                         <genre authority="marcgt">legislation</genre>
5425                                 </xsl:when>
5426                                 <xsl:when test="contains($controlField008-24,'m')">
5427                                         <genre authority="marcgt">theses</genre>
5428                                 </xsl:when>
5429                                 <xsl:when test="contains($controlField008-24,'n')">
5430                                         <genre authority="marcgt">survey of literature</genre>
5431                                 </xsl:when>
5432                                 <xsl:when test="contains($controlField008-24,'o')">
5433                                         <genre authority="marcgt">review</genre>
5434                                 </xsl:when>
5435                                 <xsl:when test="contains($controlField008-24,'p')">
5436                                         <genre authority="marcgt">programmed text</genre>
5437                                 </xsl:when>
5438                                 <xsl:when test="contains($controlField008-24,'q')">
5439                                         <genre authority="marcgt">filmography</genre>
5440                                 </xsl:when>
5441                                 <xsl:when test="contains($controlField008-24,'r')">
5442                                         <genre authority="marcgt">directory</genre>
5443                                 </xsl:when>
5444                                 <xsl:when test="contains($controlField008-24,'s')">
5445                                         <genre authority="marcgt">statistics</genre>
5446                                 </xsl:when>
5447                                 <xsl:when test="contains($controlField008-24,'t')">
5448                                         <genre authority="marcgt">technical report</genre>
5449                                 </xsl:when>
5450                                 <xsl:when test="contains($controlField008-24,'v')">
5451                                         <genre authority="marcgt">legal case and case notes</genre>
5452                                 </xsl:when>
5453                                 <xsl:when test="contains($controlField008-24,'w')">
5454                                         <genre authority="marcgt">law report or digest</genre>
5455                                 </xsl:when>
5456                                 <xsl:when test="contains($controlField008-24,'z')">
5457                                         <genre authority="marcgt">treaty</genre>
5458                                 </xsl:when>
5459                         </xsl:choose>
5460                         <xsl:variable name="controlField008-29" select="substring($controlField008,30,1)"/>
5461                         <xsl:choose>
5462                                 <xsl:when test="$controlField008-29='1'">
5463                                         <genre authority="marcgt">conference publication</genre>
5464                                 </xsl:when>
5465                         </xsl:choose>
5466                 </xsl:if>
5467                 <xsl:if test="$typeOf008='CF'">
5468                         <xsl:variable name="controlField008-26" select="substring($controlField008,27,1)"/>
5469                         <xsl:choose>
5470                                 <xsl:when test="$controlField008-26='a'">
5471                                         <genre authority="marcgt">numeric data</genre>
5472                                 </xsl:when>
5473                                 <xsl:when test="$controlField008-26='e'">
5474                                         <genre authority="marcgt">database</genre>
5475                                 </xsl:when>
5476                                 <xsl:when test="$controlField008-26='f'">
5477                                         <genre authority="marcgt">font</genre>
5478                                 </xsl:when>
5479                                 <xsl:when test="$controlField008-26='g'">
5480                                         <genre authority="marcgt">game</genre>
5481                                 </xsl:when>
5482                         </xsl:choose>
5483                 </xsl:if>
5484                 <xsl:if test="$typeOf008='BK'">
5485                         <xsl:if test="substring($controlField008,25,1)='j'">
5486                                 <genre authority="marcgt">patent</genre>
5487                         </xsl:if>
5488                         <xsl:if test="substring($controlField008,25,1)='2'">
5489                                 <genre authority="marcgt">offprint</genre>
5490                         </xsl:if>
5491                         <xsl:if test="substring($controlField008,31,1)='1'">
5492                                 <genre authority="marcgt">festschrift</genre>
5493                         </xsl:if>
5494                         <xsl:variable name="controlField008-34" select="substring($controlField008,35,1)"/>
5495                         <xsl:if
5496                                 test="$controlField008-34='a' or $controlField008-34='b' or $controlField008-34='c' or $controlField008-34='d'">
5497                                 <genre authority="marcgt">biography</genre>
5498                         </xsl:if>
5499                         <xsl:variable name="controlField008-33" select="substring($controlField008,34,1)"/>
5500                         <xsl:choose>
5501                                 <xsl:when test="$controlField008-33='e'">
5502                                         <genre authority="marcgt">essay</genre>
5503                                 </xsl:when>
5504                                 <xsl:when test="$controlField008-33='d'">
5505                                         <genre authority="marcgt">drama</genre>
5506                                 </xsl:when>
5507                                 <xsl:when test="$controlField008-33='c'">
5508                                         <genre authority="marcgt">comic strip</genre>
5509                                 </xsl:when>
5510                                 <xsl:when test="$controlField008-33='l'">
5511                                         <genre authority="marcgt">fiction</genre>
5512                                 </xsl:when>
5513                                 <xsl:when test="$controlField008-33='h'">
5514                                         <genre authority="marcgt">humor, satire</genre>
5515                                 </xsl:when>
5516                                 <xsl:when test="$controlField008-33='i'">
5517                                         <genre authority="marcgt">letter</genre>
5518                                 </xsl:when>
5519                                 <xsl:when test="$controlField008-33='f'">
5520                                         <genre authority="marcgt">novel</genre>
5521                                 </xsl:when>
5522                                 <xsl:when test="$controlField008-33='j'">
5523                                         <genre authority="marcgt">short story</genre>
5524                                 </xsl:when>
5525                                 <xsl:when test="$controlField008-33='s'">
5526                                         <genre authority="marcgt">speech</genre>
5527                                 </xsl:when>
5528                         </xsl:choose>
5529                 </xsl:if>
5530                 <xsl:if test="$typeOf008='MU'">
5531                         <xsl:variable name="controlField008-30-31" select="substring($controlField008,31,2)"/>
5532                         <xsl:if test="contains($controlField008-30-31,'b')">
5533                                 <genre authority="marcgt">biography</genre>
5534                         </xsl:if>
5535                         <xsl:if test="contains($controlField008-30-31,'c')">
5536                                 <genre authority="marcgt">conference publication</genre>
5537                         </xsl:if>
5538                         <xsl:if test="contains($controlField008-30-31,'d')">
5539                                 <genre authority="marcgt">drama</genre>
5540                         </xsl:if>
5541                         <xsl:if test="contains($controlField008-30-31,'e')">
5542                                 <genre authority="marcgt">essay</genre>
5543                         </xsl:if>
5544                         <xsl:if test="contains($controlField008-30-31,'f')">
5545                                 <genre authority="marcgt">fiction</genre>
5546                         </xsl:if>
5547                         <xsl:if test="contains($controlField008-30-31,'o')">
5548                                 <genre authority="marcgt">folktale</genre>
5549                         </xsl:if>
5550                         <xsl:if test="contains($controlField008-30-31,'h')">
5551                                 <genre authority="marcgt">history</genre>
5552                         </xsl:if>
5553                         <xsl:if test="contains($controlField008-30-31,'k')">
5554                                 <genre authority="marcgt">humor, satire</genre>
5555                         </xsl:if>
5556                         <xsl:if test="contains($controlField008-30-31,'m')">
5557                                 <genre authority="marcgt">memoir</genre>
5558                         </xsl:if>
5559                         <xsl:if test="contains($controlField008-30-31,'p')">
5560                                 <genre authority="marcgt">poetry</genre>
5561                         </xsl:if>
5562                         <xsl:if test="contains($controlField008-30-31,'r')">
5563                                 <genre authority="marcgt">rehearsal</genre>
5564                         </xsl:if>
5565                         <xsl:if test="contains($controlField008-30-31,'g')">
5566                                 <genre authority="marcgt">reporting</genre>
5567                         </xsl:if>
5568                         <xsl:if test="contains($controlField008-30-31,'s')">
5569                                 <genre authority="marcgt">sound</genre>
5570                         </xsl:if>
5571                         <xsl:if test="contains($controlField008-30-31,'l')">
5572                                 <genre authority="marcgt">speech</genre>
5573                         </xsl:if>
5574                 </xsl:if>
5575                 <xsl:if test="$typeOf008='VM'">
5576                         <xsl:variable name="controlField008-33" select="substring($controlField008,34,1)"/>
5577                         <xsl:choose>
5578                                 <xsl:when test="$controlField008-33='a'">
5579                                         <genre authority="marcgt">art original</genre>
5580                                 </xsl:when>
5581                                 <xsl:when test="$controlField008-33='b'">
5582                                         <genre authority="marcgt">kit</genre>
5583                                 </xsl:when>
5584                                 <xsl:when test="$controlField008-33='c'">
5585                                         <genre authority="marcgt">art reproduction</genre>
5586                                 </xsl:when>
5587                                 <xsl:when test="$controlField008-33='d'">
5588                                         <genre authority="marcgt">diorama</genre>
5589                                 </xsl:when>
5590                                 <xsl:when test="$controlField008-33='f'">
5591                                         <genre authority="marcgt">filmstrip</genre>
5592                                 </xsl:when>
5593                                 <xsl:when test="$controlField008-33='g'">
5594                                         <genre authority="marcgt">legal article</genre>
5595                                 </xsl:when>
5596                                 <xsl:when test="$controlField008-33='i'">
5597                                         <genre authority="marcgt">picture</genre>
5598                                 </xsl:when>
5599                                 <xsl:when test="$controlField008-33='k'">
5600                                         <genre authority="marcgt">graphic</genre>
5601                                 </xsl:when>
5602                                 <xsl:when test="$controlField008-33='l'">
5603                                         <genre authority="marcgt">technical drawing</genre>
5604                                 </xsl:when>
5605                                 <xsl:when test="$controlField008-33='m'">
5606                                         <genre authority="marcgt">motion picture</genre>
5607                                 </xsl:when>
5608                                 <xsl:when test="$controlField008-33='n'">
5609                                         <genre authority="marcgt">chart</genre>
5610                                 </xsl:when>
5611                                 <xsl:when test="$controlField008-33='o'">
5612                                         <genre authority="marcgt">flash card</genre>
5613                                 </xsl:when>
5614                                 <xsl:when test="$controlField008-33='p'">
5615                                         <genre authority="marcgt">microscope slide</genre>
5616                                 </xsl:when>
5617                                 <xsl:when
5618                                         test="$controlField008-33='q' or marc:controlfield[@tag=007][substring(text(),1,1)='a'][substring(text(),2,1)='q']">
5619                                         <genre authority="marcgt">model</genre>
5620                                 </xsl:when>
5621                                 <xsl:when test="$controlField008-33='r'">
5622                                         <genre authority="marcgt">realia</genre>
5623                                 </xsl:when>
5624                                 <xsl:when test="$controlField008-33='s'">
5625                                         <genre authority="marcgt">slide</genre>
5626                                 </xsl:when>
5627                                 <xsl:when test="$controlField008-33='t'">
5628                                         <genre authority="marcgt">transparency</genre>
5629                                 </xsl:when>
5630                                 <xsl:when test="$controlField008-33='v'">
5631                                         <genre authority="marcgt">videorecording</genre>
5632                                 </xsl:when>
5633                                 <xsl:when test="$controlField008-33='w'">
5634                                         <genre authority="marcgt">toy</genre>
5635                                 </xsl:when>
5636                         </xsl:choose>
5637                 </xsl:if>
5638
5639                 <!-- 1.20 047 genre tmee-->
5640
5641                 <xsl:for-each select="marc:datafield[@tag=047]">
5642                         <genre authority="marcgt">
5643                                 <xsl:attribute name="authority">
5644                                         <xsl:value-of select="marc:subfield[@code='2']"/>
5645                                 </xsl:attribute>
5646                                 <xsl:call-template name="subfieldSelect">
5647                                         <xsl:with-param name="codes">abcdef</xsl:with-param>
5648                                         <xsl:with-param name="delimeter">-</xsl:with-param>
5649                                 </xsl:call-template>
5650                         </genre>
5651                 </xsl:for-each>
5652                 <xsl:for-each select="marc:datafield[@tag=655]">
5653                         <genre authority="marcgt">
5654                                 <xsl:attribute name="authority">
5655                                         <xsl:value-of select="marc:subfield[@code='2']"/>
5656                                 </xsl:attribute>
5657                                 <xsl:call-template name="subfieldSelect">
5658                                         <xsl:with-param name="codes">abvxyz</xsl:with-param>
5659                                         <xsl:with-param name="delimeter">-</xsl:with-param>
5660                                 </xsl:call-template>
5661                         </genre>
5662                 </xsl:for-each>
5663                 <originInfo>
5664                         <xsl:variable name="MARCpublicationCode"
5665                                 select="normalize-space(substring($controlField008,16,3))"/>
5666                         <xsl:if test="translate($MARCpublicationCode,'|','')">
5667                                 <place>
5668                                         <placeTerm>
5669                                                 <xsl:attribute name="type">code</xsl:attribute>
5670                                                 <xsl:attribute name="authority">marccountry</xsl:attribute>
5671                                                 <xsl:value-of select="$MARCpublicationCode"/>
5672                                         </placeTerm>
5673                                 </place>
5674                         </xsl:if>
5675                         <xsl:for-each select="marc:datafield[@tag=044]/marc:subfield[@code='c']">
5676                                 <place>
5677                                         <placeTerm>
5678                                                 <xsl:attribute name="type">code</xsl:attribute>
5679                                                 <xsl:attribute name="authority">iso3166</xsl:attribute>
5680                                                 <xsl:value-of select="."/>
5681                                         </placeTerm>
5682                                 </place>
5683                         </xsl:for-each>
5684                         <xsl:for-each select="marc:datafield[@tag=260]/marc:subfield[@code='a']">
5685                                 <place>
5686                                         <placeTerm>
5687                                                 <xsl:attribute name="type">text</xsl:attribute>
5688                                                 <xsl:call-template name="chopPunctuationFront">
5689                                                         <xsl:with-param name="chopString">
5690                                                                 <xsl:call-template name="chopPunctuation">
5691                                                                         <xsl:with-param name="chopString" select="."/>
5692                                                                 </xsl:call-template>
5693                                                         </xsl:with-param>
5694                                                 </xsl:call-template>
5695                                         </placeTerm>
5696                                 </place>
5697                         </xsl:for-each>
5698                         <xsl:for-each select="marc:datafield[@tag=046]/marc:subfield[@code='m']">
5699                                 <dateValid point="start">
5700                                         <xsl:value-of select="."/>
5701                                 </dateValid>
5702                         </xsl:for-each>
5703                         <xsl:for-each select="marc:datafield[@tag=046]/marc:subfield[@code='n']">
5704                                 <dateValid point="end">
5705                                         <xsl:value-of select="."/>
5706                                 </dateValid>
5707                         </xsl:for-each>
5708                         <xsl:for-each select="marc:datafield[@tag=046]/marc:subfield[@code='j']">
5709                                 <dateModified>
5710                                         <xsl:value-of select="."/>
5711                                 </dateModified>
5712                         </xsl:for-each>
5713                         <xsl:for-each
5714                                 select="marc:datafield[@tag=260]/marc:subfield[@code='b' or @code='c' or @code='g']">
5715                                 <xsl:choose>
5716                                         <xsl:when test="@code='b'">
5717                                                 <publisher>
5718                                                         <xsl:call-template name="chopPunctuation">
5719                                                                 <xsl:with-param name="chopString" select="."/>
5720                                                                 <xsl:with-param name="punctuation">
5721                                                                         <xsl:text>:,;/ </xsl:text>
5722                                                                 </xsl:with-param>
5723                                                         </xsl:call-template>
5724                                                 </publisher>
5725                                         </xsl:when>
5726                                         <xsl:when test="@code='c'">
5727                                                 <dateIssued>
5728                                                         <xsl:call-template name="chopPunctuation">
5729                                                                 <xsl:with-param name="chopString" select="."/>
5730                                                         </xsl:call-template>
5731                                                 </dateIssued>
5732                                         </xsl:when>
5733                                         <xsl:when test="@code='g'">
5734                                                 <dateCreated>
5735                                                         <xsl:value-of select="."/>
5736                                                 </dateCreated>
5737                                         </xsl:when>
5738                                 </xsl:choose>
5739                         </xsl:for-each>
5740                         <xsl:variable name="dataField260c">
5741                                 <xsl:call-template name="chopPunctuation">
5742                                         <xsl:with-param name="chopString"
5743                                                 select="marc:datafield[@tag=260]/marc:subfield[@code='c']"/>
5744                                 </xsl:call-template>
5745                         </xsl:variable>
5746                         <xsl:variable name="controlField008-7-10"
5747                                 select="normalize-space(substring($controlField008, 8, 4))"/>
5748                         <xsl:variable name="controlField008-11-14"
5749                                 select="normalize-space(substring($controlField008, 12, 4))"/>
5750                         <xsl:variable name="controlField008-6"
5751                                 select="normalize-space(substring($controlField008, 7, 1))"/>
5752                         <xsl:if
5753                                 test="$controlField008-6='e' or $controlField008-6='p' or $controlField008-6='r' or $controlField008-6='t' or $controlField008-6='s'">
5754                                 <xsl:if test="$controlField008-7-10 and ($controlField008-7-10 != $dataField260c)">
5755                                         <dateIssued encoding="marc">
5756                                                 <xsl:value-of select="$controlField008-7-10"/>
5757                                         </dateIssued>
5758                                 </xsl:if>
5759                         </xsl:if>
5760                         <xsl:if
5761                                 test="$controlField008-6='c' or $controlField008-6='d' or $controlField008-6='i' or $controlField008-6='k' or $controlField008-6='m' or $controlField008-6='q' or $controlField008-6='u'">
5762                                 <xsl:if test="$controlField008-7-10">
5763                                         <dateIssued encoding="marc" point="start">
5764                                                 <xsl:value-of select="$controlField008-7-10"/>
5765                                         </dateIssued>
5766                                 </xsl:if>
5767                         </xsl:if>
5768                         <xsl:if
5769                                 test="$controlField008-6='c' or $controlField008-6='d' or $controlField008-6='i' or $controlField008-6='k' or $controlField008-6='m' or $controlField008-6='q' or $controlField008-6='u'">
5770                                 <xsl:if test="$controlField008-11-14">
5771                                         <dateIssued encoding="marc" point="end">
5772                                                 <xsl:value-of select="$controlField008-11-14"/>
5773                                         </dateIssued>
5774                                 </xsl:if>
5775                         </xsl:if>
5776                         <xsl:if test="$controlField008-6='q'">
5777                                 <xsl:if test="$controlField008-7-10">
5778                                         <dateIssued encoding="marc" point="start" qualifier="questionable">
5779                                                 <xsl:value-of select="$controlField008-7-10"/>
5780                                         </dateIssued>
5781                                 </xsl:if>
5782                         </xsl:if>
5783                         <xsl:if test="$controlField008-6='q'">
5784                                 <xsl:if test="$controlField008-11-14">
5785                                         <dateIssued encoding="marc" point="end" qualifier="questionable">
5786                                                 <xsl:value-of select="$controlField008-11-14"/>
5787                                         </dateIssued>
5788                                 </xsl:if>
5789                         </xsl:if>
5790                         <xsl:if test="$controlField008-6='t'">
5791                                 <xsl:if test="$controlField008-11-14">
5792                                         <copyrightDate encoding="marc">
5793                                                 <xsl:value-of select="$controlField008-11-14"/>
5794                                         </copyrightDate>
5795                                 </xsl:if>
5796                         </xsl:if>
5797                         <xsl:for-each
5798                                 select="marc:datafield[@tag=033][@ind1=0 or @ind1=1]/marc:subfield[@code='a']">
5799                                 <dateCaptured encoding="iso8601">
5800                                         <xsl:value-of select="."/>
5801                                 </dateCaptured>
5802                         </xsl:for-each>
5803                         <xsl:for-each select="marc:datafield[@tag=033][@ind1=2]/marc:subfield[@code='a'][1]">
5804                                 <dateCaptured encoding="iso8601" point="start">
5805                                         <xsl:value-of select="."/>
5806                                 </dateCaptured>
5807                         </xsl:for-each>
5808                         <xsl:for-each select="marc:datafield[@tag=033][@ind1=2]/marc:subfield[@code='a'][2]">
5809                                 <dateCaptured encoding="iso8601" point="end">
5810                                         <xsl:value-of select="."/>
5811                                 </dateCaptured>
5812                         </xsl:for-each>
5813                         <xsl:for-each select="marc:datafield[@tag=250]/marc:subfield[@code='a']">
5814                                 <edition>
5815                                         <xsl:value-of select="."/>
5816                                 </edition>
5817                         </xsl:for-each>
5818                         <xsl:for-each select="marc:leader">
5819                                 <issuance>
5820                                         <xsl:choose>
5821                                                 <xsl:when
5822                                                         test="$leader7='a' or $leader7='c' or $leader7='d' or $leader7='m'"
5823                                                         >monographic</xsl:when>
5824                                                 <xsl:when test="$leader7='b' or $leader7='i' or $leader7='s'"
5825                                                 >continuing</xsl:when>
5826                                         </xsl:choose>
5827                                 </issuance>
5828                         </xsl:for-each>
5829                         <xsl:for-each select="marc:datafield[@tag=310]|marc:datafield[@tag=321]">
5830                                 <frequency>
5831                                         <xsl:call-template name="subfieldSelect">
5832                                                 <xsl:with-param name="codes">ab</xsl:with-param>
5833                                         </xsl:call-template>
5834                                 </frequency>
5835                         </xsl:for-each>
5836                 </originInfo>
5837                 <xsl:variable name="controlField008-35-37"
5838                         select="normalize-space(translate(substring($controlField008,36,3),'|#',''))"/>
5839                 <xsl:if test="$controlField008-35-37">
5840                         <language>
5841                                 <languageTerm authority="iso639-2b" type="code">
5842                                         <xsl:value-of select="substring($controlField008,36,3)"/>
5843                                 </languageTerm>
5844                         </language>
5845                 </xsl:if>
5846                 <xsl:for-each select="marc:datafield[@tag=041]">
5847                         <xsl:for-each
5848                                 select="marc:subfield[@code='a' or @code='b' or @code='d' or @code='e' or @code='f' or @code='g' or @code='h']">
5849                                 <xsl:variable name="langCodes" select="."/>
5850                                 <xsl:choose>
5851                                         <xsl:when test="../marc:subfield[@code='2']='rfc3066'">
5852                                                 <!-- not stacked but could be repeated -->
5853                                                 <xsl:call-template name="rfcLanguages">
5854                                                         <xsl:with-param name="nodeNum">
5855                                                                 <xsl:value-of select="1"/>
5856                                                         </xsl:with-param>
5857                                                         <xsl:with-param name="usedLanguages">
5858                                                                 <xsl:text/>
5859                                                         </xsl:with-param>
5860                                                         <xsl:with-param name="controlField008-35-37">
5861                                                                 <xsl:value-of select="$controlField008-35-37"/>
5862                                                         </xsl:with-param>
5863                                                 </xsl:call-template>
5864                                         </xsl:when>
5865                                         <xsl:otherwise>
5866                                                 <!-- iso -->
5867                                                 <xsl:variable name="allLanguages">
5868                                                         <xsl:copy-of select="$langCodes"/>
5869                                                 </xsl:variable>
5870                                                 <xsl:variable name="currentLanguage">
5871                                                         <xsl:value-of select="substring($allLanguages,1,3)"/>
5872                                                 </xsl:variable>
5873                                                 <xsl:call-template name="isoLanguage">
5874                                                         <xsl:with-param name="currentLanguage">
5875                                                                 <xsl:value-of select="substring($allLanguages,1,3)"/>
5876                                                         </xsl:with-param>
5877                                                         <xsl:with-param name="remainingLanguages">
5878                                                                 <xsl:value-of
5879                                                                         select="substring($allLanguages,4,string-length($allLanguages)-3)"
5880                                                                 />
5881                                                         </xsl:with-param>
5882                                                         <xsl:with-param name="usedLanguages">
5883                                                                 <xsl:if test="$controlField008-35-37">
5884                                                                         <xsl:value-of select="$controlField008-35-37"/>
5885                                                                 </xsl:if>
5886                                                         </xsl:with-param>
5887                                                 </xsl:call-template>
5888                                         </xsl:otherwise>
5889                                 </xsl:choose>
5890                         </xsl:for-each>
5891                 </xsl:for-each>
5892                 <xsl:variable name="physicalDescription">
5893                         <!--3.2 change tmee 007/11 -->
5894                         <xsl:if test="$typeOf008='CF' and marc:controlfield[@tag=007][substring(.,12,1)='a']">
5895                                 <digitalOrigin>reformatted digital</digitalOrigin>
5896                         </xsl:if>
5897                         <xsl:if test="$typeOf008='CF' and marc:controlfield[@tag=007][substring(.,12,1)='b']">
5898                                 <digitalOrigin>digitized microfilm</digitalOrigin>
5899                         </xsl:if>
5900                         <xsl:if test="$typeOf008='CF' and marc:controlfield[@tag=007][substring(.,12,1)='d']">
5901                                 <digitalOrigin>digitized other analog</digitalOrigin>
5902                         </xsl:if>
5903                         <xsl:variable name="controlField008-23" select="substring($controlField008,24,1)"/>
5904                         <xsl:variable name="controlField008-29" select="substring($controlField008,30,1)"/>
5905                         <xsl:variable name="check008-23">
5906                                 <xsl:if
5907                                         test="$typeOf008='BK' or $typeOf008='MU' or $typeOf008='SE' or $typeOf008='MM'">
5908                                         <xsl:value-of select="true()"/>
5909                                 </xsl:if>
5910                         </xsl:variable>
5911                         <xsl:variable name="check008-29">
5912                                 <xsl:if test="$typeOf008='MP' or $typeOf008='VM'">
5913                                         <xsl:value-of select="true()"/>
5914                                 </xsl:if>
5915                         </xsl:variable>
5916                         <xsl:choose>
5917                                 <xsl:when
5918                                         test="($check008-23 and $controlField008-23='f') or ($check008-29 and $controlField008-29='f')">
5919                                         <form authority="marcform">braille</form>
5920                                 </xsl:when>
5921                                 <xsl:when
5922                                         test="($controlField008-23=' ' and ($leader6='c' or $leader6='d')) or (($typeOf008='BK' or $typeOf008='SE') and ($controlField008-23=' ' or $controlField008='r'))">
5923                                         <form authority="marcform">print</form>
5924                                 </xsl:when>
5925                                 <xsl:when
5926                                         test="$leader6 = 'm' or ($check008-23 and $controlField008-23='s') or ($check008-29 and $controlField008-29='s')">
5927                                         <form authority="marcform">electronic</form>
5928                                 </xsl:when>
5929                                 <xsl:when
5930                                         test="($check008-23 and $controlField008-23='b') or ($check008-29 and $controlField008-29='b')">
5931                                         <form authority="marcform">microfiche</form>
5932                                 </xsl:when>
5933                                 <xsl:when
5934                                         test="($check008-23 and $controlField008-23='a') or ($check008-29 and $controlField008-29='a')">
5935                                         <form authority="marcform">microfilm</form>
5936                                 </xsl:when>
5937                         </xsl:choose>
5938                         <!-- 1/04 fix -->
5939                         <xsl:if test="marc:datafield[@tag=130]/marc:subfield[@code='h']">
5940                                 <form authority="gmd">
5941                                         <xsl:call-template name="chopBrackets">
5942                                                 <xsl:with-param name="chopString">
5943                                                         <xsl:value-of select="marc:datafield[@tag=130]/marc:subfield[@code='h']"
5944                                                         />
5945                                                 </xsl:with-param>
5946                                         </xsl:call-template>
5947                                 </form>
5948                         </xsl:if>
5949                         <xsl:if test="marc:datafield[@tag=240]/marc:subfield[@code='h']">
5950                                 <form authority="gmd">
5951                                         <xsl:call-template name="chopBrackets">
5952                                                 <xsl:with-param name="chopString">
5953                                                         <xsl:value-of select="marc:datafield[@tag=240]/marc:subfield[@code='h']"
5954                                                         />
5955                                                 </xsl:with-param>
5956                                         </xsl:call-template>
5957                                 </form>
5958                         </xsl:if>
5959                         <xsl:if test="marc:datafield[@tag=242]/marc:subfield[@code='h']">
5960                                 <form authority="gmd">
5961                                         <xsl:call-template name="chopBrackets">
5962                                                 <xsl:with-param name="chopString">
5963                                                         <xsl:value-of select="marc:datafield[@tag=242]/marc:subfield[@code='h']"
5964                                                         />
5965                                                 </xsl:with-param>
5966                                         </xsl:call-template>
5967                                 </form>
5968                         </xsl:if>
5969                         <xsl:if test="marc:datafield[@tag=245]/marc:subfield[@code='h']">
5970                                 <form authority="gmd">
5971                                         <xsl:call-template name="chopBrackets">
5972                                                 <xsl:with-param name="chopString">
5973                                                         <xsl:value-of select="marc:datafield[@tag=245]/marc:subfield[@code='h']"
5974                                                         />
5975                                                 </xsl:with-param>
5976                                         </xsl:call-template>
5977                                 </form>
5978                         </xsl:if>
5979                         <xsl:if test="marc:datafield[@tag=246]/marc:subfield[@code='h']">
5980                                 <form authority="gmd">
5981                                         <xsl:call-template name="chopBrackets">
5982                                                 <xsl:with-param name="chopString">
5983                                                         <xsl:value-of select="marc:datafield[@tag=246]/marc:subfield[@code='h']"
5984                                                         />
5985                                                 </xsl:with-param>
5986                                         </xsl:call-template>
5987                                 </form>
5988                         </xsl:if>
5989                         <xsl:if test="marc:datafield[@tag=730]/marc:subfield[@code='h']">
5990                                 <form authority="gmd">
5991                                         <xsl:call-template name="chopBrackets">
5992                                                 <xsl:with-param name="chopString">
5993                                                         <xsl:value-of select="marc:datafield[@tag=730]/marc:subfield[@code='h']"
5994                                                         />
5995                                                 </xsl:with-param>
5996                                         </xsl:call-template>
5997                                 </form>
5998                         </xsl:if>
5999                         <xsl:for-each select="marc:datafield[@tag=256]/marc:subfield[@code='a']">
6000                                 <form>
6001                                         <xsl:value-of select="."/>
6002                                 </form>
6003                         </xsl:for-each>
6004                         <xsl:for-each select="marc:controlfield[@tag=007][substring(text(),1,1)='c']">
6005                                 <xsl:choose>
6006                                         <xsl:when test="substring(text(),14,1)='a'">
6007                                                 <reformattingQuality>access</reformattingQuality>
6008                                         </xsl:when>
6009                                         <xsl:when test="substring(text(),14,1)='p'">
6010                                                 <reformattingQuality>preservation</reformattingQuality>
6011                                         </xsl:when>
6012                                         <xsl:when test="substring(text(),14,1)='r'">
6013                                                 <reformattingQuality>replacement</reformattingQuality>
6014                                         </xsl:when>
6015                                 </xsl:choose>
6016                         </xsl:for-each>
6017                         <!--3.2 change tmee 007/01 -->
6018                         <xsl:if
6019                                 test="marc:controlfield[@tag=007][substring(text(),1,1)='c'][substring(text(),2,1)='b']">
6020                                 <form authority="smd">chip cartridge</form>
6021                         </xsl:if>
6022                         <xsl:if
6023                                 test="marc:controlfield[@tag=007][substring(text(),1,1)='c'][substring(text(),2,1)='c']">
6024                                 <form authority="smd">computer optical disc cartridge</form>
6025                         </xsl:if>
6026                         <xsl:if
6027                                 test="marc:controlfield[@tag=007][substring(text(),1,1)='c'][substring(text(),2,1)='j']">
6028                                 <form authority="smd">magnetic disc</form>
6029                         </xsl:if>
6030                         <xsl:if
6031                                 test="marc:controlfield[@tag=007][substring(text(),1,1)='c'][substring(text(),2,1)='m']">
6032                                 <form authority="smd">magneto-optical disc</form>
6033                         </xsl:if>
6034                         <xsl:if
6035                                 test="marc:controlfield[@tag=007][substring(text(),1,1)='c'][substring(text(),2,1)='o']">
6036                                 <form authority="smd">optical disc</form>
6037                         </xsl:if>
6038                         <xsl:if
6039                                 test="marc:controlfield[@tag=007][substring(text(),1,1)='c'][substring(text(),2,1)='r']">
6040                                 <form authority="smd">remote</form>
6041                         </xsl:if>
6042                         <xsl:if
6043                                 test="marc:controlfield[@tag=007][substring(text(),1,1)='c'][substring(text(),2,1)='a']">
6044                                 <form authority="smd">tape cartridge</form>
6045                         </xsl:if>
6046                         <xsl:if
6047                                 test="marc:controlfield[@tag=007][substring(text(),1,1)='c'][substring(text(),2,1)='f']">
6048                                 <form authority="smd">tape cassette</form>
6049                         </xsl:if>
6050                         <xsl:if
6051                                 test="marc:controlfield[@tag=007][substring(text(),1,1)='c'][substring(text(),2,1)='h']">
6052                                 <form authority="smd">tape reel</form>
6053                         </xsl:if>
6054
6055                         <xsl:if
6056                                 test="marc:controlfield[@tag=007][substring(text(),1,1)='d'][substring(text(),2,1)='a']">
6057                                 <form authority="smd">celestial globe</form>
6058                         </xsl:if>
6059                         <xsl:if
6060                                 test="marc:controlfield[@tag=007][substring(text(),1,1)='d'][substring(text(),2,1)='e']">
6061                                 <form authority="smd">earth moon globe</form>
6062                         </xsl:if>
6063                         <xsl:if
6064                                 test="marc:controlfield[@tag=007][substring(text(),1,1)='d'][substring(text(),2,1)='b']">
6065                                 <form authority="smd">planetary or lunar globe</form>
6066                         </xsl:if>
6067                         <xsl:if
6068                                 test="marc:controlfield[@tag=007][substring(text(),1,1)='d'][substring(text(),2,1)='c']">
6069                                 <form authority="smd">terrestrial globe</form>
6070                         </xsl:if>
6071
6072                         <xsl:if
6073                                 test="marc:controlfield[@tag=007][substring(text(),1,1)='o'][substring(text(),2,1)='o']">
6074                                 <form authority="smd">kit</form>
6075                         </xsl:if>
6076
6077                         <xsl:if
6078                                 test="marc:controlfield[@tag=007][substring(text(),1,1)='a'][substring(text(),2,1)='d']">
6079                                 <form authority="smd">atlas</form>
6080                         </xsl:if>
6081                         <xsl:if
6082                                 test="marc:controlfield[@tag=007][substring(text(),1,1)='a'][substring(text(),2,1)='g']">
6083                                 <form authority="smd">diagram</form>
6084                         </xsl:if>
6085                         <xsl:if
6086                                 test="marc:controlfield[@tag=007][substring(text(),1,1)='a'][substring(text(),2,1)='j']">
6087                                 <form authority="smd">map</form>
6088                         </xsl:if>
6089                         <xsl:if
6090                                 test="marc:controlfield[@tag=007][substring(text(),1,1)='a'][substring(text(),2,1)='q']">
6091                                 <form authority="smd">model</form>
6092                         </xsl:if>
6093                         <xsl:if
6094                                 test="marc:controlfield[@tag=007][substring(text(),1,1)='a'][substring(text(),2,1)='k']">
6095                                 <form authority="smd">profile</form>
6096                         </xsl:if>
6097                         <xsl:if
6098                                 test="marc:controlfield[@tag=007][substring(text(),1,1)='a'][substring(text(),2,1)='r']">
6099                                 <form authority="smd">remote-sensing image</form>
6100                         </xsl:if>
6101                         <xsl:if
6102                                 test="marc:controlfield[@tag=007][substring(text(),1,1)='a'][substring(text(),2,1)='s']">
6103                                 <form authority="smd">section</form>
6104                         </xsl:if>
6105                         <xsl:if
6106                                 test="marc:controlfield[@tag=007][substring(text(),1,1)='a'][substring(text(),2,1)='y']">
6107                                 <form authority="smd">view</form>
6108                         </xsl:if>
6109
6110                         <xsl:if
6111                                 test="marc:controlfield[@tag=007][substring(text(),1,1)='h'][substring(text(),2,1)='a']">
6112                                 <form authority="smd">aperture card</form>
6113                         </xsl:if>
6114                         <xsl:if
6115                                 test="marc:controlfield[@tag=007][substring(text(),1,1)='h'][substring(text(),2,1)='e']">
6116                                 <form authority="smd">microfiche</form>
6117                         </xsl:if>
6118                         <xsl:if
6119                                 test="marc:controlfield[@tag=007][substring(text(),1,1)='h'][substring(text(),2,1)='f']">
6120                                 <form authority="smd">microfiche cassette</form>
6121                         </xsl:if>
6122                         <xsl:if
6123                                 test="marc:controlfield[@tag=007][substring(text(),1,1)='h'][substring(text(),2,1)='b']">
6124                                 <form authority="smd">microfilm cartridge</form>
6125                         </xsl:if>
6126                         <xsl:if
6127                                 test="marc:controlfield[@tag=007][substring(text(),1,1)='h'][substring(text(),2,1)='c']">
6128                                 <form authority="smd">microfilm cassette</form>
6129                         </xsl:if>
6130                         <xsl:if
6131                                 test="marc:controlfield[@tag=007][substring(text(),1,1)='h'][substring(text(),2,1)='d']">
6132                                 <form authority="smd">microfilm reel</form>
6133                         </xsl:if>
6134                         <xsl:if
6135                                 test="marc:controlfield[@tag=007][substring(text(),1,1)='h'][substring(text(),2,1)='g']">
6136                                 <form authority="smd">microopaque</form>
6137                         </xsl:if>
6138
6139                         <xsl:if
6140                                 test="marc:controlfield[@tag=007][substring(text(),1,1)='m'][substring(text(),2,1)='c']">
6141                                 <form authority="smd">film cartridge</form>
6142                         </xsl:if>
6143                         <xsl:if
6144                                 test="marc:controlfield[@tag=007][substring(text(),1,1)='m'][substring(text(),2,1)='f']">
6145                                 <form authority="smd">film cassette</form>
6146                         </xsl:if>
6147                         <xsl:if
6148                                 test="marc:controlfield[@tag=007][substring(text(),1,1)='m'][substring(text(),2,1)='r']">
6149                                 <form authority="smd">film reel</form>
6150                         </xsl:if>
6151
6152                         <xsl:if
6153                                 test="marc:controlfield[@tag=007][substring(text(),1,1)='k'][substring(text(),2,1)='n']">
6154                                 <form authority="smd">chart</form>
6155                         </xsl:if>
6156                         <xsl:if
6157                                 test="marc:controlfield[@tag=007][substring(text(),1,1)='k'][substring(text(),2,1)='c']">
6158                                 <form authority="smd">collage</form>
6159                         </xsl:if>
6160                         <xsl:if
6161                                 test="marc:controlfield[@tag=007][substring(text(),1,1)='k'][substring(text(),2,1)='d']">
6162                                 <form authority="smd">drawing</form>
6163                         </xsl:if>
6164                         <xsl:if
6165                                 test="marc:controlfield[@tag=007][substring(text(),1,1)='k'][substring(text(),2,1)='o']">
6166                                 <form authority="smd">flash card</form>
6167                         </xsl:if>
6168                         <xsl:if
6169                                 test="marc:controlfield[@tag=007][substring(text(),1,1)='k'][substring(text(),2,1)='e']">
6170                                 <form authority="smd">painting</form>
6171                         </xsl:if>
6172                         <xsl:if
6173                                 test="marc:controlfield[@tag=007][substring(text(),1,1)='k'][substring(text(),2,1)='f']">
6174                                 <form authority="smd">photomechanical print</form>
6175                         </xsl:if>
6176                         <xsl:if
6177                                 test="marc:controlfield[@tag=007][substring(text(),1,1)='k'][substring(text(),2,1)='g']">
6178                                 <form authority="smd">photonegative</form>
6179                         </xsl:if>
6180                         <xsl:if
6181                                 test="marc:controlfield[@tag=007][substring(text(),1,1)='k'][substring(text(),2,1)='h']">
6182                                 <form authority="smd">photoprint</form>
6183                         </xsl:if>
6184                         <xsl:if
6185                                 test="marc:controlfield[@tag=007][substring(text(),1,1)='k'][substring(text(),2,1)='i']">
6186                                 <form authority="smd">picture</form>
6187                         </xsl:if>
6188                         <xsl:if
6189                                 test="marc:controlfield[@tag=007][substring(text(),1,1)='k'][substring(text(),2,1)='j']">
6190                                 <form authority="smd">print</form>
6191                         </xsl:if>
6192                         <xsl:if
6193                                 test="marc:controlfield[@tag=007][substring(text(),1,1)='k'][substring(text(),2,1)='l']">
6194                                 <form authority="smd">technical drawing</form>
6195                         </xsl:if>
6196
6197                         <xsl:if
6198                                 test="marc:controlfield[@tag=007][substring(text(),1,1)='q'][substring(text(),2,1)='q']">
6199                                 <form authority="smd">notated music</form>
6200                         </xsl:if>
6201
6202                         <xsl:if
6203                                 test="marc:controlfield[@tag=007][substring(text(),1,1)='g'][substring(text(),2,1)='d']">
6204                                 <form authority="smd">filmslip</form>
6205                         </xsl:if>
6206                         <xsl:if
6207                                 test="marc:controlfield[@tag=007][substring(text(),1,1)='g'][substring(text(),2,1)='c']">
6208                                 <form authority="smd">filmstrip cartridge</form>
6209                         </xsl:if>
6210                         <xsl:if
6211                                 test="marc:controlfield[@tag=007][substring(text(),1,1)='g'][substring(text(),2,1)='o']">
6212                                 <form authority="smd">filmstrip roll</form>
6213                         </xsl:if>
6214                         <xsl:if
6215                                 test="marc:controlfield[@tag=007][substring(text(),1,1)='g'][substring(text(),2,1)='f']">
6216                                 <form authority="smd">other filmstrip type</form>
6217                         </xsl:if>
6218                         <xsl:if
6219                                 test="marc:controlfield[@tag=007][substring(text(),1,1)='g'][substring(text(),2,1)='s']">
6220                                 <form authority="smd">slide</form>
6221                         </xsl:if>
6222                         <xsl:if
6223                                 test="marc:controlfield[@tag=007][substring(text(),1,1)='g'][substring(text(),2,1)='t']">
6224                                 <form authority="smd">transparency</form>
6225                         </xsl:if>
6226                         <xsl:if
6227                                 test="marc:controlfield[@tag=007][substring(text(),1,1)='r'][substring(text(),2,1)='r']">
6228                                 <form authority="smd">remote-sensing image</form>
6229                         </xsl:if>
6230                         <xsl:if
6231                                 test="marc:controlfield[@tag=007][substring(text(),1,1)='s'][substring(text(),2,1)='e']">
6232                                 <form authority="smd">cylinder</form>
6233                         </xsl:if>
6234                         <xsl:if
6235                                 test="marc:controlfield[@tag=007][substring(text(),1,1)='s'][substring(text(),2,1)='q']">
6236                                 <form authority="smd">roll</form>
6237                         </xsl:if>
6238                         <xsl:if
6239                                 test="marc:controlfield[@tag=007][substring(text(),1,1)='s'][substring(text(),2,1)='g']">
6240                                 <form authority="smd">sound cartridge</form>
6241                         </xsl:if>
6242                         <xsl:if
6243                                 test="marc:controlfield[@tag=007][substring(text(),1,1)='s'][substring(text(),2,1)='s']">
6244                                 <form authority="smd">sound cassette</form>
6245                         </xsl:if>
6246                         <xsl:if
6247                                 test="marc:controlfield[@tag=007][substring(text(),1,1)='s'][substring(text(),2,1)='d']">
6248                                 <form authority="smd">sound disc</form>
6249                         </xsl:if>
6250                         <xsl:if
6251                                 test="marc:controlfield[@tag=007][substring(text(),1,1)='s'][substring(text(),2,1)='t']">
6252                                 <form authority="smd">sound-tape reel</form>
6253                         </xsl:if>
6254                         <xsl:if
6255                                 test="marc:controlfield[@tag=007][substring(text(),1,1)='s'][substring(text(),2,1)='i']">
6256                                 <form authority="smd">sound-track film</form>
6257                         </xsl:if>
6258                         <xsl:if
6259                                 test="marc:controlfield[@tag=007][substring(text(),1,1)='s'][substring(text(),2,1)='w']">
6260                                 <form authority="smd">wire recording</form>
6261                         </xsl:if>
6262
6263                         <xsl:if
6264                                 test="marc:controlfield[@tag=007][substring(text(),1,1)='f'][substring(text(),2,1)='c']">
6265                                 <form authority="smd">braille</form>
6266                         </xsl:if>
6267                         <xsl:if
6268                                 test="marc:controlfield[@tag=007][substring(text(),1,1)='f'][substring(text(),2,1)='b']">
6269                                 <form authority="smd">combination</form>
6270                         </xsl:if>
6271                         <xsl:if
6272                                 test="marc:controlfield[@tag=007][substring(text(),1,1)='f'][substring(text(),2,1)='a']">
6273                                 <form authority="smd">moon</form>
6274                         </xsl:if>
6275                         <xsl:if
6276                                 test="marc:controlfield[@tag=007][substring(text(),1,1)='f'][substring(text(),2,1)='d']">
6277                                 <form authority="smd">tactile, with no writing system</form>
6278                         </xsl:if>
6279
6280                         <xsl:if
6281                                 test="marc:controlfield[@tag=007][substring(text(),1,1)='t'][substring(text(),2,1)='c']">
6282                                 <form authority="smd">braille</form>
6283                         </xsl:if>
6284                         <xsl:if
6285                                 test="marc:controlfield[@tag=007][substring(text(),1,1)='t'][substring(text(),2,1)='b']">
6286                                 <form authority="smd">large print</form>
6287                         </xsl:if>
6288                         <xsl:if
6289                                 test="marc:controlfield[@tag=007][substring(text(),1,1)='t'][substring(text(),2,1)='a']">
6290                                 <form authority="smd">regular print</form>
6291                         </xsl:if>
6292                         <xsl:if
6293                                 test="marc:controlfield[@tag=007][substring(text(),1,1)='t'][substring(text(),2,1)='d']">
6294                                 <form authority="smd">text in looseleaf binder</form>
6295                         </xsl:if>
6296
6297                         <xsl:if
6298                                 test="marc:controlfield[@tag=007][substring(text(),1,1)='v'][substring(text(),2,1)='c']">
6299                                 <form authority="smd">videocartridge</form>
6300                         </xsl:if>
6301                         <xsl:if
6302                                 test="marc:controlfield[@tag=007][substring(text(),1,1)='v'][substring(text(),2,1)='f']">
6303                                 <form authority="smd">videocassette</form>
6304                         </xsl:if>
6305                         <xsl:if
6306                                 test="marc:controlfield[@tag=007][substring(text(),1,1)='v'][substring(text(),2,1)='d']">
6307                                 <form authority="smd">videodisc</form>
6308                         </xsl:if>
6309                         <xsl:if
6310                                 test="marc:controlfield[@tag=007][substring(text(),1,1)='v'][substring(text(),2,1)='r']">
6311                                 <form authority="smd">videoreel</form>
6312                         </xsl:if>
6313
6314                         <xsl:for-each
6315                                 select="marc:datafield[@tag=856]/marc:subfield[@code='q'][string-length(.)&gt;1]">
6316                                 <internetMediaType>
6317                                         <xsl:value-of select="."/>
6318                                 </internetMediaType>
6319                         </xsl:for-each>
6320                         <xsl:for-each select="marc:datafield[@tag=300]">
6321                                 <extent>
6322                                         <xsl:call-template name="subfieldSelect">
6323                                                 <xsl:with-param name="codes">abce</xsl:with-param>
6324                                         </xsl:call-template>
6325                                 </extent>
6326                         </xsl:for-each>
6327                 </xsl:variable>
6328                 <xsl:if test="string-length(normalize-space($physicalDescription))">
6329                         <physicalDescription>
6330                                 <xsl:copy-of select="$physicalDescription"/>
6331                         </physicalDescription>
6332                 </xsl:if>
6333                 <xsl:for-each select="marc:datafield[@tag=520]">
6334                         <abstract>
6335                                 <xsl:call-template name="uri"/>
6336                                 <xsl:call-template name="subfieldSelect">
6337                                         <xsl:with-param name="codes">ab</xsl:with-param>
6338                                 </xsl:call-template>
6339                         </abstract>
6340                 </xsl:for-each>
6341                 <xsl:for-each select="marc:datafield[@tag=505]">
6342                         <tableOfContents>
6343                                 <xsl:call-template name="uri"/>
6344                                 <xsl:call-template name="subfieldSelect">
6345                                         <xsl:with-param name="codes">agrt</xsl:with-param>
6346                                 </xsl:call-template>
6347                         </tableOfContents>
6348                 </xsl:for-each>
6349                 <xsl:for-each select="marc:datafield[@tag=521]">
6350                         <targetAudience>
6351                                 <xsl:call-template name="subfieldSelect">
6352                                         <xsl:with-param name="codes">ab</xsl:with-param>
6353                                 </xsl:call-template>
6354                         </targetAudience>
6355                 </xsl:for-each>
6356                 <xsl:if test="$typeOf008='BK' or $typeOf008='CF' or $typeOf008='MU' or $typeOf008='VM'">
6357                         <xsl:variable name="controlField008-22" select="substring($controlField008,23,1)"/>
6358                         <xsl:choose>
6359                                 <!-- 01/04 fix -->
6360                                 <xsl:when test="$controlField008-22='d'">
6361                                         <targetAudience authority="marctarget">adolescent</targetAudience>
6362                                 </xsl:when>
6363                                 <xsl:when test="$controlField008-22='e'">
6364                                         <targetAudience authority="marctarget">adult</targetAudience>
6365                                 </xsl:when>
6366                                 <xsl:when test="$controlField008-22='g'">
6367                                         <targetAudience authority="marctarget">general</targetAudience>
6368                                 </xsl:when>
6369                                 <xsl:when
6370                                         test="$controlField008-22='b' or $controlField008-22='c' or $controlField008-22='j'">
6371                                         <targetAudience authority="marctarget">juvenile</targetAudience>
6372                                 </xsl:when>
6373                                 <xsl:when test="$controlField008-22='a'">
6374                                         <targetAudience authority="marctarget">preschool</targetAudience>
6375                                 </xsl:when>
6376                                 <xsl:when test="$controlField008-22='f'">
6377                                         <targetAudience authority="marctarget">specialized</targetAudience>
6378                                 </xsl:when>
6379                         </xsl:choose>
6380                 </xsl:if>
6381                 <xsl:for-each select="marc:datafield[@tag=245]/marc:subfield[@code='c']">
6382                         <note type="statement of responsibility">
6383                                 <xsl:value-of select="."/>
6384                         </note>
6385                 </xsl:for-each>
6386                 <xsl:for-each select="marc:datafield[@tag=500]">
6387                         <note>
6388                                 <xsl:value-of select="marc:subfield[@code='a']"/>
6389                                 <xsl:call-template name="uri"/>
6390                         </note>
6391                 </xsl:for-each>
6392
6393                 <!--3.2 change tmee additional note fields-->
6394
6395                 <xsl:for-each select="marc:datafield[@tag=502]">
6396                         <note type="thesis">
6397                                 <xsl:call-template name="uri"/>
6398                                 <xsl:variable name="str">
6399                                         <xsl:for-each select="marc:subfield[@code!='6' or @code!='8']">
6400                                                 <xsl:value-of select="."/>
6401                                                 <xsl:text> </xsl:text>
6402                                         </xsl:for-each>
6403                                 </xsl:variable>
6404                                 <xsl:value-of select="substring($str,1,string-length($str)-1)"/>
6405                         </note>
6406                 </xsl:for-each>
6407
6408                 <xsl:for-each select="marc:datafield[@tag=504]">
6409                         <note type="bibliography">
6410                                 <xsl:call-template name="uri"/>
6411                                 <xsl:variable name="str">
6412                                         <xsl:for-each select="marc:subfield[@code!='6' or @code!='8']">
6413                                                 <xsl:value-of select="."/>
6414                                                 <xsl:text> </xsl:text>
6415                                         </xsl:for-each>
6416                                 </xsl:variable>
6417                                 <xsl:value-of select="substring($str,1,string-length($str)-1)"/>
6418                         </note>
6419                 </xsl:for-each>
6420
6421                 <xsl:for-each select="marc:datafield[@tag=508]">
6422                         <note type="creation/production credits">
6423                                 <xsl:call-template name="uri"/>
6424                                 <xsl:variable name="str">
6425                                         <xsl:for-each select="marc:subfield[@code!='6' or @code!='8']">
6426                                                 <xsl:value-of select="."/>
6427                                                 <xsl:text> </xsl:text>
6428                                         </xsl:for-each>
6429                                 </xsl:variable>
6430                                 <xsl:value-of select="substring($str,1,string-length($str)-1)"/>
6431                         </note>
6432                 </xsl:for-each>
6433
6434                 <xsl:for-each select="marc:datafield[@tag=506]">
6435                         <note type="restrictions">
6436                                 <xsl:call-template name="uri"/>
6437                                 <xsl:variable name="str">
6438                                         <xsl:for-each select="marc:subfield[@code!='6' or @code!='8']">
6439                                                 <xsl:value-of select="."/>
6440                                                 <xsl:text> </xsl:text>
6441                                         </xsl:for-each>
6442                                 </xsl:variable>
6443                                 <xsl:value-of select="substring($str,1,string-length($str)-1)"/>
6444                         </note>
6445                 </xsl:for-each>
6446
6447                 <xsl:for-each select="marc:datafield[@tag=510]">
6448                         <note type="citation/reference">
6449                                 <xsl:call-template name="uri"/>
6450                                 <xsl:variable name="str">
6451                                         <xsl:for-each select="marc:subfield[@code!='6' or @code!='8']">
6452                                                 <xsl:value-of select="."/>
6453                                                 <xsl:text> </xsl:text>
6454                                         </xsl:for-each>
6455                                 </xsl:variable>
6456                                 <xsl:value-of select="substring($str,1,string-length($str)-1)"/>
6457                         </note>
6458                 </xsl:for-each>
6459
6460
6461                 <xsl:for-each select="marc:datafield[@tag=511]">
6462                         <note type="performers">
6463                                 <xsl:call-template name="uri"/>
6464                                 <xsl:value-of select="marc:subfield[@code='a']"/>
6465                         </note>
6466                 </xsl:for-each>
6467                 <xsl:for-each select="marc:datafield[@tag=518]">
6468                         <note type="venue">
6469                                 <xsl:call-template name="uri"/>
6470                                 <xsl:value-of select="marc:subfield[@code='a']"/>
6471                         </note>
6472                 </xsl:for-each>
6473
6474                 <xsl:for-each select="marc:datafield[@tag=530]">
6475                         <note type="additional physical form">
6476                                 <xsl:call-template name="uri"/>
6477                                 <xsl:variable name="str">
6478                                         <xsl:for-each select="marc:subfield[@code!='6' or @code!='8']">
6479                                                 <xsl:value-of select="."/>
6480                                                 <xsl:text> </xsl:text>
6481                                         </xsl:for-each>
6482                                 </xsl:variable>
6483                                 <xsl:value-of select="substring($str,1,string-length($str)-1)"/>
6484                         </note>
6485                 </xsl:for-each>
6486
6487                 <xsl:for-each select="marc:datafield[@tag=533]">
6488                         <note type="reproduction">
6489                                 <xsl:call-template name="uri"/>
6490                                 <xsl:variable name="str">
6491                                         <xsl:for-each select="marc:subfield[@code!='6' or @code!='8']">
6492                                                 <xsl:value-of select="."/>
6493                                                 <xsl:text> </xsl:text>
6494                                         </xsl:for-each>
6495                                 </xsl:variable>
6496                                 <xsl:value-of select="substring($str,1,string-length($str)-1)"/>
6497                         </note>
6498                 </xsl:for-each>
6499
6500                 <xsl:for-each select="marc:datafield[@tag=534]">
6501                         <note type="original version">
6502                                 <xsl:call-template name="uri"/>
6503                                 <xsl:variable name="str">
6504                                         <xsl:for-each select="marc:subfield[@code!='6' or @code!='8']">
6505                                                 <xsl:value-of select="."/>
6506                                                 <xsl:text> </xsl:text>
6507                                         </xsl:for-each>
6508                                 </xsl:variable>
6509                                 <xsl:value-of select="substring($str,1,string-length($str)-1)"/>
6510                         </note>
6511                 </xsl:for-each>
6512
6513                 <xsl:for-each select="marc:datafield[@tag=538]">
6514                         <note type="system details">
6515                                 <xsl:call-template name="uri"/>
6516                                 <xsl:variable name="str">
6517                                         <xsl:for-each select="marc:subfield[@code!='6' or @code!='8']">
6518                                                 <xsl:value-of select="."/>
6519                                                 <xsl:text> </xsl:text>
6520                                         </xsl:for-each>
6521                                 </xsl:variable>
6522                                 <xsl:value-of select="substring($str,1,string-length($str)-1)"/>
6523                         </note>
6524                 </xsl:for-each>
6525
6526                 <xsl:for-each select="marc:datafield[@tag=583]">
6527                         <note type="action">
6528                                 <xsl:call-template name="uri"/>
6529                                 <xsl:variable name="str">
6530                                         <xsl:for-each select="marc:subfield[@code!='6' or @code!='8']">
6531                                                 <xsl:value-of select="."/>
6532                                                 <xsl:text> </xsl:text>
6533                                         </xsl:for-each>
6534                                 </xsl:variable>
6535                                 <xsl:value-of select="substring($str,1,string-length($str)-1)"/>
6536                         </note>
6537                 </xsl:for-each>
6538
6539                 <xsl:for-each
6540                         select="marc:datafield[@tag=501 or @tag=507 or @tag=513 or @tag=514 or @tag=515 or @tag=516 or @tag=522 or @tag=524 or @tag=525 or @tag=526 or @tag=535 or @tag=536 or @tag=540 or @tag=541 or @tag=544 or @tag=545 or @tag=546 or @tag=547 or @tag=550 or @tag=552 or @tag=555 or @tag=556 or @tag=561 or @tag=562 or @tag=565 or @tag=567 or @tag=580 or @tag=581 or @tag=584 or @tag=585 or @tag=586]">
6541                         <note>
6542                                 <xsl:call-template name="uri"/>
6543                                 <xsl:variable name="str">
6544                                         <xsl:for-each select="marc:subfield[@code!='6' or @code!='8']">
6545                                                 <xsl:value-of select="."/>
6546                                                 <xsl:text> </xsl:text>
6547                                         </xsl:for-each>
6548                                 </xsl:variable>
6549                                 <xsl:value-of select="substring($str,1,string-length($str)-1)"/>
6550                         </note>
6551                 </xsl:for-each>
6552                 <xsl:for-each
6553                         select="marc:datafield[@tag=034][marc:subfield[@code='d' or @code='e' or @code='f' or @code='g']]">
6554                         <subject>
6555                                 <cartographics>
6556                                         <coordinates>
6557                                                 <xsl:call-template name="subfieldSelect">
6558                                                         <xsl:with-param name="codes">defg</xsl:with-param>
6559                                                 </xsl:call-template>
6560                                         </coordinates>
6561                                 </cartographics>
6562                         </subject>
6563                 </xsl:for-each>
6564                 <xsl:for-each select="marc:datafield[@tag=043]">
6565                         <subject>
6566                                 <xsl:for-each select="marc:subfield[@code='a' or @code='b' or @code='c']">
6567                                         <geographicCode>
6568                                                 <xsl:attribute name="authority">
6569                                                         <xsl:if test="@code='a'">
6570                                                                 <xsl:text>marcgac</xsl:text>
6571                                                         </xsl:if>
6572                                                         <xsl:if test="@code='b'">
6573                                                                 <xsl:value-of select="following-sibling::marc:subfield[@code=2]"/>
6574                                                         </xsl:if>
6575                                                         <xsl:if test="@code='c'">
6576                                                                 <xsl:text>iso3166</xsl:text>
6577                                                         </xsl:if>
6578                                                 </xsl:attribute>
6579                                                 <xsl:value-of select="self::marc:subfield"/>
6580                                         </geographicCode>
6581                                 </xsl:for-each>
6582                         </subject>
6583                 </xsl:for-each>
6584                 <!-- tmee 2006/11/27 -->
6585                 <xsl:for-each select="marc:datafield[@tag=255]">
6586                         <subject>
6587                                 <xsl:for-each select="marc:subfield[@code='a' or @code='b' or @code='c']">
6588                                         <cartographics>
6589                                                 <xsl:if test="@code='a'">
6590                                                         <scale>
6591                                                                 <xsl:value-of select="."/>
6592                                                         </scale>
6593                                                 </xsl:if>
6594                                                 <xsl:if test="@code='b'">
6595                                                         <projection>
6596                                                                 <xsl:value-of select="."/>
6597                                                         </projection>
6598                                                 </xsl:if>
6599                                                 <xsl:if test="@code='c'">
6600                                                         <coordinates>
6601                                                                 <xsl:value-of select="."/>
6602                                                         </coordinates>
6603                                                 </xsl:if>
6604                                         </cartographics>
6605                                 </xsl:for-each>
6606                         </subject>
6607                 </xsl:for-each>
6608
6609                 <xsl:apply-templates select="marc:datafield[653 &gt;= @tag and @tag &gt;= 600]"/>
6610                 <xsl:apply-templates select="marc:datafield[@tag=656]"/>
6611                 <xsl:for-each select="marc:datafield[@tag=752 or @tag=662]">
6612                         <subject>
6613                                 <hierarchicalGeographic>
6614                                         <xsl:for-each select="marc:subfield[@code='a']">
6615                                                 <country>
6616                                                         <xsl:call-template name="chopPunctuation">
6617                                                                 <xsl:with-param name="chopString" select="."/>
6618                                                         </xsl:call-template>
6619                                                 </country>
6620                                         </xsl:for-each>
6621                                         <xsl:for-each select="marc:subfield[@code='b']">
6622                                                 <state>
6623                                                         <xsl:call-template name="chopPunctuation">
6624                                                                 <xsl:with-param name="chopString" select="."/>
6625                                                         </xsl:call-template>
6626                                                 </state>
6627                                         </xsl:for-each>
6628                                         <xsl:for-each select="marc:subfield[@code='c']">
6629                                                 <county>
6630                                                         <xsl:call-template name="chopPunctuation">
6631                                                                 <xsl:with-param name="chopString" select="."/>
6632                                                         </xsl:call-template>
6633                                                 </county>
6634                                         </xsl:for-each>
6635                                         <xsl:for-each select="marc:subfield[@code='d']">
6636                                                 <city>
6637                                                         <xsl:call-template name="chopPunctuation">
6638                                                                 <xsl:with-param name="chopString" select="."/>
6639                                                         </xsl:call-template>
6640                                                 </city>
6641                                         </xsl:for-each>
6642                                         <xsl:for-each select="marc:subfield[@code='e']">
6643                                                 <citySection>
6644                                                         <xsl:call-template name="chopPunctuation">
6645                                                                 <xsl:with-param name="chopString" select="."/>
6646                                                         </xsl:call-template>
6647                                                 </citySection>
6648                                         </xsl:for-each>
6649                                         <xsl:for-each select="marc:subfield[@code='g']">
6650                                                 <region>
6651                                                         <xsl:call-template name="chopPunctuation">
6652                                                                 <xsl:with-param name="chopString" select="."/>
6653                                                         </xsl:call-template>
6654                                                 </region>
6655                                         </xsl:for-each>
6656                                         <xsl:for-each select="marc:subfield[@code='h']">
6657                                                 <extraterrestrialArea>
6658                                                         <xsl:call-template name="chopPunctuation">
6659                                                                 <xsl:with-param name="chopString" select="."/>
6660                                                         </xsl:call-template>
6661                                                 </extraterrestrialArea>
6662                                         </xsl:for-each>
6663                                 </hierarchicalGeographic>
6664                         </subject>
6665                 </xsl:for-each>
6666                 <xsl:for-each select="marc:datafield[@tag=045][marc:subfield[@code='b']]">
6667                         <subject>
6668                                 <xsl:choose>
6669                                         <xsl:when test="@ind1=2">
6670                                                 <temporal encoding="iso8601" point="start">
6671                                                         <xsl:call-template name="chopPunctuation">
6672                                                                 <xsl:with-param name="chopString">
6673                                                                         <xsl:value-of select="marc:subfield[@code='b'][1]"/>
6674                                                                 </xsl:with-param>
6675                                                         </xsl:call-template>
6676                                                 </temporal>
6677                                                 <temporal encoding="iso8601" point="end">
6678                                                         <xsl:call-template name="chopPunctuation">
6679                                                                 <xsl:with-param name="chopString">
6680                                                                         <xsl:value-of select="marc:subfield[@code='b'][2]"/>
6681                                                                 </xsl:with-param>
6682                                                         </xsl:call-template>
6683                                                 </temporal>
6684                                         </xsl:when>
6685                                         <xsl:otherwise>
6686                                                 <xsl:for-each select="marc:subfield[@code='b']">
6687                                                         <temporal encoding="iso8601">
6688                                                                 <xsl:call-template name="chopPunctuation">
6689                                                                         <xsl:with-param name="chopString" select="."/>
6690                                                                 </xsl:call-template>
6691                                                         </temporal>
6692                                                 </xsl:for-each>
6693                                         </xsl:otherwise>
6694                                 </xsl:choose>
6695                         </subject>
6696                 </xsl:for-each>
6697                 <xsl:for-each select="marc:datafield[@tag=050]">
6698                         <xsl:for-each select="marc:subfield[@code='b']">
6699                                 <classification authority="lcc">
6700                                         <xsl:if test="../marc:subfield[@code='3']">
6701                                                 <xsl:attribute name="displayLabel">
6702                                                         <xsl:value-of select="../marc:subfield[@code='3']"/>
6703                                                 </xsl:attribute>
6704                                         </xsl:if>
6705                                         <xsl:value-of select="preceding-sibling::marc:subfield[@code='a'][1]"/>
6706                                         <xsl:text> </xsl:text>
6707                                         <xsl:value-of select="text()"/>
6708                                 </classification>
6709                         </xsl:for-each>
6710                         <xsl:for-each
6711                                 select="marc:subfield[@code='a'][not(following-sibling::marc:subfield[@code='b'])]">
6712                                 <classification authority="lcc">
6713                                         <xsl:if test="../marc:subfield[@code='3']">
6714                                                 <xsl:attribute name="displayLabel">
6715                                                         <xsl:value-of select="../marc:subfield[@code='3']"/>
6716                                                 </xsl:attribute>
6717                                         </xsl:if>
6718                                         <xsl:value-of select="text()"/>
6719                                 </classification>
6720                         </xsl:for-each>
6721                 </xsl:for-each>
6722                 <xsl:for-each select="marc:datafield[@tag=082]">
6723                         <classification authority="ddc">
6724                                 <xsl:if test="marc:subfield[@code='2']">
6725                                         <xsl:attribute name="edition">
6726                                                 <xsl:value-of select="marc:subfield[@code='2']"/>
6727                                         </xsl:attribute>
6728                                 </xsl:if>
6729                                 <xsl:call-template name="subfieldSelect">
6730                                         <xsl:with-param name="codes">ab</xsl:with-param>
6731                                 </xsl:call-template>
6732                         </classification>
6733                 </xsl:for-each>
6734                 <xsl:for-each select="marc:datafield[@tag=080]">
6735                         <classification authority="udc">
6736                                 <xsl:call-template name="subfieldSelect">
6737                                         <xsl:with-param name="codes">abx</xsl:with-param>
6738                                 </xsl:call-template>
6739                         </classification>
6740                 </xsl:for-each>
6741                 <xsl:for-each select="marc:datafield[@tag=060]">
6742                         <classification authority="nlm">
6743                                 <xsl:call-template name="subfieldSelect">
6744                                         <xsl:with-param name="codes">ab</xsl:with-param>
6745                                 </xsl:call-template>
6746                         </classification>
6747                 </xsl:for-each>
6748                 <xsl:for-each select="marc:datafield[@tag=086][@ind1=0]">
6749                         <classification authority="sudocs">
6750                                 <xsl:value-of select="marc:subfield[@code='a']"/>
6751                         </classification>
6752                 </xsl:for-each>
6753                 <xsl:for-each select="marc:datafield[@tag=086][@ind1=1]">
6754                         <classification authority="candoc">
6755                                 <xsl:value-of select="marc:subfield[@code='a']"/>
6756                         </classification>
6757                 </xsl:for-each>
6758                 <xsl:for-each select="marc:datafield[@tag=086]">
6759                         <classification>
6760                                 <xsl:attribute name="authority">
6761                                         <xsl:value-of select="marc:subfield[@code='2']"/>
6762                                 </xsl:attribute>
6763                                 <xsl:value-of select="marc:subfield[@code='a']"/>
6764                         </classification>
6765                 </xsl:for-each>
6766                 <xsl:for-each select="marc:datafield[@tag=084]">
6767                         <classification>
6768                                 <xsl:attribute name="authority">
6769                                         <xsl:value-of select="marc:subfield[@code='2']"/>
6770                                 </xsl:attribute>
6771                                 <xsl:call-template name="subfieldSelect">
6772                                         <xsl:with-param name="codes">ab</xsl:with-param>
6773                                 </xsl:call-template>
6774                         </classification>
6775                 </xsl:for-each>
6776                 <xsl:for-each select="marc:datafield[@tag=440]">
6777                         <relatedItem type="series">
6778                                 <titleInfo>
6779                                         <title>
6780                                                 <xsl:call-template name="chopPunctuation">
6781                                                         <xsl:with-param name="chopString">
6782                                                                 <xsl:call-template name="subfieldSelect">
6783                                                                         <xsl:with-param name="codes">av</xsl:with-param>
6784                                                                 </xsl:call-template>
6785                                                         </xsl:with-param>
6786                                                 </xsl:call-template>
6787                                         </title>
6788                                         <xsl:call-template name="part"/>
6789                                 </titleInfo>
6790                         </relatedItem>
6791                 </xsl:for-each>
6792                 <xsl:for-each select="marc:datafield[@tag=490][@ind1=0]">
6793                         <relatedItem type="series">
6794                                 <titleInfo>
6795                                         <title>
6796                                                 <xsl:call-template name="chopPunctuation">
6797                                                         <xsl:with-param name="chopString">
6798                                                                 <xsl:call-template name="subfieldSelect">
6799                                                                         <xsl:with-param name="codes">av</xsl:with-param>
6800                                                                 </xsl:call-template>
6801                                                         </xsl:with-param>
6802                                                 </xsl:call-template>
6803                                         </title>
6804                                         <xsl:call-template name="part"/>
6805                                 </titleInfo>
6806                         </relatedItem>
6807                 </xsl:for-each>
6808                 <xsl:for-each select="marc:datafield[@tag=510]">
6809                         <relatedItem type="isReferencedBy">
6810                                 <note>
6811                                         <xsl:call-template name="subfieldSelect">
6812                                                 <xsl:with-param name="codes">abcx3</xsl:with-param>
6813                                         </xsl:call-template>
6814                                 </note>
6815                         </relatedItem>
6816                 </xsl:for-each>
6817                 <xsl:for-each select="marc:datafield[@tag=534]">
6818                         <relatedItem type="original">
6819                                 <xsl:call-template name="relatedTitle"/>
6820                                 <xsl:call-template name="relatedName"/>
6821                                 <xsl:if test="marc:subfield[@code='b' or @code='c']">
6822                                         <originInfo>
6823                                                 <xsl:for-each select="marc:subfield[@code='c']">
6824                                                         <publisher>
6825                                                                 <xsl:value-of select="."/>
6826                                                         </publisher>
6827                                                 </xsl:for-each>
6828                                                 <xsl:for-each select="marc:subfield[@code='b']">
6829                                                         <edition>
6830                                                                 <xsl:value-of select="."/>
6831                                                         </edition>
6832                                                 </xsl:for-each>
6833                                         </originInfo>
6834                                 </xsl:if>
6835                                 <xsl:call-template name="relatedIdentifierISSN"/>
6836                                 <xsl:for-each select="marc:subfield[@code='z']">
6837                                         <identifier type="isbn">
6838                                                 <xsl:value-of select="."/>
6839                                         </identifier>
6840                                 </xsl:for-each>
6841                                 <xsl:call-template name="relatedNote"/>
6842                         </relatedItem>
6843                 </xsl:for-each>
6844                 <xsl:for-each select="marc:datafield[@tag=700][marc:subfield[@code='t']]">
6845                         <relatedItem>
6846                                 <xsl:call-template name="constituentOrRelatedType"/>
6847                                 <titleInfo>
6848                                         <title>
6849                                                 <xsl:call-template name="chopPunctuation">
6850                                                         <xsl:with-param name="chopString">
6851                                                                 <xsl:call-template name="specialSubfieldSelect">
6852                                                                         <xsl:with-param name="anyCodes">tfklmorsv</xsl:with-param>
6853                                                                         <xsl:with-param name="axis">t</xsl:with-param>
6854                                                                         <xsl:with-param name="afterCodes">g</xsl:with-param>
6855                                                                 </xsl:call-template>
6856                                                         </xsl:with-param>
6857                                                 </xsl:call-template>
6858                                         </title>
6859                                         <xsl:call-template name="part"/>
6860                                 </titleInfo>
6861                                 <name type="personal">
6862                                         <namePart>
6863                                                 <xsl:call-template name="specialSubfieldSelect">
6864                                                         <xsl:with-param name="anyCodes">aq</xsl:with-param>
6865                                                         <xsl:with-param name="axis">t</xsl:with-param>
6866                                                         <xsl:with-param name="beforeCodes">g</xsl:with-param>
6867                                                 </xsl:call-template>
6868                                         </namePart>
6869                                         <xsl:call-template name="termsOfAddress"/>
6870                                         <xsl:call-template name="nameDate"/>
6871                                         <xsl:call-template name="role"/>
6872                                 </name>
6873                                 <xsl:call-template name="relatedForm"/>
6874                                 <xsl:call-template name="relatedIdentifierISSN"/>
6875                         </relatedItem>
6876                 </xsl:for-each>
6877                 <xsl:for-each select="marc:datafield[@tag=710][marc:subfield[@code='t']]">
6878                         <relatedItem>
6879                                 <xsl:call-template name="constituentOrRelatedType"/>
6880                                 <titleInfo>
6881                                         <title>
6882                                                 <xsl:call-template name="chopPunctuation">
6883                                                         <xsl:with-param name="chopString">
6884                                                                 <xsl:call-template name="specialSubfieldSelect">
6885                                                                         <xsl:with-param name="anyCodes">tfklmorsv</xsl:with-param>
6886                                                                         <xsl:with-param name="axis">t</xsl:with-param>
6887                                                                         <xsl:with-param name="afterCodes">dg</xsl:with-param>
6888                                                                 </xsl:call-template>
6889                                                         </xsl:with-param>
6890                                                 </xsl:call-template>
6891                                         </title>
6892                                         <xsl:call-template name="relatedPartNumName"/>
6893                                 </titleInfo>
6894                                 <name type="corporate">
6895                                         <xsl:for-each select="marc:subfield[@code='a']">
6896                                                 <namePart>
6897                                                         <xsl:value-of select="."/>
6898                                                 </namePart>
6899                                         </xsl:for-each>
6900                                         <xsl:for-each select="marc:subfield[@code='b']">
6901                                                 <namePart>
6902                                                         <xsl:value-of select="."/>
6903                                                 </namePart>
6904                                         </xsl:for-each>
6905                                         <xsl:variable name="tempNamePart">
6906                                                 <xsl:call-template name="specialSubfieldSelect">
6907                                                         <xsl:with-param name="anyCodes">c</xsl:with-param>
6908                                                         <xsl:with-param name="axis">t</xsl:with-param>
6909                                                         <xsl:with-param name="beforeCodes">dgn</xsl:with-param>
6910                                                 </xsl:call-template>
6911                                         </xsl:variable>
6912                                         <xsl:if test="normalize-space($tempNamePart)">
6913                                                 <namePart>
6914                                                         <xsl:value-of select="$tempNamePart"/>
6915                                                 </namePart>
6916                                         </xsl:if>
6917                                         <xsl:call-template name="role"/>
6918                                 </name>
6919                                 <xsl:call-template name="relatedForm"/>
6920                                 <xsl:call-template name="relatedIdentifierISSN"/>
6921                         </relatedItem>
6922                 </xsl:for-each>
6923                 <xsl:for-each select="marc:datafield[@tag=711][marc:subfield[@code='t']]">
6924                         <relatedItem>
6925                                 <xsl:call-template name="constituentOrRelatedType"/>
6926                                 <titleInfo>
6927                                         <title>
6928                                                 <xsl:call-template name="chopPunctuation">
6929                                                         <xsl:with-param name="chopString">
6930                                                                 <xsl:call-template name="specialSubfieldSelect">
6931                                                                         <xsl:with-param name="anyCodes">tfklsv</xsl:with-param>
6932                                                                         <xsl:with-param name="axis">t</xsl:with-param>
6933                                                                         <xsl:with-param name="afterCodes">g</xsl:with-param>
6934                                                                 </xsl:call-template>
6935                                                         </xsl:with-param>
6936                                                 </xsl:call-template>
6937                                         </title>
6938                                         <xsl:call-template name="relatedPartNumName"/>
6939                                 </titleInfo>
6940                                 <name type="conference">
6941                                         <namePart>
6942                                                 <xsl:call-template name="specialSubfieldSelect">
6943                                                         <xsl:with-param name="anyCodes">aqdc</xsl:with-param>
6944                                                         <xsl:with-param name="axis">t</xsl:with-param>
6945                                                         <xsl:with-param name="beforeCodes">gn</xsl:with-param>
6946                                                 </xsl:call-template>
6947                                         </namePart>
6948                                 </name>
6949                                 <xsl:call-template name="relatedForm"/>
6950                                 <xsl:call-template name="relatedIdentifierISSN"/>
6951                         </relatedItem>
6952                 </xsl:for-each>
6953                 <xsl:for-each select="marc:datafield[@tag=730][@ind2=2]">
6954                         <relatedItem>
6955                                 <xsl:call-template name="constituentOrRelatedType"/>
6956                                 <titleInfo>
6957                                         <title>
6958                                                 <xsl:call-template name="chopPunctuation">
6959                                                         <xsl:with-param name="chopString">
6960                                                                 <xsl:call-template name="subfieldSelect">
6961                                                                         <xsl:with-param name="codes">adfgklmorsv</xsl:with-param>
6962                                                                 </xsl:call-template>
6963                                                         </xsl:with-param>
6964                                                 </xsl:call-template>
6965                                         </title>
6966                                         <xsl:call-template name="part"/>
6967                                 </titleInfo>
6968                                 <xsl:call-template name="relatedForm"/>
6969                                 <xsl:call-template name="relatedIdentifierISSN"/>
6970                         </relatedItem>
6971                 </xsl:for-each>
6972                 <xsl:for-each select="marc:datafield[@tag=740][@ind2=2]">
6973                         <relatedItem>
6974                                 <xsl:call-template name="constituentOrRelatedType"/>
6975                                 <titleInfo>
6976                                         <title>
6977                                                 <xsl:call-template name="chopPunctuation">
6978                                                         <xsl:with-param name="chopString">
6979                                                                 <xsl:value-of select="marc:subfield[@code='a']"/>
6980                                                         </xsl:with-param>
6981                                                 </xsl:call-template>
6982                                         </title>
6983                                         <xsl:call-template name="part"/>
6984                                 </titleInfo>
6985                                 <xsl:call-template name="relatedForm"/>
6986                         </relatedItem>
6987                 </xsl:for-each>
6988                 <xsl:for-each select="marc:datafield[@tag=760]|marc:datafield[@tag=762]">
6989                         <relatedItem type="series">
6990                                 <xsl:call-template name="relatedItem76X-78X"/>
6991                         </relatedItem>
6992                 </xsl:for-each>
6993                 <xsl:for-each
6994                         select="marc:datafield[@tag=765]|marc:datafield[@tag=767]|marc:datafield[@tag=777]|marc:datafield[@tag=787]">
6995                         <relatedItem>
6996                                 <xsl:call-template name="relatedItem76X-78X"/>
6997                         </relatedItem>
6998                 </xsl:for-each>
6999                 <xsl:for-each select="marc:datafield[@tag=775]">
7000                         <relatedItem type="otherVersion">
7001                                 <xsl:call-template name="relatedItem76X-78X"/>
7002                         </relatedItem>
7003                 </xsl:for-each>
7004                 <xsl:for-each select="marc:datafield[@tag=770]|marc:datafield[@tag=774]">
7005                         <relatedItem type="constituent">
7006                                 <xsl:call-template name="relatedItem76X-78X"/>
7007                         </relatedItem>
7008                 </xsl:for-each>
7009                 <xsl:for-each select="marc:datafield[@tag=772]|marc:datafield[@tag=773]">
7010                         <relatedItem type="host">
7011                                 <xsl:call-template name="relatedItem76X-78X"/>
7012                         </relatedItem>
7013                 </xsl:for-each>
7014                 <xsl:for-each select="marc:datafield[@tag=776]">
7015                         <relatedItem type="otherFormat">
7016                                 <xsl:call-template name="relatedItem76X-78X"/>
7017                         </relatedItem>
7018                 </xsl:for-each>
7019                 <xsl:for-each select="marc:datafield[@tag=780]">
7020                         <relatedItem type="preceding">
7021                                 <xsl:call-template name="relatedItem76X-78X"/>
7022                         </relatedItem>
7023                 </xsl:for-each>
7024                 <xsl:for-each select="marc:datafield[@tag=785]">
7025                         <relatedItem type="succeeding">
7026                                 <xsl:call-template name="relatedItem76X-78X"/>
7027                         </relatedItem>
7028                 </xsl:for-each>
7029                 <xsl:for-each select="marc:datafield[@tag=786]">
7030                         <relatedItem type="original">
7031                                 <xsl:call-template name="relatedItem76X-78X"/>
7032                         </relatedItem>
7033                 </xsl:for-each>
7034                 <xsl:for-each select="marc:datafield[@tag=800]">
7035                         <relatedItem type="series">
7036                                 <titleInfo>
7037                                         <title>
7038                                                 <xsl:call-template name="chopPunctuation">
7039                                                         <xsl:with-param name="chopString">
7040                                                                 <xsl:call-template name="specialSubfieldSelect">
7041                                                                         <xsl:with-param name="anyCodes">tfklmorsv</xsl:with-param>
7042                                                                         <xsl:with-param name="axis">t</xsl:with-param>
7043                                                                         <xsl:with-param name="afterCodes">g</xsl:with-param>
7044                                                                 </xsl:call-template>
7045                                                         </xsl:with-param>
7046                                                 </xsl:call-template>
7047                                         </title>
7048                                         <xsl:call-template name="part"/>
7049                                 </titleInfo>
7050                                 <name type="personal">
7051                                         <namePart>
7052                                                 <xsl:call-template name="chopPunctuation">
7053                                                         <xsl:with-param name="chopString">
7054                                                                 <xsl:call-template name="specialSubfieldSelect">
7055                                                                         <xsl:with-param name="anyCodes">aq</xsl:with-param>
7056                                                                         <xsl:with-param name="axis">t</xsl:with-param>
7057                                                                         <xsl:with-param name="beforeCodes">g</xsl:with-param>
7058                                                                 </xsl:call-template>
7059                                                         </xsl:with-param>
7060                                                 </xsl:call-template>
7061                                         </namePart>
7062                                         <xsl:call-template name="termsOfAddress"/>
7063                                         <xsl:call-template name="nameDate"/>
7064                                         <xsl:call-template name="role"/>
7065                                 </name>
7066                                 <xsl:call-template name="relatedForm"/>
7067                         </relatedItem>
7068                 </xsl:for-each>
7069                 <xsl:for-each select="marc:datafield[@tag=810]">
7070                         <relatedItem type="series">
7071                                 <titleInfo>
7072                                         <title>
7073                                                 <xsl:call-template name="chopPunctuation">
7074                                                         <xsl:with-param name="chopString">
7075                                                                 <xsl:call-template name="specialSubfieldSelect">
7076                                                                         <xsl:with-param name="anyCodes">tfklmorsv</xsl:with-param>
7077                                                                         <xsl:with-param name="axis">t</xsl:with-param>
7078                                                                         <xsl:with-param name="afterCodes">dg</xsl:with-param>
7079                                                                 </xsl:call-template>
7080                                                         </xsl:with-param>
7081                                                 </xsl:call-template>
7082                                         </title>
7083                                         <xsl:call-template name="relatedPartNumName"/>
7084                                 </titleInfo>
7085                                 <name type="corporate">
7086                                         <xsl:for-each select="marc:subfield[@code='a']">
7087                                                 <namePart>
7088                                                         <xsl:value-of select="."/>
7089                                                 </namePart>
7090                                         </xsl:for-each>
7091                                         <xsl:for-each select="marc:subfield[@code='b']">
7092                                                 <namePart>
7093                                                         <xsl:value-of select="."/>
7094                                                 </namePart>
7095                                         </xsl:for-each>
7096                                         <namePart>
7097                                                 <xsl:call-template name="specialSubfieldSelect">
7098                                                         <xsl:with-param name="anyCodes">c</xsl:with-param>
7099                                                         <xsl:with-param name="axis">t</xsl:with-param>
7100                                                         <xsl:with-param name="beforeCodes">dgn</xsl:with-param>
7101                                                 </xsl:call-template>
7102                                         </namePart>
7103                                         <xsl:call-template name="role"/>
7104                                 </name>
7105                                 <xsl:call-template name="relatedForm"/>
7106                         </relatedItem>
7107                 </xsl:for-each>
7108                 <xsl:for-each select="marc:datafield[@tag=811]">
7109                         <relatedItem type="series">
7110                                 <titleInfo>
7111                                         <title>
7112                                                 <xsl:call-template name="chopPunctuation">
7113                                                         <xsl:with-param name="chopString">
7114                                                                 <xsl:call-template name="specialSubfieldSelect">
7115                                                                         <xsl:with-param name="anyCodes">tfklsv</xsl:with-param>
7116                                                                         <xsl:with-param name="axis">t</xsl:with-param>
7117                                                                         <xsl:with-param name="afterCodes">g</xsl:with-param>
7118                                                                 </xsl:call-template>
7119                                                         </xsl:with-param>
7120                                                 </xsl:call-template>
7121                                         </title>
7122                                         <xsl:call-template name="relatedPartNumName"/>
7123                                 </titleInfo>
7124                                 <name type="conference">
7125                                         <namePart>
7126                                                 <xsl:call-template name="specialSubfieldSelect">
7127                                                         <xsl:with-param name="anyCodes">aqdc</xsl:with-param>
7128                                                         <xsl:with-param name="axis">t</xsl:with-param>
7129                                                         <xsl:with-param name="beforeCodes">gn</xsl:with-param>
7130                                                 </xsl:call-template>
7131                                         </namePart>
7132                                         <xsl:call-template name="role"/>
7133                                 </name>
7134                                 <xsl:call-template name="relatedForm"/>
7135                         </relatedItem>
7136                 </xsl:for-each>
7137                 <xsl:for-each select="marc:datafield[@tag='830']">
7138                         <relatedItem type="series">
7139                                 <titleInfo>
7140                                         <title>
7141                                                 <xsl:call-template name="chopPunctuation">
7142                                                         <xsl:with-param name="chopString">
7143                                                                 <xsl:call-template name="subfieldSelect">
7144                                                                         <xsl:with-param name="codes">adfgklmorsv</xsl:with-param>
7145                                                                 </xsl:call-template>
7146                                                         </xsl:with-param>
7147                                                 </xsl:call-template>
7148                                         </title>
7149                                         <xsl:call-template name="part"/>
7150                                 </titleInfo>
7151                                 <xsl:call-template name="relatedForm"/>
7152                         </relatedItem>
7153                 </xsl:for-each>
7154                 <xsl:for-each select="marc:datafield[@tag='856'][@ind2='2']/marc:subfield[@code='q']">
7155                         <relatedItem>
7156                                 <internetMediaType>
7157                                         <xsl:value-of select="."/>
7158                                 </internetMediaType>
7159                         </relatedItem>
7160                 </xsl:for-each>
7161                 <xsl:for-each select="marc:datafield[@tag='020']">
7162                         <xsl:call-template name="isInvalid">
7163                                 <xsl:with-param name="type">isbn</xsl:with-param>
7164                         </xsl:call-template>
7165                         <xsl:if test="marc:subfield[@code='a']">
7166                                 <identifier type="isbn">
7167                                         <xsl:value-of select="marc:subfield[@code='a']"/>
7168                                 </identifier>
7169                         </xsl:if>
7170                 </xsl:for-each>
7171                 <xsl:for-each select="marc:datafield[@tag='024'][@ind1='0']">
7172                         <xsl:call-template name="isInvalid">
7173                                 <xsl:with-param name="type">isrc</xsl:with-param>
7174                         </xsl:call-template>
7175                         <xsl:if test="marc:subfield[@code='a']">
7176                                 <identifier type="isrc">
7177                                         <xsl:value-of select="marc:subfield[@code='a']"/>
7178                                 </identifier>
7179                         </xsl:if>
7180                 </xsl:for-each>
7181                 <xsl:for-each select="marc:datafield[@tag='024'][@ind1='2']">
7182                         <xsl:call-template name="isInvalid">
7183                                 <xsl:with-param name="type">ismn</xsl:with-param>
7184                         </xsl:call-template>
7185                         <xsl:if test="marc:subfield[@code='a']">
7186                                 <identifier type="ismn">
7187                                         <xsl:value-of select="marc:subfield[@code='a']"/>
7188                                 </identifier>
7189                         </xsl:if>
7190                 </xsl:for-each>
7191                 <xsl:for-each select="marc:datafield[@tag='024'][@ind1='4']">
7192                         <xsl:call-template name="isInvalid">
7193                                 <xsl:with-param name="type">sici</xsl:with-param>
7194                         </xsl:call-template>
7195                         <identifier type="sici">
7196                                 <xsl:call-template name="subfieldSelect">
7197                                         <xsl:with-param name="codes">ab</xsl:with-param>
7198                                 </xsl:call-template>
7199                         </identifier>
7200                 </xsl:for-each>
7201                 <xsl:for-each select="marc:datafield[@tag='022']">
7202                         <xsl:if test="marc:subfield[@code='a']">
7203                                 <xsl:call-template name="isInvalid">
7204                                         <xsl:with-param name="type">issn</xsl:with-param>
7205                                 </xsl:call-template>
7206                                 <identifier type="issn">
7207                                         <xsl:value-of select="marc:subfield[@code='a']"/>
7208                                 </identifier>
7209                         </xsl:if>
7210                         <xsl:if test="marc:subfield[@code='l']">
7211                                 <xsl:call-template name="isInvalid">
7212                                         <xsl:with-param name="type">issn-l</xsl:with-param>
7213                                 </xsl:call-template>
7214                                 <identifier type="issn-l">
7215                                         <xsl:value-of select="marc:subfield[@code='l']"/>
7216                                 </identifier>
7217                         </xsl:if>
7218                 </xsl:for-each>
7219
7220
7221
7222                 <xsl:for-each select="marc:datafield[@tag='010']">
7223                         <xsl:call-template name="isInvalid">
7224                                 <xsl:with-param name="type">lccn</xsl:with-param>
7225                         </xsl:call-template>
7226                         <identifier type="lccn">
7227                                 <xsl:value-of select="normalize-space(marc:subfield[@code='a'])"/>
7228                         </identifier>
7229                 </xsl:for-each>
7230                 <xsl:for-each select="marc:datafield[@tag='028']">
7231                         <identifier>
7232                                 <xsl:attribute name="type">
7233                                         <xsl:choose>
7234                                                 <xsl:when test="@ind1='0'">issue number</xsl:when>
7235                                                 <xsl:when test="@ind1='1'">matrix number</xsl:when>
7236                                                 <xsl:when test="@ind1='2'">music plate</xsl:when>
7237                                                 <xsl:when test="@ind1='3'">music publisher</xsl:when>
7238                                                 <xsl:when test="@ind1='4'">videorecording identifier</xsl:when>
7239                                         </xsl:choose>
7240                                 </xsl:attribute>
7241                                 <!--<xsl:call-template name="isInvalid"/>-->
7242                                 <!-- no $z in 028 -->
7243                                 <xsl:call-template name="subfieldSelect">
7244                                         <xsl:with-param name="codes">
7245                                                 <xsl:choose>
7246                                                         <xsl:when test="@ind1='0'">ba</xsl:when>
7247                                                         <xsl:otherwise>ab</xsl:otherwise>
7248                                                 </xsl:choose>
7249                                         </xsl:with-param>
7250                                 </xsl:call-template>
7251                         </identifier>
7252                 </xsl:for-each>
7253                 <xsl:for-each select="marc:datafield[@tag='037']">
7254                         <identifier type="stock number">
7255                                 <!--<xsl:call-template name="isInvalid"/>-->
7256                                 <!-- no $z in 037 -->
7257                                 <xsl:call-template name="subfieldSelect">
7258                                         <xsl:with-param name="codes">ab</xsl:with-param>
7259                                 </xsl:call-template>
7260                         </identifier>
7261                 </xsl:for-each>
7262                 <xsl:for-each select="marc:datafield[@tag='856'][marc:subfield[@code='u']]">
7263                         <identifier>
7264                                 <xsl:attribute name="type">
7265                                         <xsl:choose>
7266                                                 <xsl:when
7267                                                         test="starts-with(marc:subfield[@code='u'],'urn:doi') or starts-with(marc:subfield[@code='u'],'doi')"
7268                                                         >doi</xsl:when>
7269                                                 <xsl:when
7270                                                         test="starts-with(marc:subfield[@code='u'],'urn:hdl') or starts-with(marc:subfield[@code='u'],'hdl') or starts-with(marc:subfield[@code='u'],'http://hdl.loc.gov')"
7271                                                         >hdl</xsl:when>
7272                                                 <xsl:otherwise>uri</xsl:otherwise>
7273                                         </xsl:choose>
7274                                 </xsl:attribute>
7275                                 <xsl:choose>
7276                                         <xsl:when
7277                                                 test="starts-with(marc:subfield[@code='u'],'urn:hdl') or starts-with(marc:subfield[@code='u'],'hdl') or starts-with(marc:subfield[@code='u'],'http://hdl.loc.gov') ">
7278                                                 <xsl:value-of
7279                                                         select="concat('hdl:',substring-after(marc:subfield[@code='u'],'http://hdl.loc.gov/'))"
7280                                                 />
7281                                         </xsl:when>
7282                                         <xsl:otherwise>
7283                                                 <xsl:value-of select="marc:subfield[@code='u']"/>
7284                                         </xsl:otherwise>
7285                                 </xsl:choose>
7286                         </identifier>
7287                         <xsl:if
7288                                 test="starts-with(marc:subfield[@code='u'],'urn:hdl') or starts-with(marc:subfield[@code='u'],'hdl')">
7289                                 <identifier type="hdl">
7290                                         <xsl:if test="marc:subfield[@code='y' or @code='3' or @code='z']">
7291                                                 <xsl:attribute name="displayLabel">
7292                                                         <xsl:call-template name="subfieldSelect">
7293                                                                 <xsl:with-param name="codes">y3z</xsl:with-param>
7294                                                         </xsl:call-template>
7295                                                 </xsl:attribute>
7296                                         </xsl:if>
7297                                         <xsl:value-of
7298                                                 select="concat('hdl:',substring-after(marc:subfield[@code='u'],'http://hdl.loc.gov/'))"
7299                                         />
7300                                 </identifier>
7301                         </xsl:if>
7302                 </xsl:for-each>
7303                 <xsl:for-each select="marc:datafield[@tag=024][@ind1=1]">
7304                         <identifier type="upc">
7305                                 <xsl:call-template name="isInvalid"/>
7306                                 <xsl:value-of select="marc:subfield[@code='a']"/>
7307                         </identifier>
7308                 </xsl:for-each>
7309
7310                 <!-- 1/04 fix added $y -->
7311
7312                 <!-- 1.21  tmee-->
7313                 <xsl:for-each select="marc:datafield[@tag=856][@ind2=1][marc:subfield[@code='u']]">
7314                         <relatedItem type="otherVersion">
7315                                 <location>
7316                                         <url>
7317                                                 <xsl:if test="marc:subfield[@code='y' or @code='3']">
7318                                                         <xsl:attribute name="displayLabel">
7319                                                                 <xsl:call-template name="subfieldSelect">
7320                                                                         <xsl:with-param name="codes">y3</xsl:with-param>
7321                                                                 </xsl:call-template>
7322                                                         </xsl:attribute>
7323                                                 </xsl:if>
7324                                                 <xsl:if test="marc:subfield[@code='z' ]">
7325                                                         <xsl:attribute name="note">
7326                                                                 <xsl:call-template name="subfieldSelect">
7327                                                                         <xsl:with-param name="codes">z</xsl:with-param>
7328                                                                 </xsl:call-template>
7329                                                         </xsl:attribute>
7330                                                 </xsl:if>
7331                                                 <xsl:value-of select="marc:subfield[@code='u']"/>
7332                                         </url>
7333                                 </location>
7334                         </relatedItem>
7335                 </xsl:for-each>
7336                 <xsl:for-each select="marc:datafield[@tag=856][@ind2=2][marc:subfield[@code='u']]">
7337                         <relatedItem>
7338                                 <location>
7339                                         <url>
7340                                                 <xsl:if test="marc:subfield[@code='y' or @code='3']">
7341                                                         <xsl:attribute name="displayLabel">
7342                                                                 <xsl:call-template name="subfieldSelect">
7343                                                                         <xsl:with-param name="codes">y3</xsl:with-param>
7344                                                                 </xsl:call-template>
7345                                                         </xsl:attribute>
7346                                                 </xsl:if>
7347                                                 <xsl:if test="marc:subfield[@code='z' ]">
7348                                                         <xsl:attribute name="note">
7349                                                                 <xsl:call-template name="subfieldSelect">
7350                                                                         <xsl:with-param name="codes">z</xsl:with-param>
7351                                                                 </xsl:call-template>
7352                                                         </xsl:attribute>
7353                                                 </xsl:if>
7354                                                 <xsl:value-of select="marc:subfield[@code='u']"/>
7355                                         </url>
7356                                 </location>
7357                         </relatedItem>
7358                 </xsl:for-each>
7359
7360                 <!-- 3.2 change tmee 856z  -->
7361
7362                 <!-- 1.24  tmee  -->
7363                 <xsl:for-each select="marc:datafield[@tag=852]">
7364                         <location>
7365                                 <xsl:if test="marc:subfield[@code='a' or @code='b' or @code='e']">
7366                                         <physicalLocation>
7367                                                 <xsl:call-template name="subfieldSelect">
7368                                                         <xsl:with-param name="codes">abe</xsl:with-param>
7369                                                 </xsl:call-template>
7370                                         </physicalLocation>
7371                                 </xsl:if>
7372
7373                                 <xsl:if test="marc:subfield[@code='u']">
7374                                         <physicalLocation>
7375                                                 <xsl:call-template name="uri"/>
7376                                                 <xsl:call-template name="subfieldSelect">
7377                                                         <xsl:with-param name="codes">u</xsl:with-param>
7378                                                 </xsl:call-template>
7379                                         </physicalLocation>
7380                                 </xsl:if>
7381
7382                                 <xsl:if
7383                                         test="marc:subfield[@code='h' or @code='i' or @code='j' or @code='k' or @code='l' or @code='m' or @code='t']">
7384                                         <shelfLocation>
7385                                                 <xsl:call-template name="subfieldSelect">
7386                                                         <xsl:with-param name="codes">hijklmt</xsl:with-param>
7387                                                 </xsl:call-template>
7388                                         </shelfLocation>
7389                                 </xsl:if>
7390                         </location>
7391                 </xsl:for-each>
7392
7393                 <xsl:for-each select="marc:datafield[@tag=506]">
7394                         <accessCondition type="restrictionOnAccess">
7395                                 <xsl:call-template name="subfieldSelect">
7396                                         <xsl:with-param name="codes">abcd35</xsl:with-param>
7397                                 </xsl:call-template>
7398                         </accessCondition>
7399                 </xsl:for-each>
7400                 <xsl:for-each select="marc:datafield[@tag=540]">
7401                         <accessCondition type="useAndReproduction">
7402                                 <xsl:call-template name="subfieldSelect">
7403                                         <xsl:with-param name="codes">abcde35</xsl:with-param>
7404                                 </xsl:call-template>
7405                         </accessCondition>
7406                 </xsl:for-each>
7407
7408                 <recordInfo>
7409                         <!-- 1.25  tmee-->
7410
7411
7412                         <xsl:for-each select="marc:leader[substring($leader,19,1)='a']">
7413                                 <descriptionStandard>aacr2</descriptionStandard>
7414                         </xsl:for-each>
7415
7416                         <xsl:for-each select="marc:datafield[@tag=040]">
7417                                 <xsl:if test="marc:subfield[@code='e']">
7418                                         <descriptionStandard>
7419                                                 <xsl:value-of select="marc:subfield[@code='e']"/>
7420                                         </descriptionStandard>
7421                                 </xsl:if>
7422                                 <recordContentSource authority="marcorg">
7423                                         <xsl:value-of select="marc:subfield[@code='a']"/>
7424                                 </recordContentSource>
7425                         </xsl:for-each>
7426                         <xsl:for-each select="marc:controlfield[@tag=008]">
7427                                 <recordCreationDate encoding="marc">
7428                                         <xsl:value-of select="substring(.,1,6)"/>
7429                                 </recordCreationDate>
7430                         </xsl:for-each>
7431
7432                         <xsl:for-each select="marc:controlfield[@tag=005]">
7433                                 <recordChangeDate encoding="iso8601">
7434                                         <xsl:value-of select="."/>
7435                                 </recordChangeDate>
7436                         </xsl:for-each>
7437                         <xsl:for-each select="marc:controlfield[@tag=001]">
7438                                 <recordIdentifier>
7439                                         <xsl:if test="../marc:controlfield[@tag=003]">
7440                                                 <xsl:attribute name="source">
7441                                                         <xsl:value-of select="../marc:controlfield[@tag=003]"/>
7442                                                 </xsl:attribute>
7443                                         </xsl:if>
7444                                         <xsl:value-of select="."/>
7445                                 </recordIdentifier>
7446                         </xsl:for-each>
7447                         <xsl:for-each select="marc:datafield[@tag=040]/marc:subfield[@code='b']">
7448                                 <languageOfCataloging>
7449                                         <languageTerm authority="iso639-2b" type="code">
7450                                                 <xsl:value-of select="."/>
7451                                         </languageTerm>
7452                                 </languageOfCataloging>
7453                         </xsl:for-each>
7454                 </recordInfo>
7455         </xsl:template>
7456         <xsl:template name="displayForm">
7457                 <xsl:for-each select="marc:subfield[@code='c']">
7458                         <displayForm>
7459                                 <xsl:value-of select="."/>
7460                         </displayForm>
7461                 </xsl:for-each>
7462         </xsl:template>
7463         <xsl:template name="affiliation">
7464                 <xsl:for-each select="marc:subfield[@code='u']">
7465                         <affiliation>
7466                                 <xsl:value-of select="."/>
7467                         </affiliation>
7468                 </xsl:for-each>
7469         </xsl:template>
7470         <xsl:template name="uri">
7471                 <xsl:for-each select="marc:subfield[@code='u']">
7472                         <xsl:attribute name="xlink:href">
7473                                 <xsl:value-of select="."/>
7474                         </xsl:attribute>
7475                 </xsl:for-each>
7476                 <xsl:for-each select="marc:subfield[@code='0']">
7477                         <xsl:choose>
7478                                 <xsl:when test="contains(text(), ')')">
7479                                         <xsl:attribute name="xlink:href">
7480                                                 <xsl:value-of select="substring-after(text(), ')')"></xsl:value-of>
7481                                         </xsl:attribute>
7482                                 </xsl:when>
7483                                 <xsl:otherwise>
7484                                         <xsl:attribute name="xlink:href">
7485                                                 <xsl:value-of select="."></xsl:value-of>
7486                                         </xsl:attribute>
7487                                 </xsl:otherwise>
7488                         </xsl:choose>
7489                 </xsl:for-each>
7490         </xsl:template>
7491         <xsl:template name="role">
7492                 <xsl:for-each select="marc:subfield[@code='e']">
7493                         <role>
7494                                 <roleTerm type="text">
7495                                         <xsl:value-of select="."/>
7496                                 </roleTerm>
7497                         </role>
7498                 </xsl:for-each>
7499                 <xsl:for-each select="marc:subfield[@code='4']">
7500                         <role>
7501                                 <roleTerm authority="marcrelator" type="code">
7502                                         <xsl:value-of select="."/>
7503                                 </roleTerm>
7504                         </role>
7505                 </xsl:for-each>
7506         </xsl:template>
7507         <xsl:template name="part">
7508                 <xsl:variable name="partNumber">
7509                         <xsl:call-template name="specialSubfieldSelect">
7510                                 <xsl:with-param name="axis">n</xsl:with-param>
7511                                 <xsl:with-param name="anyCodes">n</xsl:with-param>
7512                                 <xsl:with-param name="afterCodes">fgkdlmor</xsl:with-param>
7513                         </xsl:call-template>
7514                 </xsl:variable>
7515                 <xsl:variable name="partName">
7516                         <xsl:call-template name="specialSubfieldSelect">
7517                                 <xsl:with-param name="axis">p</xsl:with-param>
7518                                 <xsl:with-param name="anyCodes">p</xsl:with-param>
7519                                 <xsl:with-param name="afterCodes">fgkdlmor</xsl:with-param>
7520                         </xsl:call-template>
7521                 </xsl:variable>
7522                 <xsl:if test="string-length(normalize-space($partNumber))">
7523                         <partNumber>
7524                                 <xsl:call-template name="chopPunctuation">
7525                                         <xsl:with-param name="chopString" select="$partNumber"/>
7526                                 </xsl:call-template>
7527                         </partNumber>
7528                 </xsl:if>
7529                 <xsl:if test="string-length(normalize-space($partName))">
7530                         <partName>
7531                                 <xsl:call-template name="chopPunctuation">
7532                                         <xsl:with-param name="chopString" select="$partName"/>
7533                                 </xsl:call-template>
7534                         </partName>
7535                 </xsl:if>
7536         </xsl:template>
7537         <xsl:template name="relatedPart">
7538                 <xsl:if test="@tag=773">
7539                         <xsl:for-each select="marc:subfield[@code='g']">
7540                                 <part>
7541                                         <text>
7542                                                 <xsl:value-of select="."/>
7543                                         </text>
7544                                 </part>
7545                         </xsl:for-each>
7546                         <xsl:for-each select="marc:subfield[@code='q']">
7547                                 <part>
7548                                         <xsl:call-template name="parsePart"/>
7549                                 </part>
7550                         </xsl:for-each>
7551                 </xsl:if>
7552         </xsl:template>
7553         <xsl:template name="relatedPartNumName">
7554                 <xsl:variable name="partNumber">
7555                         <xsl:call-template name="specialSubfieldSelect">
7556                                 <xsl:with-param name="axis">g</xsl:with-param>
7557                                 <xsl:with-param name="anyCodes">g</xsl:with-param>
7558                                 <xsl:with-param name="afterCodes">pst</xsl:with-param>
7559                         </xsl:call-template>
7560                 </xsl:variable>
7561                 <xsl:variable name="partName">
7562                         <xsl:call-template name="specialSubfieldSelect">
7563                                 <xsl:with-param name="axis">p</xsl:with-param>
7564                                 <xsl:with-param name="anyCodes">p</xsl:with-param>
7565                                 <xsl:with-param name="afterCodes">fgkdlmor</xsl:with-param>
7566                         </xsl:call-template>
7567                 </xsl:variable>
7568                 <xsl:if test="string-length(normalize-space($partNumber))">
7569                         <partNumber>
7570                                 <xsl:value-of select="$partNumber"/>
7571                         </partNumber>
7572                 </xsl:if>
7573                 <xsl:if test="string-length(normalize-space($partName))">
7574                         <partName>
7575                                 <xsl:value-of select="$partName"/>
7576                         </partName>
7577                 </xsl:if>
7578         </xsl:template>
7579         <xsl:template name="relatedName">
7580                 <xsl:for-each select="marc:subfield[@code='a']">
7581                         <name>
7582                                 <namePart>
7583                                         <xsl:value-of select="."/>
7584                                 </namePart>
7585                         </name>
7586                 </xsl:for-each>
7587         </xsl:template>
7588         <xsl:template name="relatedForm">
7589                 <xsl:for-each select="marc:subfield[@code='h']">
7590                         <physicalDescription>
7591                                 <form>
7592                                         <xsl:value-of select="."/>
7593                                 </form>
7594                         </physicalDescription>
7595                 </xsl:for-each>
7596         </xsl:template>
7597         <xsl:template name="relatedExtent">
7598                 <xsl:for-each select="marc:subfield[@code='h']">
7599                         <physicalDescription>
7600                                 <extent>
7601                                         <xsl:value-of select="."/>
7602                                 </extent>
7603                         </physicalDescription>
7604                 </xsl:for-each>
7605         </xsl:template>
7606         <xsl:template name="relatedNote">
7607                 <xsl:for-each select="marc:subfield[@code='n']">
7608                         <note>
7609                                 <xsl:value-of select="."/>
7610                         </note>
7611                 </xsl:for-each>
7612         </xsl:template>
7613         <xsl:template name="relatedSubject">
7614                 <xsl:for-each select="marc:subfield[@code='j']">
7615                         <subject>
7616                                 <temporal encoding="iso8601">
7617                                         <xsl:call-template name="chopPunctuation">
7618                                                 <xsl:with-param name="chopString" select="."/>
7619                                         </xsl:call-template>
7620                                 </temporal>
7621                         </subject>
7622                 </xsl:for-each>
7623         </xsl:template>
7624         <xsl:template name="relatedIdentifierISSN">
7625                 <xsl:for-each select="marc:subfield[@code='x']">
7626                         <identifier type="issn">
7627                                 <xsl:value-of select="."/>
7628                         </identifier>
7629                 </xsl:for-each>
7630         </xsl:template>
7631         <xsl:template name="relatedIdentifierLocal">
7632                 <xsl:for-each select="marc:subfield[@code='w']">
7633                         <identifier type="local">
7634                                 <xsl:value-of select="."/>
7635                         </identifier>
7636                 </xsl:for-each>
7637         </xsl:template>
7638         <xsl:template name="relatedIdentifier">
7639                 <xsl:for-each select="marc:subfield[@code='o']">
7640                         <identifier>
7641                                 <xsl:value-of select="."/>
7642                         </identifier>
7643                 </xsl:for-each>
7644         </xsl:template>
7645         <xsl:template name="relatedItem76X-78X">
7646                 <xsl:call-template name="displayLabel"/>
7647                 <xsl:call-template name="relatedTitle76X-78X"/>
7648                 <xsl:call-template name="relatedName"/>
7649                 <xsl:call-template name="relatedOriginInfo"/>
7650                 <xsl:call-template name="relatedLanguage"/>
7651                 <xsl:call-template name="relatedExtent"/>
7652                 <xsl:call-template name="relatedNote"/>
7653                 <xsl:call-template name="relatedSubject"/>
7654                 <xsl:call-template name="relatedIdentifier"/>
7655                 <xsl:call-template name="relatedIdentifierISSN"/>
7656                 <xsl:call-template name="relatedIdentifierLocal"/>
7657                 <xsl:call-template name="relatedPart"/>
7658         </xsl:template>
7659         <xsl:template name="subjectGeographicZ">
7660                 <geographic>
7661                         <xsl:call-template name="chopPunctuation">
7662                                 <xsl:with-param name="chopString" select="."/>
7663                         </xsl:call-template>
7664                 </geographic>
7665         </xsl:template>
7666         <xsl:template name="subjectTemporalY">
7667                 <temporal>
7668                         <xsl:call-template name="chopPunctuation">
7669                                 <xsl:with-param name="chopString" select="."/>
7670                         </xsl:call-template>
7671                 </temporal>
7672         </xsl:template>
7673         <xsl:template name="subjectTopic">
7674                 <topic>
7675                         <xsl:call-template name="chopPunctuation">
7676                                 <xsl:with-param name="chopString" select="."/>
7677                         </xsl:call-template>
7678                 </topic>
7679         </xsl:template>
7680         <!-- 3.2 change tmee 6xx $v genre -->
7681         <xsl:template name="subjectGenre">
7682                 <genre>
7683                         <xsl:call-template name="chopPunctuation">
7684                                 <xsl:with-param name="chopString" select="."/>
7685                         </xsl:call-template>
7686                 </genre>
7687         </xsl:template>
7688
7689         <xsl:template name="nameABCDN">
7690                 <xsl:for-each select="marc:subfield[@code='a']">
7691                         <namePart>
7692                                 <xsl:call-template name="chopPunctuation">
7693                                         <xsl:with-param name="chopString" select="."/>
7694                                 </xsl:call-template>
7695                         </namePart>
7696                 </xsl:for-each>
7697                 <xsl:for-each select="marc:subfield[@code='b']">
7698                         <namePart>
7699                                 <xsl:value-of select="."/>
7700                         </namePart>
7701                 </xsl:for-each>
7702                 <xsl:if
7703                         test="marc:subfield[@code='c'] or marc:subfield[@code='d'] or marc:subfield[@code='n']">
7704                         <namePart>
7705                                 <xsl:call-template name="subfieldSelect">
7706                                         <xsl:with-param name="codes">cdn</xsl:with-param>
7707                                 </xsl:call-template>
7708                         </namePart>
7709                 </xsl:if>
7710         </xsl:template>
7711         <xsl:template name="nameABCDQ">
7712                 <namePart>
7713                         <xsl:call-template name="chopPunctuation">
7714                                 <xsl:with-param name="chopString">
7715                                         <xsl:call-template name="subfieldSelect">
7716                                                 <xsl:with-param name="codes">aq</xsl:with-param>
7717                                         </xsl:call-template>
7718                                 </xsl:with-param>
7719                                 <xsl:with-param name="punctuation">
7720                                         <xsl:text>:,;/ </xsl:text>
7721                                 </xsl:with-param>
7722                         </xsl:call-template>
7723                 </namePart>
7724                 <xsl:call-template name="termsOfAddress"/>
7725                 <xsl:call-template name="nameDate"/>
7726         </xsl:template>
7727         <xsl:template name="nameACDEQ">
7728                 <namePart>
7729                         <xsl:call-template name="subfieldSelect">
7730                                 <xsl:with-param name="codes">acdeq</xsl:with-param>
7731                         </xsl:call-template>
7732                 </namePart>
7733         </xsl:template>
7734         <xsl:template name="constituentOrRelatedType">
7735                 <xsl:if test="@ind2=2">
7736                         <xsl:attribute name="type">constituent</xsl:attribute>
7737                 </xsl:if>
7738         </xsl:template>
7739         <xsl:template name="relatedTitle">
7740                 <xsl:for-each select="marc:subfield[@code='t']">
7741                         <titleInfo>
7742                                 <title>
7743                                         <xsl:call-template name="chopPunctuation">
7744                                                 <xsl:with-param name="chopString">
7745                                                         <xsl:value-of select="."/>
7746                                                 </xsl:with-param>
7747                                         </xsl:call-template>
7748                                 </title>
7749                         </titleInfo>
7750                 </xsl:for-each>
7751         </xsl:template>
7752         <xsl:template name="relatedTitle76X-78X">
7753                 <xsl:for-each select="marc:subfield[@code='t']">
7754                         <titleInfo>
7755                                 <title>
7756                                         <xsl:call-template name="chopPunctuation">
7757                                                 <xsl:with-param name="chopString">
7758                                                         <xsl:value-of select="."/>
7759                                                 </xsl:with-param>
7760                                         </xsl:call-template>
7761                                 </title>
7762                                 <xsl:if test="marc:datafield[@tag!=773]and marc:subfield[@code='g']">
7763                                         <xsl:call-template name="relatedPartNumName"/>
7764                                 </xsl:if>
7765                         </titleInfo>
7766                 </xsl:for-each>
7767                 <xsl:for-each select="marc:subfield[@code='p']">
7768                         <titleInfo type="abbreviated">
7769                                 <title>
7770                                         <xsl:call-template name="chopPunctuation">
7771                                                 <xsl:with-param name="chopString">
7772                                                         <xsl:value-of select="."/>
7773                                                 </xsl:with-param>
7774                                         </xsl:call-template>
7775                                 </title>
7776                                 <xsl:if test="marc:datafield[@tag!=773]and marc:subfield[@code='g']">
7777                                         <xsl:call-template name="relatedPartNumName"/>
7778                                 </xsl:if>
7779                         </titleInfo>
7780                 </xsl:for-each>
7781                 <xsl:for-each select="marc:subfield[@code='s']">
7782                         <titleInfo type="uniform">
7783                                 <title>
7784                                         <xsl:call-template name="chopPunctuation">
7785                                                 <xsl:with-param name="chopString">
7786                                                         <xsl:value-of select="."/>
7787                                                 </xsl:with-param>
7788                                         </xsl:call-template>
7789                                 </title>
7790                                 <xsl:if test="marc:datafield[@tag!=773]and marc:subfield[@code='g']">
7791                                         <xsl:call-template name="relatedPartNumName"/>
7792                                 </xsl:if>
7793                         </titleInfo>
7794                 </xsl:for-each>
7795         </xsl:template>
7796         <xsl:template name="relatedOriginInfo">
7797                 <xsl:if test="marc:subfield[@code='b' or @code='d'] or marc:subfield[@code='f']">
7798                         <originInfo>
7799                                 <xsl:if test="@tag=775">
7800                                         <xsl:for-each select="marc:subfield[@code='f']">
7801                                                 <place>
7802                                                         <placeTerm>
7803                                                                 <xsl:attribute name="type">code</xsl:attribute>
7804                                                                 <xsl:attribute name="authority">marcgac</xsl:attribute>
7805                                                                 <xsl:value-of select="."/>
7806                                                         </placeTerm>
7807                                                 </place>
7808                                         </xsl:for-each>
7809                                 </xsl:if>
7810                                 <xsl:for-each select="marc:subfield[@code='d']">
7811                                         <publisher>
7812                                                 <xsl:value-of select="."/>
7813                                         </publisher>
7814                                 </xsl:for-each>
7815                                 <xsl:for-each select="marc:subfield[@code='b']">
7816                                         <edition>
7817                                                 <xsl:value-of select="."/>
7818                                         </edition>
7819                                 </xsl:for-each>
7820                         </originInfo>
7821                 </xsl:if>
7822         </xsl:template>
7823         <xsl:template name="relatedLanguage">
7824                 <xsl:for-each select="marc:subfield[@code='e']">
7825                         <xsl:call-template name="getLanguage">
7826                                 <xsl:with-param name="langString">
7827                                         <xsl:value-of select="."/>
7828                                 </xsl:with-param>
7829                         </xsl:call-template>
7830                 </xsl:for-each>
7831         </xsl:template>
7832         <xsl:template name="nameDate">
7833                 <xsl:for-each select="marc:subfield[@code='d']">
7834                         <namePart type="date">
7835                                 <xsl:call-template name="chopPunctuation">
7836                                         <xsl:with-param name="chopString" select="."/>
7837                                 </xsl:call-template>
7838                         </namePart>
7839                 </xsl:for-each>
7840         </xsl:template>
7841         <xsl:template name="subjectAuthority">
7842                 <xsl:if test="@ind2!=4">
7843                         <xsl:if test="@ind2!=' '">
7844                                 <xsl:if test="@ind2!=8">
7845                                         <xsl:if test="@ind2!=9">
7846                                                 <xsl:attribute name="authority">
7847                                                         <xsl:choose>
7848                                                                 <xsl:when test="@ind2=0">lcsh</xsl:when>
7849                                                                 <xsl:when test="@ind2=1">lcshac</xsl:when>
7850                                                                 <xsl:when test="@ind2=2">mesh</xsl:when>
7851                                                                 <!-- 1/04 fix -->
7852                                                                 <xsl:when test="@ind2=3">nal</xsl:when>
7853                                                                 <xsl:when test="@ind2=5">csh</xsl:when>
7854                                                                 <xsl:when test="@ind2=6">rvm</xsl:when>
7855                                                                 <xsl:when test="@ind2=7">
7856                                                                         <xsl:value-of select="marc:subfield[@code='2']"/>
7857                                                                 </xsl:when>
7858                                                         </xsl:choose>
7859                                                 </xsl:attribute>
7860                                         </xsl:if>
7861                                 </xsl:if>
7862                         </xsl:if>
7863                 </xsl:if>
7864         </xsl:template>
7865         <xsl:template name="subjectAnyOrder">
7866                 <xsl:for-each select="marc:subfield[@code='v' or @code='x' or @code='y' or @code='z']">
7867                         <xsl:choose>
7868                                 <xsl:when test="@code='v'">
7869                                         <xsl:call-template name="subjectGenre"/>
7870                                 </xsl:when>
7871                                 <xsl:when test="@code='x'">
7872                                         <xsl:call-template name="subjectTopic"/>
7873                                 </xsl:when>
7874                                 <xsl:when test="@code='y'">
7875                                         <xsl:call-template name="subjectTemporalY"/>
7876                                 </xsl:when>
7877                                 <xsl:when test="@code='z'">
7878                                         <xsl:call-template name="subjectGeographicZ"/>
7879                                 </xsl:when>
7880                         </xsl:choose>
7881                 </xsl:for-each>
7882         </xsl:template>
7883         <xsl:template name="specialSubfieldSelect">
7884                 <xsl:param name="anyCodes"/>
7885                 <xsl:param name="axis"/>
7886                 <xsl:param name="beforeCodes"/>
7887                 <xsl:param name="afterCodes"/>
7888                 <xsl:variable name="str">
7889                         <xsl:for-each select="marc:subfield">
7890                                 <xsl:if
7891                                         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])">
7892                                         <xsl:value-of select="text()"/>
7893                                         <xsl:text> </xsl:text>
7894                                 </xsl:if>
7895                         </xsl:for-each>
7896                 </xsl:variable>
7897                 <xsl:value-of select="substring($str,1,string-length($str)-1)"/>
7898         </xsl:template>
7899
7900         <!-- 3.2 change tmee 6xx $v genre -->
7901         <xsl:template match="marc:datafield[@tag=600]">
7902                 <subject>
7903                         <xsl:call-template name="subjectAuthority"/>
7904                         <name type="personal">
7905                                 <xsl:call-template name="termsOfAddress"/>
7906                                 <namePart>
7907                                         <xsl:call-template name="chopPunctuation">
7908                                                 <xsl:with-param name="chopString">
7909                                                         <xsl:call-template name="subfieldSelect">
7910                                                                 <xsl:with-param name="codes">aq</xsl:with-param>
7911                                                         </xsl:call-template>
7912                                                 </xsl:with-param>
7913                                         </xsl:call-template>
7914                                 </namePart>
7915                                 <xsl:call-template name="nameDate"/>
7916                                 <xsl:call-template name="affiliation"/>
7917                                 <xsl:call-template name="role"/>
7918                         </name>
7919                         <xsl:call-template name="subjectAnyOrder"/>
7920                 </subject>
7921         </xsl:template>
7922         <xsl:template match="marc:datafield[@tag=610]">
7923                 <subject>
7924                         <xsl:call-template name="subjectAuthority"/>
7925                         <name type="corporate">
7926                                 <xsl:for-each select="marc:subfield[@code='a']">
7927                                         <namePart>
7928                                                 <xsl:value-of select="."/>
7929                                         </namePart>
7930                                 </xsl:for-each>
7931                                 <xsl:for-each select="marc:subfield[@code='b']">
7932                                         <namePart>
7933                                                 <xsl:value-of select="."/>
7934                                         </namePart>
7935                                 </xsl:for-each>
7936                                 <xsl:if test="marc:subfield[@code='c' or @code='d' or @code='n' or @code='p']">
7937                                         <namePart>
7938                                                 <xsl:call-template name="subfieldSelect">
7939                                                         <xsl:with-param name="codes">cdnp</xsl:with-param>
7940                                                 </xsl:call-template>
7941                                         </namePart>
7942                                 </xsl:if>
7943                                 <xsl:call-template name="role"/>
7944                         </name>
7945                         <xsl:call-template name="subjectAnyOrder"/>
7946                 </subject>
7947         </xsl:template>
7948         <xsl:template match="marc:datafield[@tag=611]">
7949                 <subject>
7950                         <xsl:call-template name="subjectAuthority"/>
7951                         <name type="conference">
7952                                 <namePart>
7953                                         <xsl:call-template name="subfieldSelect">
7954                                                 <xsl:with-param name="codes">abcdeqnp</xsl:with-param>
7955                                         </xsl:call-template>
7956                                 </namePart>
7957                                 <xsl:for-each select="marc:subfield[@code='4']">
7958                                         <role>
7959                                                 <roleTerm authority="marcrelator" type="code">
7960                                                         <xsl:value-of select="."/>
7961                                                 </roleTerm>
7962                                         </role>
7963                                 </xsl:for-each>
7964                         </name>
7965                         <xsl:call-template name="subjectAnyOrder"/>
7966                 </subject>
7967         </xsl:template>
7968         <xsl:template match="marc:datafield[@tag=630]">
7969                 <subject>
7970                         <xsl:call-template name="subjectAuthority"/>
7971                         <titleInfo>
7972                                 <title>
7973                                         <xsl:call-template name="chopPunctuation">
7974                                                 <xsl:with-param name="chopString">
7975                                                         <xsl:call-template name="subfieldSelect">
7976                                                                 <xsl:with-param name="codes">adfhklor</xsl:with-param>
7977                                                         </xsl:call-template>
7978                                                 </xsl:with-param>
7979                                         </xsl:call-template>
7980                                 </title>
7981                                 <xsl:call-template name="part"/>
7982                         </titleInfo>
7983                         <xsl:call-template name="subjectAnyOrder"/>
7984                 </subject>
7985         </xsl:template>
7986         <!-- 1.27 648 tmee-->
7987         <xsl:template match="marc:datafield[@tag=648]">
7988                 <subject>
7989                         <xsl:if test="marc:subfield[@code=2]">
7990                                 <xsl:attribute name="authority">
7991                                         <xsl:value-of select="marc:subfield[@code=2]"/>
7992                                 </xsl:attribute>
7993                         </xsl:if>
7994                         <xsl:call-template name="uri"/>
7995
7996                         <xsl:call-template name="subjectAuthority"/>
7997                         <temporal>
7998                                 <xsl:call-template name="chopPunctuation">
7999                                         <xsl:with-param name="chopString">
8000                                                 <xsl:call-template name="subfieldSelect">
8001                                                         <xsl:with-param name="codes">abcd</xsl:with-param>
8002                                                 </xsl:call-template>
8003                                         </xsl:with-param>
8004                                 </xsl:call-template>
8005                         </temporal>
8006                         <xsl:call-template name="subjectAnyOrder"/>
8007
8008                 </subject>
8009         </xsl:template>
8010         <xsl:template match="marc:datafield[@tag=650]">
8011                 <subject>
8012                         <xsl:call-template name="subjectAuthority"/>
8013                         <topic>
8014                                 <xsl:call-template name="chopPunctuation">
8015                                         <xsl:with-param name="chopString">
8016                                                 <xsl:call-template name="subfieldSelect">
8017                                                         <xsl:with-param name="codes">abcd</xsl:with-param>
8018                                                 </xsl:call-template>
8019                                         </xsl:with-param>
8020                                 </xsl:call-template>
8021                         </topic>
8022                         <xsl:call-template name="subjectAnyOrder"/>
8023                 </subject>
8024         </xsl:template>
8025         <xsl:template match="marc:datafield[@tag=651]">
8026                 <subject>
8027                         <xsl:call-template name="subjectAuthority"/>
8028                         <xsl:for-each select="marc:subfield[@code='a']">
8029                                 <geographic>
8030                                         <xsl:call-template name="chopPunctuation">
8031                                                 <xsl:with-param name="chopString" select="."/>
8032                                         </xsl:call-template>
8033                                 </geographic>
8034                         </xsl:for-each>
8035                         <xsl:call-template name="subjectAnyOrder"/>
8036                 </subject>
8037         </xsl:template>
8038         <xsl:template match="marc:datafield[@tag=653]">
8039                 <subject>
8040                         <xsl:for-each select="marc:subfield[@code='a']">
8041                                 <topic>
8042                                         <xsl:value-of select="."/>
8043                                 </topic>
8044                         </xsl:for-each>
8045                 </subject>
8046         </xsl:template>
8047         <xsl:template match="marc:datafield[@tag=656]">
8048                 <subject>
8049                         <xsl:if test="marc:subfield[@code=2]">
8050                                 <xsl:attribute name="authority">
8051                                         <xsl:value-of select="marc:subfield[@code=2]"/>
8052                                 </xsl:attribute>
8053                         </xsl:if>
8054                         <occupation>
8055                                 <xsl:call-template name="chopPunctuation">
8056                                         <xsl:with-param name="chopString">
8057                                                 <xsl:value-of select="marc:subfield[@code='a']"/>
8058                                         </xsl:with-param>
8059                                 </xsl:call-template>
8060                         </occupation>
8061                 </subject>
8062         </xsl:template>
8063         <xsl:template name="termsOfAddress">
8064                 <xsl:if test="marc:subfield[@code='b' or @code='c']">
8065                         <namePart type="termsOfAddress">
8066                                 <xsl:call-template name="chopPunctuation">
8067                                         <xsl:with-param name="chopString">
8068                                                 <xsl:call-template name="subfieldSelect">
8069                                                         <xsl:with-param name="codes">bc</xsl:with-param>
8070                                                 </xsl:call-template>
8071                                         </xsl:with-param>
8072                                 </xsl:call-template>
8073                         </namePart>
8074                 </xsl:if>
8075         </xsl:template>
8076         <xsl:template name="displayLabel">
8077                 <xsl:if test="marc:subfield[@code='i']">
8078                         <xsl:attribute name="displayLabel">
8079                                 <xsl:value-of select="marc:subfield[@code='i']"/>
8080                         </xsl:attribute>
8081                 </xsl:if>
8082                 <xsl:if test="marc:subfield[@code='3']">
8083                         <xsl:attribute name="displayLabel">
8084                                 <xsl:value-of select="marc:subfield[@code='3']"/>
8085                         </xsl:attribute>
8086                 </xsl:if>
8087         </xsl:template>
8088         <xsl:template name="isInvalid">
8089                 <xsl:param name="type"/>
8090                 <xsl:if
8091                         test="marc:subfield[@code='z'] or marc:subfield[@code='y']  or marc:subfield[@code='m']">
8092                         <identifier>
8093                                 <xsl:attribute name="type">
8094                                         <xsl:value-of select="$type"/>
8095                                 </xsl:attribute>
8096                                 <xsl:attribute name="invalid">
8097                                         <xsl:text>yes</xsl:text>
8098                                 </xsl:attribute>
8099                                 <xsl:if test="marc:subfield[@code='z']">
8100                                         <xsl:value-of select="marc:subfield[@code='z']"/>
8101                                 </xsl:if>
8102                                 <xsl:if test="marc:subfield[@code='y']">
8103                                         <xsl:value-of select="marc:subfield[@code='y']"/>
8104                                 </xsl:if>
8105                                 <xsl:if test="marc:subfield[@code='m']">
8106                                         <xsl:value-of select="marc:subfield[@code='m']"/>
8107                                 </xsl:if>
8108                         </identifier>
8109                 </xsl:if>
8110         </xsl:template>
8111         <xsl:template name="subtitle">
8112                 <xsl:if test="marc:subfield[@code='b']">
8113                         <subTitle>
8114                                 <xsl:call-template name="chopPunctuation">
8115                                         <xsl:with-param name="chopString">
8116                                                 <xsl:value-of select="marc:subfield[@code='b']"/>
8117                                                 <!--<xsl:call-template name="subfieldSelect">
8118                                                         <xsl:with-param name="codes">b</xsl:with-param>
8119                                                 </xsl:call-template>-->
8120                                         </xsl:with-param>
8121                                 </xsl:call-template>
8122                         </subTitle>
8123                 </xsl:if>
8124         </xsl:template>
8125         <xsl:template name="script">
8126                 <xsl:param name="scriptCode"/>
8127                 <xsl:attribute name="script">
8128                         <xsl:choose>
8129                                 <xsl:when test="$scriptCode='(3'">Arabic</xsl:when>
8130                                 <xsl:when test="$scriptCode='(B'">Latin</xsl:when>
8131                                 <xsl:when test="$scriptCode='$1'">Chinese, Japanese, Korean</xsl:when>
8132                                 <xsl:when test="$scriptCode='(N'">Cyrillic</xsl:when>
8133                                 <xsl:when test="$scriptCode='(2'">Hebrew</xsl:when>
8134                                 <xsl:when test="$scriptCode='(S'">Greek</xsl:when>
8135                         </xsl:choose>
8136                 </xsl:attribute>
8137         </xsl:template>
8138         <xsl:template name="parsePart">
8139                 <!-- assumes 773$q= 1:2:3<4
8140                      with up to 3 levels and one optional start page
8141                 -->
8142                 <xsl:variable name="level1">
8143                         <xsl:choose>
8144                                 <xsl:when test="contains(text(),':')">
8145                                         <!-- 1:2 -->
8146                                         <xsl:value-of select="substring-before(text(),':')"/>
8147                                 </xsl:when>
8148                                 <xsl:when test="not(contains(text(),':'))">
8149                                         <!-- 1 or 1<3 -->
8150                                         <xsl:if test="contains(text(),'&lt;')">
8151                                                 <!-- 1<3 -->
8152                                                 <xsl:value-of select="substring-before(text(),'&lt;')"/>
8153                                         </xsl:if>
8154                                         <xsl:if test="not(contains(text(),'&lt;'))">
8155                                                 <!-- 1 -->
8156                                                 <xsl:value-of select="text()"/>
8157                                         </xsl:if>
8158                                 </xsl:when>
8159                         </xsl:choose>
8160                 </xsl:variable>
8161                 <xsl:variable name="sici2">
8162                         <xsl:choose>
8163                                 <xsl:when test="starts-with(substring-after(text(),$level1),':')">
8164                                         <xsl:value-of select="substring(substring-after(text(),$level1),2)"/>
8165                                 </xsl:when>
8166                                 <xsl:otherwise>
8167                                         <xsl:value-of select="substring-after(text(),$level1)"/>
8168                                 </xsl:otherwise>
8169                         </xsl:choose>
8170                 </xsl:variable>
8171                 <xsl:variable name="level2">
8172                         <xsl:choose>
8173                                 <xsl:when test="contains($sici2,':')">
8174                                         <!--  2:3<4  -->
8175                                         <xsl:value-of select="substring-before($sici2,':')"/>
8176                                 </xsl:when>
8177                                 <xsl:when test="contains($sici2,'&lt;')">
8178                                         <!-- 1: 2<4 -->
8179                                         <xsl:value-of select="substring-before($sici2,'&lt;')"/>
8180                                 </xsl:when>
8181                                 <xsl:otherwise>
8182                                         <xsl:value-of select="$sici2"/>
8183                                         <!-- 1:2 -->
8184                                 </xsl:otherwise>
8185                         </xsl:choose>
8186                 </xsl:variable>
8187                 <xsl:variable name="sici3">
8188                         <xsl:choose>
8189                                 <xsl:when test="starts-with(substring-after($sici2,$level2),':')">
8190                                         <xsl:value-of select="substring(substring-after($sici2,$level2),2)"/>
8191                                 </xsl:when>
8192                                 <xsl:otherwise>
8193                                         <xsl:value-of select="substring-after($sici2,$level2)"/>
8194                                 </xsl:otherwise>
8195                         </xsl:choose>
8196                 </xsl:variable>
8197                 <xsl:variable name="level3">
8198                         <xsl:choose>
8199                                 <xsl:when test="contains($sici3,'&lt;')">
8200                                         <!-- 2<4 -->
8201                                         <xsl:value-of select="substring-before($sici3,'&lt;')"/>
8202                                 </xsl:when>
8203                                 <xsl:otherwise>
8204                                         <xsl:value-of select="$sici3"/>
8205                                         <!-- 3 -->
8206                                 </xsl:otherwise>
8207                         </xsl:choose>
8208                 </xsl:variable>
8209                 <xsl:variable name="page">
8210                         <xsl:if test="contains(text(),'&lt;')">
8211                                 <xsl:value-of select="substring-after(text(),'&lt;')"/>
8212                         </xsl:if>
8213                 </xsl:variable>
8214                 <xsl:if test="$level1">
8215                         <detail level="1">
8216                                 <number>
8217                                         <xsl:value-of select="$level1"/>
8218                                 </number>
8219                         </detail>
8220                 </xsl:if>
8221                 <xsl:if test="$level2">
8222                         <detail level="2">
8223                                 <number>
8224                                         <xsl:value-of select="$level2"/>
8225                                 </number>
8226                         </detail>
8227                 </xsl:if>
8228                 <xsl:if test="$level3">
8229                         <detail level="3">
8230                                 <number>
8231                                         <xsl:value-of select="$level3"/>
8232                                 </number>
8233                         </detail>
8234                 </xsl:if>
8235                 <xsl:if test="$page">
8236                         <extent unit="page">
8237                                 <start>
8238                                         <xsl:value-of select="$page"/>
8239                                 </start>
8240                         </extent>
8241                 </xsl:if>
8242         </xsl:template>
8243         <xsl:template name="getLanguage">
8244                 <xsl:param name="langString"/>
8245                 <xsl:param name="controlField008-35-37"/>
8246                 <xsl:variable name="length" select="string-length($langString)"/>
8247                 <xsl:choose>
8248                         <xsl:when test="$length=0"/>
8249                         <xsl:when test="$controlField008-35-37=substring($langString,1,3)">
8250                                 <xsl:call-template name="getLanguage">
8251                                         <xsl:with-param name="langString" select="substring($langString,4,$length)"/>
8252                                         <xsl:with-param name="controlField008-35-37" select="$controlField008-35-37"/>
8253                                 </xsl:call-template>
8254                         </xsl:when>
8255                         <xsl:otherwise>
8256                                 <language>
8257                                         <languageTerm authority="iso639-2b" type="code">
8258                                                 <xsl:value-of select="substring($langString,1,3)"/>
8259                                         </languageTerm>
8260                                 </language>
8261                                 <xsl:call-template name="getLanguage">
8262                                         <xsl:with-param name="langString" select="substring($langString,4,$length)"/>
8263                                         <xsl:with-param name="controlField008-35-37" select="$controlField008-35-37"/>
8264                                 </xsl:call-template>
8265                         </xsl:otherwise>
8266                 </xsl:choose>
8267         </xsl:template>
8268         <xsl:template name="isoLanguage">
8269                 <xsl:param name="currentLanguage"/>
8270                 <xsl:param name="usedLanguages"/>
8271                 <xsl:param name="remainingLanguages"/>
8272                 <xsl:choose>
8273                         <xsl:when test="string-length($currentLanguage)=0"/>
8274                         <xsl:when test="not(contains($usedLanguages, $currentLanguage))">
8275                                 <language>
8276                                         <xsl:if test="@code!='a'">
8277                                                 <xsl:attribute name="objectPart">
8278                                                         <xsl:choose>
8279                                                                 <xsl:when test="@code='b'">summary or subtitle</xsl:when>
8280                                                                 <xsl:when test="@code='d'">sung or spoken text</xsl:when>
8281                                                                 <xsl:when test="@code='e'">libretto</xsl:when>
8282                                                                 <xsl:when test="@code='f'">table of contents</xsl:when>
8283                                                                 <xsl:when test="@code='g'">accompanying material</xsl:when>
8284                                                                 <xsl:when test="@code='h'">translation</xsl:when>
8285                                                         </xsl:choose>
8286                                                 </xsl:attribute>
8287                                         </xsl:if>
8288                                         <languageTerm authority="iso639-2b" type="code">
8289                                                 <xsl:value-of select="$currentLanguage"/>
8290                                         </languageTerm>
8291                                 </language>
8292                                 <xsl:call-template name="isoLanguage">
8293                                         <xsl:with-param name="currentLanguage">
8294                                                 <xsl:value-of select="substring($remainingLanguages,1,3)"/>
8295                                         </xsl:with-param>
8296                                         <xsl:with-param name="usedLanguages">
8297                                                 <xsl:value-of select="concat($usedLanguages,$currentLanguage)"/>
8298                                         </xsl:with-param>
8299                                         <xsl:with-param name="remainingLanguages">
8300                                                 <xsl:value-of
8301                                                         select="substring($remainingLanguages,4,string-length($remainingLanguages))"
8302                                                 />
8303                                         </xsl:with-param>
8304                                 </xsl:call-template>
8305                         </xsl:when>
8306                         <xsl:otherwise>
8307                                 <xsl:call-template name="isoLanguage">
8308                                         <xsl:with-param name="currentLanguage">
8309                                                 <xsl:value-of select="substring($remainingLanguages,1,3)"/>
8310                                         </xsl:with-param>
8311                                         <xsl:with-param name="usedLanguages">
8312                                                 <xsl:value-of select="concat($usedLanguages,$currentLanguage)"/>
8313                                         </xsl:with-param>
8314                                         <xsl:with-param name="remainingLanguages">
8315                                                 <xsl:value-of
8316                                                         select="substring($remainingLanguages,4,string-length($remainingLanguages))"
8317                                                 />
8318                                         </xsl:with-param>
8319                                 </xsl:call-template>
8320                         </xsl:otherwise>
8321                 </xsl:choose>
8322         </xsl:template>
8323         <xsl:template name="chopBrackets">
8324                 <xsl:param name="chopString"/>
8325                 <xsl:variable name="string">
8326                         <xsl:call-template name="chopPunctuation">
8327                                 <xsl:with-param name="chopString" select="$chopString"/>
8328                         </xsl:call-template>
8329                 </xsl:variable>
8330                 <xsl:if test="substring($string, 1,1)='['">
8331                         <xsl:value-of select="substring($string,2, string-length($string)-2)"/>
8332                 </xsl:if>
8333                 <xsl:if test="substring($string, 1,1)!='['">
8334                         <xsl:value-of select="$string"/>
8335                 </xsl:if>
8336         </xsl:template>
8337         <xsl:template name="rfcLanguages">
8338                 <xsl:param name="nodeNum"/>
8339                 <xsl:param name="usedLanguages"/>
8340                 <xsl:param name="controlField008-35-37"/>
8341                 <xsl:variable name="currentLanguage" select="."/>
8342                 <xsl:choose>
8343                         <xsl:when test="not($currentLanguage)"/>
8344                         <xsl:when
8345                                 test="$currentLanguage!=$controlField008-35-37 and $currentLanguage!='rfc3066'">
8346                                 <xsl:if test="not(contains($usedLanguages,$currentLanguage))">
8347                                         <language>
8348                                                 <xsl:if test="@code!='a'">
8349                                                         <xsl:attribute name="objectPart">
8350                                                                 <xsl:choose>
8351                                                                         <xsl:when test="@code='b'">summary or subtitle</xsl:when>
8352                                                                         <xsl:when test="@code='d'">sung or spoken text</xsl:when>
8353                                                                         <xsl:when test="@code='e'">libretto</xsl:when>
8354                                                                         <xsl:when test="@code='f'">table of contents</xsl:when>
8355                                                                         <xsl:when test="@code='g'">accompanying material</xsl:when>
8356                                                                         <xsl:when test="@code='h'">translation</xsl:when>
8357                                                                 </xsl:choose>
8358                                                         </xsl:attribute>
8359                                                 </xsl:if>
8360                                                 <languageTerm authority="rfc3066" type="code">
8361                                                         <xsl:value-of select="$currentLanguage"/>
8362                                                 </languageTerm>
8363                                         </language>
8364                                 </xsl:if>
8365                         </xsl:when>
8366                         <xsl:otherwise> </xsl:otherwise>
8367                 </xsl:choose>
8368         </xsl:template>
8369
8370     <xsl:template name="datafield">
8371                 <xsl:param name="tag"/>
8372                 <xsl:param name="ind1">
8373                         <xsl:text> </xsl:text>
8374                 </xsl:param>
8375                 <xsl:param name="ind2">
8376                         <xsl:text> </xsl:text>
8377                 </xsl:param>
8378                 <xsl:param name="subfields"/>
8379                 <xsl:element name="marc:datafield">
8380                         <xsl:attribute name="tag">
8381                                 <xsl:value-of select="$tag"/>
8382                         </xsl:attribute>
8383                         <xsl:attribute name="ind1">
8384                                 <xsl:value-of select="$ind1"/>
8385                         </xsl:attribute>
8386                         <xsl:attribute name="ind2">
8387                                 <xsl:value-of select="$ind2"/>
8388                         </xsl:attribute>
8389                         <xsl:copy-of select="$subfields"/>
8390                 </xsl:element>
8391         </xsl:template>
8392
8393         <xsl:template name="subfieldSelect">
8394                 <xsl:param name="codes">abcdefghijklmnopqrstuvwxyz</xsl:param>
8395                 <xsl:param name="delimeter">
8396                         <xsl:text> </xsl:text>
8397                 </xsl:param>
8398                 <xsl:variable name="str">
8399                         <xsl:for-each select="marc:subfield">
8400                                 <xsl:if test="contains($codes, @code)">
8401                                         <xsl:value-of select="text()"/>
8402                                         <xsl:value-of select="$delimeter"/>
8403                                 </xsl:if>
8404                         </xsl:for-each>
8405                 </xsl:variable>
8406                 <xsl:value-of select="substring($str,1,string-length($str)-string-length($delimeter))"/>
8407         </xsl:template>
8408
8409         <xsl:template name="buildSpaces">
8410                 <xsl:param name="spaces"/>
8411                 <xsl:param name="char">
8412                         <xsl:text> </xsl:text>
8413                 </xsl:param>
8414                 <xsl:if test="$spaces>0">
8415                         <xsl:value-of select="$char"/>
8416                         <xsl:call-template name="buildSpaces">
8417                                 <xsl:with-param name="spaces" select="$spaces - 1"/>
8418                                 <xsl:with-param name="char" select="$char"/>
8419                         </xsl:call-template>
8420                 </xsl:if>
8421         </xsl:template>
8422
8423         <xsl:template name="chopPunctuation">
8424                 <xsl:param name="chopString"/>
8425                 <xsl:param name="punctuation">
8426                         <xsl:text>.:,;/ </xsl:text>
8427                 </xsl:param>
8428                 <xsl:variable name="length" select="string-length($chopString)"/>
8429                 <xsl:choose>
8430                         <xsl:when test="$length=0"/>
8431                         <xsl:when test="contains($punctuation, substring($chopString,$length,1))">
8432                                 <xsl:call-template name="chopPunctuation">
8433                                         <xsl:with-param name="chopString" select="substring($chopString,1,$length - 1)"/>
8434                                         <xsl:with-param name="punctuation" select="$punctuation"/>
8435                                 </xsl:call-template>
8436                         </xsl:when>
8437                         <xsl:when test="not($chopString)"/>
8438                         <xsl:otherwise>
8439                                 <xsl:value-of select="$chopString"/>
8440                         </xsl:otherwise>
8441                 </xsl:choose>
8442         </xsl:template>
8443
8444         <xsl:template name="chopPunctuationFront">
8445                 <xsl:param name="chopString"/>
8446                 <xsl:variable name="length" select="string-length($chopString)"/>
8447                 <xsl:choose>
8448                         <xsl:when test="$length=0"/>
8449                         <xsl:when test="contains('.:,;/[ ', substring($chopString,1,1))">
8450                                 <xsl:call-template name="chopPunctuationFront">
8451                                         <xsl:with-param name="chopString" select="substring($chopString,2,$length - 1)"
8452                                         />
8453                                 </xsl:call-template>
8454                         </xsl:when>
8455                         <xsl:when test="not($chopString)"/>
8456                         <xsl:otherwise>
8457                                 <xsl:value-of select="$chopString"/>
8458                         </xsl:otherwise>
8459                 </xsl:choose>
8460         </xsl:template>
8461
8462         <xsl:template name="chopPunctuationBack">
8463                 <xsl:param name="chopString"/>
8464                 <xsl:param name="punctuation">
8465                         <xsl:text>.:,;/] </xsl:text>
8466                 </xsl:param>
8467                 <xsl:variable name="length" select="string-length($chopString)"/>
8468                 <xsl:choose>
8469                         <xsl:when test="$length=0"/>
8470                         <xsl:when test="contains($punctuation, substring($chopString,$length,1))">
8471                                 <xsl:call-template name="chopPunctuation">
8472                                         <xsl:with-param name="chopString" select="substring($chopString,1,$length - 1)"/>
8473                                         <xsl:with-param name="punctuation" select="$punctuation"/>
8474                                 </xsl:call-template>
8475                         </xsl:when>
8476                         <xsl:when test="not($chopString)"/>
8477                         <xsl:otherwise>
8478                                 <xsl:value-of select="$chopString"/>
8479                         </xsl:otherwise>
8480                 </xsl:choose>
8481         </xsl:template>
8482
8483         <!-- nate added 12/14/2007 for lccn.loc.gov: url encode ampersand, etc. -->
8484         <xsl:template name="url-encode">
8485
8486                 <xsl:param name="str"/>
8487
8488                 <xsl:if test="$str">
8489                         <xsl:variable name="first-char" select="substring($str,1,1)"/>
8490                         <xsl:choose>
8491                                 <xsl:when test="contains($safe,$first-char)">
8492                                         <xsl:value-of select="$first-char"/>
8493                                 </xsl:when>
8494                                 <xsl:otherwise>
8495                                         <xsl:variable name="codepoint">
8496                                                 <xsl:choose>
8497                                                         <xsl:when test="contains($ascii,$first-char)">
8498                                                                 <xsl:value-of
8499                                                                         select="string-length(substring-before($ascii,$first-char)) + 32"
8500                                                                 />
8501                                                         </xsl:when>
8502                                                         <xsl:when test="contains($latin1,$first-char)">
8503                                                                 <xsl:value-of
8504                                                                         select="string-length(substring-before($latin1,$first-char)) + 160"/>
8505                                                                 <!-- was 160 -->
8506                                                         </xsl:when>
8507                                                         <xsl:otherwise>
8508                                                                 <xsl:message terminate="no">Warning: string contains a character
8509                                                                         that is out of range! Substituting "?".</xsl:message>
8510                                                                 <xsl:text>63</xsl:text>
8511                                                         </xsl:otherwise>
8512                                                 </xsl:choose>
8513                                         </xsl:variable>
8514                                         <xsl:variable name="hex-digit1"
8515                                                 select="substring($hex,floor($codepoint div 16) + 1,1)"/>
8516                                         <xsl:variable name="hex-digit2" select="substring($hex,$codepoint mod 16 + 1,1)"/>
8517                                         <!-- <xsl:value-of select="concat('%',$hex-digit2)"/> -->
8518                                         <xsl:value-of select="concat('%',$hex-digit1,$hex-digit2)"/>
8519                                 </xsl:otherwise>
8520                         </xsl:choose>
8521                         <xsl:if test="string-length($str) &gt; 1">
8522                                 <xsl:call-template name="url-encode">
8523                                         <xsl:with-param name="str" select="substring($str,2)"/>
8524                                 </xsl:call-template>
8525                         </xsl:if>
8526                 </xsl:if>
8527         </xsl:template>
8528 </xsl:stylesheet>$XXXX$ where name = $$mods33$$;
8529
8530
8531
8532 SELECT evergreen.upgrade_deps_block_check('1103', :eg_version);
8533
8534 INSERT INTO config.metabib_field (id, field_class, name, label, browse_field)
8535     VALUES (45, 'keyword', 'blob', 'All searchable fields', FALSE);
8536
8537 INSERT INTO config.metabib_field (id, field_class, name, format, weight,
8538     label, xpath, display_field, search_field, browse_field, facet_field)
8539 VALUES (
8540     53, 'title', 'maintitle', 'marcxml', 10,
8541     oils_i18n_gettext(53, 'Main Title', 'cmf', 'label'),
8542     $$//*[@tag='245']/*[@code='a']$$,
8543     FALSE, TRUE, FALSE, FALSE
8544 );
8545
8546 INSERT INTO config.metabib_field_virtual_map (real, virtual)
8547     SELECT  id,
8548             45
8549       FROM  config.metabib_field
8550       WHERE search_field
8551             AND id NOT IN (15, 45, 38, 40) -- keyword|keyword, self, edition, publisher
8552             AND id NOT IN (SELECT real FROM config.metabib_field_virtual_map);
8553
8554 UPDATE config.metabib_field SET xpath=$$//mods32:mods/mods32:subject[not(descendant::mods32:geographicCode)]$$ WHERE id = 16;
8555
8556 UPDATE config.metabib_field_virtual_map SET weight = -1 WHERE real = 39;
8557 UPDATE config.metabib_field_virtual_map SET weight = 0 WHERE real = 41;
8558 UPDATE config.metabib_field_virtual_map SET weight = 0 WHERE real = 42;
8559 UPDATE config.metabib_field_virtual_map SET weight = 0 WHERE real = 46;
8560 UPDATE config.metabib_field_virtual_map SET weight = 0 WHERE real = 47;
8561 UPDATE config.metabib_field_virtual_map SET weight = 0 WHERE real = 48;
8562 UPDATE config.metabib_field_virtual_map SET weight = 0 WHERE real = 50;
8563 UPDATE config.metabib_field_virtual_map SET weight = 8 WHERE real = 6;
8564 UPDATE config.metabib_field_virtual_map SET weight = 8 WHERE real = 8;
8565 UPDATE config.metabib_field_virtual_map SET weight = 8 WHERE real = 16;
8566 UPDATE config.metabib_field_virtual_map SET weight = 12 WHERE real = 53;
8567
8568 -- Stemming for genre
8569 INSERT INTO config.metabib_field_ts_map (metabib_field, ts_config)
8570     SELECT 33, 'english_nostop' WHERE NOT EXISTS (
8571         SELECT 1 FROM config.metabib_field_ts_map WHERE metabib_field = 33 AND ts_config = 'english_nostop'
8572     )
8573 ;
8574
8575
8576 SELECT evergreen.upgrade_deps_block_check('1104', :eg_version);
8577
8578 INSERT INTO config.org_unit_setting_type
8579 ( name, grp, label, description, datatype )
8580 VALUES
8581         ('circ.clear_hold_on_checkout',
8582          'circ',
8583         oils_i18n_gettext('circ.clear_hold_on_checkout',
8584                 'Clear hold when other patron checks out item',
8585                 'coust', 'label'),
8586         oils_i18n_gettext('circ.clear_hold_on_checkout',
8587             'Default to cancel the hold when patron A checks out item on hold for patron B.',
8588                 'coust', 'description'),
8589         'bool');
8590
8591
8592
8593 SELECT evergreen.upgrade_deps_block_check('1105', :eg_version);
8594
8595 INSERT into config.org_unit_setting_type (name, label, grp, description, datatype) 
8596 values ('webstaff.circ.itemsout_notice_count_excludes_courtesies','Exclude Courtesy Notices from Patrons Itemsout Notices Count',
8597     'circ', 'Exclude Courtesy Notices from Patron Itemsout Notices Count', 'bool');
8598
8599
8600 SELECT evergreen.upgrade_deps_block_check('1106', :eg_version);
8601
8602 ALTER TABLE money.billing
8603         ADD COLUMN create_date TIMESTAMP WITH TIME ZONE,
8604         ADD COLUMN period_start    TIMESTAMP WITH TIME ZONE,
8605         ADD COLUMN period_end  TIMESTAMP WITH TIME ZONE;
8606
8607 --Disable materialized update trigger
8608 --It takes forever, and doesn't matter yet for what we are doing, as the
8609 --view definition is unchanged (still using billing_ts)
8610 ALTER TABLE money.billing DISABLE TRIGGER mat_summary_upd_tgr;
8611
8612 --Limit to btype=1 / 'Overdue Materials'
8613 --Update day-granular fines first (i.e. 24 hour, 1 day, 2 day, etc., all of which are multiples of 86400 seconds), and simply remove the time portion of timestamp
8614 UPDATE money.billing mb
8615         SET period_start = date_trunc('day', billing_ts), period_end = date_trunc('day', billing_ts) + (ac.fine_interval - '1 second')
8616         FROM action.circulation ac
8617 WHERE mb.xact = ac.id
8618         AND mb.btype = 1
8619         AND (EXTRACT(EPOCH FROM ac.fine_interval))::integer % 86400 = 0;
8620
8621 --Update fines for non-day intervals
8622 UPDATE money.billing mb
8623         SET period_start = billing_ts - ac.fine_interval + interval '1 sec', period_end = billing_ts
8624         FROM action.circulation ac
8625 WHERE mb.xact = ac.id
8626         AND mb.btype = 1
8627         AND (EXTRACT(EPOCH FROM ac.fine_interval))::integer % 86400 > 0;
8628
8629 SET CONSTRAINTS ALL IMMEDIATE;
8630 UPDATE money.billing SET create_date = COALESCE(period_start, billing_ts);
8631
8632 --Re-enable update trigger
8633 ALTER TABLE money.billing ENABLE TRIGGER mat_summary_upd_tgr;
8634
8635 ALTER TABLE money.billing ALTER COLUMN create_date SET DEFAULT NOW();
8636 ALTER TABLE money.billing ALTER COLUMN create_date SET NOT NULL;
8637
8638 CREATE INDEX m_b_create_date_idx ON money.billing (create_date);
8639 CREATE INDEX m_b_period_start_idx ON money.billing (period_start);
8640 CREATE INDEX m_b_period_end_idx ON money.billing (period_end);
8641
8642 CREATE OR REPLACE FUNCTION money.maintain_billing_ts () RETURNS TRIGGER AS $$
8643 BEGIN
8644         NEW.billing_ts := COALESCE(NEW.period_end, NEW.create_date);
8645         RETURN NEW;
8646 END;
8647 $$ LANGUAGE PLPGSQL;
8648 CREATE TRIGGER maintain_billing_ts_tgr BEFORE INSERT OR UPDATE ON money.billing FOR EACH ROW EXECUTE PROCEDURE money.maintain_billing_ts();
8649
8650   
8651 SELECT evergreen.upgrade_deps_block_check('1108', :eg_version);
8652
8653 CREATE OR REPLACE FUNCTION asset.merge_record_assets( target_record BIGINT, source_record BIGINT ) RETURNS INT AS $func$
8654 DECLARE
8655     moved_objects INT := 0;
8656     source_cn     asset.call_number%ROWTYPE;
8657     target_cn     asset.call_number%ROWTYPE;
8658     metarec       metabib.metarecord%ROWTYPE;
8659     hold          action.hold_request%ROWTYPE;
8660     ser_rec       serial.record_entry%ROWTYPE;
8661     ser_sub       serial.subscription%ROWTYPE;
8662     acq_lineitem  acq.lineitem%ROWTYPE;
8663     acq_request   acq.user_request%ROWTYPE;
8664     booking       booking.resource_type%ROWTYPE;
8665     source_part   biblio.monograph_part%ROWTYPE;
8666     target_part   biblio.monograph_part%ROWTYPE;
8667     multi_home    biblio.peer_bib_copy_map%ROWTYPE;
8668     uri_count     INT := 0;
8669     counter       INT := 0;
8670     uri_datafield TEXT;
8671     uri_text      TEXT := '';
8672 BEGIN
8673
8674     -- move any 856 entries on records that have at least one MARC-mapped URI entry
8675     SELECT  INTO uri_count COUNT(*)
8676       FROM  asset.uri_call_number_map m
8677             JOIN asset.call_number cn ON (m.call_number = cn.id)
8678       WHERE cn.record = source_record;
8679
8680     IF uri_count > 0 THEN
8681         
8682         -- This returns more nodes than you might expect:
8683         -- 7 instead of 1 for an 856 with $u $y $9
8684         SELECT  COUNT(*) INTO counter
8685           FROM  oils_xpath_table(
8686                     'id',
8687                     'marc',
8688                     'biblio.record_entry',
8689                     '//*[@tag="856"]',
8690                     'id=' || source_record
8691                 ) as t(i int,c text);
8692     
8693         FOR i IN 1 .. counter LOOP
8694             SELECT  '<datafield xmlns="http://www.loc.gov/MARC21/slim"' || 
8695                         ' tag="856"' ||
8696                         ' ind1="' || FIRST(ind1) || '"'  ||
8697                         ' ind2="' || FIRST(ind2) || '">' ||
8698                         STRING_AGG(
8699                             '<subfield code="' || subfield || '">' ||
8700                             regexp_replace(
8701                                 regexp_replace(
8702                                     regexp_replace(data,'&','&amp;','g'),
8703                                     '>', '&gt;', 'g'
8704                                 ),
8705                                 '<', '&lt;', 'g'
8706                             ) || '</subfield>', ''
8707                         ) || '</datafield>' INTO uri_datafield
8708               FROM  oils_xpath_table(
8709                         'id',
8710                         'marc',
8711                         'biblio.record_entry',
8712                         '//*[@tag="856"][position()=' || i || ']/@ind1|' ||
8713                         '//*[@tag="856"][position()=' || i || ']/@ind2|' ||
8714                         '//*[@tag="856"][position()=' || i || ']/*/@code|' ||
8715                         '//*[@tag="856"][position()=' || i || ']/*[@code]',
8716                         'id=' || source_record
8717                     ) as t(id int,ind1 text, ind2 text,subfield text,data text);
8718
8719             -- As most of the results will be NULL, protect against NULLifying
8720             -- the valid content that we do generate
8721             uri_text := uri_text || COALESCE(uri_datafield, '');
8722         END LOOP;
8723
8724         IF uri_text <> '' THEN
8725             UPDATE  biblio.record_entry
8726               SET   marc = regexp_replace(marc,'(</[^>]*record>)', uri_text || E'\\1')
8727               WHERE id = target_record;
8728         END IF;
8729
8730     END IF;
8731
8732         -- Find and move metarecords to the target record
8733         SELECT  INTO metarec *
8734           FROM  metabib.metarecord
8735           WHERE master_record = source_record;
8736
8737         IF FOUND THEN
8738                 UPDATE  metabib.metarecord
8739                   SET   master_record = target_record,
8740                         mods = NULL
8741                   WHERE id = metarec.id;
8742
8743                 moved_objects := moved_objects + 1;
8744         END IF;
8745
8746         -- Find call numbers attached to the source ...
8747         FOR source_cn IN SELECT * FROM asset.call_number WHERE record = source_record LOOP
8748
8749                 SELECT  INTO target_cn *
8750                   FROM  asset.call_number
8751                   WHERE label = source_cn.label
8752             AND prefix = source_cn.prefix
8753             AND suffix = source_cn.suffix
8754                         AND owning_lib = source_cn.owning_lib
8755                         AND record = target_record
8756                         AND NOT deleted;
8757
8758                 -- ... and if there's a conflicting one on the target ...
8759                 IF FOUND THEN
8760
8761                         -- ... move the copies to that, and ...
8762                         UPDATE  asset.copy
8763                           SET   call_number = target_cn.id
8764                           WHERE call_number = source_cn.id;
8765
8766                         -- ... move V holds to the move-target call number
8767                         FOR hold IN SELECT * FROM action.hold_request WHERE target = source_cn.id AND hold_type = 'V' LOOP
8768                 
8769                                 UPDATE  action.hold_request
8770                                   SET   target = target_cn.id
8771                                   WHERE id = hold.id;
8772                 
8773                                 moved_objects := moved_objects + 1;
8774                         END LOOP;
8775         
8776             UPDATE asset.call_number SET deleted = TRUE WHERE id = source_cn.id;
8777
8778                 -- ... if not ...
8779                 ELSE
8780                         -- ... just move the call number to the target record
8781                         UPDATE  asset.call_number
8782                           SET   record = target_record
8783                           WHERE id = source_cn.id;
8784                 END IF;
8785
8786                 moved_objects := moved_objects + 1;
8787         END LOOP;
8788
8789         -- Find T holds targeting the source record ...
8790         FOR hold IN SELECT * FROM action.hold_request WHERE target = source_record AND hold_type = 'T' LOOP
8791
8792                 -- ... and move them to the target record
8793                 UPDATE  action.hold_request
8794                   SET   target = target_record
8795                   WHERE id = hold.id;
8796
8797                 moved_objects := moved_objects + 1;
8798         END LOOP;
8799
8800         -- Find serial records targeting the source record ...
8801         FOR ser_rec IN SELECT * FROM serial.record_entry WHERE record = source_record LOOP
8802                 -- ... and move them to the target record
8803                 UPDATE  serial.record_entry
8804                   SET   record = target_record
8805                   WHERE id = ser_rec.id;
8806
8807                 moved_objects := moved_objects + 1;
8808         END LOOP;
8809
8810         -- Find serial subscriptions targeting the source record ...
8811         FOR ser_sub IN SELECT * FROM serial.subscription WHERE record_entry = source_record LOOP
8812                 -- ... and move them to the target record
8813                 UPDATE  serial.subscription
8814                   SET   record_entry = target_record
8815                   WHERE id = ser_sub.id;
8816
8817                 moved_objects := moved_objects + 1;
8818         END LOOP;
8819
8820         -- Find booking resource types targeting the source record ...
8821         FOR booking IN SELECT * FROM booking.resource_type WHERE record = source_record LOOP
8822                 -- ... and move them to the target record
8823                 UPDATE  booking.resource_type
8824                   SET   record = target_record
8825                   WHERE id = booking.id;
8826
8827                 moved_objects := moved_objects + 1;
8828         END LOOP;
8829
8830         -- Find acq lineitems targeting the source record ...
8831         FOR acq_lineitem IN SELECT * FROM acq.lineitem WHERE eg_bib_id = source_record LOOP
8832                 -- ... and move them to the target record
8833                 UPDATE  acq.lineitem
8834                   SET   eg_bib_id = target_record
8835                   WHERE id = acq_lineitem.id;
8836
8837                 moved_objects := moved_objects + 1;
8838         END LOOP;
8839
8840         -- Find acq user purchase requests targeting the source record ...
8841         FOR acq_request IN SELECT * FROM acq.user_request WHERE eg_bib = source_record LOOP
8842                 -- ... and move them to the target record
8843                 UPDATE  acq.user_request
8844                   SET   eg_bib = target_record
8845                   WHERE id = acq_request.id;
8846
8847                 moved_objects := moved_objects + 1;
8848         END LOOP;
8849
8850         -- Find parts attached to the source ...
8851         FOR source_part IN SELECT * FROM biblio.monograph_part WHERE record = source_record LOOP
8852
8853                 SELECT  INTO target_part *
8854                   FROM  biblio.monograph_part
8855                   WHERE label = source_part.label
8856                         AND record = target_record;
8857
8858                 -- ... and if there's a conflicting one on the target ...
8859                 IF FOUND THEN
8860
8861                         -- ... move the copy-part maps to that, and ...
8862                         UPDATE  asset.copy_part_map
8863                           SET   part = target_part.id
8864                           WHERE part = source_part.id;
8865
8866                         -- ... move P holds to the move-target part
8867                         FOR hold IN SELECT * FROM action.hold_request WHERE target = source_part.id AND hold_type = 'P' LOOP
8868                 
8869                                 UPDATE  action.hold_request
8870                                   SET   target = target_part.id
8871                                   WHERE id = hold.id;
8872                 
8873                                 moved_objects := moved_objects + 1;
8874                         END LOOP;
8875
8876                 -- ... if not ...
8877                 ELSE
8878                         -- ... just move the part to the target record
8879                         UPDATE  biblio.monograph_part
8880                           SET   record = target_record
8881                           WHERE id = source_part.id;
8882                 END IF;
8883
8884                 moved_objects := moved_objects + 1;
8885         END LOOP;
8886
8887         -- Find multi_home items attached to the source ...
8888         FOR multi_home IN SELECT * FROM biblio.peer_bib_copy_map WHERE peer_record = source_record LOOP
8889                 -- ... and move them to the target record
8890                 UPDATE  biblio.peer_bib_copy_map
8891                   SET   peer_record = target_record
8892                   WHERE id = multi_home.id;
8893
8894                 moved_objects := moved_objects + 1;
8895         END LOOP;
8896
8897         -- And delete mappings where the item's home bib was merged with the peer bib
8898         DELETE FROM biblio.peer_bib_copy_map WHERE peer_record = (
8899                 SELECT (SELECT record FROM asset.call_number WHERE id = call_number)
8900                 FROM asset.copy WHERE id = target_copy
8901         );
8902
8903     -- Apply merge tracking
8904     UPDATE biblio.record_entry 
8905         SET merge_date = NOW() WHERE id = target_record;
8906
8907     UPDATE biblio.record_entry
8908         SET merge_date = NOW(), merged_to = target_record
8909         WHERE id = source_record;
8910
8911     -- replace book bag entries of source_record with target_record
8912     UPDATE container.biblio_record_entry_bucket_item
8913         SET target_biblio_record_entry = target_record
8914         WHERE bucket IN (SELECT id FROM container.biblio_record_entry_bucket WHERE btype = 'bookbag')
8915         AND target_biblio_record_entry = source_record;
8916
8917     -- Finally, "delete" the source record
8918     DELETE FROM biblio.record_entry WHERE id = source_record;
8919
8920         -- That's all, folks!
8921         RETURN moved_objects;
8922 END;
8923 $func$ LANGUAGE plpgsql;
8924
8925
8926 SELECT evergreen.upgrade_deps_block_check('1109', :eg_version);
8927
8928 INSERT into config.org_unit_setting_type (name, label, grp, description, datatype)
8929 values ('circ.staff_placed_holds_fallback_to_ws_ou','Workstation OU fallback for staff-placed holds',
8930         'circ', 'For staff-placed holds, in the absence of a patron preferred pickup location, fall back to using the staff workstation OU (rather than patron home OU)', 'bool');
8931
8932
8933 COMMIT;
8934
8935 \qecho 
8936 \qecho Reingesting all records.  This may take a while. 
8937 \qecho This command can be stopped (control-c) and rerun later if needed: 
8938 \qecho 
8939 \qecho DO $FUNC$
8940 \qecho DECLARE
8941 \qecho     same_marc BOOL;
8942 \qecho BEGIN
8943 \qecho     SELECT INTO same_marc enabled FROM config.internal_flag WHERE name = 'ingest.reingest.force_on_same_marc';
8944 \qecho     UPDATE config.internal_flag SET enabled = true WHERE name = 'ingest.reingest.force_on_same_marc';
8945 \qecho     UPDATE biblio.record_entry SET id=id WHERE not deleted AND id > 0;
8946 \qecho     UPDATE config.internal_flag SET enabled = same_marc WHERE name = 'ingest.reingest.force_on_same_marc';
8947 \qecho END;
8948 \qecho $FUNC$;
8949
8950 DO $FUNC$
8951 DECLARE
8952     same_marc BOOL;
8953 BEGIN
8954     SELECT INTO same_marc enabled FROM config.internal_flag WHERE name = 'ingest.reingest.force_on_same_marc';
8955     UPDATE config.internal_flag SET enabled = true WHERE name = 'ingest.reingest.force_on_same_marc';
8956     UPDATE biblio.record_entry SET id=id WHERE not deleted AND id > 0;
8957     UPDATE config.internal_flag SET enabled = same_marc WHERE name = 'ingest.reingest.force_on_same_marc';
8958 END;
8959 $FUNC$;