Pull in 2.5 version upgrade files
authorDan Wells <dbw2@calvin.edu>
Thu, 5 Dec 2013 14:49:05 +0000 (09:49 -0500)
committerDan Wells <dbw2@calvin.edu>
Thu, 5 Dec 2013 14:49:05 +0000 (09:49 -0500)
For 2.5.0 and 2.5.1

Signed-off-by: Dan Wells <dbw2@calvin.edu>
Open-ILS/src/sql/Pg/version-upgrade/2.4.3-2.5.0-upgrade-db.sql [new file with mode: 0644]
Open-ILS/src/sql/Pg/version-upgrade/2.5.0-2.5.1-upgrade-db.sql [new file with mode: 0644]

diff --git a/Open-ILS/src/sql/Pg/version-upgrade/2.4.3-2.5.0-upgrade-db.sql b/Open-ILS/src/sql/Pg/version-upgrade/2.4.3-2.5.0-upgrade-db.sql
new file mode 100644 (file)
index 0000000..80531c9
--- /dev/null
@@ -0,0 +1,16017 @@
+--Upgrade Script for 2.4.3 to 2.5.0
+
+\qecho **** Libraries that upgraded or installed 2.0 before May 2011 never
+\qecho **** got this schema, so add it first.
+\qecho **** If this fails, don't worry, it probably won't be an issue.
+\qecho
+
+BEGIN;
+
+CREATE SCHEMA staging;
+
+CREATE TABLE staging.user_stage (
+        row_id                  BIGSERIAL PRIMARY KEY,
+        row_date                TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
+        usrname                 TEXT NOT NULL,
+        profile                 TEXT,
+        email                   TEXT,
+        passwd                  TEXT,
+        ident_type              INT DEFAULT 3,
+        first_given_name        TEXT,
+        second_given_name       TEXT,
+        family_name             TEXT,
+        day_phone               TEXT,
+        evening_phone           TEXT,
+        home_ou                 INT DEFAULT 2,
+        dob                     TEXT,
+        complete                BOOL DEFAULT FALSE
+);
+
+CREATE TABLE staging.card_stage ( -- for new library barcodes
+        row_id          BIGSERIAL PRIMARY KEY,
+        row_date        TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
+        usrname         TEXT NOT NULL,
+        barcode         TEXT NOT NULL,
+        complete        BOOL DEFAULT FALSE
+);
+
+CREATE TABLE staging.mailing_address_stage (
+        row_id          BIGSERIAL PRIMARY KEY,
+        row_date        TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
+        usrname         TEXT NOT NULL,  -- user's SIS barcode, for linking
+        street1         TEXT,
+        street2         TEXT,
+        city            TEXT NOT NULL DEFAULT '',
+        state           TEXT    NOT NULL DEFAULT 'OK',
+        country         TEXT NOT NULL DEFAULT 'US',
+        post_code       TEXT NOT NULL,
+        complete        BOOL DEFAULT FALSE
+);
+
+CREATE TABLE staging.billing_address_stage (
+        LIKE staging.mailing_address_stage INCLUDING DEFAULTS
+);
+
+ALTER TABLE staging.billing_address_stage ADD PRIMARY KEY (row_id);
+
+CREATE TABLE staging.statcat_stage (
+        row_id          BIGSERIAL PRIMARY KEY,
+        row_date        TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
+        usrname         TEXT NOT NULL,
+        statcat         TEXT NOT NULL, -- for things like 'Year of study'
+        value           TEXT NOT NULL, -- and the value, such as 'Freshman'
+        complete        BOOL DEFAULT FALSE
+);
+
+COMMIT;
+
+
+\qecho **** REAL 2.5 upgrade starting now...
+
+\set eg_version '''2.5.0'''
+BEGIN;
+INSERT INTO config.upgrade_log (version, applied_to) VALUES ('2.5.0', :eg_version);
+
+SELECT evergreen.upgrade_deps_block_check('0794', :eg_version);
+
+INSERT INTO config.standing_penalty (id,name,label,block_list,staff_alert)
+    VALUES (5,'PATRON_EXCEEDS_LOST_COUNT',oils_i18n_gettext(5, 'Patron exceeds max lost item threshold', 'csp', 'label'),'CIRC|FULFILL|HOLD|CAPTURE|RENEW', TRUE);
+
+INSERT INTO config.org_unit_setting_type ( name, grp, label, description, datatype ) VALUES (
+    'circ.tally_lost', 'circ',
+    oils_i18n_gettext(
+        'circ.tally_lost',
+        'Include Lost circulations in lump sum tallies in Patron Display.',
+        'coust',
+        'label'),
+    oils_i18n_gettext(
+        'circ.tally_lost',
+        'In the Patron Display interface, the number of total active circulations for a given patron is presented in the Summary sidebar and underneath the Items Out navigation button.  This setting will include Lost circulations as counting toward these tallies.',
+        'coust',
+        'description'),
+    'bool'
+);
+
+-- Function: actor.calculate_system_penalties(integer, integer)
+-- DROP FUNCTION actor.calculate_system_penalties(integer, integer);
+
+CREATE OR REPLACE FUNCTION actor.calculate_system_penalties(match_user integer, context_org integer)
+    RETURNS SETOF actor.usr_standing_penalty AS
+$BODY$
+DECLARE
+    user_object             actor.usr%ROWTYPE;
+    new_sp_row            actor.usr_standing_penalty%ROWTYPE;
+    existing_sp_row       actor.usr_standing_penalty%ROWTYPE;
+    collections_fines      permission.grp_penalty_threshold%ROWTYPE;
+    max_fines               permission.grp_penalty_threshold%ROWTYPE;
+    max_overdue           permission.grp_penalty_threshold%ROWTYPE;
+    max_items_out       permission.grp_penalty_threshold%ROWTYPE;
+    max_lost                        permission.grp_penalty_threshold%ROWTYPE;
+    tmp_grp                 INT;
+    items_overdue        INT;
+    items_out              INT;
+    items_lost             INT;
+    context_org_list     INT[];
+    current_fines          NUMERIC(8,2) := 0.0;
+    tmp_fines               NUMERIC(8,2);
+    tmp_groc               RECORD;
+    tmp_circ                RECORD;
+    tmp_org                actor.org_unit%ROWTYPE;
+    tmp_penalty          config.standing_penalty%ROWTYPE;
+    tmp_depth            INTEGER;
+BEGIN
+    SELECT INTO user_object * FROM actor.usr WHERE id = match_user;
+
+    -- Max fines
+    SELECT INTO tmp_org * FROM actor.org_unit WHERE id = context_org;
+
+    -- Fail if the user has a high fine balance
+    LOOP
+        tmp_grp := user_object.profile;
+        LOOP
+            SELECT * INTO max_fines FROM permission.grp_penalty_threshold WHERE grp = tmp_grp AND penalty = 1 AND org_unit = tmp_org.id;
+
+            IF max_fines.threshold IS NULL THEN
+                SELECT parent INTO tmp_grp FROM permission.grp_tree WHERE id = tmp_grp;
+            ELSE
+                EXIT;
+            END IF;
+
+            IF tmp_grp IS NULL THEN
+                EXIT;
+            END IF;
+        END LOOP;
+
+        IF max_fines.threshold IS NOT NULL OR tmp_org.parent_ou IS NULL THEN
+            EXIT;
+        END IF;
+
+        SELECT * INTO tmp_org FROM actor.org_unit WHERE id = tmp_org.parent_ou;
+
+    END LOOP;
+
+    IF max_fines.threshold IS NOT NULL THEN
+
+        RETURN QUERY
+            SELECT  *
+              FROM  actor.usr_standing_penalty
+              WHERE usr = match_user
+                    AND org_unit = max_fines.org_unit
+                    AND (stop_date IS NULL or stop_date > NOW())
+                    AND standing_penalty = 1;
+
+        SELECT INTO context_org_list ARRAY_ACCUM(id) FROM actor.org_unit_full_path( max_fines.org_unit );
+
+        SELECT  SUM(f.balance_owed) INTO current_fines
+          FROM  money.materialized_billable_xact_summary f
+                JOIN (
+                    SELECT  r.id
+                      FROM  booking.reservation r
+                      WHERE r.usr = match_user
+                            AND r.pickup_lib IN (SELECT * FROM unnest(context_org_list))
+                            AND xact_finish IS NULL
+                                UNION ALL
+                    SELECT  g.id
+                      FROM  money.grocery g
+                      WHERE g.usr = match_user
+                            AND g.billing_location IN (SELECT * FROM unnest(context_org_list))
+                            AND xact_finish IS NULL
+                                UNION ALL
+                    SELECT  circ.id
+                      FROM  action.circulation circ
+                      WHERE circ.usr = match_user
+                            AND circ.circ_lib IN (SELECT * FROM unnest(context_org_list))
+                            AND xact_finish IS NULL ) l USING (id);
+
+        IF current_fines >= max_fines.threshold THEN
+            new_sp_row.usr := match_user;
+            new_sp_row.org_unit := max_fines.org_unit;
+            new_sp_row.standing_penalty := 1;
+            RETURN NEXT new_sp_row;
+        END IF;
+    END IF;
+
+    -- Start over for max overdue
+    SELECT INTO tmp_org * FROM actor.org_unit WHERE id = context_org;
+
+    -- Fail if the user has too many overdue items
+    LOOP
+        tmp_grp := user_object.profile;
+        LOOP
+
+            SELECT * INTO max_overdue FROM permission.grp_penalty_threshold WHERE grp = tmp_grp AND penalty = 2 AND org_unit = tmp_org.id;
+
+            IF max_overdue.threshold IS NULL THEN
+                SELECT parent INTO tmp_grp FROM permission.grp_tree WHERE id = tmp_grp;
+            ELSE
+                EXIT;
+            END IF;
+
+            IF tmp_grp IS NULL THEN
+                EXIT;
+            END IF;
+        END LOOP;
+
+        IF max_overdue.threshold IS NOT NULL OR tmp_org.parent_ou IS NULL THEN
+            EXIT;
+        END IF;
+
+        SELECT INTO tmp_org * FROM actor.org_unit WHERE id = tmp_org.parent_ou;
+
+    END LOOP;
+
+    IF max_overdue.threshold IS NOT NULL THEN
+
+        RETURN QUERY
+            SELECT  *
+              FROM  actor.usr_standing_penalty
+              WHERE usr = match_user
+                    AND org_unit = max_overdue.org_unit
+                    AND (stop_date IS NULL or stop_date > NOW())
+                    AND standing_penalty = 2;
+
+        SELECT  INTO items_overdue COUNT(*)
+          FROM  action.circulation circ
+                JOIN  actor.org_unit_full_path( max_overdue.org_unit ) fp ON (circ.circ_lib = fp.id)
+          WHERE circ.usr = match_user
+            AND circ.checkin_time IS NULL
+            AND circ.due_date < NOW()
+            AND (circ.stop_fines = 'MAXFINES' OR circ.stop_fines IS NULL);
+
+        IF items_overdue >= max_overdue.threshold::INT THEN
+            new_sp_row.usr := match_user;
+            new_sp_row.org_unit := max_overdue.org_unit;
+            new_sp_row.standing_penalty := 2;
+            RETURN NEXT new_sp_row;
+        END IF;
+    END IF;
+
+    -- Start over for max out
+    SELECT INTO tmp_org * FROM actor.org_unit WHERE id = context_org;
+
+    -- Fail if the user has too many checked out items
+    LOOP
+        tmp_grp := user_object.profile;
+        LOOP
+            SELECT * INTO max_items_out FROM permission.grp_penalty_threshold WHERE grp = tmp_grp AND penalty = 3 AND org_unit = tmp_org.id;
+
+            IF max_items_out.threshold IS NULL THEN
+                SELECT parent INTO tmp_grp FROM permission.grp_tree WHERE id = tmp_grp;
+            ELSE
+                EXIT;
+            END IF;
+
+            IF tmp_grp IS NULL THEN
+                EXIT;
+            END IF;
+        END LOOP;
+
+        IF max_items_out.threshold IS NOT NULL OR tmp_org.parent_ou IS NULL THEN
+            EXIT;
+        END IF;
+
+        SELECT INTO tmp_org * FROM actor.org_unit WHERE id = tmp_org.parent_ou;
+
+    END LOOP;
+
+    -- Fail if the user has too many items checked out
+    IF max_items_out.threshold IS NOT NULL THEN
+        RETURN QUERY
+            SELECT  *
+            FROM  actor.usr_standing_penalty
+            WHERE usr = match_user
+                AND org_unit = max_items_out.org_unit
+                AND (stop_date IS NULL or stop_date > NOW())
+                AND standing_penalty = 3;
+            SELECT  INTO items_out COUNT(*)
+            FROM  action.circulation circ
+            JOIN  actor.org_unit_full_path( max_items_out.org_unit ) fp ON (circ.circ_lib = fp.id)
+            WHERE circ.usr = match_user
+                AND circ.checkin_time IS NULL
+                AND (circ.stop_fines IN (
+                    SELECT 'MAXFINES'::TEXT
+                    UNION ALL
+                    SELECT 'LONGOVERDUE'::TEXT
+                    UNION ALL
+                    SELECT 'LOST'::TEXT
+                    WHERE 'true' ILIKE
+                    (
+                        SELECT CASE
+                            WHEN (SELECT value FROM actor.org_unit_ancestor_setting('circ.tally_lost', circ.circ_lib)) ILIKE 'true' THEN 'true'
+                            ELSE 'false'
+                        END
+                    )
+                    UNION ALL
+                    SELECT 'CLAIMSRETURNED'::TEXT
+                    WHERE 'false' ILIKE
+                        (
+                            SELECT CASE
+                            WHEN (SELECT value FROM actor.org_unit_ancestor_setting('circ.do_not_tally_claims_returned', circ.circ_lib)) ILIKE 'true' THEN 'true'
+                            ELSE 'false'
+                            END
+                        )
+                    ) OR circ.stop_fines IS NULL)
+                AND xact_finish IS NULL;
+
+    IF items_out >= max_items_out.threshold::INT THEN
+        new_sp_row.usr := match_user;
+        new_sp_row.org_unit := max_items_out.org_unit;
+        new_sp_row.standing_penalty := 3;
+        RETURN NEXT new_sp_row;
+   END IF;
+END IF;
+
+    -- Start over for max lost
+    SELECT INTO tmp_org * FROM actor.org_unit WHERE id = context_org;
+
+    -- Fail if the user has too many lost items
+    LOOP
+        tmp_grp := user_object.profile;
+        LOOP
+            SELECT * INTO max_lost FROM permission.grp_penalty_threshold WHERE grp = tmp_grp AND penalty = 5 AND org_unit = tmp_org.id;
+            IF max_lost.threshold IS NULL THEN
+                SELECT parent INTO tmp_grp FROM permission.grp_tree WHERE id = tmp_grp;
+            ELSE
+                EXIT;
+            END IF;
+
+            IF tmp_grp IS NULL THEN
+                EXIT;
+            END IF;
+        END LOOP;
+
+        IF max_lost.threshold IS NOT NULL OR tmp_org.parent_ou IS NULL THEN
+            EXIT;
+        END IF;
+
+        SELECT INTO tmp_org * FROM actor.org_unit WHERE id = tmp_org.parent_ou;
+
+    END LOOP;
+
+    IF max_lost.threshold IS NOT NULL THEN
+        RETURN QUERY
+            SELECT  *
+            FROM  actor.usr_standing_penalty
+            WHERE usr = match_user
+                AND org_unit = max_lost.org_unit
+                AND (stop_date IS NULL or stop_date > NOW())
+                AND standing_penalty = 5;
+
+        SELECT INTO items_lost COUNT(*)
+        FROM  action.circulation circ
+            JOIN  actor.org_unit_full_path( max_lost.org_unit ) fp ON (circ.circ_lib = fp.id)
+            WHERE circ.usr = match_user
+                AND circ.checkin_time IS NULL
+                AND (circ.stop_fines = 'LOST')
+                AND xact_finish IS NULL;
+
+        IF items_lost >= max_lost.threshold::INT AND 0 < max_lost.threshold::INT THEN
+            new_sp_row.usr := match_user;
+            new_sp_row.org_unit := max_lost.org_unit;
+            new_sp_row.standing_penalty := 5;
+            RETURN NEXT new_sp_row;
+        END IF;
+    END IF;
+
+    -- Start over for collections warning
+    SELECT INTO tmp_org * FROM actor.org_unit WHERE id = context_org;
+
+    -- Fail if the user has a collections-level fine balance
+    LOOP
+        tmp_grp := user_object.profile;
+        LOOP
+            SELECT * INTO max_fines FROM permission.grp_penalty_threshold WHERE grp = tmp_grp AND penalty = 4 AND org_unit = tmp_org.id;
+            IF max_fines.threshold IS NULL THEN
+                SELECT parent INTO tmp_grp FROM permission.grp_tree WHERE id = tmp_grp;
+            ELSE
+                EXIT;
+            END IF;
+
+            IF tmp_grp IS NULL THEN
+                EXIT;
+            END IF;
+        END LOOP;
+
+        IF max_fines.threshold IS NOT NULL OR tmp_org.parent_ou IS NULL THEN
+            EXIT;
+        END IF;
+
+        SELECT * INTO tmp_org FROM actor.org_unit WHERE id = tmp_org.parent_ou;
+
+    END LOOP;
+
+    IF max_fines.threshold IS NOT NULL THEN
+
+        RETURN QUERY
+            SELECT  *
+                FROM  actor.usr_standing_penalty
+                WHERE usr = match_user
+                    AND org_unit = max_fines.org_unit
+                    AND (stop_date IS NULL or stop_date > NOW())
+                    AND standing_penalty = 4;
+
+        SELECT INTO context_org_list ARRAY_ACCUM(id) FROM actor.org_unit_full_path( max_fines.org_unit );
+
+        SELECT  SUM(f.balance_owed) INTO current_fines
+            FROM  money.materialized_billable_xact_summary f
+                JOIN (
+                    SELECT  r.id
+                        FROM  booking.reservation r
+                        WHERE r.usr = match_user
+                            AND r.pickup_lib IN (SELECT * FROM unnest(context_org_list))
+                            AND r.xact_finish IS NULL
+                                UNION ALL
+                    SELECT  g.id
+                        FROM  money.grocery g
+                        WHERE g.usr = match_user
+                            AND g.billing_location IN (SELECT * FROM unnest(context_org_list))
+                            AND g.xact_finish IS NULL
+                                UNION ALL
+                    SELECT  circ.id
+                        FROM  action.circulation circ
+                        WHERE circ.usr = match_user
+                            AND circ.circ_lib IN (SELECT * FROM unnest(context_org_list))
+                            AND circ.xact_finish IS NULL ) l USING (id);
+
+        IF current_fines >= max_fines.threshold THEN
+            new_sp_row.usr := match_user;
+            new_sp_row.org_unit := max_fines.org_unit;
+            new_sp_row.standing_penalty := 4;
+            RETURN NEXT new_sp_row;
+        END IF;
+    END IF;
+
+    -- Start over for in collections
+    SELECT INTO tmp_org * FROM actor.org_unit WHERE id = context_org;
+
+    -- Remove the in-collections penalty if the user has paid down enough
+    -- This penalty is different, because this code is not responsible for creating 
+    -- new in-collections penalties, only for removing them
+    LOOP
+        tmp_grp := user_object.profile;
+        LOOP
+            SELECT * INTO max_fines FROM permission.grp_penalty_threshold WHERE grp = tmp_grp AND penalty = 30 AND org_unit = tmp_org.id;
+
+            IF max_fines.threshold IS NULL THEN
+                SELECT parent INTO tmp_grp FROM permission.grp_tree WHERE id = tmp_grp;
+            ELSE
+                EXIT;
+            END IF;
+
+            IF tmp_grp IS NULL THEN
+                EXIT;
+            END IF;
+        END LOOP;
+
+        IF max_fines.threshold IS NOT NULL OR tmp_org.parent_ou IS NULL THEN
+            EXIT;
+        END IF;
+
+        SELECT * INTO tmp_org FROM actor.org_unit WHERE id = tmp_org.parent_ou;
+
+    END LOOP;
+
+    IF max_fines.threshold IS NOT NULL THEN
+
+        SELECT INTO context_org_list ARRAY_ACCUM(id) FROM actor.org_unit_full_path( max_fines.org_unit );
+
+        -- first, see if the user had paid down to the threshold
+        SELECT  SUM(f.balance_owed) INTO current_fines
+          FROM  money.materialized_billable_xact_summary f
+                JOIN (
+                    SELECT  r.id
+                        FROM  booking.reservation r
+                        WHERE r.usr = match_user
+                            AND r.pickup_lib IN (SELECT * FROM unnest(context_org_list))
+                            AND r.xact_finish IS NULL
+                                UNION ALL
+                    SELECT  g.id
+                        FROM  money.grocery g
+                        WHERE g.usr = match_user
+                            AND g.billing_location IN (SELECT * FROM unnest(context_org_list))
+                            AND g.xact_finish IS NULL
+                                UNION ALL
+                    SELECT  circ.id
+                        FROM  action.circulation circ
+                        WHERE circ.usr = match_user
+                            AND circ.circ_lib IN (SELECT * FROM unnest(context_org_list))
+                            AND circ.xact_finish IS NULL ) l USING (id);
+
+        IF current_fines IS NULL OR current_fines <= max_fines.threshold THEN
+            -- patron has paid down enough
+
+            SELECT INTO tmp_penalty * FROM config.standing_penalty WHERE id = 30;
+
+            IF tmp_penalty.org_depth IS NOT NULL THEN
+
+                -- since this code is not responsible for applying the penalty, it can't 
+                -- guarantee the current context org will match the org at which the penalty 
+                --- was applied.  search up the org tree until we hit the configured penalty depth
+                SELECT INTO tmp_org * FROM actor.org_unit WHERE id = context_org;
+                SELECT INTO tmp_depth depth FROM actor.org_unit_type WHERE id = tmp_org.ou_type;
+
+                WHILE tmp_depth >= tmp_penalty.org_depth LOOP
+
+                    RETURN QUERY
+                        SELECT  *
+                            FROM  actor.usr_standing_penalty
+                            WHERE usr = match_user
+                                AND org_unit = tmp_org.id
+                                AND (stop_date IS NULL or stop_date > NOW())
+                                AND standing_penalty = 30;
+
+                    IF tmp_org.parent_ou IS NULL THEN
+                        EXIT;
+                    END IF;
+
+                    SELECT * INTO tmp_org FROM actor.org_unit WHERE id = tmp_org.parent_ou;
+                    SELECT INTO tmp_depth depth FROM actor.org_unit_type WHERE id = tmp_org.ou_type;
+                END LOOP;
+
+            ELSE
+
+                -- no penalty depth is defined, look for exact matches
+
+                RETURN QUERY
+                    SELECT  *
+                        FROM  actor.usr_standing_penalty
+                        WHERE usr = match_user
+                            AND org_unit = max_fines.org_unit
+                            AND (stop_date IS NULL or stop_date > NOW())
+                            AND standing_penalty = 30;
+            END IF;
+
+        END IF;
+
+    END IF;
+
+    RETURN;
+END;
+$BODY$
+    LANGUAGE plpgsql VOLATILE
+    COST 100
+    ROWS 1000;
+
+
+SELECT evergreen.upgrade_deps_block_check('0795', :eg_version);
+
+CREATE OR REPLACE FUNCTION 
+    evergreen.z3950_attr_name_is_valid(TEXT) RETURNS BOOLEAN AS $func$
+    SELECT EXISTS (SELECT 1 FROM config.z3950_attr WHERE name = $1);
+$func$ LANGUAGE SQL STRICT IMMUTABLE;
+
+COMMENT ON FUNCTION evergreen.z3950_attr_name_is_valid(TEXT) IS $$
+Results in TRUE if there exists at least one config.z3950_attr
+with the provided name.  Used by config.z3950_index_field_map
+to verify z3950_attr_type maps.
+$$;
+
+CREATE TABLE config.z3950_index_field_map (
+    id              SERIAL  PRIMARY KEY,
+    label           TEXT    NOT NULL, -- i18n
+    metabib_field   INTEGER REFERENCES config.metabib_field(id),
+    record_attr     TEXT    REFERENCES config.record_attr_definition(name),
+    z3950_attr      INTEGER REFERENCES config.z3950_attr(id),
+    z3950_attr_type TEXT,-- REFERENCES config.z3950_attr(name)
+    CONSTRAINT metabib_field_or_record_attr CHECK (
+        metabib_field IS NOT NULL OR 
+        record_attr IS NOT NULL
+    ),
+    CONSTRAINT attr_or_attr_type CHECK (
+        z3950_attr IS NOT NULL OR 
+        z3950_attr_type IS NOT NULL
+    ),
+    -- ensure the selected z3950_attr_type refers to a valid attr name
+    CONSTRAINT valid_z3950_attr_type CHECK (
+        z3950_attr_type IS NULL OR 
+            evergreen.z3950_attr_name_is_valid(z3950_attr_type)
+    )
+);
+
+-- seed data
+
+INSERT INTO config.z3950_index_field_map 
+    (id, label, metabib_field, z3950_attr_type) VALUES 
+(1, oils_i18n_gettext(1, 'Title',   'czifm', 'label'), 5,  'title'),
+(2, oils_i18n_gettext(2, 'Author',  'czifm', 'label'), 8,  'author'),
+(3, oils_i18n_gettext(3, 'ISBN',    'czifm', 'label'), 18, 'isbn'),
+(4, oils_i18n_gettext(4, 'ISSN',    'czifm', 'label'), 19, 'issn'),
+(5, oils_i18n_gettext(5, 'LCCN',    'czifm', 'label'), 30, 'lccn');
+
+INSERT INTO config.z3950_index_field_map 
+    (id, label, record_attr, z3950_attr_type) VALUES 
+(6, oils_i18n_gettext(6, 'Pubdate',  'czifm', 'label'),'pubdate', 'pubdate'),
+(7, oils_i18n_gettext(7, 'Item Type', 'czifm', 'label'),'item_type', 'item_type');
+
+
+-- let's leave room for more stock mappings
+SELECT SETVAL('config.z3950_index_field_map_id_seq'::TEXT, 1000);
+
+INSERT INTO config.org_unit_setting_type
+    (name, grp, label, description, datatype)
+    VALUES (
+        'cat.z3950.batch.max_parallel',
+        'cat',
+        oils_i18n_gettext(
+            'cat.z3950.batch.max_parallel',
+            'Maximum Parallel Z39.50 Batch Searches',
+            'coust',
+            'label'
+        ),
+        oils_i18n_gettext(
+            'cat.z3950.batch.max_parallel',
+            'The maximum number of Z39.50 searches that can be in-flight at any given time when performing batch Z39.50 searches',
+            'coust',
+            'description'
+        ),
+        'integer'
+    );
+
+INSERT INTO config.org_unit_setting_type
+    (name, grp, label, description, datatype)
+    VALUES (
+        'cat.z3950.batch.max_results',
+        'cat',
+        oils_i18n_gettext(
+            'cat.z3950.batch.max_results',
+            'Maximum Z39.50 Batch Search Results',
+            'coust',
+            'label'
+        ),
+        oils_i18n_gettext(
+            'cat.z3950.batch.max_results',
+            'The maximum number of search results to retrieve and queue for each record + Z39 source during batch Z39.50 searches',
+            'coust',
+            'description'
+        ),
+        'integer'
+    );
+
+INSERT INTO vandelay.bib_attr_definition (id, code, description, xpath) 
+    VALUES (
+        16, 
+        'zsource',
+        oils_i18n_gettext(16, 'Z39.50 Source', 'vqbrad', 'description'),
+        '//*[@tag="901"]/*[@code="z"]'
+    );
+
+
+
+
+SELECT evergreen.upgrade_deps_block_check('0796', :eg_version);
+
+ALTER TABLE vandelay.bib_queue ADD COLUMN match_bucket
+   INTEGER REFERENCES container.biblio_record_entry_bucket(id)
+   ON DELETE SET NULL DEFERRABLE INITIALLY DEFERRED;  
+
+CREATE OR REPLACE FUNCTION vandelay.match_bib_record() RETURNS TRIGGER AS $func$
+DECLARE
+    incoming_existing_id    TEXT;
+    test_result             vandelay.match_set_test_result%ROWTYPE;
+    tmp_rec                 BIGINT;
+    match_set               INT;
+    match_bucket            INT;
+BEGIN
+    IF TG_OP IN ('INSERT','UPDATE') AND NEW.imported_as IS NOT NULL THEN
+        RETURN NEW;
+    END IF;
+
+    DELETE FROM vandelay.bib_match WHERE queued_record = NEW.id;
+
+    SELECT q.match_set INTO match_set FROM vandelay.bib_queue q WHERE q.id = NEW.queue;
+
+    IF match_set IS NOT NULL THEN
+        NEW.quality := vandelay.measure_record_quality( NEW.marc, match_set );
+    END IF;
+
+    -- Perfect matches on 901$c exit early with a match with high quality.
+    incoming_existing_id :=
+        oils_xpath_string('//*[@tag="901"]/*[@code="c"][1]', NEW.marc);
+
+    IF incoming_existing_id IS NOT NULL AND incoming_existing_id != '' THEN
+        SELECT id INTO tmp_rec FROM biblio.record_entry WHERE id = incoming_existing_id::bigint;
+        IF tmp_rec IS NOT NULL THEN
+            INSERT INTO vandelay.bib_match (queued_record, eg_record, match_score, quality) 
+                SELECT
+                    NEW.id, 
+                    b.id,
+                    9999,
+                    -- note: no match_set means quality==0
+                    vandelay.measure_record_quality( b.marc, match_set )
+                FROM biblio.record_entry b
+                WHERE id = incoming_existing_id::bigint;
+        END IF;
+    END IF;
+
+    IF match_set IS NULL THEN
+        RETURN NEW;
+    END IF;
+
+    SELECT q.match_bucket INTO match_bucket FROM vandelay.bib_queue q WHERE q.id = NEW.queue;
+
+    FOR test_result IN SELECT * FROM
+        vandelay.match_set_test_marcxml(match_set, NEW.marc, match_bucket) LOOP
+
+        INSERT INTO vandelay.bib_match ( queued_record, eg_record, match_score, quality )
+            SELECT  
+                NEW.id,
+                test_result.record,
+                test_result.quality,
+                vandelay.measure_record_quality( b.marc, match_set )
+               FROM  biblio.record_entry b
+               WHERE id = test_result.record;
+
+    END LOOP;
+
+    RETURN NEW;
+END;
+$func$ LANGUAGE PLPGSQL;
+
+
+DROP FUNCTION IF EXISTS vandelay.match_set_test_marcxml(INTEGER, TEXT);
+
+CREATE OR REPLACE FUNCTION vandelay.match_set_test_marcxml(
+    match_set_id INTEGER, record_xml TEXT, bucket_id INTEGER 
+) RETURNS SETOF vandelay.match_set_test_result AS $$
+DECLARE
+    tags_rstore HSTORE;
+    svf_rstore  HSTORE;
+    coal        TEXT;
+    joins       TEXT;
+    query_      TEXT;
+    wq          TEXT;
+    qvalue      INTEGER;
+    rec         RECORD;
+BEGIN
+    tags_rstore := vandelay.flatten_marc_hstore(record_xml);
+    svf_rstore := vandelay.extract_rec_attrs(record_xml);
+
+    CREATE TEMPORARY TABLE _vandelay_tmp_qrows (q INTEGER);
+    CREATE TEMPORARY TABLE _vandelay_tmp_jrows (j TEXT);
+
+    -- generate the where clause and return that directly (into wq), and as
+    -- a side-effect, populate the _vandelay_tmp_[qj]rows tables.
+    wq := vandelay.get_expr_from_match_set(match_set_id, tags_rstore);
+
+    query_ := 'SELECT DISTINCT(record), ';
+
+    -- qrows table is for the quality bits we add to the SELECT clause
+    SELECT ARRAY_TO_STRING(
+        ARRAY_ACCUM('COALESCE(n' || q::TEXT || '.quality, 0)'), ' + '
+    ) INTO coal FROM _vandelay_tmp_qrows;
+
+    -- our query string so far is the SELECT clause and the inital FROM.
+    -- no JOINs yet nor the WHERE clause
+    query_ := query_ || coal || ' AS quality ' || E'\n';
+
+    -- jrows table is for the joins we must make (and the real text conditions)
+    SELECT ARRAY_TO_STRING(ARRAY_ACCUM(j), E'\n') INTO joins
+        FROM _vandelay_tmp_jrows;
+
+    -- add those joins and the where clause to our query.
+    query_ := query_ || joins || E'\n';
+
+    -- join the record bucket
+    IF bucket_id IS NOT NULL THEN
+        query_ := query_ || 'JOIN container.biblio_record_entry_bucket_item ' ||
+            'brebi ON (brebi.target_biblio_record_entry = record ' ||
+            'AND brebi.bucket = ' || bucket_id || E')\n';
+    END IF;
+
+    query_ := query_ || 'JOIN biblio.record_entry bre ON (bre.id = record) ' || 'WHERE ' || wq || ' AND not bre.deleted';
+
+    -- this will return rows of record,quality
+    FOR rec IN EXECUTE query_ USING tags_rstore, svf_rstore LOOP
+        RETURN NEXT rec;
+    END LOOP;
+
+    DROP TABLE _vandelay_tmp_qrows;
+    DROP TABLE _vandelay_tmp_jrows;
+    RETURN;
+END;
+$$ LANGUAGE PLPGSQL;
+
+
+SELECT evergreen.upgrade_deps_block_check('0797', :eg_version);
+
+-- New global flags for the purge function
+INSERT INTO config.global_flag  (name, label, enabled)
+    VALUES (
+        'history.hold.retention_age',
+        oils_i18n_gettext('history.hold.retention_age', 'Historical Hold Retention Age', 'cgf', 'label'),
+        TRUE
+    ),(
+        'history.hold.retention_age_fulfilled',
+        oils_i18n_gettext('history.hold.retention_age_fulfilled', 'Historical Hold Retention Age - Fulfilled', 'cgf', 'label'),
+        FALSE
+    ),(
+        'history.hold.retention_age_canceled',
+        oils_i18n_gettext('history.hold.retention_age_canceled', 'Historical Hold Retention Age - Canceled (Default)', 'cgf', 'label'),
+        FALSE
+    ),(
+        'history.hold.retention_age_canceled_1',
+        oils_i18n_gettext('history.hold.retention_age_canceled_1', 'Historical Hold Retention Age - Canceled (Untarged expiration)', 'cgf', 'label'),
+        FALSE
+    ),(
+        'history.hold.retention_age_canceled_2',
+        oils_i18n_gettext('history.hold.retention_age_canceled_2', 'Historical Hold Retention Age - Canceled (Hold Shelf expiration)', 'cgf', 'label'),
+        FALSE
+    ),(
+        'history.hold.retention_age_canceled_3',
+        oils_i18n_gettext('history.hold.retention_age_canceled_3', 'Historical Hold Retention Age - Canceled (Patron via phone)', 'cgf', 'label'),
+        TRUE
+    ),(
+        'history.hold.retention_age_canceled_4',
+        oils_i18n_gettext('history.hold.retention_age_canceled_4', 'Historical Hold Retention Age - Canceled (Patron in person)', 'cgf', 'label'),
+        TRUE
+    ),(
+        'history.hold.retention_age_canceled_5',
+        oils_i18n_gettext('history.hold.retention_age_canceled_5', 'Historical Hold Retention Age - Canceled (Staff forced)', 'cgf', 'label'),
+        TRUE
+    ),(
+        'history.hold.retention_age_canceled_6',
+        oils_i18n_gettext('history.hold.retention_age_canceled_6', 'Historical Hold Retention Age - Canceled (Patron via OPAC)', 'cgf', 'label'),
+        FALSE
+    );
+
+CREATE OR REPLACE FUNCTION action.purge_holds() RETURNS INT AS $func$
+DECLARE
+  current_hold RECORD;
+  purged_holds INT;
+  cgf_d INTERVAL;
+  cgf_f INTERVAL;
+  cgf_c INTERVAL;
+  prev_usr INT;
+  user_start TIMESTAMPTZ;
+  user_age INTERVAL;
+  user_count INT;
+BEGIN
+  purged_holds := 0;
+  SELECT INTO cgf_d value::INTERVAL FROM config.global_flag WHERE name = 'history.hold.retention_age' AND enabled;
+  SELECT INTO cgf_f value::INTERVAL FROM config.global_flag WHERE name = 'history.hold.retention_age_fulfilled' AND enabled;
+  SELECT INTO cgf_c value::INTERVAL FROM config.global_flag WHERE name = 'history.hold.retention_age_canceled' AND enabled;
+  FOR current_hold IN
+    SELECT
+      rank() OVER (PARTITION BY usr ORDER BY COALESCE(fulfillment_time, cancel_time) DESC),
+      cgf_cs.value::INTERVAL as cgf_cs,
+      ahr.*
+    FROM
+      action.hold_request ahr
+      LEFT JOIN config.global_flag cgf_cs ON (ahr.cancel_cause IS NOT NULL AND cgf_cs.name = 'history.hold.retention_age_canceled_' || ahr.cancel_cause AND cgf_cs.enabled)
+    WHERE
+      (fulfillment_time IS NOT NULL OR cancel_time IS NOT NULL)
+  LOOP
+    IF prev_usr IS NULL OR prev_usr != current_hold.usr THEN
+      prev_usr := current_hold.usr;
+      SELECT INTO user_start oils_json_to_text(value)::TIMESTAMPTZ FROM actor.usr_setting WHERE usr = prev_usr AND name = 'history.hold.retention_start';
+      SELECT INTO user_age oils_json_to_text(value)::INTERVAL FROM actor.usr_setting WHERE usr = prev_usr AND name = 'history.hold.retention_age';
+      SELECT INTO user_count oils_json_to_text(value)::INT FROM actor.usr_setting WHERE usr = prev_usr AND name = 'history.hold.retention_count';
+      IF user_start IS NOT NULL THEN
+        user_age := LEAST(user_age, AGE(NOW(), user_start));
+      END IF;
+      IF user_count IS NULL THEN
+        user_count := 1000; -- Assumption based on the user visible holds routine
+      END IF;
+    END IF;
+    -- Library keep age trumps user keep anything, for purposes of being able to hold on to things when staff canceled and such.
+    IF current_hold.fulfillment_time IS NOT NULL AND current_hold.fulfillment_time > NOW() - COALESCE(cgf_f, cgf_d) THEN
+      CONTINUE;
+    END IF;
+    IF current_hold.cancel_time IS NOT NULL AND current_hold.cancel_time > NOW() - COALESCE(current_hold.cgf_cs, cgf_c, cgf_d) THEN
+      CONTINUE;
+    END IF;
+
+    -- User keep age needs combining with count. If too old AND within the count, keep!
+    IF user_start IS NOT NULL AND COALESCE(current_hold.fulfillment_time, current_hold.cancel_time) > NOW() - user_age AND current_hold.rank <= user_count THEN
+      CONTINUE;
+    END IF;
+
+    -- All checks should have passed, delete!
+    DELETE FROM action.hold_request WHERE id = current_hold.id;
+    purged_holds := purged_holds + 1;
+  END LOOP;
+  RETURN purged_holds;
+END;
+$func$ LANGUAGE plpgsql;
+
+CREATE OR REPLACE FUNCTION action.usr_visible_holds (usr_id INT) RETURNS SETOF action.hold_request AS $func$
+DECLARE
+    h               action.hold_request%ROWTYPE;
+    view_age        INTERVAL;
+    view_count      INT;
+    usr_view_count  actor.usr_setting%ROWTYPE;
+    usr_view_age    actor.usr_setting%ROWTYPE;
+    usr_view_start  actor.usr_setting%ROWTYPE;
+BEGIN
+    SELECT * INTO usr_view_count FROM actor.usr_setting WHERE usr = usr_id AND name = 'history.hold.retention_count';
+    SELECT * INTO usr_view_age FROM actor.usr_setting WHERE usr = usr_id AND name = 'history.hold.retention_age';
+    SELECT * INTO usr_view_start FROM actor.usr_setting WHERE usr = usr_id AND name = 'history.hold.retention_start';
+
+    FOR h IN
+        SELECT  *
+          FROM  action.hold_request
+          WHERE usr = usr_id
+                AND fulfillment_time IS NULL
+                AND cancel_time IS NULL
+          ORDER BY request_time DESC
+    LOOP
+        RETURN NEXT h;
+    END LOOP;
+
+    IF usr_view_start.value IS NULL THEN
+        RETURN;
+    END IF;
+
+    IF usr_view_age.value IS NOT NULL THEN
+        -- User opted in and supplied a retention age
+        IF oils_json_to_text(usr_view_age.value)::INTERVAL > AGE(NOW(), oils_json_to_text(usr_view_start.value)::TIMESTAMPTZ) THEN
+            view_age := AGE(NOW(), oils_json_to_text(usr_view_start.value)::TIMESTAMPTZ);
+        ELSE
+            view_age := oils_json_to_text(usr_view_age.value)::INTERVAL;
+        END IF;
+    ELSE
+        -- User opted in
+        view_age := AGE(NOW(), oils_json_to_text(usr_view_start.value)::TIMESTAMPTZ);
+    END IF;
+
+    IF usr_view_count.value IS NOT NULL THEN
+        view_count := oils_json_to_text(usr_view_count.value)::INT;
+    ELSE
+        view_count := 1000;
+    END IF;
+
+    -- show some fulfilled/canceled holds
+    FOR h IN
+        SELECT  *
+          FROM  action.hold_request
+          WHERE usr = usr_id
+                AND ( fulfillment_time IS NOT NULL OR cancel_time IS NOT NULL )
+                AND COALESCE(fulfillment_time, cancel_time) > NOW() - view_age
+          ORDER BY COALESCE(fulfillment_time, cancel_time) DESC
+          LIMIT view_count
+    LOOP
+        RETURN NEXT h;
+    END LOOP;
+
+    RETURN;
+END;
+$func$ LANGUAGE PLPGSQL;
+
+CREATE TABLE action.aged_hold_request (
+    usr_post_code              TEXT,
+    usr_home_ou                INT     NOT NULL,
+    usr_profile                INT     NOT NULL,
+    usr_birth_year             INT,
+    staff_placed        BOOLEAN NOT NULL,
+    LIKE action.hold_request
+);
+ALTER TABLE action.aged_hold_request
+      ADD PRIMARY KEY (id),
+      DROP COLUMN usr,
+      DROP COLUMN requestor,
+      DROP COLUMN sms_carrier,
+      ALTER COLUMN phone_notify TYPE BOOLEAN
+            USING CASE WHEN phone_notify IS NULL OR phone_notify = '' THEN FALSE ELSE TRUE END,
+      ALTER COLUMN sms_notify TYPE BOOLEAN
+            USING CASE WHEN sms_notify IS NULL OR sms_notify = '' THEN FALSE ELSE TRUE END,
+      ALTER COLUMN phone_notify SET NOT NULL,
+      ALTER COLUMN sms_notify SET NOT NULL;
+CREATE INDEX aged_hold_request_target_idx ON action.aged_hold_request (target);
+CREATE INDEX aged_hold_request_pickup_lib_idx ON action.aged_hold_request (pickup_lib);
+CREATE INDEX aged_hold_request_current_copy_idx ON action.aged_hold_request (current_copy);
+CREATE INDEX aged_hold_request_fulfillment_staff_idx ON action.aged_hold_request ( fulfillment_staff );
+
+CREATE OR REPLACE VIEW action.all_hold_request AS
+    SELECT DISTINCT
+           COALESCE(a.post_code, b.post_code) AS usr_post_code,
+           p.home_ou AS usr_home_ou,
+           p.profile AS usr_profile,
+           EXTRACT(YEAR FROM p.dob)::INT AS usr_birth_year,
+           CAST(ahr.requestor <> ahr.usr AS BOOLEAN) AS staff_placed,
+           ahr.id,
+           ahr.request_time,
+           ahr.capture_time,
+           ahr.fulfillment_time,
+           ahr.checkin_time,
+           ahr.return_time,
+           ahr.prev_check_time,
+           ahr.expire_time,
+           ahr.cancel_time,
+           ahr.cancel_cause,
+           ahr.cancel_note,
+           ahr.target,
+           ahr.current_copy,
+           ahr.fulfillment_staff,
+           ahr.fulfillment_lib,
+           ahr.request_lib,
+           ahr.selection_ou,
+           ahr.selection_depth,
+           ahr.pickup_lib,
+           ahr.hold_type,
+           ahr.holdable_formats,
+           CASE
+           WHEN ahr.phone_notify IS NULL THEN FALSE
+           WHEN ahr.phone_notify = '' THEN FALSE
+           ELSE TRUE
+           END AS phone_notify,
+           ahr.email_notify,
+           CASE
+           WHEN ahr.sms_notify IS NULL THEN FALSE
+           WHEN ahr.sms_notify = '' THEN FALSE
+           ELSE TRUE
+           END AS sms_notify,
+           ahr.frozen,
+           ahr.thaw_date,
+           ahr.shelf_time,
+           ahr.cut_in_line,
+           ahr.mint_condition,
+           ahr.shelf_expire_time,
+           ahr.current_shelf_lib
+    FROM action.hold_request ahr
+         JOIN actor.usr p ON (ahr.usr = p.id)
+         LEFT JOIN actor.usr_address a ON (p.mailing_address = a.id)
+         LEFT JOIN actor.usr_address b ON (p.billing_address = b.id)
+    UNION ALL
+    SELECT 
+           usr_post_code,
+           usr_home_ou,
+           usr_profile,
+           usr_birth_year,
+           staff_placed,
+           id,
+           request_time,
+           capture_time,
+           fulfillment_time,
+           checkin_time,
+           return_time,
+           prev_check_time,
+           expire_time,
+           cancel_time,
+           cancel_cause,
+           cancel_note,
+           target,
+           current_copy,
+           fulfillment_staff,
+           fulfillment_lib,
+           request_lib,
+           selection_ou,
+           selection_depth,
+           pickup_lib,
+           hold_type,
+           holdable_formats,
+           phone_notify,
+           email_notify,
+           sms_notify,
+           frozen,
+           thaw_date,
+           shelf_time,
+           cut_in_line,
+           mint_condition,
+           shelf_expire_time,
+           current_shelf_lib
+    FROM action.aged_hold_request;
+
+CREATE OR REPLACE FUNCTION action.age_hold_on_delete () RETURNS TRIGGER AS $$
+DECLARE
+BEGIN
+    -- Archive a copy of the old row to action.aged_hold_request
+
+    INSERT INTO action.aged_hold_request
+           (usr_post_code,
+            usr_home_ou,
+            usr_profile,
+            usr_birth_year,
+            staff_placed,
+            id,
+            request_time,
+            capture_time,
+            fulfillment_time,
+            checkin_time,
+            return_time,
+            prev_check_time,
+            expire_time,
+            cancel_time,
+            cancel_cause,
+            cancel_note,
+            target,
+            current_copy,
+            fulfillment_staff,
+            fulfillment_lib,
+            request_lib,
+            selection_ou,
+            selection_depth,
+            pickup_lib,
+            hold_type,
+            holdable_formats,
+            phone_notify,
+            email_notify,
+            sms_notify,
+            frozen,
+            thaw_date,
+            shelf_time,
+            cut_in_line,
+            mint_condition,
+            shelf_expire_time,
+            current_shelf_lib)
+      SELECT 
+           usr_post_code,
+           usr_home_ou,
+           usr_profile,
+           usr_birth_year,
+           staff_placed,
+           id,
+           request_time,
+           capture_time,
+           fulfillment_time,
+           checkin_time,
+           return_time,
+           prev_check_time,
+           expire_time,
+           cancel_time,
+           cancel_cause,
+           cancel_note,
+           target,
+           current_copy,
+           fulfillment_staff,
+           fulfillment_lib,
+           request_lib,
+           selection_ou,
+           selection_depth,
+           pickup_lib,
+           hold_type,
+           holdable_formats,
+           phone_notify,
+           email_notify,
+           sms_notify,
+           frozen,
+           thaw_date,
+           shelf_time,
+           cut_in_line,
+           mint_condition,
+           shelf_expire_time,
+           current_shelf_lib
+        FROM action.all_hold_request WHERE id = OLD.id;
+
+    RETURN OLD;
+END;
+$$ LANGUAGE 'plpgsql';
+
+CREATE TRIGGER action_hold_request_aging_tgr
+       BEFORE DELETE ON action.hold_request
+       FOR EACH ROW
+       EXECUTE PROCEDURE action.age_hold_on_delete ();
+
+
+SELECT evergreen.upgrade_deps_block_check('0798', :eg_version);
+
+INSERT INTO config.global_flag (name, label)
+    VALUES (
+        'history.circ.retention_uses_last_finished',
+        oils_i18n_gettext(
+            'history.circ.retention_uses_last_finished',
+            'Historical Circulations use most recent xact_finish date instead of last circ''s.',
+            'cgf',
+            'label'
+        )
+    ),(
+        'history.circ.retention_age_is_min',
+        oils_i18n_gettext(
+            'history.circ.retention_age_is_min',
+            'Historical Circulations are kept for global retention age at a minimum, regardless of user preferences.',
+            'cgf',
+            'label'
+        )
+    );
+
+
+-- Drop old variants
+DROP FUNCTION IF EXISTS action.circ_chain(INTEGER);
+DROP FUNCTION IF EXISTS action.summarize_circ_chain(INTEGER);
+
+CREATE OR REPLACE FUNCTION action.circ_chain ( ctx_circ_id BIGINT ) RETURNS SETOF action.circulation AS $$
+DECLARE
+    tmp_circ action.circulation%ROWTYPE;
+    circ_0 action.circulation%ROWTYPE;
+BEGIN
+
+    SELECT INTO tmp_circ * FROM action.circulation WHERE id = ctx_circ_id;
+
+    IF tmp_circ IS NULL THEN
+        RETURN NEXT tmp_circ;
+    END IF;
+    circ_0 := tmp_circ;
+
+    -- find the front of the chain
+    WHILE TRUE LOOP
+        SELECT INTO tmp_circ * FROM action.circulation WHERE id = tmp_circ.parent_circ;
+        IF tmp_circ IS NULL THEN
+            EXIT;
+        END IF;
+        circ_0 := tmp_circ;
+    END LOOP;
+
+    -- now send the circs to the caller, oldest to newest
+    tmp_circ := circ_0;
+    WHILE TRUE LOOP
+        IF tmp_circ IS NULL THEN
+            EXIT;
+        END IF;
+        RETURN NEXT tmp_circ;
+        SELECT INTO tmp_circ * FROM action.circulation WHERE parent_circ = tmp_circ.id;
+    END LOOP;
+
+END;
+$$ LANGUAGE 'plpgsql';
+
+CREATE OR REPLACE FUNCTION action.summarize_circ_chain ( ctx_circ_id BIGINT ) RETURNS action.circ_chain_summary AS $$
+
+DECLARE
+
+    -- first circ in the chain
+    circ_0 action.circulation%ROWTYPE;
+
+    -- last circ in the chain
+    circ_n action.circulation%ROWTYPE;
+
+    -- circ chain under construction
+    chain action.circ_chain_summary;
+    tmp_circ action.circulation%ROWTYPE;
+
+BEGIN
+    
+    chain.num_circs := 0;
+    FOR tmp_circ IN SELECT * FROM action.circ_chain(ctx_circ_id) LOOP
+
+        IF chain.num_circs = 0 THEN
+            circ_0 := tmp_circ;
+        END IF;
+
+        chain.num_circs := chain.num_circs + 1;
+        circ_n := tmp_circ;
+    END LOOP;
+
+    chain.start_time := circ_0.xact_start;
+    chain.last_stop_fines := circ_n.stop_fines;
+    chain.last_stop_fines_time := circ_n.stop_fines_time;
+    chain.last_checkin_time := circ_n.checkin_time;
+    chain.last_checkin_scan_time := circ_n.checkin_scan_time;
+    SELECT INTO chain.checkout_workstation name FROM actor.workstation WHERE id = circ_0.workstation;
+    SELECT INTO chain.last_checkin_workstation name FROM actor.workstation WHERE id = circ_n.checkin_workstation;
+
+    IF chain.num_circs > 1 THEN
+        chain.last_renewal_time := circ_n.xact_start;
+        SELECT INTO chain.last_renewal_workstation name FROM actor.workstation WHERE id = circ_n.workstation;
+    END IF;
+
+    RETURN chain;
+
+END;
+$$ LANGUAGE 'plpgsql';
+
+CREATE OR REPLACE FUNCTION action.purge_circulations () RETURNS INT AS $func$
+DECLARE
+    usr_keep_age    actor.usr_setting%ROWTYPE;
+    usr_keep_start  actor.usr_setting%ROWTYPE;
+    org_keep_age    INTERVAL;
+    org_use_last    BOOL = false;
+    org_age_is_min  BOOL = false;
+    org_keep_count  INT;
+
+    keep_age        INTERVAL;
+
+    target_acp      RECORD;
+    circ_chain_head action.circulation%ROWTYPE;
+    circ_chain_tail action.circulation%ROWTYPE;
+
+    count_purged    INT;
+    num_incomplete  INT;
+
+    last_finished   TIMESTAMP WITH TIME ZONE;
+BEGIN
+
+    count_purged := 0;
+
+    SELECT value::INTERVAL INTO org_keep_age FROM config.global_flag WHERE name = 'history.circ.retention_age' AND enabled;
+
+    SELECT value::INT INTO org_keep_count FROM config.global_flag WHERE name = 'history.circ.retention_count' AND enabled;
+    IF org_keep_count IS NULL THEN
+        RETURN count_purged; -- Gimme a count to keep, or I keep them all, forever
+    END IF;
+
+    SELECT enabled INTO org_use_last FROM config.global_flag WHERE name = 'history.circ.retention_uses_last_finished';
+    SELECT enabled INTO org_age_is_min FROM config.global_flag WHERE name = 'history.circ.retention_age_is_min';
+
+    -- First, find copies with more than keep_count non-renewal circs
+    FOR target_acp IN
+        SELECT  target_copy,
+                COUNT(*) AS total_real_circs
+          FROM  action.circulation
+          WHERE parent_circ IS NULL
+                AND xact_finish IS NOT NULL
+          GROUP BY target_copy
+          HAVING COUNT(*) > org_keep_count
+    LOOP
+        -- And, for those, select circs that are finished and older than keep_age
+        FOR circ_chain_head IN
+            -- For reference, the subquery uses a window function to order the circs newest to oldest and number them
+            -- The outer query then uses that information to skip the most recent set the library wants to keep
+            -- End result is we don't care what order they come out in, as they are all potentials for deletion.
+            SELECT ac.* FROM action.circulation ac JOIN (
+              SELECT  rank() OVER (ORDER BY xact_start DESC), ac.id
+                FROM  action.circulation ac
+                WHERE ac.target_copy = target_acp.target_copy
+                  AND ac.parent_circ IS NULL
+                ORDER BY ac.xact_start ) ranked USING (id)
+                WHERE ranked.rank > org_keep_count
+        LOOP
+
+            SELECT * INTO circ_chain_tail FROM action.circ_chain(circ_chain_head.id) ORDER BY xact_start DESC LIMIT 1;
+            SELECT COUNT(CASE WHEN xact_finish IS NULL THEN 1 ELSE NULL END), MAX(xact_finish) INTO num_incomplete, last_finished FROM action.circ_chain(circ_chain_head.id);
+            CONTINUE WHEN circ_chain_tail.xact_finish IS NULL OR num_incomplete > 0;
+
+            IF NOT org_use_last THEN
+                last_finished := circ_chain_tail.xact_finish;
+            END IF;
+
+            -- Now get the user settings, if any, to block purging if the user wants to keep more circs
+            usr_keep_age.value := NULL;
+            SELECT * INTO usr_keep_age FROM actor.usr_setting WHERE usr = circ_chain_head.usr AND name = 'history.circ.retention_age';
+
+            usr_keep_start.value := NULL;
+            SELECT * INTO usr_keep_start FROM actor.usr_setting WHERE usr = circ_chain_head.usr AND name = 'history.circ.retention_start';
+
+            IF usr_keep_age.value IS NOT NULL AND usr_keep_start.value IS NOT NULL THEN
+                IF oils_json_to_text(usr_keep_age.value)::INTERVAL > AGE(NOW(), oils_json_to_text(usr_keep_start.value)::TIMESTAMPTZ) THEN
+                    keep_age := AGE(NOW(), oils_json_to_text(usr_keep_start.value)::TIMESTAMPTZ);
+                ELSE
+                    keep_age := oils_json_to_text(usr_keep_age.value)::INTERVAL;
+                END IF;
+            ELSIF usr_keep_start.value IS NOT NULL THEN
+                keep_age := AGE(NOW(), oils_json_to_text(usr_keep_start.value)::TIMESTAMPTZ);
+            ELSE
+                keep_age := COALESCE( org_keep_age, '2000 years'::INTERVAL );
+            END IF;
+
+            IF org_age_is_min THEN
+                keep_age := GREATEST( keep_age, org_keep_age );
+            END IF;
+
+            CONTINUE WHEN AGE(NOW(), last_finished) < keep_age;
+
+            -- We've passed the purging tests, purge the circ chain starting at the end
+            -- A trigger should auto-purge the rest of the chain.
+            DELETE FROM action.circulation WHERE id = circ_chain_tail.id;
+
+            count_purged := count_purged + 1;
+
+        END LOOP;
+    END LOOP;
+
+    return count_purged;
+END;
+$func$ LANGUAGE PLPGSQL;
+
+
+SELECT evergreen.upgrade_deps_block_check('0799', :eg_version);
+
+-- allow state to be null
+ALTER TABLE actor.usr_address ALTER COLUMN state DROP NOT NULL;
+
+-- create new YAOUS
+INSERT into config.org_unit_setting_type
+    (name, grp, label, description, datatype)
+    VALUES (
+        'ui.patron.edit.au.state.require',
+        'gui',
+        oils_i18n_gettext(
+            'ui.patron.edit.au.state.require',
+            'Require State field on patron registration',
+            'coust',
+            'label'
+        ),
+        oils_i18n_gettext(
+            'ui.patron.edit.au.state.require',
+            'The State field will be required on the patron registration screen.',
+            'coust',
+            'description'
+        ),
+        'bool'
+    );
+
+INSERT into config.org_unit_setting_type
+    (name, grp, label, description, datatype)
+    VALUES (
+        'ui.patron.edit.au.state.show',
+        'gui',
+        oils_i18n_gettext(
+            'ui.patron.edit.au.state.show',
+            'Show State field on patron registration',
+            'coust',
+            'label'
+        ),
+        oils_i18n_gettext(
+            'ui.patron.edit.au.state.show',
+            'The State field will be shown on the patron registration screen. Showing a field makes it appear with required fields even when not required. If the field is required this setting is ignored.',
+            'coust',
+            'description'
+        ),
+        'bool'
+    ); 
+
+INSERT into config.org_unit_setting_type
+    (name, grp, label, description, datatype)
+    VALUES (
+        'ui.patron.edit.au.state.suggest',
+        'gui',
+        oils_i18n_gettext(
+            'ui.patron.edit.au.state.suggest',
+            'Suggest State field on patron registration',
+            'coust',
+            'label'
+        ),
+        oils_i18n_gettext(
+            'ui.patron.edit.au.state.suggest',
+            'The State field will be suggested on the patron registration screen. Suggesting a field makes it appear when suggested fields are shown. If the field is shown or required this setting is ignored.',
+            'coust',
+            'description'
+        ),
+        'bool'
+    );         
+
+
+SELECT evergreen.upgrade_deps_block_check('0801', :eg_version);
+
+INSERT into config.org_unit_setting_type
+( name, grp, label, description, datatype, fm_class ) VALUES
+( 'ui.patron.edit.ac.barcode.regex', 'gui',
+    oils_i18n_gettext('ui.patron.edit.ac.barcode.regex',
+        'Regex for barcodes on patron registration',
+        'coust', 'label'),
+    oils_i18n_gettext('ui.patron.edit.ac.barcode.regex',
+        'The Regular Expression for validation on barcodes in patron registration.',
+        'coust', 'description'),
+    'string', null);
+
+
+SELECT evergreen.upgrade_deps_block_check('0802', :eg_version);
+
+CREATE OR REPLACE FUNCTION authority.normalize_heading( marcxml TEXT, no_thesaurus BOOL ) RETURNS TEXT AS $func$
+DECLARE
+    acsaf           authority.control_set_authority_field%ROWTYPE;
+    tag_used        TEXT;
+    nfi_used        TEXT;
+    sf              TEXT;
+    sf_node         TEXT;
+    tag_node        TEXT;
+    thes_code       TEXT;
+    cset            INT;
+    heading_text    TEXT;
+    tmp_text        TEXT;
+    first_sf        BOOL;
+    auth_id         INT DEFAULT COALESCE(NULLIF(oils_xpath_string('//*[@tag="901"]/*[local-name()="subfield" and @code="c"]', marcxml), ''), '0')::INT;
+BEGIN
+    SELECT control_set INTO cset FROM authority.record_entry WHERE id = auth_id;
+
+    IF cset IS NULL THEN
+        SELECT  control_set INTO cset
+          FROM  authority.control_set_authority_field
+          WHERE tag IN ( SELECT  UNNEST(XPATH('//*[starts-with(@tag,"1")]/@tag',marcxml::XML)::TEXT[]))
+          LIMIT 1;
+    END IF;
+
+    thes_code := vandelay.marc21_extract_fixed_field(marcxml,'Subj');
+    IF thes_code IS NULL THEN
+        thes_code := '|';
+    ELSIF thes_code = 'z' THEN
+        thes_code := COALESCE( oils_xpath_string('//*[@tag="040"]/*[@code="f"][1]', marcxml), '' );
+    END IF;
+
+    heading_text := '';
+    FOR acsaf IN SELECT * FROM authority.control_set_authority_field WHERE control_set = cset AND main_entry IS NULL LOOP
+        tag_used := acsaf.tag;
+        nfi_used := acsaf.nfi;
+        first_sf := TRUE;
+
+        FOR tag_node IN SELECT unnest(oils_xpath('//*[@tag="'||tag_used||'"]',marcxml)) LOOP
+            FOR sf_node IN SELECT unnest(oils_xpath('//*[contains("'||acsaf.sf_list||'",@code)]',tag_node)) LOOP
+
+                tmp_text := oils_xpath_string('.', sf_node);
+                sf := oils_xpath_string('./@code', sf_node);
+
+                IF first_sf AND tmp_text IS NOT NULL AND nfi_used IS NOT NULL THEN
+
+                    tmp_text := SUBSTRING(
+                        tmp_text FROM
+                        COALESCE(
+                            NULLIF(
+                                REGEXP_REPLACE(
+                                    oils_xpath_string('./@ind'||nfi_used, tag_node),
+                                    $$\D+$$,
+                                    '',
+                                    'g'
+                                ),
+                                ''
+                            )::INT,
+                            0
+                        ) + 1
+                    );
+
+                END IF;
+
+                first_sf := FALSE;
+
+                IF tmp_text IS NOT NULL AND tmp_text <> '' THEN
+                    heading_text := heading_text || E'\u2021' || sf || ' ' || tmp_text;
+                END IF;
+            END LOOP;
+
+            EXIT WHEN heading_text <> '';
+        END LOOP;
+
+        EXIT WHEN heading_text <> '';
+    END LOOP;
+
+    IF heading_text <> '' THEN
+        IF no_thesaurus IS TRUE THEN
+            heading_text := tag_used || ' ' || public.naco_normalize(heading_text);
+        ELSE
+            heading_text := tag_used || '_' || COALESCE(nfi_used,'-') || '_' || thes_code || ' ' || public.naco_normalize(heading_text);
+        END IF;
+    ELSE
+        heading_text := 'NOHEADING_' || thes_code || ' ' || MD5(marcxml);
+    END IF;
+
+    RETURN heading_text;
+END;
+$func$ LANGUAGE PLPGSQL IMMUTABLE;
+
+CREATE OR REPLACE FUNCTION authority.simple_heading_set( marcxml TEXT ) RETURNS SETOF authority.simple_heading AS $func$
+DECLARE
+    res             authority.simple_heading%ROWTYPE;
+    acsaf           authority.control_set_authority_field%ROWTYPE;
+    tag_used        TEXT;
+    nfi_used        TEXT;
+    sf              TEXT;
+    cset            INT;
+    heading_text    TEXT;
+    sort_text       TEXT;
+    tmp_text        TEXT;
+    tmp_xml         TEXT;
+    first_sf        BOOL;
+    auth_id         INT DEFAULT oils_xpath_string('//*[@tag="901"]/*[local-name()="subfield" and @code="c"]', marcxml)::INT;
+BEGIN
+
+    res.record := auth_id;
+
+    SELECT  control_set INTO cset
+      FROM  authority.control_set_authority_field
+      WHERE tag IN ( SELECT UNNEST(XPATH('//*[starts-with(@tag,"1")]/@tag',marcxml::XML)::TEXT[]) )
+      LIMIT 1;
+
+    FOR acsaf IN SELECT * FROM authority.control_set_authority_field WHERE control_set = cset LOOP
+
+        res.atag := acsaf.id;
+        tag_used := acsaf.tag;
+        nfi_used := acsaf.nfi;
+
+        FOR tmp_xml IN SELECT UNNEST(XPATH('//*[@tag="'||tag_used||'"]', marcxml::XML)) LOOP
+
+            heading_text := public.naco_normalize(
+                COALESCE(
+                    oils_xpath_string('//*[contains("'||acsaf.sf_list||'",@code)]',tmp_xml::TEXT, ' '),
+                    ''
+                )
+            );
+
+            IF nfi_used IS NOT NULL THEN
+
+                sort_text := SUBSTRING(
+                    heading_text FROM
+                    COALESCE(
+                        NULLIF(
+                            REGEXP_REPLACE(
+                                oils_xpath_string('./@ind'||nfi_used, tmp_xml::TEXT),
+                                $$\D+$$,
+                                '',
+                                'g'
+                            ),
+                            ''
+                        )::INT,
+                        0
+                    ) + 1
+                );
+
+            ELSE
+                sort_text := heading_text;
+            END IF;
+
+            IF heading_text IS NOT NULL AND heading_text <> '' THEN
+                res.value := heading_text;
+                res.sort_value := sort_text;
+                RETURN NEXT res;
+            END IF;
+
+        END LOOP;
+
+    END LOOP;
+
+    RETURN;
+END;
+$func$ LANGUAGE PLPGSQL IMMUTABLE;
+
+
+
+-- check whether patch can be applied
+SELECT evergreen.upgrade_deps_block_check('0805', :eg_version);
+
+INSERT INTO config.global_flag (name, label, enabled)
+    VALUES (
+        'circ.desk_renewal.use_original_circ_lib',
+        oils_i18n_gettext(
+            'circ.desk_renewal.use_original_circ_lib',
+            'Circ: Use original circulation library on desk renewal instead of user home library',
+            'cgf',
+            'label'
+        ),
+        FALSE
+    );
+
+
+-- check whether patch can be applied
+SELECT evergreen.upgrade_deps_block_check('0806', :eg_version);
+
+INSERT INTO action.hold_request_cancel_cause (id,label) 
+    VALUES (7,'Patron via SIP');
+
+
+-- check whether patch can be applied
+SELECT evergreen.upgrade_deps_block_check('0807', :eg_version);
+
+ALTER TABLE config.usr_setting_type
+    ADD COLUMN reg_default TEXT;
+
+
+SELECT evergreen.upgrade_deps_block_check('0809', :eg_version);
+
+ALTER TABLE actor.org_address ALTER COLUMN state DROP NOT NULL;
+
+
+-- Evergreen DB patch 0812.data.add_library_info_url_OUS.sql
+--
+-- Adds YAOUS for enabling information links from the TPAC to a library URL
+--
+
+-- check whether patch can be applied
+SELECT evergreen.upgrade_deps_block_check('0812', :eg_version);
+
+-- FIXME: add/check SQL statements to perform the upgrade
+INSERT into config.org_unit_setting_type
+( name, grp, label, description, datatype, fm_class ) VALUES
+( 'lib.info_url', 'lib',
+    oils_i18n_gettext('lib.info_url',
+        'Library information URL (such as "http://example.com/about.html")',
+        'coust', 'label'),
+    oils_i18n_gettext('lib.info_url',
+        'URL for information on this library, such as contact information, hours of operation, and directions. If set, the library name in the copy details section links to that URL. Use a complete URL, such as "http://example.com/hours.html".',
+        'coust', 'description'),
+    'string', null)
+;
+
+
+SELECT evergreen.upgrade_deps_block_check('0813', :eg_version);
+
+-- Don't require state in the auditor tracking for user addresses
+
+ALTER TABLE auditor.actor_usr_address_history ALTER COLUMN state DROP NOT NULL;
+
+-- Change constraint on actor.org_unit_setting_log to be deferrable initially
+
+ALTER TABLE config.org_unit_setting_type_log
+  DROP CONSTRAINT org_unit_setting_type_log_field_name_fkey,
+  ADD CONSTRAINT org_unit_setting_type_log_field_name_fkey FOREIGN KEY (field_name)
+    REFERENCES config.org_unit_setting_type (name) MATCH SIMPLE
+    ON UPDATE NO ACTION ON DELETE NO ACTION DEFERRABLE INITIALLY DEFERRED;
+
+-- Fix names in the org unit setting configuration
+
+UPDATE config.org_unit_setting_type SET name = overlay(name placing 'aua' from 16 for 2) where name like 'ui.patron.edit.au.state.%';
+
+-- Fix names if they have already been set in the editor
+
+UPDATE actor.org_unit_setting SET name = overlay(name placing 'aua' from 16 for 2) where name like 'ui.patron.edit.au.state.%';
+
+-- and the logs too
+
+UPDATE config.org_unit_setting_type_log SET field_name = overlay(field_name placing 'aua' from 16 for 2) where field_name like 'ui.patron.edit.au.state.%';
+
+
+SELECT evergreen.upgrade_deps_block_check('0814', :eg_version);
+
+UPDATE permission.perm_list
+SET description = 'Allow a user to delete a provider'
+WHERE code = 'DELETE_PROVIDER';
+
+
+-- check whether patch can be applied
+SELECT evergreen.upgrade_deps_block_check('0815', :eg_version);
+
+ALTER TABLE authority.control_set_authority_field
+    ADD COLUMN linking_subfield CHAR(1);
+
+UPDATE authority.control_set_authority_field
+    SET linking_subfield = '0' WHERE main_entry IS NOT NULL;
+
+CREATE TABLE authority.authority_linking (
+    id      BIGSERIAL PRIMARY KEY,
+    source  BIGINT REFERENCES authority.record_entry (id) NOT NULL,
+    target  BIGINT REFERENCES authority.record_entry (id) NOT NULL,
+    field   INT REFERENCES authority.control_set_authority_field (id) NOT NULL
+);
+
+-- Given an authority record's ID, control set ID (if known), and marc::XML,
+-- return all links to other authority records in the form of rows that
+-- can be inserted into authority.authority_linking.
+CREATE OR REPLACE FUNCTION authority.calculate_authority_linking(
+    rec_id BIGINT, rec_control_set INT, rec_marc_xml XML
+) RETURNS SETOF authority.authority_linking AS $func$
+DECLARE
+    acsaf       authority.control_set_authority_field%ROWTYPE;
+    link        TEXT;
+    aal         authority.authority_linking%ROWTYPE;
+BEGIN
+    IF rec_control_set IS NULL THEN
+        -- No control_set on record?  Guess at one
+        SELECT control_set INTO rec_control_set
+            FROM authority.control_set_authority_field
+            WHERE tag IN (
+                SELECT UNNEST(
+                    XPATH('//*[starts-with(@tag,"1")]/@tag',rec_marc_xml::XML)::TEXT[]
+                )
+            ) LIMIT 1;
+
+        IF NOT FOUND THEN
+            RAISE WARNING 'Could not even guess at control set for authority record %', rec_id;
+            RETURN;
+        END IF;
+    END IF;
+
+    aal.source := rec_id;
+
+    FOR acsaf IN
+        SELECT * FROM authority.control_set_authority_field
+        WHERE control_set = rec_control_set
+            AND linking_subfield IS NOT NULL
+            AND main_entry IS NOT NULL
+    LOOP
+        link := SUBSTRING(
+            (XPATH('//*[@tag="' || acsaf.tag || '"]/*[@code="' ||
+                acsaf.linking_subfield || '"]/text()', rec_marc_xml))[1]::TEXT,
+            '\d+$'
+        );
+
+        -- Ignore links that are null, malformed, circular, or point to
+        -- non-existent authority records.
+        IF link IS NOT NULL AND link::BIGINT <> rec_id THEN
+            PERFORM * FROM authority.record_entry WHERE id = link::BIGINT;
+            IF FOUND THEN
+                aal.target := link::BIGINT;
+                aal.field := acsaf.id;
+                RETURN NEXT aal;
+            END IF;
+        END IF;
+    END LOOP;
+END;
+$func$ LANGUAGE PLPGSQL;
+
+
+-- AFTER UPDATE OR INSERT trigger for authority.record_entry
+CREATE OR REPLACE FUNCTION authority.indexing_ingest_or_delete () RETURNS TRIGGER AS $func$
+BEGIN
+
+    IF NEW.deleted IS TRUE THEN -- If this authority is deleted
+        DELETE FROM authority.bib_linking WHERE authority = NEW.id; -- Avoid updating fields in bibs that are no longer visible
+        DELETE FROM authority.full_rec WHERE record = NEW.id; -- Avoid validating fields against deleted authority records
+        DELETE FROM authority.simple_heading WHERE record = NEW.id;
+          -- Should remove matching $0 from controlled fields at the same time?
+
+        -- XXX What do we about the actual linking subfields present in
+        -- authority records that target this one when this happens?
+        DELETE FROM authority.authority_linking
+            WHERE source = NEW.id OR target = NEW.id;
+
+        RETURN NEW; -- and we're done
+    END IF;
+
+    IF TG_OP = 'UPDATE' THEN -- re-ingest?
+        PERFORM * FROM config.internal_flag WHERE name = 'ingest.reingest.force_on_same_marc' AND enabled;
+
+        IF NOT FOUND AND OLD.marc = NEW.marc THEN -- don't do anything if the MARC didn't change
+            RETURN NEW;
+        END IF;
+
+        -- Propagate these updates to any linked bib records
+        PERFORM authority.propagate_changes(NEW.id) FROM authority.record_entry WHERE id = NEW.id;
+
+        DELETE FROM authority.simple_heading WHERE record = NEW.id;
+        DELETE FROM authority.authority_linking WHERE source = NEW.id;
+    END IF;
+
+    INSERT INTO authority.authority_linking (source, target, field)
+        SELECT source, target, field FROM authority.calculate_authority_linking(
+            NEW.id, NEW.control_set, NEW.marc::XML
+        );
+
+    INSERT INTO authority.simple_heading (record,atag,value,sort_value)
+        SELECT record, atag, value, sort_value FROM authority.simple_heading_set(NEW.marc);
+
+    -- Flatten and insert the afr data
+    PERFORM * FROM config.internal_flag WHERE name = 'ingest.disable_authority_full_rec' AND enabled;
+    IF NOT FOUND THEN
+        PERFORM authority.reingest_authority_full_rec(NEW.id);
+        PERFORM * FROM config.internal_flag WHERE name = 'ingest.disable_authority_rec_descriptor' AND enabled;
+        IF NOT FOUND THEN
+            PERFORM authority.reingest_authority_rec_descriptor(NEW.id);
+        END IF;
+    END IF;
+
+    RETURN NEW;
+END;
+$func$ LANGUAGE PLPGSQL;
+
+
+-- check whether patch can be applied
+SELECT evergreen.upgrade_deps_block_check('0816', :eg_version);
+
+-- To avoid problems with altering a table column after doing an
+-- update.
+ALTER TABLE authority.control_set_authority_field
+    DISABLE TRIGGER ALL;
+
+ALTER TABLE authority.control_set_authority_field
+    ADD COLUMN display_sf_list TEXT;
+
+UPDATE authority.control_set_authority_field
+    SET display_sf_list = REGEXP_REPLACE(sf_list, '[w254]', '', 'g');
+
+ALTER TABLE authority.control_set_authority_field
+    ALTER COLUMN display_sf_list SET NOT NULL;
+
+ALTER TABLE authority.control_set_authority_field
+    ENABLE TRIGGER ALL;
+
+ALTER TABLE metabib.browse_entry_def_map
+    ADD COLUMN authority BIGINT REFERENCES authority.record_entry (id)
+        ON DELETE SET NULL;
+
+ALTER TABLE config.metabib_field ADD COLUMN authority_xpath TEXT;
+ALTER TABLE config.metabib_field ADD COLUMN browse_sort_xpath TEXT;
+
+UPDATE config.metabib_field
+    SET authority_xpath = '//@xlink:href'
+    WHERE
+        format = 'mods32' AND
+        field_class IN ('subject','series','title','author') AND
+        browse_field IS TRUE;
+
+ALTER TYPE metabib.field_entry_template ADD ATTRIBUTE authority BIGINT;
+ALTER TYPE metabib.field_entry_template ADD ATTRIBUTE sort_value TEXT;
+
+CREATE OR REPLACE FUNCTION metabib.reingest_metabib_field_entries( bib_id BIGINT, skip_facet BOOL DEFAULT FALSE, skip_browse BOOL DEFAULT FALSE, skip_search BOOL DEFAULT FALSE ) RETURNS VOID AS $func$
+DECLARE
+    fclass          RECORD;
+    ind_data        metabib.field_entry_template%ROWTYPE;
+    mbe_row         metabib.browse_entry%ROWTYPE;
+    mbe_id          BIGINT;
+    b_skip_facet    BOOL;
+    b_skip_browse   BOOL;
+    b_skip_search   BOOL;
+    value_prepped   TEXT;
+BEGIN
+
+    SELECT COALESCE(NULLIF(skip_facet, FALSE), EXISTS (SELECT enabled FROM config.internal_flag WHERE name =  'ingest.skip_facet_indexing' AND enabled)) INTO b_skip_facet;
+    SELECT COALESCE(NULLIF(skip_browse, FALSE), EXISTS (SELECT enabled FROM config.internal_flag WHERE name =  'ingest.skip_browse_indexing' AND enabled)) INTO b_skip_browse;
+    SELECT COALESCE(NULLIF(skip_search, FALSE), EXISTS (SELECT enabled FROM config.internal_flag WHERE name =  'ingest.skip_search_indexing' AND enabled)) INTO b_skip_search;
+
+    PERFORM * FROM config.internal_flag WHERE name = 'ingest.assume_inserts_only' AND enabled;
+    IF NOT FOUND THEN
+        IF NOT b_skip_search THEN
+            FOR fclass IN SELECT * FROM config.metabib_class LOOP
+                -- RAISE NOTICE 'Emptying out %', fclass.name;
+                EXECUTE $$DELETE FROM metabib.$$ || fclass.name || $$_field_entry WHERE source = $$ || bib_id;
+            END LOOP;
+        END IF;
+        IF NOT b_skip_facet THEN
+            DELETE FROM metabib.facet_entry WHERE source = bib_id;
+        END IF;
+        IF NOT b_skip_browse THEN
+            DELETE FROM metabib.browse_entry_def_map WHERE source = bib_id;
+        END IF;
+    END IF;
+
+    FOR ind_data IN SELECT * FROM biblio.extract_metabib_field_entry( bib_id ) LOOP
+        IF ind_data.field < 0 THEN
+            ind_data.field = -1 * ind_data.field;
+        END IF;
+
+        IF ind_data.facet_field AND NOT b_skip_facet THEN
+            INSERT INTO metabib.facet_entry (field, source, value)
+                VALUES (ind_data.field, ind_data.source, ind_data.value);
+        END IF;
+
+        IF ind_data.browse_field AND NOT b_skip_browse THEN
+            -- A caveat about this SELECT: this should take care of replacing
+            -- old mbe rows when data changes, but not if normalization (by
+            -- which I mean specifically the output of
+            -- evergreen.oils_tsearch2()) changes.  It may or may not be
+            -- expensive to add a comparison of index_vector to index_vector
+            -- to the WHERE clause below.
+
+            value_prepped := metabib.browse_normalize(ind_data.value, ind_data.field);
+            SELECT INTO mbe_row * FROM metabib.browse_entry
+                WHERE value = value_prepped AND sort_value = ind_data.sort_value;
+
+            IF FOUND THEN
+                mbe_id := mbe_row.id;
+            ELSE
+                INSERT INTO metabib.browse_entry
+                    ( value, sort_value ) VALUES
+                    ( value_prepped, ind_data.sort_value );
+
+                mbe_id := CURRVAL('metabib.browse_entry_id_seq'::REGCLASS);
+            END IF;
+
+            INSERT INTO metabib.browse_entry_def_map (entry, def, source, authority)
+                VALUES (mbe_id, ind_data.field, ind_data.source, ind_data.authority);
+        END IF;
+
+        IF ind_data.search_field AND NOT b_skip_search THEN
+            EXECUTE $$
+                INSERT INTO metabib.$$ || ind_data.field_class || $$_field_entry (field, source, value)
+                    VALUES ($$ ||
+                        quote_literal(ind_data.field) || $$, $$ ||
+                        quote_literal(ind_data.source) || $$, $$ ||
+                        quote_literal(ind_data.value) ||
+                    $$);$$;
+        END IF;
+
+    END LOOP;
+
+    IF NOT b_skip_search THEN
+        PERFORM metabib.update_combined_index_vectors(bib_id);
+    END IF;
+
+    RETURN;
+END;
+$func$ LANGUAGE PLPGSQL;
+
+
+CREATE OR REPLACE FUNCTION biblio.extract_metabib_field_entry ( rid BIGINT, default_joiner TEXT ) RETURNS SETOF metabib.field_entry_template AS $func$
+DECLARE
+    bib     biblio.record_entry%ROWTYPE;
+    idx     config.metabib_field%ROWTYPE;
+    xfrm        config.xml_transform%ROWTYPE;
+    prev_xfrm   TEXT;
+    transformed_xml TEXT;
+    xml_node    TEXT;
+    xml_node_list   TEXT[];
+    facet_text  TEXT;
+    browse_text TEXT;
+    sort_value  TEXT;
+    raw_text    TEXT;
+    curr_text   TEXT;
+    joiner      TEXT := default_joiner; -- XXX will index defs supply a joiner?
+    authority_text TEXT;
+    authority_link BIGINT;
+    output_row  metabib.field_entry_template%ROWTYPE;
+BEGIN
+
+    -- Get the record
+    SELECT INTO bib * FROM biblio.record_entry WHERE id = rid;
+
+    -- Loop over the indexing entries
+    FOR idx IN SELECT * FROM config.metabib_field ORDER BY format LOOP
+
+        SELECT INTO xfrm * from config.xml_transform WHERE name = idx.format;
+
+        -- See if we can skip the XSLT ... it's expensive
+        IF prev_xfrm IS NULL OR prev_xfrm <> xfrm.name THEN
+            -- Can't skip the transform
+            IF xfrm.xslt <> '---' THEN
+                transformed_xml := oils_xslt_process(bib.marc,xfrm.xslt);
+            ELSE
+                transformed_xml := bib.marc;
+            END IF;
+
+            prev_xfrm := xfrm.name;
+        END IF;
+
+        xml_node_list := oils_xpath( idx.xpath, transformed_xml, ARRAY[ARRAY[xfrm.prefix, xfrm.namespace_uri]] );
+
+        raw_text := NULL;
+        FOR xml_node IN SELECT x FROM unnest(xml_node_list) AS x LOOP
+            CONTINUE WHEN xml_node !~ E'^\\s*<';
+
+            curr_text := ARRAY_TO_STRING(
+                oils_xpath( '//text()',
+                    REGEXP_REPLACE( -- This escapes all &s not followed by "amp;".  Data ise returned from oils_xpath (above) in UTF-8, not entity encoded
+                        REGEXP_REPLACE( -- This escapes embeded <s
+                            xml_node,
+                            $re$(>[^<]+)(<)([^>]+<)$re$,
+                            E'\\1&lt;\\3',
+                            'g'
+                        ),
+                        '&(?!amp;)',
+                        '&amp;',
+                        'g'
+                    )
+                ),
+                ' '
+            );
+
+            CONTINUE WHEN curr_text IS NULL OR curr_text = '';
+
+            IF raw_text IS NOT NULL THEN
+                raw_text := raw_text || joiner;
+            END IF;
+
+            raw_text := COALESCE(raw_text,'') || curr_text;
+
+            -- autosuggest/metabib.browse_entry
+            IF idx.browse_field THEN
+
+                IF idx.browse_xpath IS NOT NULL AND idx.browse_xpath <> '' THEN
+                    browse_text := oils_xpath_string( idx.browse_xpath, xml_node, joiner, ARRAY[ARRAY[xfrm.prefix, xfrm.namespace_uri]] );
+                ELSE
+                    browse_text := curr_text;
+                END IF;
+
+                IF idx.browse_sort_xpath IS NOT NULL AND
+                    idx.browse_sort_xpath <> '' THEN
+
+                    sort_value := oils_xpath_string(
+                        idx.browse_sort_xpath, xml_node, joiner,
+                        ARRAY[ARRAY[xfrm.prefix, xfrm.namespace_uri]]
+                    );
+                ELSE
+                    sort_value := browse_text;
+                END IF;
+
+                output_row.field_class = idx.field_class;
+                output_row.field = idx.id;
+                output_row.source = rid;
+                output_row.value = BTRIM(REGEXP_REPLACE(browse_text, E'\\s+', ' ', 'g'));
+                output_row.sort_value :=
+                    public.search_normalize(sort_value);
+
+                output_row.authority := NULL;
+
+                IF idx.authority_xpath IS NOT NULL AND idx.authority_xpath <> '' THEN
+                    authority_text := oils_xpath_string(
+                        idx.authority_xpath, xml_node, joiner,
+                        ARRAY[
+                            ARRAY[xfrm.prefix, xfrm.namespace_uri],
+                            ARRAY['xlink','http://www.w3.org/1999/xlink']
+                        ]
+                    );
+
+                    IF authority_text ~ '^\d+$' THEN
+                        authority_link := authority_text::BIGINT;
+                        PERFORM * FROM authority.record_entry WHERE id = authority_link;
+                        IF FOUND THEN
+                            output_row.authority := authority_link;
+                        END IF;
+                    END IF;
+
+                END IF;
+
+                output_row.browse_field = TRUE;
+                RETURN NEXT output_row;
+                output_row.browse_field = FALSE;
+                output_row.sort_value := NULL;
+            END IF;
+
+            -- insert raw node text for faceting
+            IF idx.facet_field THEN
+
+                IF idx.facet_xpath IS NOT NULL AND idx.facet_xpath <> '' THEN
+                    facet_text := oils_xpath_string( idx.facet_xpath, xml_node, joiner, ARRAY[ARRAY[xfrm.prefix, xfrm.namespace_uri]] );
+                ELSE
+                    facet_text := curr_text;
+                END IF;
+
+                output_row.field_class = idx.field_class;
+                output_row.field = -1 * idx.id;
+                output_row.source = rid;
+                output_row.value = BTRIM(REGEXP_REPLACE(facet_text, E'\\s+', ' ', 'g'));
+
+                output_row.facet_field = TRUE;
+                RETURN NEXT output_row;
+                output_row.facet_field = FALSE;
+            END IF;
+
+        END LOOP;
+
+        CONTINUE WHEN raw_text IS NULL OR raw_text = '';
+
+        -- insert combined node text for searching
+        IF idx.search_field THEN
+            output_row.field_class = idx.field_class;
+            output_row.field = idx.id;
+            output_row.source = rid;
+            output_row.value = BTRIM(REGEXP_REPLACE(raw_text, E'\\s+', ' ', 'g'));
+
+            output_row.search_field = TRUE;
+            RETURN NEXT output_row;
+            output_row.search_field = FALSE;
+        END IF;
+
+    END LOOP;
+
+END;
+
+$func$ LANGUAGE PLPGSQL;
+
+
+-- 953.data.MODS32-xsl.sql
+UPDATE config.xml_transform SET xslt=$$<?xml version="1.0" encoding="UTF-8"?>
+<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">
+       <xsl:output encoding="UTF-8" indent="yes" method="xml"/>
+<!--
+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
+
+Revision 1.13 - Changed order of output under cartographics to reflect schema  2006/11/28 tmee
+       
+Revision 1.12 - Updated to reflect MODS 3.2 Mapping  2006/10/11 tmee
+               
+Revision 1.11 - The attribute objectPart moved from <languageTerm> to <language>
+      2006/04/08  jrad
+
+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
+
+Revision 1.9 subfield $y was added to field 242 2004/09/02 10:57 jrad
+
+Revision 1.8 Subject chopPunctuation expanded and attribute fixes 2004/08/12 jrad
+
+Revision 1.7 2004/03/25 08:29 jrad
+
+Revision 1.6 various validation fixes 2004/02/20 ntra
+
+Revision 1.5  2003/10/02 16:18:58  ntra
+MODS2 to MODS3 updates, language unstacking and 
+de-duping, chopPunctuation expanded
+
+Revision 1.3  2003/04/03 00:07:19  ntra
+Revision 1.3 Additional Changes not related to MODS Version 2.0 by ntra
+
+Revision 1.2  2003/03/24 19:37:42  ckeith
+Added Log Comment
+
+-->
+       <xsl:template match="/">
+               <xsl:choose>
+                       <xsl:when test="//marc:collection">
+                               <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">
+                                       <xsl:for-each select="//marc:collection/marc:record">
+                                               <mods version="3.2">
+                                                       <xsl:call-template name="marcRecord"/>
+                                               </mods>
+                                       </xsl:for-each>
+                               </modsCollection>
+                       </xsl:when>
+                       <xsl:otherwise>
+                               <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">
+                                       <xsl:for-each select="//marc:record">
+                                               <xsl:call-template name="marcRecord"/>
+                                       </xsl:for-each>
+                               </mods>
+                       </xsl:otherwise>
+               </xsl:choose>
+       </xsl:template>
+       <xsl:template name="marcRecord">
+               <xsl:variable name="leader" select="marc:leader"/>
+               <xsl:variable name="leader6" select="substring($leader,7,1)"/>
+               <xsl:variable name="leader7" select="substring($leader,8,1)"/>
+               <xsl:variable name="controlField008" select="marc:controlfield[@tag='008']"/>
+               <xsl:variable name="typeOf008">
+                       <xsl:choose>
+                               <xsl:when test="$leader6='a'">
+                                       <xsl:choose>
+                                               <xsl:when test="$leader7='a' or $leader7='c' or $leader7='d' or $leader7='m'">BK</xsl:when>
+                                               <xsl:when test="$leader7='b' or $leader7='i' or $leader7='s'">SE</xsl:when>
+                                       </xsl:choose>
+                               </xsl:when>
+                               <xsl:when test="$leader6='t'">BK</xsl:when>
+                               <xsl:when test="$leader6='p'">MM</xsl:when>
+                               <xsl:when test="$leader6='m'">CF</xsl:when>
+                               <xsl:when test="$leader6='e' or $leader6='f'">MP</xsl:when>
+                               <xsl:when test="$leader6='g' or $leader6='k' or $leader6='o' or $leader6='r'">VM</xsl:when>
+                               <xsl:when test="$leader6='c' or $leader6='d' or $leader6='i' or $leader6='j'">MU</xsl:when>
+                       </xsl:choose>
+               </xsl:variable>
+               <xsl:for-each select="marc:datafield[@tag='245']">
+                       <titleInfo>
+                               <xsl:variable name="title">
+                                       <xsl:choose>
+                                               <xsl:when test="marc:subfield[@code='b']">
+                                                       <xsl:call-template name="specialSubfieldSelect">
+                                                               <xsl:with-param name="axis">b</xsl:with-param>
+                                                               <xsl:with-param name="beforeCodes">afgk</xsl:with-param>
+                                                       </xsl:call-template>
+                                               </xsl:when>
+                                               <xsl:otherwise>
+                                                       <xsl:call-template name="subfieldSelect">
+                                                               <xsl:with-param name="codes">abfgk</xsl:with-param>
+                                                       </xsl:call-template>
+                                               </xsl:otherwise>
+                                       </xsl:choose>
+                               </xsl:variable>
+                               <xsl:variable name="titleChop">
+                                       <xsl:call-template name="chopPunctuation">
+                                               <xsl:with-param name="chopString">
+                                                       <xsl:value-of select="$title"/>
+                                               </xsl:with-param>
+                                       </xsl:call-template>
+                               </xsl:variable>
+                               <xsl:choose>
+                                       <xsl:when test="@ind2>0">
+                                               <nonSort>
+                                                       <xsl:value-of select="substring($titleChop,1,@ind2)"/>
+                                               </nonSort>
+                                               <title>
+                                                       <xsl:value-of select="substring($titleChop,@ind2+1)"/>
+                                               </title>
+                                       </xsl:when>
+                                       <xsl:otherwise>
+                                               <title>
+                                                       <xsl:value-of select="$titleChop"/>
+                                               </title>
+                                       </xsl:otherwise>
+                               </xsl:choose>
+                               <xsl:if test="marc:subfield[@code='b']">
+                                       <subTitle>
+                                               <xsl:call-template name="chopPunctuation">
+                                                       <xsl:with-param name="chopString">
+                                                               <xsl:call-template name="specialSubfieldSelect">
+                                                                       <xsl:with-param name="axis">b</xsl:with-param>
+                                                                       <xsl:with-param name="anyCodes">b</xsl:with-param>
+                                                                       <xsl:with-param name="afterCodes">afgk</xsl:with-param>
+                                                               </xsl:call-template>
+                                                       </xsl:with-param>
+                                               </xsl:call-template>
+                                       </subTitle>
+                               </xsl:if>
+                               <xsl:call-template name="part"></xsl:call-template>
+                       </titleInfo>
+                       <!-- A form of title that ignores non-filing characters; useful
+                                for not converting "L'Oreal" into "L' Oreal" at index time -->
+                       <titleNonfiling>
+                               <xsl:variable name="title">
+                                       <xsl:choose>
+                                               <xsl:when test="marc:subfield[@code='b']">
+                                                       <xsl:call-template name="specialSubfieldSelect">
+                                                               <xsl:with-param name="axis">b</xsl:with-param>
+                                                               <xsl:with-param name="beforeCodes">afgk</xsl:with-param>
+                                                       </xsl:call-template>
+                                               </xsl:when>
+                                               <xsl:otherwise>
+                                                       <xsl:call-template name="subfieldSelect">
+                                                               <xsl:with-param name="codes">abfgk</xsl:with-param>
+                                                       </xsl:call-template>
+                                               </xsl:otherwise>
+                                       </xsl:choose>
+                               </xsl:variable>
+                               <title>
+                                       <xsl:value-of select="$title"/>
+                               </title>
+                               <xsl:if test="marc:subfield[@code='b']">
+                                       <subTitle>
+                                               <xsl:call-template name="chopPunctuation">
+                                                       <xsl:with-param name="chopString">
+                                                               <xsl:call-template name="specialSubfieldSelect">
+                                                                       <xsl:with-param name="axis">b</xsl:with-param>
+                                                                       <xsl:with-param name="anyCodes">b</xsl:with-param>
+                                                                       <xsl:with-param name="afterCodes">afgk</xsl:with-param>
+                                                               </xsl:call-template>
+                                                       </xsl:with-param>
+                                               </xsl:call-template>
+                                       </subTitle>
+                               </xsl:if>
+                               <xsl:call-template name="part"></xsl:call-template>
+                       </titleNonfiling>
+               </xsl:for-each>
+               <xsl:for-each select="marc:datafield[@tag='210']">
+                       <titleInfo type="abbreviated">
+                               <title>
+                                       <xsl:call-template name="chopPunctuation">
+                                               <xsl:with-param name="chopString">
+                                                       <xsl:call-template name="subfieldSelect">
+                                                               <xsl:with-param name="codes">a</xsl:with-param>
+                                                       </xsl:call-template>
+                                               </xsl:with-param>
+                                       </xsl:call-template>
+                               </title>
+                               <xsl:call-template name="subtitle"/>
+                       </titleInfo>
+               </xsl:for-each>
+               <xsl:for-each select="marc:datafield[@tag='242']">
+                       <xsl:variable name="titleChop">
+                               <xsl:call-template name="chopPunctuation">
+                                       <xsl:with-param name="chopString">
+                                               <xsl:call-template name="subfieldSelect">
+                                                       <!-- 1/04 removed $h, b -->
+                                                       <xsl:with-param name="codes">a</xsl:with-param>
+                                               </xsl:call-template>
+                                       </xsl:with-param>
+                               </xsl:call-template>
+                       </xsl:variable>
+                       <titleInfo type="translated">
+                               <!--09/01/04 Added subfield $y-->
+                               <xsl:for-each select="marc:subfield[@code='y']">
+                                       <xsl:attribute name="lang">
+                                               <xsl:value-of select="text()"/>
+                                       </xsl:attribute>
+                               </xsl:for-each>
+                               <title>
+                                       <xsl:value-of select="$titleChop" />
+                               </title>
+                               <!-- 1/04 fix -->
+                               <xsl:call-template name="subtitle"/>
+                               <xsl:call-template name="part"/>
+                       </titleInfo>
+                       <titleInfo type="translated-nfi">
+                               <xsl:for-each select="marc:subfield[@code='y']">
+                                       <xsl:attribute name="lang">
+                                               <xsl:value-of select="text()"/>
+                                       </xsl:attribute>
+                               </xsl:for-each>
+                               <xsl:choose>
+                                       <xsl:when test="@ind2>0">
+                                               <nonSort>
+                                                       <xsl:value-of select="substring($titleChop,1,@ind2)"/>
+                                               </nonSort>
+                                               <title>
+                                                       <xsl:value-of select="substring($titleChop,@ind2+1)"/>
+                                               </title>
+                                       </xsl:when>
+                                       <xsl:otherwise>
+                                               <title>
+                                                       <xsl:value-of select="$titleChop" />
+                                               </title>
+                                       </xsl:otherwise>
+                               </xsl:choose>
+                               <xsl:call-template name="subtitle"/>
+                               <xsl:call-template name="part"/>
+                       </titleInfo>
+               </xsl:for-each>
+               <xsl:for-each select="marc:datafield[@tag='246']">
+                       <titleInfo type="alternative">
+                               <xsl:for-each select="marc:subfield[@code='i']">
+                                       <xsl:attribute name="displayLabel">
+                                               <xsl:value-of select="text()"/>
+                                       </xsl:attribute>
+                               </xsl:for-each>
+                               <title>
+                                       <xsl:call-template name="chopPunctuation">
+                                               <xsl:with-param name="chopString">
+                                                       <xsl:call-template name="subfieldSelect">
+                                                               <!-- 1/04 removed $h, $b -->
+                                                               <xsl:with-param name="codes">af</xsl:with-param>
+                                                       </xsl:call-template>
+                                               </xsl:with-param>
+                                       </xsl:call-template>
+                               </title>
+                               <xsl:call-template name="subtitle"/>
+                               <xsl:call-template name="part"/>
+                       </titleInfo>
+               </xsl:for-each>
+               <xsl:for-each select="marc:datafield[@tag='130']|marc:datafield[@tag='240']|marc:datafield[@tag='730'][@ind2!='2']">
+                       <xsl:variable name="nfi">
+                               <xsl:choose>
+                                       <xsl:when test="@tag='240'">
+                                               <xsl:value-of select="@ind2"/>
+                                       </xsl:when>
+                                       <xsl:otherwise>
+                                               <xsl:value-of select="@ind1"/>
+                                       </xsl:otherwise>
+                               </xsl:choose>
+                       </xsl:variable>
+                       <xsl:variable name="titleChop">
+                               <xsl:call-template name="uri" />
+                               <xsl:variable name="str">
+                                       <xsl:for-each select="marc:subfield">
+                                               <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'])))">
+                                                       <xsl:value-of select="text()"/>
+                                                       <xsl:text> </xsl:text>
+                                               </xsl:if>
+                                       </xsl:for-each>
+                               </xsl:variable>
+                               <xsl:call-template name="chopPunctuation">
+                                       <xsl:with-param name="chopString">
+                                               <xsl:value-of select="substring($str,1,string-length($str)-1)"/>
+                                       </xsl:with-param>
+                               </xsl:call-template>
+                       </xsl:variable>
+                       <titleInfo type="uniform">
+                               <title>
+                                       <xsl:value-of select="$titleChop"/>
+                               </title>
+                               <xsl:call-template name="part"/>
+                       </titleInfo>
+                       <titleInfo type="uniform-nfi">
+                               <xsl:choose>
+                                       <xsl:when test="$nfi>0">
+                                               <nonSort>
+                                                       <xsl:value-of select="substring($titleChop,1,$nfi)"/>
+                                               </nonSort>
+                                               <title>
+                                                       <xsl:value-of select="substring($titleChop,$nfi+1)"/>
+                                               </title>
+                                       </xsl:when>
+                                       <xsl:otherwise>
+                                               <title>
+                                                       <xsl:value-of select="$titleChop"/>
+                                               </title>
+                                       </xsl:otherwise>
+                               </xsl:choose>
+                               <xsl:call-template name="part"/>
+                       </titleInfo>
+               </xsl:for-each>
+               <xsl:for-each select="marc:datafield[@tag='740'][@ind2!='2']">
+                       <xsl:variable name="titleChop">
+                               <xsl:call-template name="chopPunctuation">
+                                       <xsl:with-param name="chopString">
+                                               <xsl:call-template name="subfieldSelect">
+                                                       <xsl:with-param name="codes">ah</xsl:with-param>
+                                               </xsl:call-template>
+                                       </xsl:with-param>
+                               </xsl:call-template>
+                       </xsl:variable>
+                       <titleInfo type="alternative">
+                               <title>
+                                       <xsl:value-of select="$titleChop" />
+                               </title>
+                               <xsl:call-template name="part"/>
+                       </titleInfo>
+                       <titleInfo type="alternative-nfi">
+                               <xsl:choose>
+                                       <xsl:when test="@ind1>0">
+                                               <nonSort>
+                                                       <xsl:value-of select="substring($titleChop,1,@ind1)"/>
+                                               </nonSort>
+                                               <title>
+                                                       <xsl:value-of select="substring($titleChop,@ind1+1)"/>
+                                               </title>
+                                       </xsl:when>
+                                       <xsl:otherwise>
+                                               <title>
+                                                       <xsl:value-of select="$titleChop" />
+                                               </title>
+                                       </xsl:otherwise>
+                               </xsl:choose>
+                               <xsl:call-template name="part"/>
+                       </titleInfo>
+               </xsl:for-each>
+               <xsl:for-each select="marc:datafield[@tag='100']">
+                       <name type="personal">
+                               <xsl:call-template name="uri" />
+                               <xsl:call-template name="nameABCDQ"/>
+                               <xsl:call-template name="affiliation"/>
+                               <role>
+                                       <roleTerm authority="marcrelator" type="text">creator</roleTerm>
+                               </role>
+                               <xsl:call-template name="role"/>
+                       </name>
+               </xsl:for-each>
+               <xsl:for-each select="marc:datafield[@tag='110']">
+                       <name type="corporate">
+                               <xsl:call-template name="uri" />
+                               <xsl:call-template name="nameABCDN"/>
+                               <role>
+                                       <roleTerm authority="marcrelator" type="text">creator</roleTerm>
+                               </role>
+                               <xsl:call-template name="role"/>
+                       </name>
+               </xsl:for-each>
+               <xsl:for-each select="marc:datafield[@tag='111']">
+                       <name type="conference">
+                               <xsl:call-template name="uri" />
+                               <xsl:call-template name="nameACDEQ"/>
+                               <role>
+                                       <roleTerm authority="marcrelator" type="text">creator</roleTerm>
+                               </role>
+                               <xsl:call-template name="role"/>
+                       </name>
+               </xsl:for-each>
+               <xsl:for-each select="marc:datafield[@tag='700'][not(marc:subfield[@code='t'])]">
+                       <name type="personal">
+                               <xsl:call-template name="uri" />
+                               <xsl:call-template name="nameABCDQ"/>
+                               <xsl:call-template name="affiliation"/>
+                               <xsl:call-template name="role"/>
+                       </name>
+               </xsl:for-each>
+               <xsl:for-each select="marc:datafield[@tag='710'][not(marc:subfield[@code='t'])]">
+                       <name type="corporate">
+                               <xsl:call-template name="uri" />
+                               <xsl:call-template name="nameABCDN"/>
+                               <xsl:call-template name="role"/>
+                       </name>
+               </xsl:for-each>
+               <xsl:for-each select="marc:datafield[@tag='711'][not(marc:subfield[@code='t'])]">
+                       <name type="conference">
+                               <xsl:call-template name="uri" />
+                               <xsl:call-template name="nameACDEQ"/>
+                               <xsl:call-template name="role"/>
+                       </name>
+               </xsl:for-each>
+               <xsl:for-each select="marc:datafield[@tag='720'][not(marc:subfield[@code='t'])]">
+                       <name>
+                               <xsl:if test="@ind1=1">
+                                       <xsl:attribute name="type">
+                                               <xsl:text>personal</xsl:text>
+                                       </xsl:attribute>
+                               </xsl:if>
+                               <namePart>
+                                       <xsl:value-of select="marc:subfield[@code='a']"/>
+                               </namePart>
+                               <xsl:call-template name="role"/>
+                       </name>
+               </xsl:for-each>
+               <typeOfResource>
+                       <xsl:if test="$leader7='c'">
+                               <xsl:attribute name="collection">yes</xsl:attribute>
+                       </xsl:if>
+                       <xsl:if test="$leader6='d' or $leader6='f' or $leader6='p' or $leader6='t'">
+                               <xsl:attribute name="manuscript">yes</xsl:attribute>
+                       </xsl:if>
+                       <xsl:choose>
+                               <xsl:when test="$leader6='a' or $leader6='t'">text</xsl:when>
+                               <xsl:when test="$leader6='e' or $leader6='f'">cartographic</xsl:when>
+                               <xsl:when test="$leader6='c' or $leader6='d'">notated music</xsl:when>
+                               <xsl:when test="$leader6='i'">sound recording-nonmusical</xsl:when>
+                               <xsl:when test="$leader6='j'">sound recording-musical</xsl:when>
+                               <xsl:when test="$leader6='k'">still image</xsl:when>
+                               <xsl:when test="$leader6='g'">moving image</xsl:when>
+                               <xsl:when test="$leader6='r'">three dimensional object</xsl:when>
+                               <xsl:when test="$leader6='m'">software, multimedia</xsl:when>
+                               <xsl:when test="$leader6='p'">mixed material</xsl:when>
+                       </xsl:choose>
+               </typeOfResource>
+               <xsl:if test="substring($controlField008,26,1)='d'">
+                       <genre authority="marc">globe</genre>
+               </xsl:if>
+               <xsl:if test="marc:controlfield[@tag='007'][substring(text(),1,1)='a'][substring(text(),2,1)='r']">
+                       <genre authority="marc">remote sensing image</genre>
+               </xsl:if>
+               <xsl:if test="$typeOf008='MP'">
+                       <xsl:variable name="controlField008-25" select="substring($controlField008,26,1)"></xsl:variable>
+                       <xsl:choose>
+                               <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']">
+                                       <genre authority="marc">map</genre>
+                               </xsl:when>
+                               <xsl:when test="$controlField008-25='e' or marc:controlfield[@tag=007][substring(text(),1,1)='a'][substring(text(),2,1)='d']">
+                                       <genre authority="marc">atlas</genre>
+                               </xsl:when>
+                       </xsl:choose>
+               </xsl:if>
+               <xsl:if test="$typeOf008='SE'">
+                       <xsl:variable name="controlField008-21" select="substring($controlField008,22,1)"></xsl:variable>
+                       <xsl:choose>
+                               <xsl:when test="$controlField008-21='d'">
+                                       <genre authority="marc">database</genre>
+                               </xsl:when>
+                               <xsl:when test="$controlField008-21='l'">
+                                       <genre authority="marc">loose-leaf</genre>
+                               </xsl:when>
+                               <xsl:when test="$controlField008-21='m'">
+                                       <genre authority="marc">series</genre>
+                               </xsl:when>
+                               <xsl:when test="$controlField008-21='n'">
+                                       <genre authority="marc">newspaper</genre>
+                               </xsl:when>
+                               <xsl:when test="$controlField008-21='p'">
+                                       <genre authority="marc">periodical</genre>
+                               </xsl:when>
+                               <xsl:when test="$controlField008-21='w'">
+                                       <genre authority="marc">web site</genre>
+                               </xsl:when>
+                       </xsl:choose>
+               </xsl:if>
+               <xsl:if test="$typeOf008='BK' or $typeOf008='SE'">
+                       <xsl:variable name="controlField008-24" select="substring($controlField008,25,4)"></xsl:variable>
+                       <xsl:choose>
+                               <xsl:when test="contains($controlField008-24,'a')">
+                                       <genre authority="marc">abstract or summary</genre>
+                               </xsl:when>
+                               <xsl:when test="contains($controlField008-24,'b')">
+                                       <genre authority="marc">bibliography</genre>
+                               </xsl:when>
+                               <xsl:when test="contains($controlField008-24,'c')">
+                                       <genre authority="marc">catalog</genre>
+                               </xsl:when>
+                               <xsl:when test="contains($controlField008-24,'d')">
+                                       <genre authority="marc">dictionary</genre>
+                               </xsl:when>
+                               <xsl:when test="contains($controlField008-24,'e')">
+                                       <genre authority="marc">encyclopedia</genre>
+                               </xsl:when>
+                               <xsl:when test="contains($controlField008-24,'f')">
+                                       <genre authority="marc">handbook</genre>
+                               </xsl:when>
+                               <xsl:when test="contains($controlField008-24,'g')">
+                                       <genre authority="marc">legal article</genre>
+                               </xsl:when>
+                               <xsl:when test="contains($controlField008-24,'i')">
+                                       <genre authority="marc">index</genre>
+                               </xsl:when>
+                               <xsl:when test="contains($controlField008-24,'k')">
+                                       <genre authority="marc">discography</genre>
+                               </xsl:when>
+                               <xsl:when test="contains($controlField008-24,'l')">
+                                       <genre authority="marc">legislation</genre>
+                               </xsl:when>
+                               <xsl:when test="contains($controlField008-24,'m')">
+                                       <genre authority="marc">theses</genre>
+                               </xsl:when>
+                               <xsl:when test="contains($controlField008-24,'n')">
+                                       <genre authority="marc">survey of literature</genre>
+                               </xsl:when>
+                               <xsl:when test="contains($controlField008-24,'o')">
+                                       <genre authority="marc">review</genre>
+                               </xsl:when>
+                               <xsl:when test="contains($controlField008-24,'p')">
+                                       <genre authority="marc">programmed text</genre>
+                               </xsl:when>
+                               <xsl:when test="contains($controlField008-24,'q')">
+                                       <genre authority="marc">filmography</genre>
+                               </xsl:when>
+                               <xsl:when test="contains($controlField008-24,'r')">
+                                       <genre authority="marc">directory</genre>
+                               </xsl:when>
+                               <xsl:when test="contains($controlField008-24,'s')">
+                                       <genre authority="marc">statistics</genre>
+                               </xsl:when>
+                               <xsl:when test="contains($controlField008-24,'t')">
+                                       <genre authority="marc">technical report</genre>
+                               </xsl:when>
+                               <xsl:when test="contains($controlField008-24,'v')">
+                                       <genre authority="marc">legal case and case notes</genre>
+                               </xsl:when>
+                               <xsl:when test="contains($controlField008-24,'w')">
+                                       <genre authority="marc">law report or digest</genre>
+                               </xsl:when>
+                               <xsl:when test="contains($controlField008-24,'z')">
+                                       <genre authority="marc">treaty</genre>
+                               </xsl:when>
+                       </xsl:choose>
+                       <xsl:variable name="controlField008-29" select="substring($controlField008,30,1)"></xsl:variable>
+                       <xsl:choose>
+                               <xsl:when test="$controlField008-29='1'">
+                                       <genre authority="marc">conference publication</genre>
+                               </xsl:when>
+                       </xsl:choose>
+               </xsl:if>
+               <xsl:if test="$typeOf008='CF'">
+                       <xsl:variable name="controlField008-26" select="substring($controlField008,27,1)"></xsl:variable>
+                       <xsl:choose>
+                               <xsl:when test="$controlField008-26='a'">
+                                       <genre authority="marc">numeric data</genre>
+                               </xsl:when>
+                               <xsl:when test="$controlField008-26='e'">
+                                       <genre authority="marc">database</genre>
+                               </xsl:when>
+                               <xsl:when test="$controlField008-26='f'">
+                                       <genre authority="marc">font</genre>
+                               </xsl:when>
+                               <xsl:when test="$controlField008-26='g'">
+                                       <genre authority="marc">game</genre>
+                               </xsl:when>
+                       </xsl:choose>
+               </xsl:if>
+               <xsl:if test="$typeOf008='BK'">
+                       <xsl:if test="substring($controlField008,25,1)='j'">
+                               <genre authority="marc">patent</genre>
+                       </xsl:if>
+                       <xsl:if test="substring($controlField008,31,1)='1'">
+                               <genre authority="marc">festschrift</genre>
+                       </xsl:if>
+                       <xsl:variable name="controlField008-34" select="substring($controlField008,35,1)"></xsl:variable>
+                       <xsl:if test="$controlField008-34='a' or $controlField008-34='b' or $controlField008-34='c' or $controlField008-34='d'">
+                               <genre authority="marc">biography</genre>
+                       </xsl:if>
+                       <xsl:variable name="controlField008-33" select="substring($controlField008,34,1)"></xsl:variable>
+                       <xsl:choose>
+                               <xsl:when test="$controlField008-33='e'">
+                                       <genre authority="marc">essay</genre>
+                               </xsl:when>
+                               <xsl:when test="$controlField008-33='d'">
+                                       <genre authority="marc">drama</genre>
+                               </xsl:when>
+                               <xsl:when test="$controlField008-33='c'">
+                                       <genre authority="marc">comic strip</genre>
+                               </xsl:when>
+                               <xsl:when test="$controlField008-33='l'">
+                                       <genre authority="marc">fiction</genre>
+                               </xsl:when>
+                               <xsl:when test="$controlField008-33='h'">
+                                       <genre authority="marc">humor, satire</genre>
+                               </xsl:when>
+                               <xsl:when test="$controlField008-33='i'">
+                                       <genre authority="marc">letter</genre>
+                               </xsl:when>
+                               <xsl:when test="$controlField008-33='f'">
+                                       <genre authority="marc">novel</genre>
+                               </xsl:when>
+                               <xsl:when test="$controlField008-33='j'">
+                                       <genre authority="marc">short story</genre>
+                               </xsl:when>
+                               <xsl:when test="$controlField008-33='s'">
+                                       <genre authority="marc">speech</genre>
+                               </xsl:when>
+                       </xsl:choose>
+               </xsl:if>
+               <xsl:if test="$typeOf008='MU'">
+                       <xsl:variable name="controlField008-30-31" select="substring($controlField008,31,2)"></xsl:variable>
+                       <xsl:if test="contains($controlField008-30-31,'b')">
+                               <genre authority="marc">biography</genre>
+                       </xsl:if>
+                       <xsl:if test="contains($controlField008-30-31,'c')">
+                               <genre authority="marc">conference publication</genre>
+                       </xsl:if>
+                       <xsl:if test="contains($controlField008-30-31,'d')">
+                               <genre authority="marc">drama</genre>
+                       </xsl:if>
+                       <xsl:if test="contains($controlField008-30-31,'e')">
+                               <genre authority="marc">essay</genre>
+                       </xsl:if>
+                       <xsl:if test="contains($controlField008-30-31,'f')">
+                               <genre authority="marc">fiction</genre>
+                       </xsl:if>
+                       <xsl:if test="contains($controlField008-30-31,'o')">
+                               <genre authority="marc">folktale</genre>
+                       </xsl:if>
+                       <xsl:if test="contains($controlField008-30-31,'h')">
+                               <genre authority="marc">history</genre>
+                       </xsl:if>
+                       <xsl:if test="contains($controlField008-30-31,'k')">
+                               <genre authority="marc">humor, satire</genre>
+                       </xsl:if>
+                       <xsl:if test="contains($controlField008-30-31,'m')">
+                               <genre authority="marc">memoir</genre>
+                       </xsl:if>
+                       <xsl:if test="contains($controlField008-30-31,'p')">
+                               <genre authority="marc">poetry</genre>
+                       </xsl:if>
+                       <xsl:if test="contains($controlField008-30-31,'r')">
+                               <genre authority="marc">rehearsal</genre>
+                       </xsl:if>
+                       <xsl:if test="contains($controlField008-30-31,'g')">
+                               <genre authority="marc">reporting</genre>
+                       </xsl:if>
+                       <xsl:if test="contains($controlField008-30-31,'s')">
+                               <genre authority="marc">sound</genre>
+                       </xsl:if>
+                       <xsl:if test="contains($controlField008-30-31,'l')">
+                               <genre authority="marc">speech</genre>
+                       </xsl:if>
+               </xsl:if>
+               <xsl:if test="$typeOf008='VM'">
+                       <xsl:variable name="controlField008-33" select="substring($controlField008,34,1)"></xsl:variable>
+                       <xsl:choose>
+                               <xsl:when test="$controlField008-33='a'">
+                                       <genre authority="marc">art original</genre>
+                               </xsl:when>
+                               <xsl:when test="$controlField008-33='b'">
+                                       <genre authority="marc">kit</genre>
+                               </xsl:when>
+                               <xsl:when test="$controlField008-33='c'">
+                                       <genre authority="marc">art reproduction</genre>
+                               </xsl:when>
+                               <xsl:when test="$controlField008-33='d'">
+                                       <genre authority="marc">diorama</genre>
+                               </xsl:when>
+                               <xsl:when test="$controlField008-33='f'">
+                                       <genre authority="marc">filmstrip</genre>
+                               </xsl:when>
+                               <xsl:when test="$controlField008-33='g'">
+                                       <genre authority="marc">legal article</genre>
+                               </xsl:when>
+                               <xsl:when test="$controlField008-33='i'">
+                                       <genre authority="marc">picture</genre>
+                               </xsl:when>
+                               <xsl:when test="$controlField008-33='k'">
+                                       <genre authority="marc">graphic</genre>
+                               </xsl:when>
+                               <xsl:when test="$controlField008-33='l'">
+                                       <genre authority="marc">technical drawing</genre>
+                               </xsl:when>
+                               <xsl:when test="$controlField008-33='m'">
+                                       <genre authority="marc">motion picture</genre>
+                               </xsl:when>
+                               <xsl:when test="$controlField008-33='n'">
+                                       <genre authority="marc">chart</genre>
+                               </xsl:when>
+                               <xsl:when test="$controlField008-33='o'">
+                                       <genre authority="marc">flash card</genre>
+                               </xsl:when>
+                               <xsl:when test="$controlField008-33='p'">
+                                       <genre authority="marc">microscope slide</genre>
+                               </xsl:when>
+                               <xsl:when test="$controlField008-33='q' or marc:controlfield[@tag=007][substring(text(),1,1)='a'][substring(text(),2,1)='q']">
+                                       <genre authority="marc">model</genre>
+                               </xsl:when>
+                               <xsl:when test="$controlField008-33='r'">
+                                       <genre authority="marc">realia</genre>
+                               </xsl:when>
+                               <xsl:when test="$controlField008-33='s'">
+                                       <genre authority="marc">slide</genre>
+                               </xsl:when>
+                               <xsl:when test="$controlField008-33='t'">
+                                       <genre authority="marc">transparency</genre>
+                               </xsl:when>
+                               <xsl:when test="$controlField008-33='v'">
+                                       <genre authority="marc">videorecording</genre>
+                               </xsl:when>
+                               <xsl:when test="$controlField008-33='w'">
+                                       <genre authority="marc">toy</genre>
+                               </xsl:when>
+                       </xsl:choose>
+               </xsl:if>
+               <xsl:for-each select="marc:datafield[@tag=655]">
+                       <genre authority="marc">
+                               <xsl:attribute name="authority">
+                                       <xsl:value-of select="marc:subfield[@code='2']"/>
+                               </xsl:attribute>
+                               <xsl:call-template name="subfieldSelect">
+                                       <xsl:with-param name="codes">abvxyz</xsl:with-param>
+                                       <xsl:with-param name="delimeter">-</xsl:with-param>
+                               </xsl:call-template>
+                       </genre>
+               </xsl:for-each>
+               <originInfo>
+                       <xsl:variable name="MARCpublicationCode" select="normalize-space(substring($controlField008,16,3))"></xsl:variable>
+                       <xsl:if test="translate($MARCpublicationCode,'|','')">
+                               <place>
+                                       <placeTerm>
+                                               <xsl:attribute name="type">code</xsl:attribute>
+                                               <xsl:attribute name="authority">marccountry</xsl:attribute>
+                                               <xsl:value-of select="$MARCpublicationCode"/>
+                                       </placeTerm>
+                               </place>
+                       </xsl:if>
+                       <xsl:for-each select="marc:datafield[@tag=044]/marc:subfield[@code='c']">
+                               <place>
+                                       <placeTerm>
+                                               <xsl:attribute name="type">code</xsl:attribute>
+                                               <xsl:attribute name="authority">iso3166</xsl:attribute>
+                                               <xsl:value-of select="."/>
+                                       </placeTerm>
+                               </place>
+                       </xsl:for-each>
+                       <xsl:for-each select="marc:datafield[@tag=260]/marc:subfield[@code='a']">
+                               <place>
+                                       <placeTerm>
+                                               <xsl:attribute name="type">text</xsl:attribute>
+                                               <xsl:call-template name="chopPunctuationFront">
+                                                       <xsl:with-param name="chopString">
+                                                               <xsl:call-template name="chopPunctuation">
+                                                                       <xsl:with-param name="chopString" select="."/>
+                                                               </xsl:call-template>
+                                                       </xsl:with-param>
+                                               </xsl:call-template>
+                                       </placeTerm>
+                               </place>
+                       </xsl:for-each>
+                       <xsl:for-each select="marc:datafield[@tag=046]/marc:subfield[@code='m']">
+                               <dateValid point="start">
+                                       <xsl:value-of select="."/>
+                               </dateValid>
+                       </xsl:for-each>
+                       <xsl:for-each select="marc:datafield[@tag=046]/marc:subfield[@code='n']">
+                               <dateValid point="end">
+                                       <xsl:value-of select="."/>
+                               </dateValid>
+                       </xsl:for-each>
+                       <xsl:for-each select="marc:datafield[@tag=046]/marc:subfield[@code='j']">
+                               <dateModified>
+                                       <xsl:value-of select="."/>
+                               </dateModified>
+                       </xsl:for-each>
+                       <xsl:for-each select="marc:datafield[@tag=260]/marc:subfield[@code='b' or @code='c' or @code='g']">
+                               <xsl:choose>
+                                       <xsl:when test="@code='b'">
+                                               <publisher>
+                                                       <xsl:call-template name="chopPunctuation">
+                                                               <xsl:with-param name="chopString" select="."/>
+                                                               <xsl:with-param name="punctuation">
+                                                                       <xsl:text>:,;/ </xsl:text>
+                                                               </xsl:with-param>
+                                                       </xsl:call-template>
+                                               </publisher>
+                                       </xsl:when>
+                                       <xsl:when test="@code='c'">
+                                               <dateIssued>
+                                                       <xsl:call-template name="chopPunctuation">
+                                                               <xsl:with-param name="chopString" select="."/>
+                                                       </xsl:call-template>
+                                               </dateIssued>
+                                       </xsl:when>
+                                       <xsl:when test="@code='g'">
+                                               <dateCreated>
+                                                       <xsl:value-of select="."/>
+                                               </dateCreated>
+                                       </xsl:when>
+                               </xsl:choose>
+                       </xsl:for-each>
+                       <xsl:variable name="dataField260c">
+                               <xsl:call-template name="chopPunctuation">
+                                       <xsl:with-param name="chopString" select="marc:datafield[@tag=260]/marc:subfield[@code='c']"></xsl:with-param>
+                               </xsl:call-template>
+                       </xsl:variable>
+                       <xsl:variable name="controlField008-7-10" select="normalize-space(substring($controlField008, 8, 4))"></xsl:variable>
+                       <xsl:variable name="controlField008-11-14" select="normalize-space(substring($controlField008, 12, 4))"></xsl:variable>
+                       <xsl:variable name="controlField008-6" select="normalize-space(substring($controlField008, 7, 1))"></xsl:variable>
+                       <xsl:if test="$controlField008-6='e' or $controlField008-6='p' or $controlField008-6='r' or $controlField008-6='t' or $controlField008-6='s'">
+                               <xsl:if test="$controlField008-7-10 and ($controlField008-7-10 != $dataField260c)">
+                                       <dateIssued encoding="marc">
+                                               <xsl:value-of select="$controlField008-7-10"/>
+                                       </dateIssued>
+                               </xsl:if>
+                       </xsl:if>
+                       <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'">
+                               <xsl:if test="$controlField008-7-10">
+                                       <dateIssued encoding="marc" point="start">
+                                               <xsl:value-of select="$controlField008-7-10"/>
+                                       </dateIssued>
+                               </xsl:if>
+                       </xsl:if>
+                       <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'">
+                               <xsl:if test="$controlField008-11-14">
+                                       <dateIssued encoding="marc" point="end">
+                                               <xsl:value-of select="$controlField008-11-14"/>
+                                       </dateIssued>
+                               </xsl:if>
+                       </xsl:if>
+                       <xsl:if test="$controlField008-6='q'">
+                               <xsl:if test="$controlField008-7-10">
+                                       <dateIssued encoding="marc" point="start" qualifier="questionable">
+                                               <xsl:value-of select="$controlField008-7-10"/>
+                                       </dateIssued>
+                               </xsl:if>
+                       </xsl:if>
+                       <xsl:if test="$controlField008-6='q'">
+                               <xsl:if test="$controlField008-11-14">
+                                       <dateIssued encoding="marc" point="end" qualifier="questionable">
+                                               <xsl:value-of select="$controlField008-11-14"/>
+                                       </dateIssued>
+                               </xsl:if>
+                       </xsl:if>
+                       <xsl:if test="$controlField008-6='t'">
+                               <xsl:if test="$controlField008-11-14">
+                                       <copyrightDate encoding="marc">
+                                               <xsl:value-of select="$controlField008-11-14"/>
+                                       </copyrightDate>
+                               </xsl:if>
+                       </xsl:if>
+                       <xsl:for-each select="marc:datafield[@tag=033][@ind1=0 or @ind1=1]/marc:subfield[@code='a']">
+                               <dateCaptured encoding="iso8601">
+                                       <xsl:value-of select="."/>
+                               </dateCaptured>
+                       </xsl:for-each>
+                       <xsl:for-each select="marc:datafield[@tag=033][@ind1=2]/marc:subfield[@code='a'][1]">
+                               <dateCaptured encoding="iso8601" point="start">
+                                       <xsl:value-of select="."/>
+                               </dateCaptured>
+                       </xsl:for-each>
+                       <xsl:for-each select="marc:datafield[@tag=033][@ind1=2]/marc:subfield[@code='a'][2]">
+                               <dateCaptured encoding="iso8601" point="end">
+                                       <xsl:value-of select="."/>
+                               </dateCaptured>
+                       </xsl:for-each>
+                       <xsl:for-each select="marc:datafield[@tag=250]/marc:subfield[@code='a']">
+                               <edition>
+                                       <xsl:value-of select="."/>
+                               </edition>
+                       </xsl:for-each>
+                       <xsl:for-each select="marc:leader">
+                               <issuance>
+                                       <xsl:choose>
+                                               <xsl:when test="$leader7='a' or $leader7='c' or $leader7='d' or $leader7='m'">monographic</xsl:when>
+                                               <xsl:when test="$leader7='b' or $leader7='i' or $leader7='s'">continuing</xsl:when>
+                                       </xsl:choose>
+                               </issuance>
+                       </xsl:for-each>
+                       <xsl:for-each select="marc:datafield[@tag=310]|marc:datafield[@tag=321]">
+                               <frequency>
+                                       <xsl:call-template name="subfieldSelect">
+                                               <xsl:with-param name="codes">ab</xsl:with-param>
+                                       </xsl:call-template>
+                               </frequency>
+                       </xsl:for-each>
+               </originInfo>
+               <xsl:variable name="controlField008-35-37" select="normalize-space(translate(substring($controlField008,36,3),'|#',''))"></xsl:variable>
+               <xsl:if test="$controlField008-35-37">
+                       <language>
+                               <languageTerm authority="iso639-2b" type="code">
+                                       <xsl:value-of select="substring($controlField008,36,3)"/>
+                               </languageTerm>
+                       </language>
+               </xsl:if>
+               <xsl:for-each select="marc:datafield[@tag=041]">
+                       <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']">
+                               <xsl:variable name="langCodes" select="."/>
+                               <xsl:choose>
+                                       <xsl:when test="../marc:subfield[@code='2']='rfc3066'">
+                                               <!-- not stacked but could be repeated -->
+                                               <xsl:call-template name="rfcLanguages">
+                                                       <xsl:with-param name="nodeNum">
+                                                               <xsl:value-of select="1"/>
+                                                       </xsl:with-param>
+                                                       <xsl:with-param name="usedLanguages">
+                                                               <xsl:text></xsl:text>
+                                                       </xsl:with-param>
+                                                       <xsl:with-param name="controlField008-35-37">
+                                                               <xsl:value-of select="$controlField008-35-37"></xsl:value-of>
+                                                       </xsl:with-param>
+                                               </xsl:call-template>
+                                       </xsl:when>
+                                       <xsl:otherwise>
+                                               <!-- iso -->
+                                               <xsl:variable name="allLanguages">
+                                                       <xsl:copy-of select="$langCodes"></xsl:copy-of>
+                                               </xsl:variable>
+                                               <xsl:variable name="currentLanguage">
+                                                       <xsl:value-of select="substring($allLanguages,1,3)"></xsl:value-of>
+                                               </xsl:variable>
+                                               <xsl:call-template name="isoLanguage">
+                                                       <xsl:with-param name="currentLanguage">
+                                                               <xsl:value-of select="substring($allLanguages,1,3)"></xsl:value-of>
+                                                       </xsl:with-param>
+                                                       <xsl:with-param name="remainingLanguages">
+                                                               <xsl:value-of select="substring($allLanguages,4,string-length($allLanguages)-3)"></xsl:value-of>
+                                                       </xsl:with-param>
+                                                       <xsl:with-param name="usedLanguages">
+                                                               <xsl:if test="$controlField008-35-37">
+                                                                       <xsl:value-of select="$controlField008-35-37"></xsl:value-of>
+                                                               </xsl:if>
+                                                       </xsl:with-param>
+                                               </xsl:call-template>
+                                       </xsl:otherwise>
+                               </xsl:choose>
+                       </xsl:for-each>
+               </xsl:for-each>
+               <xsl:variable name="physicalDescription">
+                       <!--3.2 change tmee 007/11 -->
+                       <xsl:if test="$typeOf008='CF' and marc:controlfield[@tag=007][substring(.,12,1)='a']">
+                               <digitalOrigin>reformatted digital</digitalOrigin>
+                       </xsl:if>
+                       <xsl:if test="$typeOf008='CF' and marc:controlfield[@tag=007][substring(.,12,1)='b']">
+                               <digitalOrigin>digitized microfilm</digitalOrigin>
+                       </xsl:if>
+                       <xsl:if test="$typeOf008='CF' and marc:controlfield[@tag=007][substring(.,12,1)='d']">
+                               <digitalOrigin>digitized other analog</digitalOrigin>
+                       </xsl:if>
+                       <xsl:variable name="controlField008-23" select="substring($controlField008,24,1)"></xsl:variable>
+                       <xsl:variable name="controlField008-29" select="substring($controlField008,30,1)"></xsl:variable>
+                       <xsl:variable name="check008-23">
+                               <xsl:if test="$typeOf008='BK' or $typeOf008='MU' or $typeOf008='SE' or $typeOf008='MM'">
+                                       <xsl:value-of select="true()"></xsl:value-of>
+                               </xsl:if>
+                       </xsl:variable>
+                       <xsl:variable name="check008-29">
+                               <xsl:if test="$typeOf008='MP' or $typeOf008='VM'">
+                                       <xsl:value-of select="true()"></xsl:value-of>
+                               </xsl:if>
+                       </xsl:variable>
+                       <xsl:choose>
+                               <xsl:when test="($check008-23 and $controlField008-23='f') or ($check008-29 and $controlField008-29='f')">
+                                       <form authority="marcform">braille</form>
+                               </xsl:when>
+                               <xsl:when test="($controlField008-23=' ' and ($leader6='c' or $leader6='d')) or (($typeOf008='BK' or $typeOf008='SE') and ($controlField008-23=' ' or $controlField008='r'))">
+                                       <form authority="marcform">print</form>
+                               </xsl:when>
+                               <xsl:when test="$leader6 = 'm' or ($check008-23 and $controlField008-23='s') or ($check008-29 and $controlField008-29='s')">
+                                       <form authority="marcform">electronic</form>
+                               </xsl:when>
+                               <xsl:when test="($check008-23 and $controlField008-23='b') or ($check008-29 and $controlField008-29='b')">
+                                       <form authority="marcform">microfiche</form>
+                               </xsl:when>
+                               <xsl:when test="($check008-23 and $controlField008-23='a') or ($check008-29 and $controlField008-29='a')">
+                                       <form authority="marcform">microfilm</form>
+                               </xsl:when>
+                       </xsl:choose>
+                       <!-- 1/04 fix -->
+                       <xsl:if test="marc:datafield[@tag=130]/marc:subfield[@code='h']">
+                               <form authority="gmd">
+                                       <xsl:call-template name="chopBrackets">
+                                               <xsl:with-param name="chopString">
+                                                       <xsl:value-of select="marc:datafield[@tag=130]/marc:subfield[@code='h']"></xsl:value-of>
+                                               </xsl:with-param>
+                                       </xsl:call-template>
+                               </form>
+                       </xsl:if>
+                       <xsl:if test="marc:datafield[@tag=240]/marc:subfield[@code='h']">
+                               <form authority="gmd">
+                                       <xsl:call-template name="chopBrackets">
+                                               <xsl:with-param name="chopString">
+                                                       <xsl:value-of select="marc:datafield[@tag=240]/marc:subfield[@code='h']"></xsl:value-of>
+                                               </xsl:with-param>
+                                       </xsl:call-template>
+                               </form>
+                       </xsl:if>
+                       <xsl:if test="marc:datafield[@tag=242]/marc:subfield[@code='h']">
+                               <form authority="gmd">
+                                       <xsl:call-template name="chopBrackets">
+                                               <xsl:with-param name="chopString">
+                                                       <xsl:value-of select="marc:datafield[@tag=242]/marc:subfield[@code='h']"></xsl:value-of>
+                                               </xsl:with-param>
+                                       </xsl:call-template>
+                               </form>
+                       </xsl:if>
+                       <xsl:if test="marc:datafield[@tag=245]/marc:subfield[@code='h']">
+                               <form authority="gmd">
+                                       <xsl:call-template name="chopBrackets">
+                                               <xsl:with-param name="chopString">
+                                                       <xsl:value-of select="marc:datafield[@tag=245]/marc:subfield[@code='h']"></xsl:value-of>
+                                               </xsl:with-param>
+                                       </xsl:call-template>
+                               </form>
+                       </xsl:if>
+                       <xsl:if test="marc:datafield[@tag=246]/marc:subfield[@code='h']">
+                               <form authority="gmd">
+                                       <xsl:call-template name="chopBrackets">
+                                               <xsl:with-param name="chopString">
+                                                       <xsl:value-of select="marc:datafield[@tag=246]/marc:subfield[@code='h']"></xsl:value-of>
+                                               </xsl:with-param>
+                                       </xsl:call-template>
+                               </form>
+                       </xsl:if>
+                       <xsl:if test="marc:datafield[@tag=730]/marc:subfield[@code='h']">
+                               <form authority="gmd">
+                                       <xsl:call-template name="chopBrackets">
+                                               <xsl:with-param name="chopString">
+                                                       <xsl:value-of select="marc:datafield[@tag=730]/marc:subfield[@code='h']"></xsl:value-of>
+                                               </xsl:with-param>
+                                       </xsl:call-template>
+                               </form>
+                       </xsl:if>
+                       <xsl:for-each select="marc:datafield[@tag=256]/marc:subfield[@code='a']">
+                               <form>
+                                       <xsl:value-of select="."></xsl:value-of>
+                               </form>
+                       </xsl:for-each>
+                       <xsl:for-each select="marc:controlfield[@tag=007][substring(text(),1,1)='c']">
+                               <xsl:choose>
+                                       <xsl:when test="substring(text(),14,1)='a'">
+                                               <reformattingQuality>access</reformattingQuality>
+                                       </xsl:when>
+                                       <xsl:when test="substring(text(),14,1)='p'">
+                                               <reformattingQuality>preservation</reformattingQuality>
+                                       </xsl:when>
+                                       <xsl:when test="substring(text(),14,1)='r'">
+                                               <reformattingQuality>replacement</reformattingQuality>
+                                       </xsl:when>
+                               </xsl:choose>
+                       </xsl:for-each>
+                       <!--3.2 change tmee 007/01 -->
+                       <xsl:if test="marc:controlfield[@tag=007][substring(text(),1,1)='c'][substring(text(),2,1)='b']">
+                               <form authority="smd">chip cartridge</form>
+                       </xsl:if>
+                       <xsl:if test="marc:controlfield[@tag=007][substring(text(),1,1)='c'][substring(text(),2,1)='c']">
+                               <form authority="smd">computer optical disc cartridge</form>
+                       </xsl:if>
+                       <xsl:if test="marc:controlfield[@tag=007][substring(text(),1,1)='c'][substring(text(),2,1)='j']">
+                               <form authority="smd">magnetic disc</form>
+                       </xsl:if>
+                       <xsl:if test="marc:controlfield[@tag=007][substring(text(),1,1)='c'][substring(text(),2,1)='m']">
+                               <form authority="smd">magneto-optical disc</form>
+                       </xsl:if>
+                       <xsl:if test="marc:controlfield[@tag=007][substring(text(),1,1)='c'][substring(text(),2,1)='o']">
+                               <form authority="smd">optical disc</form>
+                       </xsl:if>
+                       <xsl:if test="marc:controlfield[@tag=007][substring(text(),1,1)='c'][substring(text(),2,1)='r']">
+                               <form authority="smd">remote</form>
+                       </xsl:if>
+                       <xsl:if test="marc:controlfield[@tag=007][substring(text(),1,1)='c'][substring(text(),2,1)='a']">
+                               <form authority="smd">tape cartridge</form>
+                       </xsl:if>
+                       <xsl:if test="marc:controlfield[@tag=007][substring(text(),1,1)='c'][substring(text(),2,1)='f']">
+                               <form authority="smd">tape cassette</form>
+                       </xsl:if>
+                       <xsl:if test="marc:controlfield[@tag=007][substring(text(),1,1)='c'][substring(text(),2,1)='h']">
+                               <form authority="smd">tape reel</form>
+                       </xsl:if>
+                       
+                       <xsl:if test="marc:controlfield[@tag=007][substring(text(),1,1)='d'][substring(text(),2,1)='a']">
+                               <form authority="smd">celestial globe</form>
+                       </xsl:if>
+                       <xsl:if test="marc:controlfield[@tag=007][substring(text(),1,1)='d'][substring(text(),2,1)='e']">
+                               <form authority="smd">earth moon globe</form>
+                       </xsl:if>
+                       <xsl:if test="marc:controlfield[@tag=007][substring(text(),1,1)='d'][substring(text(),2,1)='b']">
+                               <form authority="smd">planetary or lunar globe</form>
+                       </xsl:if>
+                       <xsl:if test="marc:controlfield[@tag=007][substring(text(),1,1)='d'][substring(text(),2,1)='c']">
+                               <form authority="smd">terrestrial globe</form>
+                       </xsl:if>
+                       
+                       <xsl:if test="marc:controlfield[@tag=007][substring(text(),1,1)='o'][substring(text(),2,1)='o']">
+                               <form authority="smd">kit</form>
+                       </xsl:if>
+                       
+                       <xsl:if test="marc:controlfield[@tag=007][substring(text(),1,1)='a'][substring(text(),2,1)='d']">
+                               <form authority="smd">atlas</form>
+                       </xsl:if>
+                       <xsl:if test="marc:controlfield[@tag=007][substring(text(),1,1)='a'][substring(text(),2,1)='g']">
+                               <form authority="smd">diagram</form>
+                       </xsl:if>
+                       <xsl:if test="marc:controlfield[@tag=007][substring(text(),1,1)='a'][substring(text(),2,1)='j']">
+                               <form authority="smd">map</form>
+                       </xsl:if>
+                       <xsl:if test="marc:controlfield[@tag=007][substring(text(),1,1)='a'][substring(text(),2,1)='q']">
+                               <form authority="smd">model</form>
+                       </xsl:if>
+                       <xsl:if test="marc:controlfield[@tag=007][substring(text(),1,1)='a'][substring(text(),2,1)='k']">
+                               <form authority="smd">profile</form>
+                       </xsl:if>
+                       <xsl:if test="marc:controlfield[@tag=007][substring(text(),1,1)='a'][substring(text(),2,1)='r']">
+                               <form authority="smd">remote-sensing image</form>
+                       </xsl:if>
+                       <xsl:if test="marc:controlfield[@tag=007][substring(text(),1,1)='a'][substring(text(),2,1)='s']">
+                               <form authority="smd">section</form>
+                       </xsl:if>
+                       <xsl:if test="marc:controlfield[@tag=007][substring(text(),1,1)='a'][substring(text(),2,1)='y']">
+                               <form authority="smd">view</form>
+                       </xsl:if>
+                       
+                       <xsl:if test="marc:controlfield[@tag=007][substring(text(),1,1)='h'][substring(text(),2,1)='a']">
+                               <form authority="smd">aperture card</form>
+                       </xsl:if>
+                       <xsl:if test="marc:controlfield[@tag=007][substring(text(),1,1)='h'][substring(text(),2,1)='e']">
+                               <form authority="smd">microfiche</form>
+                       </xsl:if>
+                       <xsl:if test="marc:controlfield[@tag=007][substring(text(),1,1)='h'][substring(text(),2,1)='f']">
+                               <form authority="smd">microfiche cassette</form>
+                       </xsl:if>
+                       <xsl:if test="marc:controlfield[@tag=007][substring(text(),1,1)='h'][substring(text(),2,1)='b']">
+                               <form authority="smd">microfilm cartridge</form>
+                       </xsl:if>
+                       <xsl:if test="marc:controlfield[@tag=007][substring(text(),1,1)='h'][substring(text(),2,1)='c']">
+                               <form authority="smd">microfilm cassette</form>
+                       </xsl:if>
+                       <xsl:if test="marc:controlfield[@tag=007][substring(text(),1,1)='h'][substring(text(),2,1)='d']">
+                               <form authority="smd">microfilm reel</form>
+                       </xsl:if>
+                       <xsl:if test="marc:controlfield[@tag=007][substring(text(),1,1)='h'][substring(text(),2,1)='g']">
+                               <form authority="smd">microopaque</form>
+                       </xsl:if>
+                       
+                       <xsl:if test="marc:controlfield[@tag=007][substring(text(),1,1)='m'][substring(text(),2,1)='c']">
+                               <form authority="smd">film cartridge</form>
+                       </xsl:if>
+                       <xsl:if test="marc:controlfield[@tag=007][substring(text(),1,1)='m'][substring(text(),2,1)='f']">
+                               <form authority="smd">film cassette</form>
+                       </xsl:if>
+                       <xsl:if test="marc:controlfield[@tag=007][substring(text(),1,1)='m'][substring(text(),2,1)='r']">
+                               <form authority="smd">film reel</form>
+                       </xsl:if>
+                       
+                       <xsl:if test="marc:controlfield[@tag=007][substring(text(),1,1)='k'][substring(text(),2,1)='n']">
+                               <form authority="smd">chart</form>
+                       </xsl:if>
+                       <xsl:if test="marc:controlfield[@tag=007][substring(text(),1,1)='k'][substring(text(),2,1)='c']">
+                               <form authority="smd">collage</form>
+                       </xsl:if>
+                       <xsl:if test="marc:controlfield[@tag=007][substring(text(),1,1)='k'][substring(text(),2,1)='d']">
+                               <form authority="smd">drawing</form>
+                       </xsl:if>
+                       <xsl:if test="marc:controlfield[@tag=007][substring(text(),1,1)='k'][substring(text(),2,1)='o']">
+                               <form authority="smd">flash card</form>
+                       </xsl:if>
+                       <xsl:if test="marc:controlfield[@tag=007][substring(text(),1,1)='k'][substring(text(),2,1)='e']">
+                               <form authority="smd">painting</form>
+                       </xsl:if>
+                       <xsl:if test="marc:controlfield[@tag=007][substring(text(),1,1)='k'][substring(text(),2,1)='f']">
+                               <form authority="smd">photomechanical print</form>
+                       </xsl:if>
+                       <xsl:if test="marc:controlfield[@tag=007][substring(text(),1,1)='k'][substring(text(),2,1)='g']">
+                               <form authority="smd">photonegative</form>
+                       </xsl:if>
+                       <xsl:if test="marc:controlfield[@tag=007][substring(text(),1,1)='k'][substring(text(),2,1)='h']">
+                               <form authority="smd">photoprint</form>
+                       </xsl:if>
+                       <xsl:if test="marc:controlfield[@tag=007][substring(text(),1,1)='k'][substring(text(),2,1)='i']">
+                               <form authority="smd">picture</form>
+                       </xsl:if>
+                       <xsl:if test="marc:controlfield[@tag=007][substring(text(),1,1)='k'][substring(text(),2,1)='j']">
+                               <form authority="smd">print</form>
+                       </xsl:if>
+                       <xsl:if test="marc:controlfield[@tag=007][substring(text(),1,1)='k'][substring(text(),2,1)='l']">
+                               <form authority="smd">technical drawing</form>
+                       </xsl:if>
+                       
+                       <xsl:if test="marc:controlfield[@tag=007][substring(text(),1,1)='q'][substring(text(),2,1)='q']">
+                               <form authority="smd">notated music</form>
+                       </xsl:if>
+                       
+                       <xsl:if test="marc:controlfield[@tag=007][substring(text(),1,1)='g'][substring(text(),2,1)='d']">
+                               <form authority="smd">filmslip</form>
+                       </xsl:if>
+                       <xsl:if test="marc:controlfield[@tag=007][substring(text(),1,1)='g'][substring(text(),2,1)='c']">
+                               <form authority="smd">filmstrip cartridge</form>
+                       </xsl:if>
+                       <xsl:if test="marc:controlfield[@tag=007][substring(text(),1,1)='g'][substring(text(),2,1)='o']">
+                               <form authority="smd">filmstrip roll</form>
+                       </xsl:if>
+                       <xsl:if test="marc:controlfield[@tag=007][substring(text(),1,1)='g'][substring(text(),2,1)='f']">
+                               <form authority="smd">other filmstrip type</form>
+                       </xsl:if>
+                       <xsl:if test="marc:controlfield[@tag=007][substring(text(),1,1)='g'][substring(text(),2,1)='s']">
+                               <form authority="smd">slide</form>
+                       </xsl:if>
+                       <xsl:if test="marc:controlfield[@tag=007][substring(text(),1,1)='g'][substring(text(),2,1)='t']">
+                               <form authority="smd">transparency</form>
+                       </xsl:if>
+                       <xsl:if test="marc:controlfield[@tag=007][substring(text(),1,1)='r'][substring(text(),2,1)='r']">
+                               <form authority="smd">remote-sensing image</form>
+                       </xsl:if>
+                       <xsl:if test="marc:controlfield[@tag=007][substring(text(),1,1)='s'][substring(text(),2,1)='e']">
+                               <form authority="smd">cylinder</form>
+                       </xsl:if>
+                       <xsl:if test="marc:controlfield[@tag=007][substring(text(),1,1)='s'][substring(text(),2,1)='q']">
+                               <form authority="smd">roll</form>
+                       </xsl:if>
+                       <xsl:if test="marc:controlfield[@tag=007][substring(text(),1,1)='s'][substring(text(),2,1)='g']">
+                               <form authority="smd">sound cartridge</form>
+                       </xsl:if>
+                       <xsl:if test="marc:controlfield[@tag=007][substring(text(),1,1)='s'][substring(text(),2,1)='s']">
+                               <form authority="smd">sound cassette</form>
+                       </xsl:if>
+                       <xsl:if test="marc:controlfield[@tag=007][substring(text(),1,1)='s'][substring(text(),2,1)='d']">
+                               <form authority="smd">sound disc</form>
+                       </xsl:if>
+                       <xsl:if test="marc:controlfield[@tag=007][substring(text(),1,1)='s'][substring(text(),2,1)='t']">
+                               <form authority="smd">sound-tape reel</form>
+                       </xsl:if>
+                       <xsl:if test="marc:controlfield[@tag=007][substring(text(),1,1)='s'][substring(text(),2,1)='i']">
+                               <form authority="smd">sound-track film</form>
+                       </xsl:if>
+                       <xsl:if test="marc:controlfield[@tag=007][substring(text(),1,1)='s'][substring(text(),2,1)='w']">
+                               <form authority="smd">wire recording</form>
+                       </xsl:if>
+                       
+                       <xsl:if test="marc:controlfield[@tag=007][substring(text(),1,1)='f'][substring(text(),2,1)='c']">
+                               <form authority="smd">braille</form>
+                       </xsl:if>
+                       <xsl:if test="marc:controlfield[@tag=007][substring(text(),1,1)='f'][substring(text(),2,1)='b']">
+                               <form authority="smd">combination</form>
+                       </xsl:if>
+                       <xsl:if test="marc:controlfield[@tag=007][substring(text(),1,1)='f'][substring(text(),2,1)='a']">
+                               <form authority="smd">moon</form>
+                       </xsl:if>
+                       <xsl:if test="marc:controlfield[@tag=007][substring(text(),1,1)='f'][substring(text(),2,1)='d']">
+                               <form authority="smd">tactile, with no writing system</form>
+                       </xsl:if>
+                       
+                       <xsl:if test="marc:controlfield[@tag=007][substring(text(),1,1)='t'][substring(text(),2,1)='c']">
+                               <form authority="smd">braille</form>
+                       </xsl:if>
+                       <xsl:if test="marc:controlfield[@tag=007][substring(text(),1,1)='t'][substring(text(),2,1)='b']">
+                               <form authority="smd">large print</form>
+                       </xsl:if>
+                       <xsl:if test="marc:controlfield[@tag=007][substring(text(),1,1)='t'][substring(text(),2,1)='a']">
+                               <form authority="smd">regular print</form>
+                       </xsl:if>
+                       <xsl:if test="marc:controlfield[@tag=007][substring(text(),1,1)='t'][substring(text(),2,1)='d']">
+                               <form authority="smd">text in looseleaf binder</form>
+                       </xsl:if>
+                       
+                       <xsl:if test="marc:controlfield[@tag=007][substring(text(),1,1)='v'][substring(text(),2,1)='c']">
+                               <form authority="smd">videocartridge</form>
+                       </xsl:if>
+                       <xsl:if test="marc:controlfield[@tag=007][substring(text(),1,1)='v'][substring(text(),2,1)='f']">
+                               <form authority="smd">videocassette</form>
+                       </xsl:if>
+                       <xsl:if test="marc:controlfield[@tag=007][substring(text(),1,1)='v'][substring(text(),2,1)='d']">
+                               <form authority="smd">videodisc</form>
+                       </xsl:if>
+                       <xsl:if test="marc:controlfield[@tag=007][substring(text(),1,1)='v'][substring(text(),2,1)='r']">
+                               <form authority="smd">videoreel</form>
+                       </xsl:if>
+                       
+                       <xsl:for-each select="marc:datafield[@tag=856]/marc:subfield[@code='q'][string-length(.)>1]">
+                               <internetMediaType>
+                                       <xsl:value-of select="."></xsl:value-of>
+                               </internetMediaType>
+                       </xsl:for-each>
+                       <xsl:for-each select="marc:datafield[@tag=300]">
+                               <extent>
+                                       <xsl:call-template name="subfieldSelect">
+                                               <xsl:with-param name="codes">abce</xsl:with-param>
+                                       </xsl:call-template>
+                               </extent>
+                       </xsl:for-each>
+               </xsl:variable>
+               <xsl:if test="string-length(normalize-space($physicalDescription))">
+                       <physicalDescription>
+                               <xsl:copy-of select="$physicalDescription"></xsl:copy-of>
+                       </physicalDescription>
+               </xsl:if>
+               <xsl:for-each select="marc:datafield[@tag=520]">
+                       <abstract>
+                               <xsl:call-template name="uri"></xsl:call-template>
+                               <xsl:call-template name="subfieldSelect">
+                                       <xsl:with-param name="codes">ab</xsl:with-param>
+                               </xsl:call-template>
+                       </abstract>
+               </xsl:for-each>
+               <xsl:for-each select="marc:datafield[@tag=505]">
+                       <tableOfContents>
+                               <xsl:call-template name="uri"></xsl:call-template>
+                               <xsl:call-template name="subfieldSelect">
+                                       <xsl:with-param name="codes">agrt</xsl:with-param>
+                               </xsl:call-template>
+                       </tableOfContents>
+               </xsl:for-each>
+               <xsl:for-each select="marc:datafield[@tag=521]">
+                       <targetAudience>
+                               <xsl:call-template name="subfieldSelect">
+                                       <xsl:with-param name="codes">ab</xsl:with-param>
+                               </xsl:call-template>
+                       </targetAudience>
+               </xsl:for-each>
+               <xsl:if test="$typeOf008='BK' or $typeOf008='CF' or $typeOf008='MU' or $typeOf008='VM'">
+                       <xsl:variable name="controlField008-22" select="substring($controlField008,23,1)"></xsl:variable>
+                       <xsl:choose>
+                               <!-- 01/04 fix -->
+                               <xsl:when test="$controlField008-22='d'">
+                                       <targetAudience authority="marctarget">adolescent</targetAudience>
+                               </xsl:when>
+                               <xsl:when test="$controlField008-22='e'">
+                                       <targetAudience authority="marctarget">adult</targetAudience>
+                               </xsl:when>
+                               <xsl:when test="$controlField008-22='g'">
+                                       <targetAudience authority="marctarget">general</targetAudience>
+                               </xsl:when>
+                               <xsl:when test="$controlField008-22='b' or $controlField008-22='c' or $controlField008-22='j'">
+                                       <targetAudience authority="marctarget">juvenile</targetAudience>
+                               </xsl:when>
+                               <xsl:when test="$controlField008-22='a'">
+                                       <targetAudience authority="marctarget">preschool</targetAudience>
+                               </xsl:when>
+                               <xsl:when test="$controlField008-22='f'">
+                                       <targetAudience authority="marctarget">specialized</targetAudience>
+                               </xsl:when>
+                       </xsl:choose>
+               </xsl:if>
+               <xsl:for-each select="marc:datafield[@tag=245]/marc:subfield[@code='c']">
+                       <note type="statement of responsibility">
+                               <xsl:value-of select="."></xsl:value-of>
+                       </note>
+               </xsl:for-each>
+               <xsl:for-each select="marc:datafield[@tag=500]">
+                       <note>
+                               <xsl:value-of select="marc:subfield[@code='a']"></xsl:value-of>
+                               <xsl:call-template name="uri"></xsl:call-template>
+                       </note>
+               </xsl:for-each>
+               
+               <!--3.2 change tmee additional note fields-->
+               
+               <xsl:for-each select="marc:datafield[@tag=506]">
+                       <note type="restrictions">
+                               <xsl:call-template name="uri"></xsl:call-template>
+                               <xsl:variable name="str">
+                                       <xsl:for-each select="marc:subfield[@code!='6' or @code!='8']">
+                                               <xsl:value-of select="."></xsl:value-of>
+                                               <xsl:text> </xsl:text>
+                                       </xsl:for-each>
+                               </xsl:variable>
+                               <xsl:value-of select="substring($str,1,string-length($str)-1)"></xsl:value-of>
+                       </note>
+               </xsl:for-each>
+               
+               <xsl:for-each select="marc:datafield[@tag=510]">
+                       <note  type="citation/reference">
+                               <xsl:call-template name="uri"></xsl:call-template>
+                               <xsl:variable name="str">
+                                       <xsl:for-each select="marc:subfield[@code!='6' or @code!='8']">
+                                               <xsl:value-of select="."></xsl:value-of>
+                                               <xsl:text> </xsl:text>
+                                       </xsl:for-each>
+                               </xsl:variable>
+                               <xsl:value-of select="substring($str,1,string-length($str)-1)"></xsl:value-of>
+                       </note>
+               </xsl:for-each>
+               
+                       
+               <xsl:for-each select="marc:datafield[@tag=511]">
+                       <note type="performers">
+                               <xsl:call-template name="uri"></xsl:call-template>
+                               <xsl:value-of select="marc:subfield[@code='a']"></xsl:value-of>
+                       </note>
+               </xsl:for-each>
+               <xsl:for-each select="marc:datafield[@tag=518]">
+                       <note type="venue">
+                               <xsl:call-template name="uri"></xsl:call-template>
+                               <xsl:value-of select="marc:subfield[@code='a']"></xsl:value-of>
+                       </note>
+               </xsl:for-each>
+               
+               <xsl:for-each select="marc:datafield[@tag=530]">
+                       <note  type="additional physical form">
+                               <xsl:call-template name="uri"></xsl:call-template>
+                               <xsl:variable name="str">
+                                       <xsl:for-each select="marc:subfield[@code!='6' or @code!='8']">
+                                               <xsl:value-of select="."></xsl:value-of>
+                                               <xsl:text> </xsl:text>
+                                       </xsl:for-each>
+                               </xsl:variable>
+                               <xsl:value-of select="substring($str,1,string-length($str)-1)"></xsl:value-of>
+                       </note>
+               </xsl:for-each>
+               
+               <xsl:for-each select="marc:datafield[@tag=533]">
+                       <note  type="reproduction">
+                               <xsl:call-template name="uri"></xsl:call-template>
+                               <xsl:variable name="str">
+                                       <xsl:for-each select="marc:subfield[@code!='6' or @code!='8']">
+                                               <xsl:value-of select="."></xsl:value-of>
+                                               <xsl:text> </xsl:text>
+                                       </xsl:for-each>
+                               </xsl:variable>
+                               <xsl:value-of select="substring($str,1,string-length($str)-1)"></xsl:value-of>
+                       </note>
+               </xsl:for-each>
+               
+               <xsl:for-each select="marc:datafield[@tag=534]">
+                       <note  type="original version">
+                               <xsl:call-template name="uri"></xsl:call-template>
+                               <xsl:variable name="str">
+                                       <xsl:for-each select="marc:subfield[@code!='6' or @code!='8']">
+                                               <xsl:value-of select="."></xsl:value-of>
+                                               <xsl:text> </xsl:text>
+                                       </xsl:for-each>
+                               </xsl:variable>
+                               <xsl:value-of select="substring($str,1,string-length($str)-1)"></xsl:value-of>
+                       </note>
+               </xsl:for-each>
+               
+               <xsl:for-each select="marc:datafield[@tag=538]">
+                       <note  type="system details">
+                               <xsl:call-template name="uri"></xsl:call-template>
+                               <xsl:variable name="str">
+                                       <xsl:for-each select="marc:subfield[@code!='6' or @code!='8']">
+                                               <xsl:value-of select="."></xsl:value-of>
+                                               <xsl:text> </xsl:text>
+                                       </xsl:for-each>
+                               </xsl:variable>
+                               <xsl:value-of select="substring($str,1,string-length($str)-1)"></xsl:value-of>
+                       </note>
+               </xsl:for-each>
+               
+               <xsl:for-each select="marc:datafield[@tag=583]">
+                       <note type="action">
+                               <xsl:call-template name="uri"></xsl:call-template>
+                               <xsl:variable name="str">
+                                       <xsl:for-each select="marc:subfield[@code!='6' or @code!='8']">
+                                               <xsl:value-of select="."></xsl:value-of>
+                                               <xsl:text> </xsl:text>
+                                       </xsl:for-each>
+                               </xsl:variable>
+                               <xsl:value-of select="substring($str,1,string-length($str)-1)"></xsl:value-of>
+                       </note>
+               </xsl:for-each>
+               
+
+               
+               
+               
+               <xsl:for-each select="marc:datafield[@tag=501 or @tag=502 or @tag=504 or @tag=507 or @tag=508 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]">
+                       <note>
+                               <xsl:call-template name="uri"></xsl:call-template>
+                               <xsl:variable name="str">
+                                       <xsl:for-each select="marc:subfield[@code!='6' or @code!='8']">
+                                               <xsl:value-of select="."></xsl:value-of>
+                                               <xsl:text> </xsl:text>
+                                       </xsl:for-each>
+                               </xsl:variable>
+                               <xsl:value-of select="substring($str,1,string-length($str)-1)"></xsl:value-of>
+                       </note>
+               </xsl:for-each>
+               <xsl:for-each select="marc:datafield[@tag=034][marc:subfield[@code='d' or @code='e' or @code='f' or @code='g']]">
+                       <subject>
+                               <cartographics>
+                                       <coordinates>
+                                               <xsl:call-template name="subfieldSelect">
+                                                       <xsl:with-param name="codes">defg</xsl:with-param>
+                                               </xsl:call-template>
+                                       </coordinates>
+                               </cartographics>
+                       </subject>
+               </xsl:for-each>
+               <xsl:for-each select="marc:datafield[@tag=043]">
+                       <subject>
+                               <xsl:for-each select="marc:subfield[@code='a' or @code='b' or @code='c']">
+                                       <geographicCode>
+                                               <xsl:attribute name="authority">
+                                                       <xsl:if test="@code='a'">
+                                                               <xsl:text>marcgac</xsl:text>
+                                                       </xsl:if>
+                                                       <xsl:if test="@code='b'">
+                                                               <xsl:value-of select="following-sibling::marc:subfield[@code=2]"></xsl:value-of>
+                                                       </xsl:if>
+                                                       <xsl:if test="@code='c'">
+                                                               <xsl:text>iso3166</xsl:text>
+                                                       </xsl:if>
+                                               </xsl:attribute>
+                                               <xsl:value-of select="self::marc:subfield"></xsl:value-of>
+                                       </geographicCode>
+                               </xsl:for-each>
+                       </subject>
+               </xsl:for-each>
+               <!-- tmee 2006/11/27 -->
+               <xsl:for-each select="marc:datafield[@tag=255]">
+                       <subject>
+                               <xsl:for-each select="marc:subfield[@code='a' or @code='b' or @code='c']">
+                               <cartographics>
+                                       <xsl:if test="@code='a'">
+                                               <scale>
+                                                       <xsl:value-of select="."></xsl:value-of>
+                                               </scale>
+                                       </xsl:if>
+                                       <xsl:if test="@code='b'">
+                                               <projection>
+                                                       <xsl:value-of select="."></xsl:value-of>
+                                               </projection>
+                                       </xsl:if>
+                                       <xsl:if test="@code='c'">
+                                               <coordinates>
+                                                       <xsl:value-of select="."></xsl:value-of>
+                                               </coordinates>
+                                       </xsl:if>
+                               </cartographics>
+                               </xsl:for-each>
+                       </subject>
+               </xsl:for-each>
+                               
+               <xsl:apply-templates select="marc:datafield[653 >= @tag and @tag >= 600]"></xsl:apply-templates>
+               <xsl:apply-templates select="marc:datafield[@tag=656]"></xsl:apply-templates>
+               <xsl:for-each select="marc:datafield[@tag=752]">
+                       <subject>
+                               <hierarchicalGeographic>
+                                       <xsl:for-each select="marc:subfield[@code='a']">
+                                               <country>
+                                                       <xsl:call-template name="chopPunctuation">
+                                                               <xsl:with-param name="chopString" select="."></xsl:with-param>
+                                                       </xsl:call-template>
+                                               </country>
+                                       </xsl:for-each>
+                                       <xsl:for-each select="marc:subfield[@code='b']">
+                                               <state>
+                                                       <xsl:call-template name="chopPunctuation">
+                                                               <xsl:with-param name="chopString" select="."></xsl:with-param>
+                                                       </xsl:call-template>
+                                               </state>
+                                       </xsl:for-each>
+                                       <xsl:for-each select="marc:subfield[@code='c']">
+                                               <county>
+                                                       <xsl:call-template name="chopPunctuation">
+                                                               <xsl:with-param name="chopString" select="."></xsl:with-param>
+                                                       </xsl:call-template>
+                                               </county>
+                                       </xsl:for-each>
+                                       <xsl:for-each select="marc:subfield[@code='d']">
+                                               <city>
+                                                       <xsl:call-template name="chopPunctuation">
+                                                               <xsl:with-param name="chopString" select="."></xsl:with-param>
+                                                       </xsl:call-template>
+                                               </city>
+                                       </xsl:for-each>
+                               </hierarchicalGeographic>
+                       </subject>
+               </xsl:for-each>
+               <xsl:for-each select="marc:datafield[@tag=045][marc:subfield[@code='b']]">
+                       <subject>
+                               <xsl:choose>
+                                       <xsl:when test="@ind1=2">
+                                               <temporal encoding="iso8601" point="start">
+                                                       <xsl:call-template name="chopPunctuation">
+                                                               <xsl:with-param name="chopString">
+                                                                       <xsl:value-of select="marc:subfield[@code='b'][1]"></xsl:value-of>
+                                                               </xsl:with-param>
+                                                       </xsl:call-template>
+                                               </temporal>
+                                               <temporal encoding="iso8601" point="end">
+                                                       <xsl:call-template name="chopPunctuation">
+                                                               <xsl:with-param name="chopString">
+                                                                       <xsl:value-of select="marc:subfield[@code='b'][2]"></xsl:value-of>
+                                                               </xsl:with-param>
+                                                       </xsl:call-template>
+                                               </temporal>
+                                       </xsl:when>
+                                       <xsl:otherwise>
+                                               <xsl:for-each select="marc:subfield[@code='b']">
+                                                       <temporal encoding="iso8601">
+                                                               <xsl:call-template name="chopPunctuation">
+                                                                       <xsl:with-param name="chopString" select="."></xsl:with-param>
+                                                               </xsl:call-template>
+                                                       </temporal>
+                                               </xsl:for-each>
+                                       </xsl:otherwise>
+                               </xsl:choose>
+                       </subject>
+               </xsl:for-each>
+               <xsl:for-each select="marc:datafield[@tag=050]">
+                       <xsl:for-each select="marc:subfield[@code='b']">
+                               <classification authority="lcc">
+                                       <xsl:if test="../marc:subfield[@code='3']">
+                                               <xsl:attribute name="displayLabel">
+                                                       <xsl:value-of select="../marc:subfield[@code='3']"></xsl:value-of>
+                                               </xsl:attribute>
+                                       </xsl:if>
+                                       <xsl:value-of select="preceding-sibling::marc:subfield[@code='a'][1]"></xsl:value-of>
+                                       <xsl:text> </xsl:text>
+                                       <xsl:value-of select="text()"></xsl:value-of>
+                               </classification>
+                       </xsl:for-each>
+                       <xsl:for-each select="marc:subfield[@code='a'][not(following-sibling::marc:subfield[@code='b'])]">
+                               <classification authority="lcc">
+                                       <xsl:if test="../marc:subfield[@code='3']">
+                                               <xsl:attribute name="displayLabel">
+                                                       <xsl:value-of select="../marc:subfield[@code='3']"></xsl:value-of>
+                                               </xsl:attribute>
+                                       </xsl:if>
+                                       <xsl:value-of select="text()"></xsl:value-of>
+                               </classification>
+                       </xsl:for-each>
+               </xsl:for-each>
+               <xsl:for-each select="marc:datafield[@tag=082]">
+                       <classification authority="ddc">
+                               <xsl:if test="marc:subfield[@code='2']">
+                                       <xsl:attribute name="edition">
+                                               <xsl:value-of select="marc:subfield[@code='2']"></xsl:value-of>
+                                       </xsl:attribute>
+                               </xsl:if>
+                               <xsl:call-template name="subfieldSelect">
+                                       <xsl:with-param name="codes">ab</xsl:with-param>
+                               </xsl:call-template>
+                       </classification>
+               </xsl:for-each>
+               <xsl:for-each select="marc:datafield[@tag=080]">
+                       <classification authority="udc">
+                               <xsl:call-template name="subfieldSelect">
+                                       <xsl:with-param name="codes">abx</xsl:with-param>
+                               </xsl:call-template>
+                       </classification>
+               </xsl:for-each>
+               <xsl:for-each select="marc:datafield[@tag=060]">
+                       <classification authority="nlm">
+                               <xsl:call-template name="subfieldSelect">
+                                       <xsl:with-param name="codes">ab</xsl:with-param>
+                               </xsl:call-template>
+                       </classification>
+               </xsl:for-each>
+               <xsl:for-each select="marc:datafield[@tag=086][@ind1=0]">
+                       <classification authority="sudocs">
+                               <xsl:value-of select="marc:subfield[@code='a']"></xsl:value-of>
+                       </classification>
+               </xsl:for-each>
+               <xsl:for-each select="marc:datafield[@tag=086][@ind1=1]">
+                       <classification authority="candoc">
+                               <xsl:value-of select="marc:subfield[@code='a']"></xsl:value-of>
+                       </classification>
+               </xsl:for-each>
+               <xsl:for-each select="marc:datafield[@tag=086]">
+                       <classification>
+                               <xsl:attribute name="authority">
+                                       <xsl:value-of select="marc:subfield[@code='2']"></xsl:value-of>
+                               </xsl:attribute>
+                               <xsl:value-of select="marc:subfield[@code='a']"></xsl:value-of>
+                       </classification>
+               </xsl:for-each>
+               <xsl:for-each select="marc:datafield[@tag=084]">
+                       <classification>
+                               <xsl:attribute name="authority">
+                                       <xsl:value-of select="marc:subfield[@code='2']"></xsl:value-of>
+                               </xsl:attribute>
+                               <xsl:call-template name="subfieldSelect">
+                                       <xsl:with-param name="codes">ab</xsl:with-param>
+                               </xsl:call-template>
+                       </classification>
+               </xsl:for-each>
+               <xsl:for-each select="marc:datafield[@tag=440]">
+                       <relatedItem type="series">
+                               <xsl:variable name="titleChop">
+                                       <xsl:call-template name="chopPunctuation">
+                                               <xsl:with-param name="chopString">
+                                                       <xsl:call-template name="subfieldSelect">
+                                                               <xsl:with-param name="codes">av</xsl:with-param>
+                                                       </xsl:call-template>
+                                               </xsl:with-param>
+                                       </xsl:call-template>
+                               </xsl:variable>
+                               <titleInfo>
+                                       <title>
+                                               <xsl:value-of select="$titleChop" />
+                                       </title>
+                                       <xsl:call-template name="part"></xsl:call-template>
+                               </titleInfo>
+                               <titleInfo type="nfi">
+                                       <xsl:choose>
+                                               <xsl:when test="@ind2>0">
+                                                       <nonSort>
+                                                               <xsl:value-of select="substring($titleChop,1,@ind2)"/>
+                                                       </nonSort>
+                                                       <title>
+                                                               <xsl:value-of select="substring($titleChop,@ind2+1)"/>
+                                                       </title>
+                                                       <xsl:call-template name="part"/>
+                                               </xsl:when>
+                                               <xsl:otherwise>
+                                                       <title>
+                                                               <xsl:value-of select="$titleChop" />
+                                                       </title>
+                                               </xsl:otherwise>
+                                       </xsl:choose>
+                                       <xsl:call-template name="part"></xsl:call-template>
+                               </titleInfo>
+                       </relatedItem>
+               </xsl:for-each>
+               <xsl:for-each select="marc:datafield[@tag=490][@ind1=0]">
+                       <relatedItem type="series">
+                               <titleInfo>
+                                       <title>
+                                               <xsl:call-template name="chopPunctuation">
+                                                       <xsl:with-param name="chopString">
+                                                               <xsl:call-template name="subfieldSelect">
+                                                                       <xsl:with-param name="codes">av</xsl:with-param>
+                                                               </xsl:call-template>
+                                                       </xsl:with-param>
+                                               </xsl:call-template>
+                                       </title>
+                                       <xsl:call-template name="part"></xsl:call-template>
+                               </titleInfo>
+                       </relatedItem>
+               </xsl:for-each>
+               <xsl:for-each select="marc:datafield[@tag=510]">
+                       <relatedItem type="isReferencedBy">
+                               <note>
+                                       <xsl:call-template name="subfieldSelect">
+                                               <xsl:with-param name="codes">abcx3</xsl:with-param>
+                                       </xsl:call-template>
+                               </note>
+                       </relatedItem>
+               </xsl:for-each>
+               <xsl:for-each select="marc:datafield[@tag=534]">
+                       <relatedItem type="original">
+                               <xsl:call-template name="relatedTitle"></xsl:call-template>
+                               <xsl:call-template name="relatedName"></xsl:call-template>
+                               <xsl:if test="marc:subfield[@code='b' or @code='c']">
+                                       <originInfo>
+                                               <xsl:for-each select="marc:subfield[@code='c']">
+                                                       <publisher>
+                                                               <xsl:value-of select="."></xsl:value-of>
+                                                       </publisher>
+                                               </xsl:for-each>
+                                               <xsl:for-each select="marc:subfield[@code='b']">
+                                                       <edition>
+                                                               <xsl:value-of select="."></xsl:value-of>
+                                                       </edition>
+                                               </xsl:for-each>
+                                       </originInfo>
+                               </xsl:if>
+                               <xsl:call-template name="relatedIdentifierISSN"></xsl:call-template>
+                               <xsl:for-each select="marc:subfield[@code='z']">
+                                       <identifier type="isbn">
+                                               <xsl:value-of select="."></xsl:value-of>
+                                       </identifier>
+                               </xsl:for-each>
+                               <xsl:call-template name="relatedNote"></xsl:call-template>
+                       </relatedItem>
+               </xsl:for-each>
+               <xsl:for-each select="marc:datafield[@tag=700][marc:subfield[@code='t']]">
+                       <relatedItem>
+                               <xsl:call-template name="constituentOrRelatedType"></xsl:call-template>
+                               <titleInfo>
+                                       <title>
+                                               <xsl:call-template name="chopPunctuation">
+                                                       <xsl:with-param name="chopString">
+                                                               <xsl:call-template name="specialSubfieldSelect">
+                                                                       <xsl:with-param name="anyCodes">tfklmorsv</xsl:with-param>
+                                                                       <xsl:with-param name="axis">t</xsl:with-param>
+                                                                       <xsl:with-param name="afterCodes">g</xsl:with-param>
+                                                               </xsl:call-template>
+                                                       </xsl:with-param>
+                                               </xsl:call-template>
+                                       </title>
+                                       <xsl:call-template name="part"></xsl:call-template>
+                               </titleInfo>
+                               <name type="personal">
+                                       <namePart>
+                                               <xsl:call-template name="specialSubfieldSelect">
+                                                       <xsl:with-param name="anyCodes">aq</xsl:with-param>
+                                                       <xsl:with-param name="axis">t</xsl:with-param>
+                                                       <xsl:with-param name="beforeCodes">g</xsl:with-param>
+                                               </xsl:call-template>
+                                       </namePart>
+                                       <xsl:call-template name="termsOfAddress"></xsl:call-template>
+                                       <xsl:call-template name="nameDate"></xsl:call-template>
+                                       <xsl:call-template name="role"></xsl:call-template>
+                               </name>
+                               <xsl:call-template name="relatedForm"></xsl:call-template>
+                               <xsl:call-template name="relatedIdentifierISSN"></xsl:call-template>
+                       </relatedItem>
+               </xsl:for-each>
+               <xsl:for-each select="marc:datafield[@tag=710][marc:subfield[@code='t']]">
+                       <relatedItem>
+                               <xsl:call-template name="constituentOrRelatedType"></xsl:call-template>
+                               <titleInfo>
+                                       <title>
+                                               <xsl:call-template name="chopPunctuation">
+                                                       <xsl:with-param name="chopString">
+                                                               <xsl:call-template name="specialSubfieldSelect">
+                                                                       <xsl:with-param name="anyCodes">tfklmorsv</xsl:with-param>
+                                                                       <xsl:with-param name="axis">t</xsl:with-param>
+                                                                       <xsl:with-param name="afterCodes">dg</xsl:with-param>
+                                                               </xsl:call-template>
+                                                       </xsl:with-param>
+                                               </xsl:call-template>
+                                       </title>
+                                       <xsl:call-template name="relatedPartNumName"></xsl:call-template>
+                               </titleInfo>
+                               <name type="corporate">
+                                       <xsl:for-each select="marc:subfield[@code='a']">
+                                               <namePart>
+                                                       <xsl:value-of select="."></xsl:value-of>
+                                               </namePart>
+                                       </xsl:for-each>
+                                       <xsl:for-each select="marc:subfield[@code='b']">
+                                               <namePart>
+                                                       <xsl:value-of select="."></xsl:value-of>
+                                               </namePart>
+                                       </xsl:for-each>
+                                       <xsl:variable name="tempNamePart">
+                                               <xsl:call-template name="specialSubfieldSelect">
+                                                       <xsl:with-param name="anyCodes">c</xsl:with-param>
+                                                       <xsl:with-param name="axis">t</xsl:with-param>
+                                                       <xsl:with-param name="beforeCodes">dgn</xsl:with-param>
+                                               </xsl:call-template>
+                                       </xsl:variable>
+                                       <xsl:if test="normalize-space($tempNamePart)">
+                                               <namePart>
+                                                       <xsl:value-of select="$tempNamePart"></xsl:value-of>
+                                               </namePart>
+                                       </xsl:if>
+                                       <xsl:call-template name="role"></xsl:call-template>
+                               </name>
+                               <xsl:call-template name="relatedForm"></xsl:call-template>
+                               <xsl:call-template name="relatedIdentifierISSN"></xsl:call-template>
+                       </relatedItem>
+               </xsl:for-each>
+               <xsl:for-each select="marc:datafield[@tag=711][marc:subfield[@code='t']]">
+                       <relatedItem>
+                               <xsl:call-template name="constituentOrRelatedType"></xsl:call-template>
+                               <titleInfo>
+                                       <title>
+                                               <xsl:call-template name="chopPunctuation">
+                                                       <xsl:with-param name="chopString">
+                                                               <xsl:call-template name="specialSubfieldSelect">
+                                                                       <xsl:with-param name="anyCodes">tfklsv</xsl:with-param>
+                                                                       <xsl:with-param name="axis">t</xsl:with-param>
+                                                                       <xsl:with-param name="afterCodes">g</xsl:with-param>
+                                                               </xsl:call-template>
+                                                       </xsl:with-param>
+                                               </xsl:call-template>
+                                       </title>
+                                       <xsl:call-template name="relatedPartNumName"></xsl:call-template>
+                               </titleInfo>
+                               <name type="conference">
+                                       <namePart>
+                                               <xsl:call-template name="specialSubfieldSelect">
+                                                       <xsl:with-param name="anyCodes">aqdc</xsl:with-param>
+                                                       <xsl:with-param name="axis">t</xsl:with-param>
+                                                       <xsl:with-param name="beforeCodes">gn</xsl:with-param>
+                                               </xsl:call-template>
+                                       </namePart>
+                               </name>
+                               <xsl:call-template name="relatedForm"></xsl:call-template>
+                               <xsl:call-template name="relatedIdentifierISSN"></xsl:call-template>
+                       </relatedItem>
+               </xsl:for-each>
+               <xsl:for-each select="marc:datafield[@tag=730][@ind2=2]">
+                       <relatedItem>
+                               <xsl:call-template name="constituentOrRelatedType"></xsl:call-template>
+                               <titleInfo>
+                                       <title>
+                                               <xsl:call-template name="chopPunctuation">
+                                                       <xsl:with-param name="chopString">
+                                                               <xsl:call-template name="subfieldSelect">
+                                                                       <xsl:with-param name="codes">adfgklmorsv</xsl:with-param>
+                                                               </xsl:call-template>
+                                                       </xsl:with-param>
+                                               </xsl:call-template>
+                                       </title>
+                                       <xsl:call-template name="part"></xsl:call-template>
+                               </titleInfo>
+                               <xsl:call-template name="relatedForm"></xsl:call-template>
+                               <xsl:call-template name="relatedIdentifierISSN"></xsl:call-template>
+                       </relatedItem>
+               </xsl:for-each>
+               <xsl:for-each select="marc:datafield[@tag=740][@ind2=2]">
+                       <relatedItem>
+                               <xsl:call-template name="constituentOrRelatedType"></xsl:call-template>
+                               <xsl:variable name="titleChop">
+                                       <xsl:call-template name="chopPunctuation">
+                                               <xsl:with-param name="chopString">
+                                                       <xsl:value-of select="marc:subfield[@code='a']"></xsl:value-of>
+                                               </xsl:with-param>
+                                       </xsl:call-template>
+                               </xsl:variable>
+                               <titleInfo>
+                                       <title>
+                                               <xsl:value-of select="$titleChop" />
+                                       </title>
+                                       <xsl:call-template name="part"></xsl:call-template>
+                               </titleInfo>
+                               <titleInfo type="nfi">
+                                       <xsl:choose>
+                                               <xsl:when test="@ind1>0">
+                                                       <nonSort>
+                                                               <xsl:value-of select="substring($titleChop,1,@ind1)"/>
+                                                       </nonSort>
+                                                       <title>
+                                                               <xsl:value-of select="substring($titleChop,@ind1+1)"/>
+                                                       </title>
+                                               </xsl:when>
+                                               <xsl:otherwise>
+                                                       <title>
+                                                               <xsl:value-of select="$titleChop" />
+                                                       </title>
+                                               </xsl:otherwise>
+                                       </xsl:choose>
+                                       <xsl:call-template name="part"></xsl:call-template>
+                               </titleInfo>
+                               <xsl:call-template name="relatedForm"></xsl:call-template>
+                       </relatedItem>
+               </xsl:for-each>
+               <xsl:for-each select="marc:datafield[@tag=760]|marc:datafield[@tag=762]">
+                       <relatedItem type="series">
+                               <xsl:call-template name="relatedItem76X-78X"></xsl:call-template>
+                       </relatedItem>
+               </xsl:for-each>
+               <xsl:for-each select="marc:datafield[@tag=765]|marc:datafield[@tag=767]|marc:datafield[@tag=777]|marc:datafield[@tag=787]">
+                       <relatedItem>
+                               <xsl:call-template name="relatedItem76X-78X"></xsl:call-template>
+                       </relatedItem>
+               </xsl:for-each>
+               <xsl:for-each select="marc:datafield[@tag=775]">
+                       <relatedItem type="otherVersion">
+                               <xsl:call-template name="relatedItem76X-78X"></xsl:call-template>
+                       </relatedItem>
+               </xsl:for-each>
+               <xsl:for-each select="marc:datafield[@tag=770]|marc:datafield[@tag=774]">
+                       <relatedItem type="constituent">
+                               <xsl:call-template name="relatedItem76X-78X"></xsl:call-template>
+                       </relatedItem>
+               </xsl:for-each>
+               <xsl:for-each select="marc:datafield[@tag=772]|marc:datafield[@tag=773]">
+                       <relatedItem type="host">
+                               <xsl:call-template name="relatedItem76X-78X"></xsl:call-template>
+                       </relatedItem>
+               </xsl:for-each>
+               <xsl:for-each select="marc:datafield[@tag=776]">
+                       <relatedItem type="otherFormat">
+                               <xsl:call-template name="relatedItem76X-78X"></xsl:call-template>
+                       </relatedItem>
+               </xsl:for-each>
+               <xsl:for-each select="marc:datafield[@tag=780]">
+                       <relatedItem type="preceding">
+                               <xsl:call-template name="relatedItem76X-78X"></xsl:call-template>
+                       </relatedItem>
+               </xsl:for-each>
+               <xsl:for-each select="marc:datafield[@tag=785]">
+                       <relatedItem type="succeeding">
+                               <xsl:call-template name="relatedItem76X-78X"></xsl:call-template>
+                       </relatedItem>
+               </xsl:for-each>
+               <xsl:for-each select="marc:datafield[@tag=786]">
+                       <relatedItem type="original">
+                               <xsl:call-template name="relatedItem76X-78X"></xsl:call-template>
+                       </relatedItem>
+               </xsl:for-each>
+               <xsl:for-each select="marc:datafield[@tag=800]">
+                       <relatedItem type="series">
+                               <titleInfo>
+                                       <title>
+                                               <xsl:call-template name="chopPunctuation">
+                                                       <xsl:with-param name="chopString">
+                                                               <xsl:call-template name="specialSubfieldSelect">
+                                                                       <xsl:with-param name="anyCodes">tfklmorsv</xsl:with-param>
+                                                                       <xsl:with-param name="axis">t</xsl:with-param>
+                                                                       <xsl:with-param name="afterCodes">g</xsl:with-param>
+                                                               </xsl:call-template>
+                                                       </xsl:with-param>
+                                               </xsl:call-template>
+                                       </title>
+                                       <xsl:call-template name="part"></xsl:call-template>
+                               </titleInfo>
+                               <name type="personal">
+                                       <namePart>
+                                               <xsl:call-template name="chopPunctuation">
+                                                       <xsl:with-param name="chopString">
+                                                               <xsl:call-template name="specialSubfieldSelect">
+                                                                       <xsl:with-param name="anyCodes">aq</xsl:with-param>
+                                                                       <xsl:with-param name="axis">t</xsl:with-param>
+                                                                       <xsl:with-param name="beforeCodes">g</xsl:with-param>
+                                                               </xsl:call-template>
+                                                       </xsl:with-param>
+                                               </xsl:call-template>
+                                       </namePart>
+                                       <xsl:call-template name="termsOfAddress"></xsl:call-template>
+                                       <xsl:call-template name="nameDate"></xsl:call-template>
+                                       <xsl:call-template name="role"></xsl:call-template>
+                               </name>
+                               <xsl:call-template name="relatedForm"></xsl:call-template>
+                       </relatedItem>
+               </xsl:for-each>
+               <xsl:for-each select="marc:datafield[@tag=810]">
+                       <relatedItem type="series">
+                               <titleInfo>
+                                       <title>
+                                               <xsl:call-template name="chopPunctuation">
+                                                       <xsl:with-param name="chopString">
+                                                               <xsl:call-template name="specialSubfieldSelect">
+                                                                       <xsl:with-param name="anyCodes">tfklmorsv</xsl:with-param>
+                                                                       <xsl:with-param name="axis">t</xsl:with-param>
+                                                                       <xsl:with-param name="afterCodes">dg</xsl:with-param>
+                                                               </xsl:call-template>
+                                                       </xsl:with-param>
+                                               </xsl:call-template>
+                                       </title>
+                                       <xsl:call-template name="relatedPartNumName"></xsl:call-template>
+                               </titleInfo>
+                               <name type="corporate">
+                                       <xsl:for-each select="marc:subfield[@code='a']">
+                                               <namePart>
+                                                       <xsl:value-of select="."></xsl:value-of>
+                                               </namePart>
+                                       </xsl:for-each>
+                                       <xsl:for-each select="marc:subfield[@code='b']">
+                                               <namePart>
+                                                       <xsl:value-of select="."></xsl:value-of>
+                                               </namePart>
+                                       </xsl:for-each>
+                                       <namePart>
+                                               <xsl:call-template name="specialSubfieldSelect">
+                                                       <xsl:with-param name="anyCodes">c</xsl:with-param>
+                                                       <xsl:with-param name="axis">t</xsl:with-param>
+                                                       <xsl:with-param name="beforeCodes">dgn</xsl:with-param>
+                                               </xsl:call-template>
+                                       </namePart>
+                                       <xsl:call-template name="role"></xsl:call-template>
+                               </name>
+                               <xsl:call-template name="relatedForm"></xsl:call-template>
+                       </relatedItem>
+               </xsl:for-each>
+               <xsl:for-each select="marc:datafield[@tag=811]">
+                       <relatedItem type="series">
+                               <titleInfo>
+                                       <title>
+                                               <xsl:call-template name="chopPunctuation">
+                                                       <xsl:with-param name="chopString">
+                                                               <xsl:call-template name="specialSubfieldSelect">
+                                                                       <xsl:with-param name="anyCodes">tfklsv</xsl:with-param>
+                                                                       <xsl:with-param name="axis">t</xsl:with-param>
+                                                                       <xsl:with-param name="afterCodes">g</xsl:with-param>
+                                                               </xsl:call-template>
+                                                       </xsl:with-param>
+                                               </xsl:call-template>
+                                       </title>
+                                       <xsl:call-template name="relatedPartNumName"/>
+                               </titleInfo>
+                               <name type="conference">
+                                       <namePart>
+                                               <xsl:call-template name="specialSubfieldSelect">
+                                                       <xsl:with-param name="anyCodes">aqdc</xsl:with-param>
+                                                       <xsl:with-param name="axis">t</xsl:with-param>
+                                                       <xsl:with-param name="beforeCodes">gn</xsl:with-param>
+                                               </xsl:call-template>
+                                       </namePart>
+                                       <xsl:call-template name="role"/>
+                               </name>
+                               <xsl:call-template name="relatedForm"/>
+                       </relatedItem>
+               </xsl:for-each>
+               <xsl:for-each select="marc:datafield[@tag='830']">
+                       <relatedItem type="series">
+                               <xsl:variable name="titleChop">
+                                       <xsl:call-template name="chopPunctuation">
+                                               <xsl:with-param name="chopString">
+                                                       <xsl:call-template name="subfieldSelect">
+                                                               <xsl:with-param name="codes">adfgklmorsv</xsl:with-param>
+                                                       </xsl:call-template>
+                                               </xsl:with-param>
+                                       </xsl:call-template>
+                               </xsl:variable>
+                               <titleInfo>
+                                       <title>
+                                               <xsl:value-of select="$titleChop" />
+                                       </title>
+                                       <xsl:call-template name="part"/>
+                               </titleInfo>
+                               <titleInfo type="nfi">
+                                       <xsl:choose>
+                                               <xsl:when test="@ind2>0">
+                                                       <nonSort>
+                                                               <xsl:value-of select="substring($titleChop,1,@ind2)"/>
+                                                       </nonSort>
+                                                       <title>
+                                                               <xsl:value-of select="substring($titleChop,@ind2+1)"/>
+                                                       </title>
+                                               </xsl:when>
+                                               <xsl:otherwise>
+                                                       <title>
+                                                               <xsl:value-of select="$titleChop" />
+                                                       </title>
+                                               </xsl:otherwise>
+                                       </xsl:choose>
+                                       <xsl:call-template name="part"/>
+                               </titleInfo>
+                               <xsl:call-template name="relatedForm"/>
+                       </relatedItem>
+               </xsl:for-each>
+               <xsl:for-each select="marc:datafield[@tag='856'][@ind2='2']/marc:subfield[@code='q']">
+                       <relatedItem>
+                               <internetMediaType>
+                                       <xsl:value-of select="."/>
+                               </internetMediaType>
+                       </relatedItem>
+               </xsl:for-each>
+               <xsl:for-each select="marc:datafield[@tag='020']">
+                       <xsl:call-template name="isInvalid">
+                               <xsl:with-param name="type">isbn</xsl:with-param>
+                       </xsl:call-template>
+                       <xsl:if test="marc:subfield[@code='a']">
+                               <identifier type="isbn">
+                                       <xsl:value-of select="marc:subfield[@code='a']"/>
+                               </identifier>
+                       </xsl:if>
+               </xsl:for-each>
+               <xsl:for-each select="marc:datafield[@tag='024'][@ind1='0']">
+                       <xsl:call-template name="isInvalid">
+                               <xsl:with-param name="type">isrc</xsl:with-param>
+                       </xsl:call-template>
+                       <xsl:if test="marc:subfield[@code='a']">
+                               <identifier type="isrc">
+                                       <xsl:value-of select="marc:subfield[@code='a']"/>
+                               </identifier>
+                       </xsl:if>
+               </xsl:for-each>
+               <xsl:for-each select="marc:datafield[@tag='024'][@ind1='2']">
+                       <xsl:call-template name="isInvalid">
+                               <xsl:with-param name="type">ismn</xsl:with-param>
+                       </xsl:call-template>
+                       <xsl:if test="marc:subfield[@code='a']">
+                               <identifier type="ismn">
+                                       <xsl:value-of select="marc:subfield[@code='a']"/>
+                               </identifier>
+                       </xsl:if>
+               </xsl:for-each>
+               <xsl:for-each select="marc:datafield[@tag='024'][@ind1='4']">
+                       <xsl:call-template name="isInvalid">
+                               <xsl:with-param name="type">sici</xsl:with-param>
+                       </xsl:call-template>
+                       <identifier type="sici">
+                               <xsl:call-template name="subfieldSelect">
+                                       <xsl:with-param name="codes">ab</xsl:with-param>
+                               </xsl:call-template>
+                       </identifier>
+               </xsl:for-each>
+               <xsl:for-each select="marc:datafield[@tag='022']">
+                       <xsl:call-template name="isInvalid">
+                               <xsl:with-param name="type">issn</xsl:with-param>
+                       </xsl:call-template>
+                       <identifier type="issn">
+                               <xsl:value-of select="marc:subfield[@code='a']"/>
+                       </identifier>
+               </xsl:for-each>
+               <xsl:for-each select="marc:datafield[@tag='010']">
+                       <xsl:call-template name="isInvalid">
+                               <xsl:with-param name="type">lccn</xsl:with-param>
+                       </xsl:call-template>
+                       <identifier type="lccn">
+                               <xsl:value-of select="normalize-space(marc:subfield[@code='a'])"/>
+                       </identifier>
+               </xsl:for-each>
+               <xsl:for-each select="marc:datafield[@tag='028']">
+                       <identifier>
+                               <xsl:attribute name="type">
+                                       <xsl:choose>
+                                               <xsl:when test="@ind1='0'">issue number</xsl:when>
+                                               <xsl:when test="@ind1='1'">matrix number</xsl:when>
+                                               <xsl:when test="@ind1='2'">music plate</xsl:when>
+                                               <xsl:when test="@ind1='3'">music publisher</xsl:when>
+                                               <xsl:when test="@ind1='4'">videorecording identifier</xsl:when>
+                                       </xsl:choose>
+                               </xsl:attribute>
+                               <!--<xsl:call-template name="isInvalid"/>--> <!-- no $z in 028 -->
+                               <xsl:call-template name="subfieldSelect">
+                                       <xsl:with-param name="codes">
+                                               <xsl:choose>
+                                                       <xsl:when test="@ind1='0'">ba</xsl:when>
+                                                       <xsl:otherwise>ab</xsl:otherwise>
+                                               </xsl:choose>
+                                       </xsl:with-param>
+                               </xsl:call-template>
+                       </identifier>
+               </xsl:for-each>
+               <xsl:for-each select="marc:datafield[@tag='037']">
+                       <identifier type="stock number">
+                               <!--<xsl:call-template name="isInvalid"/>--> <!-- no $z in 037 -->
+                               <xsl:call-template name="subfieldSelect">
+                                       <xsl:with-param name="codes">ab</xsl:with-param>
+                               </xsl:call-template>
+                       </identifier>
+               </xsl:for-each>
+               <xsl:for-each select="marc:datafield[@tag='856'][marc:subfield[@code='u']]">
+                       <identifier>
+                               <xsl:attribute name="type">
+                                       <xsl:choose>
+                                               <xsl:when test="starts-with(marc:subfield[@code='u'],'urn:doi') or starts-with(marc:subfield[@code='u'],'doi')">doi</xsl:when>
+                                               <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>
+                                               <xsl:otherwise>uri</xsl:otherwise>
+                                       </xsl:choose>
+                               </xsl:attribute>
+                               <xsl:choose>
+                                       <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') ">
+                                               <xsl:value-of select="concat('hdl:',substring-after(marc:subfield[@code='u'],'http://hdl.loc.gov/'))"></xsl:value-of>
+                                       </xsl:when>
+                                       <xsl:otherwise>
+                                               <xsl:value-of select="marc:subfield[@code='u']"></xsl:value-of>
+                                       </xsl:otherwise>
+                               </xsl:choose>
+                       </identifier>
+                       <xsl:if test="starts-with(marc:subfield[@code='u'],'urn:hdl') or starts-with(marc:subfield[@code='u'],'hdl')">
+                               <identifier type="hdl">
+                                       <xsl:if test="marc:subfield[@code='y' or @code='3' or @code='z']">
+                                               <xsl:attribute name="displayLabel">
+                                                       <xsl:call-template name="subfieldSelect">
+                                                               <xsl:with-param name="codes">y3z</xsl:with-param>
+                                                       </xsl:call-template>
+                                               </xsl:attribute>
+                                       </xsl:if>
+                                       <xsl:value-of select="concat('hdl:',substring-after(marc:subfield[@code='u'],'http://hdl.loc.gov/'))"></xsl:value-of>
+                               </identifier>
+                       </xsl:if>
+               </xsl:for-each>
+               <xsl:for-each select="marc:datafield[@tag=024][@ind1=1]">
+                       <identifier type="upc">
+                               <xsl:call-template name="isInvalid"/>
+                               <xsl:value-of select="marc:subfield[@code='a']"/>
+                       </identifier>
+               </xsl:for-each>
+               <!-- 1/04 fix added $y -->
+               <xsl:for-each select="marc:datafield[@tag=856][marc:subfield[@code='u']]">
+                       <location>
+                               <url>
+                                       <xsl:if test="marc:subfield[@code='y' or @code='3']">
+                                               <xsl:attribute name="displayLabel">
+                                                       <xsl:call-template name="subfieldSelect">
+                                                               <xsl:with-param name="codes">y3</xsl:with-param>
+                                                       </xsl:call-template>
+                                               </xsl:attribute>
+                                       </xsl:if>
+                                       <xsl:if test="marc:subfield[@code='z' ]">
+                                               <xsl:attribute name="note">
+                                                       <xsl:call-template name="subfieldSelect">
+                                                               <xsl:with-param name="codes">z</xsl:with-param>
+                                                       </xsl:call-template>
+                                               </xsl:attribute>
+                                       </xsl:if>
+                                       <xsl:value-of select="marc:subfield[@code='u']"></xsl:value-of>
+
+                               </url>
+                       </location>
+               </xsl:for-each>
+                       
+                       <!-- 3.2 change tmee 856z  -->
+
+               
+               <xsl:for-each select="marc:datafield[@tag=852]">
+                       <location>
+                               <physicalLocation>
+                                       <xsl:call-template name="displayLabel"></xsl:call-template>
+                                       <xsl:call-template name="subfieldSelect">
+                                               <xsl:with-param name="codes">abje</xsl:with-param>
+                                       </xsl:call-template>
+                               </physicalLocation>
+                       </location>
+               </xsl:for-each>
+               <xsl:for-each select="marc:datafield[@tag=506]">
+                       <accessCondition type="restrictionOnAccess">
+                               <xsl:call-template name="subfieldSelect">
+                                       <xsl:with-param name="codes">abcd35</xsl:with-param>
+                               </xsl:call-template>
+                       </accessCondition>
+               </xsl:for-each>
+               <xsl:for-each select="marc:datafield[@tag=540]">
+                       <accessCondition type="useAndReproduction">
+                               <xsl:call-template name="subfieldSelect">
+                                       <xsl:with-param name="codes">abcde35</xsl:with-param>
+                               </xsl:call-template>
+                       </accessCondition>
+               </xsl:for-each>
+               <recordInfo>
+                       <xsl:for-each select="marc:datafield[@tag=040]">
+                               <recordContentSource authority="marcorg">
+                                       <xsl:value-of select="marc:subfield[@code='a']"></xsl:value-of>
+                               </recordContentSource>
+                       </xsl:for-each>
+                       <xsl:for-each select="marc:controlfield[@tag=008]">
+                               <recordCreationDate encoding="marc">
+                                       <xsl:value-of select="substring(.,1,6)"></xsl:value-of>
+                               </recordCreationDate>
+                       </xsl:for-each>
+                       <xsl:for-each select="marc:controlfield[@tag=005]">
+                               <recordChangeDate encoding="iso8601">
+                                       <xsl:value-of select="."></xsl:value-of>
+                               </recordChangeDate>
+                       </xsl:for-each>
+                       <xsl:for-each select="marc:controlfield[@tag=001]">
+                               <recordIdentifier>
+                                       <xsl:if test="../marc:controlfield[@tag=003]">
+                                               <xsl:attribute name="source">
+                                                       <xsl:value-of select="../marc:controlfield[@tag=003]"></xsl:value-of>
+                                               </xsl:attribute>
+                                       </xsl:if>
+                                       <xsl:value-of select="."></xsl:value-of>
+                               </recordIdentifier>
+                       </xsl:for-each>
+                       <xsl:for-each select="marc:datafield[@tag=040]/marc:subfield[@code='b']">
+                               <languageOfCataloging>
+                                       <languageTerm authority="iso639-2b" type="code">
+                                               <xsl:value-of select="."></xsl:value-of>
+                                       </languageTerm>
+                               </languageOfCataloging>
+                       </xsl:for-each>
+               </recordInfo>
+       </xsl:template>
+       <xsl:template name="displayForm">
+               <xsl:for-each select="marc:subfield[@code='c']">
+                       <displayForm>
+                               <xsl:value-of select="."></xsl:value-of>
+                       </displayForm>
+               </xsl:for-each>
+       </xsl:template>
+       <xsl:template name="affiliation">
+               <xsl:for-each select="marc:subfield[@code='u']">
+                       <affiliation>
+                               <xsl:value-of select="."></xsl:value-of>
+                       </affiliation>
+               </xsl:for-each>
+       </xsl:template>
+       <xsl:template name="uri">
+               <xsl:for-each select="marc:subfield[@code='u']">
+                       <xsl:attribute name="xlink:href">
+                               <xsl:value-of select="."></xsl:value-of>
+                       </xsl:attribute>
+               </xsl:for-each>
+               <xsl:for-each select="marc:subfield[@code='0']">
+                       <xsl:choose>
+                               <xsl:when test="contains(text(), ')')">
+                                       <xsl:attribute name="xlink:href">
+                                               <xsl:value-of select="substring-after(text(), ')')"></xsl:value-of>
+                                       </xsl:attribute>
+                               </xsl:when>
+                               <xsl:otherwise>
+                                       <xsl:attribute name="xlink:href">
+                                               <xsl:value-of select="."></xsl:value-of>
+                                       </xsl:attribute>
+                               </xsl:otherwise>
+                       </xsl:choose>
+               </xsl:for-each>
+       </xsl:template>
+       <xsl:template name="role">
+               <xsl:for-each select="marc:subfield[@code='e']">
+                       <role>
+                               <roleTerm type="text">
+                                       <xsl:value-of select="."></xsl:value-of>
+                               </roleTerm>
+                       </role>
+               </xsl:for-each>
+               <xsl:for-each select="marc:subfield[@code='4']">
+                       <role>
+                               <roleTerm authority="marcrelator" type="code">
+                                       <xsl:value-of select="."></xsl:value-of>
+                               </roleTerm>
+                       </role>
+               </xsl:for-each>
+       </xsl:template>
+       <xsl:template name="part">
+               <xsl:variable name="partNumber">
+                       <xsl:call-template name="specialSubfieldSelect">
+                               <xsl:with-param name="axis">n</xsl:with-param>
+                               <xsl:with-param name="anyCodes">n</xsl:with-param>
+                               <xsl:with-param name="afterCodes">fgkdlmor</xsl:with-param>
+                       </xsl:call-template>
+               </xsl:variable>
+               <xsl:variable name="partName">
+                       <xsl:call-template name="specialSubfieldSelect">
+                               <xsl:with-param name="axis">p</xsl:with-param>
+                               <xsl:with-param name="anyCodes">p</xsl:with-param>
+                               <xsl:with-param name="afterCodes">fgkdlmor</xsl:with-param>
+                       </xsl:call-template>
+               </xsl:variable>
+               <xsl:if test="string-length(normalize-space($partNumber))">
+                       <partNumber>
+                               <xsl:call-template name="chopPunctuation">
+                                       <xsl:with-param name="chopString" select="$partNumber"></xsl:with-param>
+                               </xsl:call-template>
+                       </partNumber>
+               </xsl:if>
+               <xsl:if test="string-length(normalize-space($partName))">
+                       <partName>
+                               <xsl:call-template name="chopPunctuation">
+                                       <xsl:with-param name="chopString" select="$partName"></xsl:with-param>
+                               </xsl:call-template>
+                       </partName>
+               </xsl:if>
+       </xsl:template>
+       <xsl:template name="relatedPart">
+               <xsl:if test="@tag=773">
+                       <xsl:for-each select="marc:subfield[@code='g']">
+                               <part>
+                                       <text>
+                                               <xsl:value-of select="."></xsl:value-of>
+                                       </text>
+                               </part>
+                       </xsl:for-each>
+                       <xsl:for-each select="marc:subfield[@code='q']">
+                               <part>
+                                       <xsl:call-template name="parsePart"></xsl:call-template>
+                               </part>
+                       </xsl:for-each>
+               </xsl:if>
+       </xsl:template>
+       <xsl:template name="relatedPartNumName">
+               <xsl:variable name="partNumber">
+                       <xsl:call-template name="specialSubfieldSelect">
+                               <xsl:with-param name="axis">g</xsl:with-param>
+                               <xsl:with-param name="anyCodes">g</xsl:with-param>
+                               <xsl:with-param name="afterCodes">pst</xsl:with-param>
+                       </xsl:call-template>
+               </xsl:variable>
+               <xsl:variable name="partName">
+                       <xsl:call-template name="specialSubfieldSelect">
+                               <xsl:with-param name="axis">p</xsl:with-param>
+                               <xsl:with-param name="anyCodes">p</xsl:with-param>
+                               <xsl:with-param name="afterCodes">fgkdlmor</xsl:with-param>
+                       </xsl:call-template>
+               </xsl:variable>
+               <xsl:if test="string-length(normalize-space($partNumber))">
+                       <partNumber>
+                               <xsl:value-of select="$partNumber"></xsl:value-of>
+                       </partNumber>
+               </xsl:if>
+               <xsl:if test="string-length(normalize-space($partName))">
+                       <partName>
+                               <xsl:value-of select="$partName"></xsl:value-of>
+                       </partName>
+               </xsl:if>
+       </xsl:template>
+       <xsl:template name="relatedName">
+               <xsl:for-each select="marc:subfield[@code='a']">
+                       <name>
+                               <namePart>
+                                       <xsl:value-of select="."></xsl:value-of>
+                               </namePart>
+                       </name>
+               </xsl:for-each>
+       </xsl:template>
+       <xsl:template name="relatedForm">
+               <xsl:for-each select="marc:subfield[@code='h']">
+                       <physicalDescription>
+                               <form>
+                                       <xsl:value-of select="."></xsl:value-of>
+                               </form>
+                       </physicalDescription>
+               </xsl:for-each>
+       </xsl:template>
+       <xsl:template name="relatedExtent">
+               <xsl:for-each select="marc:subfield[@code='h']">
+                       <physicalDescription>
+                               <extent>
+                                       <xsl:value-of select="."></xsl:value-of>
+                               </extent>
+                       </physicalDescription>
+               </xsl:for-each>
+       </xsl:template>
+       <xsl:template name="relatedNote">
+               <xsl:for-each select="marc:subfield[@code='n']">
+                       <note>
+                               <xsl:value-of select="."></xsl:value-of>
+                       </note>
+               </xsl:for-each>
+       </xsl:template>
+       <xsl:template name="relatedSubject">
+               <xsl:for-each select="marc:subfield[@code='j']">
+                       <subject>
+                               <temporal encoding="iso8601">
+                                       <xsl:call-template name="chopPunctuation">
+                                               <xsl:with-param name="chopString" select="."></xsl:with-param>
+                                       </xsl:call-template>
+                               </temporal>
+                       </subject>
+               </xsl:for-each>
+       </xsl:template>
+       <xsl:template name="relatedIdentifierISSN">
+               <xsl:for-each select="marc:subfield[@code='x']">
+                       <identifier type="issn">
+                               <xsl:value-of select="."></xsl:value-of>
+                       </identifier>
+               </xsl:for-each>
+       </xsl:template>
+       <xsl:template name="relatedIdentifierLocal">
+               <xsl:for-each select="marc:subfield[@code='w']">
+                       <identifier type="local">
+                               <xsl:value-of select="."></xsl:value-of>
+                       </identifier>
+               </xsl:for-each>
+       </xsl:template>
+       <xsl:template name="relatedIdentifier">
+               <xsl:for-each select="marc:subfield[@code='o']">
+                       <identifier>
+                               <xsl:value-of select="."></xsl:value-of>
+                       </identifier>
+               </xsl:for-each>
+       </xsl:template>
+       <xsl:template name="relatedItem76X-78X">
+               <xsl:call-template name="displayLabel"></xsl:call-template>
+               <xsl:call-template name="relatedTitle76X-78X"></xsl:call-template>
+               <xsl:call-template name="relatedName"></xsl:call-template>
+               <xsl:call-template name="relatedOriginInfo"></xsl:call-template>
+               <xsl:call-template name="relatedLanguage"></xsl:call-template>
+               <xsl:call-template name="relatedExtent"></xsl:call-template>
+               <xsl:call-template name="relatedNote"></xsl:call-template>
+               <xsl:call-template name="relatedSubject"></xsl:call-template>
+               <xsl:call-template name="relatedIdentifier"></xsl:call-template>
+               <xsl:call-template name="relatedIdentifierISSN"></xsl:call-template>
+               <xsl:call-template name="relatedIdentifierLocal"></xsl:call-template>
+               <xsl:call-template name="relatedPart"></xsl:call-template>
+       </xsl:template>
+       <xsl:template name="subjectGeographicZ">
+               <geographic>
+                       <xsl:call-template name="chopPunctuation">
+                               <xsl:with-param name="chopString" select="."></xsl:with-param>
+                       </xsl:call-template>
+               </geographic>
+       </xsl:template>
+       <xsl:template name="subjectTemporalY">
+               <temporal>
+                       <xsl:call-template name="chopPunctuation">
+                               <xsl:with-param name="chopString" select="."></xsl:with-param>
+                       </xsl:call-template>
+               </temporal>
+       </xsl:template>
+       <xsl:template name="subjectTopic">
+               <topic>
+                       <xsl:call-template name="chopPunctuation">
+                               <xsl:with-param name="chopString" select="."></xsl:with-param>
+                       </xsl:call-template>
+               </topic>
+       </xsl:template> 
+       <!-- 3.2 change tmee 6xx $v genre -->
+       <xsl:template name="subjectGenre">
+               <genre>
+                       <xsl:call-template name="chopPunctuation">
+                               <xsl:with-param name="chopString" select="."></xsl:with-param>
+                       </xsl:call-template>
+               </genre>
+       </xsl:template>
+       
+       <xsl:template name="nameABCDN">
+               <xsl:for-each select="marc:subfield[@code='a']">
+                       <namePart>
+                               <xsl:call-template name="chopPunctuation">
+                                       <xsl:with-param name="chopString" select="."></xsl:with-param>
+                               </xsl:call-template>
+                       </namePart>
+               </xsl:for-each>
+               <xsl:for-each select="marc:subfield[@code='b']">
+                       <namePart>
+                               <xsl:value-of select="."></xsl:value-of>
+                       </namePart>
+               </xsl:for-each>
+               <xsl:if test="marc:subfield[@code='c'] or marc:subfield[@code='d'] or marc:subfield[@code='n']">
+                       <namePart>
+                               <xsl:call-template name="subfieldSelect">
+                                       <xsl:with-param name="codes">cdn</xsl:with-param>
+                               </xsl:call-template>
+                       </namePart>
+               </xsl:if>
+       </xsl:template>
+       <xsl:template name="nameABCDQ">
+               <namePart>
+                       <xsl:call-template name="chopPunctuation">
+                               <xsl:with-param name="chopString">
+                                       <xsl:call-template name="subfieldSelect">
+                                               <xsl:with-param name="codes">aq</xsl:with-param>
+                                       </xsl:call-template>
+                               </xsl:with-param>
+                               <xsl:with-param name="punctuation">
+                                       <xsl:text>:,;/ </xsl:text>
+                               </xsl:with-param>
+                       </xsl:call-template>
+               </namePart>
+               <xsl:call-template name="termsOfAddress"></xsl:call-template>
+               <xsl:call-template name="nameDate"></xsl:call-template>
+       </xsl:template>
+       <xsl:template name="nameACDEQ">
+               <namePart>
+                       <xsl:call-template name="subfieldSelect">
+                               <xsl:with-param name="codes">acdeq</xsl:with-param>
+                       </xsl:call-template>
+               </namePart>
+       </xsl:template>
+       <xsl:template name="constituentOrRelatedType">
+               <xsl:if test="@ind2=2">
+                       <xsl:attribute name="type">constituent</xsl:attribute>
+               </xsl:if>
+       </xsl:template>
+       <xsl:template name="relatedTitle">
+               <xsl:for-each select="marc:subfield[@code='t']">
+                       <titleInfo>
+                               <title>
+                                       <xsl:call-template name="chopPunctuation">
+                                               <xsl:with-param name="chopString">
+                                                       <xsl:value-of select="."></xsl:value-of>
+                                               </xsl:with-param>
+                                       </xsl:call-template>
+                               </title>
+                       </titleInfo>
+               </xsl:for-each>
+       </xsl:template>
+       <xsl:template name="relatedTitle76X-78X">
+               <xsl:for-each select="marc:subfield[@code='t']">
+                       <titleInfo>
+                               <title>
+                                       <xsl:call-template name="chopPunctuation">
+                                               <xsl:with-param name="chopString">
+                                                       <xsl:value-of select="."></xsl:value-of>
+                                               </xsl:with-param>
+                                       </xsl:call-template>
+                               </title>
+                               <xsl:if test="marc:datafield[@tag!=773]and marc:subfield[@code='g']">
+                                       <xsl:call-template name="relatedPartNumName"></xsl:call-template>
+                               </xsl:if>
+                       </titleInfo>
+               </xsl:for-each>
+               <xsl:for-each select="marc:subfield[@code='p']">
+                       <titleInfo type="abbreviated">
+                               <title>
+                                       <xsl:call-template name="chopPunctuation">
+                                               <xsl:with-param name="chopString">
+                                                       <xsl:value-of select="."></xsl:value-of>
+                                               </xsl:with-param>
+                                       </xsl:call-template>
+                               </title>
+                               <xsl:if test="marc:datafield[@tag!=773]and marc:subfield[@code='g']">
+                                       <xsl:call-template name="relatedPartNumName"></xsl:call-template>
+                               </xsl:if>
+                       </titleInfo>
+               </xsl:for-each>
+               <xsl:for-each select="marc:subfield[@code='s']">
+                       <titleInfo type="uniform">
+                               <title>
+                                       <xsl:call-template name="chopPunctuation">
+                                               <xsl:with-param name="chopString">
+                                                       <xsl:value-of select="."></xsl:value-of>
+                                               </xsl:with-param>
+                                       </xsl:call-template>
+                               </title>
+                               <xsl:if test="marc:datafield[@tag!=773]and marc:subfield[@code='g']">
+                                       <xsl:call-template name="relatedPartNumName"></xsl:call-template>
+                               </xsl:if>
+                       </titleInfo>
+               </xsl:for-each>
+       </xsl:template>
+       <xsl:template name="relatedOriginInfo">
+               <xsl:if test="marc:subfield[@code='b' or @code='d'] or marc:subfield[@code='f']">
+                       <originInfo>
+                               <xsl:if test="@tag=775">
+                                       <xsl:for-each select="marc:subfield[@code='f']">
+                                               <place>
+                                                       <placeTerm>
+                                                               <xsl:attribute name="type">code</xsl:attribute>
+                                                               <xsl:attribute name="authority">marcgac</xsl:attribute>
+                                                               <xsl:value-of select="."></xsl:value-of>
+                                                       </placeTerm>
+                                               </place>
+                                       </xsl:for-each>
+                               </xsl:if>
+                               <xsl:for-each select="marc:subfield[@code='d']">
+                                       <publisher>
+                                               <xsl:value-of select="."></xsl:value-of>
+                                       </publisher>
+                               </xsl:for-each>
+                               <xsl:for-each select="marc:subfield[@code='b']">
+                                       <edition>
+                                               <xsl:value-of select="."></xsl:value-of>
+                                       </edition>
+                               </xsl:for-each>
+                       </originInfo>
+               </xsl:if>
+       </xsl:template>
+       <xsl:template name="relatedLanguage">
+               <xsl:for-each select="marc:subfield[@code='e']">
+                       <xsl:call-template name="getLanguage">
+                               <xsl:with-param name="langString">
+                                       <xsl:value-of select="."></xsl:value-of>
+                               </xsl:with-param>
+                       </xsl:call-template>
+               </xsl:for-each>
+       </xsl:template>
+       <xsl:template name="nameDate">
+               <xsl:for-each select="marc:subfield[@code='d']">
+                       <namePart type="date">
+                               <xsl:call-template name="chopPunctuation">
+                                       <xsl:with-param name="chopString" select="."></xsl:with-param>
+                               </xsl:call-template>
+                       </namePart>
+               </xsl:for-each>
+       </xsl:template>
+       <xsl:template name="subjectAuthority">
+               <xsl:if test="@ind2!=4">
+                       <xsl:if test="@ind2!=' '">
+                               <xsl:if test="@ind2!=8">
+                                       <xsl:if test="@ind2!=9">
+                                               <xsl:attribute name="authority">
+                                                       <xsl:choose>
+                                                               <xsl:when test="@ind2=0">lcsh</xsl:when>
+                                                               <xsl:when test="@ind2=1">lcshac</xsl:when>
+                                                               <xsl:when test="@ind2=2">mesh</xsl:when>
+                                                               <!-- 1/04 fix -->
+                                                               <xsl:when test="@ind2=3">nal</xsl:when>
+                                                               <xsl:when test="@ind2=5">csh</xsl:when>
+                                                               <xsl:when test="@ind2=6">rvm</xsl:when>
+                                                               <xsl:when test="@ind2=7">
+                                                                       <xsl:value-of select="marc:subfield[@code='2']"></xsl:value-of>
+                                                               </xsl:when>
+                                                       </xsl:choose>
+                                               </xsl:attribute>
+                                       </xsl:if>
+                               </xsl:if>
+                       </xsl:if>
+               </xsl:if>
+       </xsl:template>
+       <xsl:template name="subjectAnyOrder">
+               <xsl:for-each select="marc:subfield[@code='v' or @code='x' or @code='y' or @code='z']">
+                       <xsl:choose>
+                               <xsl:when test="@code='v'">
+                                       <xsl:call-template name="subjectGenre"></xsl:call-template>
+                               </xsl:when>
+                               <xsl:when test="@code='x'">
+                                       <xsl:call-template name="subjectTopic"></xsl:call-template>
+                               </xsl:when>
+                               <xsl:when test="@code='y'">
+                                       <xsl:call-template name="subjectTemporalY"></xsl:call-template>
+                               </xsl:when>
+                               <xsl:when test="@code='z'">
+                                       <xsl:call-template name="subjectGeographicZ"></xsl:call-template>
+                               </xsl:when>
+                       </xsl:choose>
+               </xsl:for-each>
+       </xsl:template>
+       <xsl:template name="specialSubfieldSelect">
+               <xsl:param name="anyCodes"></xsl:param>
+               <xsl:param name="axis"></xsl:param>
+               <xsl:param name="beforeCodes"></xsl:param>
+               <xsl:param name="afterCodes"></xsl:param>
+               <xsl:variable name="str">
+                       <xsl:for-each select="marc:subfield">
+                               <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])">
+                                       <xsl:value-of select="text()"></xsl:value-of>
+                                       <xsl:text> </xsl:text>
+                               </xsl:if>
+                       </xsl:for-each>
+               </xsl:variable>
+               <xsl:value-of select="substring($str,1,string-length($str)-1)"></xsl:value-of>
+       </xsl:template>
+       
+       <!-- 3.2 change tmee 6xx $v genre -->
+       <xsl:template match="marc:datafield[@tag=600]">
+               <subject>
+                       <xsl:call-template name="subjectAuthority"></xsl:call-template>
+                       <name type="personal">
+                               <xsl:call-template name="uri" />
+                               <xsl:call-template name="termsOfAddress"></xsl:call-template>
+                               <namePart>
+                                       <xsl:call-template name="chopPunctuation">
+                                               <xsl:with-param name="chopString">
+                                                       <xsl:call-template name="subfieldSelect">
+                                                               <xsl:with-param name="codes">aq</xsl:with-param>
+                                                       </xsl:call-template>
+                                               </xsl:with-param>
+                                       </xsl:call-template>
+                               </namePart>
+                               <xsl:call-template name="nameDate"></xsl:call-template>
+                               <xsl:call-template name="affiliation"></xsl:call-template>
+                               <xsl:call-template name="role"></xsl:call-template>
+                       </name>
+                       <xsl:call-template name="subjectAnyOrder"></xsl:call-template>
+               </subject>
+       </xsl:template>
+       <xsl:template match="marc:datafield[@tag=610]">
+               <subject>
+                       <xsl:call-template name="subjectAuthority"></xsl:call-template>
+                       <name type="corporate">
+                               <xsl:call-template name="uri" />
+                               <xsl:for-each select="marc:subfield[@code='a']">
+                                       <namePart>
+                                               <xsl:value-of select="."></xsl:value-of>
+                                       </namePart>
+                               </xsl:for-each>
+                               <xsl:for-each select="marc:subfield[@code='b']">
+                                       <namePart>
+                                               <xsl:value-of select="."></xsl:value-of>
+                                       </namePart>
+                               </xsl:for-each>
+                               <xsl:if test="marc:subfield[@code='c' or @code='d' or @code='n' or @code='p']">
+                                       <namePart>
+                                               <xsl:call-template name="subfieldSelect">
+                                                       <xsl:with-param name="codes">cdnp</xsl:with-param>
+                                               </xsl:call-template>
+                                       </namePart>
+                               </xsl:if>
+                               <xsl:call-template name="role"></xsl:call-template>
+                       </name>
+                       <xsl:call-template name="subjectAnyOrder"></xsl:call-template>
+               </subject>
+       </xsl:template>
+       <xsl:template match="marc:datafield[@tag=611]">
+               <subject>
+                       <xsl:call-template name="subjectAuthority"></xsl:call-template>
+                       <name type="conference">
+                               <xsl:call-template name="uri" />
+                               <namePart>
+                                       <xsl:call-template name="subfieldSelect">
+                                               <xsl:with-param name="codes">abcdeqnp</xsl:with-param>
+                                       </xsl:call-template>
+                               </namePart>
+                               <xsl:for-each select="marc:subfield[@code='4']">
+                                       <role>
+                                               <roleTerm authority="marcrelator" type="code">
+                                                       <xsl:value-of select="."></xsl:value-of>
+                                               </roleTerm>
+                                       </role>
+                               </xsl:for-each>
+                       </name>
+                       <xsl:call-template name="subjectAnyOrder"></xsl:call-template>
+               </subject>
+       </xsl:template>
+       <xsl:template match="marc:datafield[@tag=630]">
+               <subject>
+                       <xsl:call-template name="subjectAuthority"></xsl:call-template>
+                       <xsl:variable name="titleChop">
+                               <xsl:call-template name="chopPunctuation">
+                                       <xsl:with-param name="chopString">
+                                               <xsl:call-template name="subfieldSelect">
+                                                       <xsl:with-param name="codes">adfhklor</xsl:with-param>
+                                               </xsl:call-template>
+                                       </xsl:with-param>
+                               </xsl:call-template>
+                       </xsl:variable>
+                       <titleInfo>
+                               <title>
+                                       <xsl:value-of select="$titleChop" />
+                               </title>
+                               <xsl:call-template name="part"></xsl:call-template