<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:marc="http://www.loc.gov/MARC21/slim" exclude-result-prefixes="xs" version="2.0">
    <xsl:output method="xml" encoding="UTF-8" indent="yes"/>
    <xsl:strip-space elements="*"/>
    
    <!-- Set up a Key to extract call number from an external XML file -->
    <xsl:key name="callNumber-lookup" match="itemNumber" use="text()"/>
    
    <xsl:template match="/">
        <marc:collection>
            <xsl:apply-templates/>
        </marc:collection>
    </xsl:template>
    
    <xsl:template match="marc:record">
        <!-- Invoke "printData" template to copy data that are specific to print format -->
        <xsl:call-template name="printData"/>
        <!-- Invoke "microformData" template to copy data that are specific to microfiche format -->
        <xsl:call-template name="microformData"/>
    </xsl:template>
    
    <!-- Template to copy data applicable to print format -->
    <xsl:template name="printData">
        <marc:record>
            <!-- Copy Print 001 -->
            <marc:controlfield tag="001">
                <xsl:copy-of select="normalize-space(substring-before(marc:controlfield[@tag='001'][matches(.,'(paper)','i')],'('))"/>
            </marc:controlfield>
            <!-- Copy 008 & 099 -->
            <xsl:copy-of select="marc:controlfield[@tag='008']|marc:datafield[@tag='099']"/>
            <!-- Insert 049 -->
            <marc:datafield tag="049" ind1=" " ind2=" ">
                <marc:subfield code="a">EEMT</marc:subfield>
            </marc:datafield>
            <!-- Remove GMD from 245 -->
            <xsl:for-each select="marc:datafield[@tag='245']">
                <xsl:variable name="tag" select="@tag"/>
                <xsl:variable name="ind1" select="@ind1"/>
                <xsl:variable name="ind2" select="@ind2"/>
                <xsl:element name="marc:datafield">
                    <xsl:attribute name="tag"><xsl:value-of select="$tag"/></xsl:attribute>
                    <xsl:attribute name="ind1"><xsl:value-of select="$ind1"/></xsl:attribute>
                    <xsl:attribute name="ind2"><xsl:value-of select="$ind2"/></xsl:attribute>
                    <xsl:element name="marc:subfield">
                        <xsl:attribute name="code">a</xsl:attribute>
                        <xsl:value-of select="normalize-space(concat(marc:subfield[@code='a'],substring-after(marc:subfield[@code='h'],']')))"/>
                    </xsl:element>
                    <xsl:copy-of select="marc:subfield[@code!='a' and @code!='h']"/>
                </xsl:element>
            </xsl:for-each>
            <!-- Insert 949 overlay command (Innovative Millennium specific) -->
            <marc:datafield tag="949" ind1=" " ind2=" ">
                <marc:subfield code="a">
                    <xsl:text>*ov=</xsl:text><xsl:value-of select="marc:datafield[@tag='907']/marc:subfield[@code='a']"/><xsl:text>;</xsl:text>
                </marc:subfield>
            </marc:datafield>
            <!-- Inovke "commonData" template to copy data that are applicable to both print and mircofiche formats -->
            <xsl:call-template name="commonData"/>
        </marc:record>
    </xsl:template>

    <!-- Template to copy data applicable to microfiche format -->
    <xsl:template name="microformData">
        <marc:record>
            <!-- Copy microfiche 001 -->
            <marc:controlfield tag="001">
                <xsl:copy-of select="normalize-space(substring-before(marc:controlfield[@tag='001'][matches(.,'(microfiche)','i')],'('))"/>
            </marc:controlfield>
            <!-- Change Form byte to "b" in 008 -->
            <xsl:element name="marc:controlfield">
                <xsl:attribute name="tag">008</xsl:attribute>
                <xsl:value-of select="substring(marc:controlfield[@tag='008'],1,23)"/>
                <xsl:text>b</xsl:text>
                <xsl:value-of select="substring(marc:controlfield[@tag='008'],25)"/>
            </xsl:element>
            <!-- Insert 049 -->
            <marc:datafield tag="049" ind1=" " ind2=" ">
                <marc:subfield code="a">EEM#</marc:subfield>
            </marc:datafield>
            <!-- Key in 099 from "mergedMicroformCallNumber.xml" (2nd XML file) -->
            <xsl:for-each select="marc:datafield[@tag='952']/marc:subfield[@code='a']">
                <xsl:variable name="itemNumber"><xsl:value-of select="text()"/></xsl:variable>
                <xsl:for-each select="document('mergedMicroformCallNumber.xml')">
                    <xsl:for-each select="key('callNumber-lookup',$itemNumber)">
                        <xsl:variable name="callNumber" select="following-sibling::callNumber"/>
                        <xsl:choose>
                            <xsl:when test="matches($callNumber,'fiche','i')">
                                <marc:datafield tag="099" ind1=" " ind2=" ">
                                    <marc:subfield code="a"><xsl:value-of select="$callNumber"/></marc:subfield>
                                </marc:datafield>
                            </xsl:when>
                        </xsl:choose>
                    </xsl:for-each>
                </xsl:for-each>
            </xsl:for-each>
            <!-- Delete the word 'paper" from GMD in 245 -->
            <xsl:for-each select="marc:datafield[@tag='245']">
                <xsl:variable name="tag" select="@tag"/>
                <xsl:variable name="ind1" select="@ind1"/>
                <xsl:variable name="ind2" select="@ind2"/>
                <xsl:element name="marc:datafield">
                    <xsl:attribute name="tag"><xsl:value-of select="$tag"/></xsl:attribute>
                    <xsl:attribute name="ind1"><xsl:value-of select="$ind1"/></xsl:attribute>
                    <xsl:attribute name="ind2"><xsl:value-of select="$ind2"/></xsl:attribute>
                    <xsl:copy-of select="marc:subfield[@code='a']"/>
                    <xsl:element name="marc:subfield">
                        <xsl:attribute name="code">h</xsl:attribute>
                        <xsl:value-of select="replace(replace(replace(marc:subfield[@code='h'],'paper','','i'),', ',''),',','')"/>
                    </xsl:element>
                    <xsl:copy-of select="marc:subfield[@code!='a' and @code!='h']"/>
                </xsl:element>
            </xsl:for-each>
            <!-- Copy 007 and 533 -->
            <xsl:copy-of select="marc:controlfield[@tag='007']|marc:datafield[@tag='533']"/>
            <!-- Inovke "commonData" template to copy data that are applicable to both print and mircofiche formats -->
            <xsl:call-template name="commonData"/>
        </marc:record>
    </xsl:template>
    
    <!-- Template to copy data applicable to both print and microfiche formats -->
    <xsl:template name="commonData">
        <!-- Copy data from all MARC fields except 001, 007, 008, 049, 099, 245, 533, 900-999 -->
        <xsl:copy-of select="*[not(self::marc:controlfield[@tag='001'])][not(self::marc:controlfield[@tag='007'])][not(self::marc:controlfield[@tag='008'])][not(self::marc:datafield[@tag='049'])][not(self::marc:datafield[@tag='099'])][not(self::marc:datafield[@tag='245'])][not(self::marc:datafield[@tag='533'])][not(self::marc:datafield['900' &lt;= @tag and @tag &lt;= '999'])]"/>
    </xsl:template>
    
</xsl:stylesheet>