                                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:attribute>
                                        </xsl:if>
                                        <xsl:value-of select="text()"/>
                                </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: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']"/>
                        </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']"/>
                        </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:attribute>
                                <xsl:value-of select="marc:subfield[@code='a']"/>
                        </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: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">
                                <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"/>
                                </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"/>
                                </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 name="relatedName"/>
                                <xsl:if test="marc:subfield[@code='b' or @code='c']">
                                        <originInfo>
                                                <xsl:for-each select="marc:subfield[@code='c']">
                                                        <publisher>
                                                                <xsl:value-of select="."/>
                                                        </publisher>
                                                </xsl:for-each>
                                                <xsl:for-each select="marc:subfield[@code='b']">
                                                        <edition>
                                                                <xsl:value-of select="."/>
                                                        </edition>
                                                </xsl:for-each>
                                        </originInfo>
                                </xsl:if>
                                <xsl:call-template name="relatedIdentifierISSN"/>
                                <xsl:for-each select="marc:subfield[@code='z']">
                                        <identifier type="isbn">
                                                <xsl:value-of select="."/>
                                        </identifier>
                                </xsl:for-each>
                                <xsl:call-template name="relatedNote"/>
                        </relatedItem>
                </xsl:for-each>
                <xsl:for-each select="marc:datafield[@tag=700][marc:subfield[@code='t']]">
                        <relatedItem>
                                <xsl:call-template name="constituentOrRelatedType"/>
                                <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"/>
                                </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 name="nameDate"/>
                                        <xsl:call-template name="role"/>
                                </name>
                                <xsl:call-template name="relatedForm"/>
                                <xsl:call-template name="relatedIdentifierISSN"/>
                        </relatedItem>
                </xsl:for-each>
                <xsl:for-each select="marc:datafield[@tag=710][marc:subfield[@code='t']]">
                        <relatedItem>
                                <xsl:call-template name="constituentOrRelatedType"/>
                                <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"/>
                                </titleInfo>
                                <name type="corporate">
                                        <xsl:for-each select="marc:subfield[@code='a']">
                                                <namePart>
                                                        <xsl:value-of select="."/>
                                                </namePart>
                                        </xsl:for-each>
                                        <xsl:for-each select="marc:subfield[@code='b']">
                                                <namePart>
                                                        <xsl:value-of select="."/>
                                                </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"/>
                                                </namePart>
                                        </xsl:if>
                                        <xsl:call-template name="role"/>
                                </name>
                                <xsl:call-template name="relatedForm"/>
                                <xsl:call-template name="relatedIdentifierISSN"/>
                        </relatedItem>
                </xsl:for-each>
                <xsl:for-each select="marc:datafield[@tag=711][marc:subfield[@code='t']]">
                        <relatedItem>
                                <xsl:call-template name="constituentOrRelatedType"/>
                                <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>
                                </name>
                                <xsl:call-template name="relatedForm"/>
                                <xsl:call-template name="relatedIdentifierISSN"/>
                        </relatedItem>
                </xsl:for-each>
                <xsl:for-each select="marc:datafield[@tag=730][@ind2=2]">
                        <relatedItem>
                                <xsl:call-template name="constituentOrRelatedType"/>
                                <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"/>
                                </titleInfo>
                                <xsl:call-template name="relatedForm"/>
                                <xsl:call-template name="relatedIdentifierISSN"/>
                        </relatedItem>
                </xsl:for-each>
                <xsl:for-each select="marc:datafield[@tag=740][@ind2=2]">
                        <relatedItem>
                                <xsl:call-template name="constituentOrRelatedType"/>
                                <titleInfo>
                                        <title>
                                                <xsl:call-template name="chopPunctuation">
                                                        <xsl:with-param name="chopString">
                                                                <xsl:value-of select="marc:subfield[@code='a']"/>
                                                        </xsl:with-param>
                                                </xsl:call-template>
                                        </title>
                                        <xsl:call-template name="part"/>
                                </titleInfo>
                                <xsl:call-template name="relatedForm"/>
                        </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"/>
                        </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"/>
                        </relatedItem>
                </xsl:for-each>
                <xsl:for-each select="marc:datafield[@tag=775]">
                        <relatedItem type="otherVersion">
                                <xsl:call-template name="relatedItem76X-78X"/>
                        </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"/>
                        </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"/>
                        </relatedItem>
                </xsl:for-each>
                <xsl:for-each select="marc:datafield[@tag=776]">
                        <relatedItem type="otherFormat">
                                <xsl:call-template name="relatedItem76X-78X"/>
                        </relatedItem>
                </xsl:for-each>
                <xsl:for-each select="marc:datafield[@tag=780]">
                        <relatedItem type="preceding">
                                <xsl:call-template name="relatedItem76X-78X"/>
                        </relatedItem>
                </xsl:for-each>
                <xsl:for-each select="marc:datafield[@tag=785]">
                        <relatedItem type="succeeding">
                                <xsl:call-template name="relatedItem76X-78X"/>
                        </relatedItem>
                </xsl:for-each>
                <xsl:for-each select="marc:datafield[@tag=786]">
                        <relatedItem type="original">
                                <xsl:call-template name="relatedItem76X-78X"/>
                        </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"/>
                                </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 name="nameDate"/>
                                        <xsl:call-template name="role"/>
                                </name>
                                <xsl:call-template name="relatedForm"/>
                        </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"/>
                                </titleInfo>
                                <name type="corporate">
                                        <xsl:for-each select="marc:subfield[@code='a']">
                                                <namePart>
                                                        <xsl:value-of select="."/>
                                                </namePart>
                                        </xsl:for-each>
                                        <xsl:for-each select="marc:subfield[@code='b']">
                                                <namePart>
                                                        <xsl:value-of select="."/>
                                                </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"/>
                                </name>
                                <xsl:call-template name="relatedForm"/>
                        </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">
                                <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"/>
                                </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:if test="marc:subfield[@code='a']">
                                <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:if>
                        <xsl:if test="marc:subfield[@code='l']">
                                <xsl:call-template name="isInvalid">
                                        <xsl:with-param name="type">issn-l</xsl:with-param>
                                </xsl:call-template>
                                <identifier type="issn-l">
                                        <xsl:value-of select="marc:subfield[@code='l']"/>
                                </identifier>
                        </xsl:if>
                </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:when>
                                        <xsl:otherwise>
                                                <xsl:value-of select="marc:subfield[@code='u']"/>
                                        </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/'))"
                                        />
                                </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 -->

                <!-- 1.21  tmee-->
                <xsl:for-each select="marc:datafield[@tag=856][@ind2=1][marc:subfield[@code='u']]">
                        <relatedItem type="otherVersion">
                                <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']"/>
                                        </url>
                                </location>
                        </relatedItem>
                </xsl:for-each>
                <xsl:for-each select="marc:datafield[@tag=856][@ind2=2][marc:subfield[@code='u']]">
                        <relatedItem>
                                <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']"/>
                                        </url>
                                </location>
                        </relatedItem>
                </xsl:for-each>

                <!-- 3.2 change tmee 856z  -->

                <!-- 1.24  tmee  -->
                <xsl:for-each select="marc:datafield[@tag=852]">
                        <location>
                                <xsl:if test="marc:subfield[@code='a' or @code='b' or @code='e']">
                                        <physicalLocation>
                                                <xsl:call-template name="subfieldSelect">
                                                        <xsl:with-param name="codes">abe</xsl:with-param>
                                                </xsl:call-template>
                                        </physicalLocation>
                                </xsl:if>

                                <xsl:if test="marc:subfield[@code='u']">
                                        <physicalLocation>
                                                <xsl:call-template name="uri"/>
                                                <xsl:call-template name="subfieldSelect">
                                                        <xsl:with-param name="codes">u</xsl:with-param>
                                                </xsl:call-template>
                                        </physicalLocation>
                                </xsl:if>

                                <xsl:if
                                        test="marc:subfield[@code='h' or @code='i' or @code='j' or @code='k' or @code='l' or @code='m' or @code='t']">
                                        <shelfLocation>
                                                <xsl:call-template name="subfieldSelect">
                                                        <xsl:with-param name="codes">hijklmt</xsl:with-param>
                                                </xsl:call-template>
                                        </shelfLocation>
                                </xsl:if>
                        </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>
                        <!-- 1.25  tmee-->


                        <xsl:for-each select="marc:leader[substring($leader,19,1)='a']">
                                <descriptionStandard>aacr2</descriptionStandard>
                        </xsl:for-each>

                        <xsl:for-each select="marc:datafield[@tag=040]">
                                <xsl:if test="marc:subfield[@code='e']">
                                        <descriptionStandard>
                                                <xsl:value-of select="marc:subfield[@code='e']"/>
                                        </descriptionStandard>
                                </xsl:if>
                                <recordContentSource authority="marcorg">
                                        <xsl:value-of select="marc:subfield[@code='a']"/>
                                </recordContentSource>
                        </xsl:for-each>
                        <xsl:for-each select="marc:controlfield[@tag=008]">
                                <recordCreationDate encoding="marc">
                                        <xsl:value-of select="substring(.,1,6)"/>
                                </recordCreationDate>
                        </xsl:for-each>

                        <xsl:for-each select="marc:controlfield[@tag=005]">
                                <recordChangeDate encoding="iso8601">
                                        <xsl:value-of select="."/>
                                </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:attribute>
                                        </xsl:if>
                                        <xsl:value-of select="."/>
                                </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="."/>
                                        </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="."/>
                        </displayForm>
                </xsl:for-each>
        </xsl:template>
        <xsl:template name="affiliation">
                <xsl:for-each select="marc:subfield[@code='u']">
                        <affiliation>
                                <xsl:value-of select="."/>
                        </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: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="."/>
                                </roleTerm>
                        </role>
                </xsl:for-each>
                <xsl:for-each select="marc:subfield[@code='4']">
                        <role>
                                <roleTerm authority="marcrelator" type="code">
                                        <xsl:value-of select="."/>
                                </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: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: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="."/>
                                        </text>
                                </part>
                        </xsl:for-each>
                        <xsl:for-each select="marc:subfield[@code='q']">
                                <part>
                                        <xsl:call-template name="parsePart"/>
                                </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"/>
                        </partNumber>
                </xsl:if>
                <xsl:if test="string-length(normalize-space($partName))">
                        <partName>
                                <xsl:value-of select="$partName"/>
                        </partName>
                </xsl:if>
        </xsl:template>
        <xsl:template name="relatedName">
                <xsl:for-each select="marc:subfield[@code='a']">
                        <name>
                                <namePart>
                                        <xsl:value-of select="."/>
                                </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="."/>
                                </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="."/>
                                </extent>
                        </physicalDescription>
                </xsl:for-each>
        </xsl:template>
        <xsl:template name="relatedNote">
                <xsl:for-each select="marc:subfield[@code='n']">
                        <note>
                                <xsl:value-of select="."/>
                        </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: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="."/>
                        </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="."/>
                        </identifier>
                </xsl:for-each>
        </xsl:template>
        <xsl:template name="relatedIdentifier">
                <xsl:for-each select="marc:subfield[@code='o']">
                        <identifier>
                                <xsl:value-of select="."/>
                        </identifier>
                </xsl:for-each>
        </xsl:template>
        <xsl:template name="relatedItem76X-78X">
                <xsl:call-template name="displayLabel"/>
                <xsl:call-template name="relatedTitle76X-78X"/>
                <xsl:call-template name="relatedName"/>
                <xsl:call-template name="relatedOriginInfo"/>
                <xsl:call-template name="relatedLanguage"/>
                <xsl:call-template name="relatedExtent"/>
                <xsl:call-template name="relatedNote"/>
                <xsl:call-template name="relatedSubject"/>
                <xsl:call-template name="relatedIdentifier"/>
                <xsl:call-template name="relatedIdentifierISSN"/>
                <xsl:call-template name="relatedIdentifierLocal"/>
                <xsl:call-template name="relatedPart"/>
        </xsl:template>
        <xsl:template name="subjectGeographicZ">
                <geographic>
                        <xsl:call-template name="chopPunctuation">
                                <xsl:with-param name="chopString" select="."/>
                        </xsl:call-template>
                </geographic>
        </xsl:template>
        <xsl:template name="subjectTemporalY">
                <temporal>
                        <xsl:call-template name="chopPunctuation">
                                <xsl:with-param name="chopString" select="."/>
                        </xsl:call-template>
                </temporal>
        </xsl:template>
        <xsl:template name="subjectTopic">
                <topic>
                        <xsl:call-template name="chopPunctuation">
                                <xsl:with-param name="chopString" select="."/>
                        </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: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:call-template>
                        </namePart>
                </xsl:for-each>
                <xsl:for-each select="marc:subfield[@code='b']">
                        <namePart>
                                <xsl:value-of select="."/>
                        </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 name="nameDate"/>
        </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: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:with-param>
                                        </xsl:call-template>
                                </title>
                                <xsl:if test="marc:datafield[@tag!=773]and marc:subfield[@code='g']">
                                        <xsl:call-template name="relatedPartNumName"/>
                                </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:with-param>
                                        </xsl:call-template>
                                </title>
                                <xsl:if test="marc:datafield[@tag!=773]and marc:subfield[@code='g']">
                                        <xsl:call-template name="relatedPartNumName"/>
                                </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:with-param>
                                        </xsl:call-template>
                                </title>
                                <xsl:if test="marc:datafield[@tag!=773]and marc:subfield[@code='g']">
                                        <xsl:call-template name="relatedPartNumName"/>
                                </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="."/>
                                                        </placeTerm>
                                                </place>
                                        </xsl:for-each>
                                </xsl:if>
                                <xsl:for-each select="marc:subfield[@code='d']">
                                        <publisher>
                                                <xsl:value-of select="."/>
                                        </publisher>
                                </xsl:for-each>
                                <xsl:for-each select="marc:subfield[@code='b']">
                                        <edition>
                                                <xsl:value-of select="."/>
                                        </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: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: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: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:when>
                                <xsl:when test="@code='x'">
                                        <xsl:call-template name="subjectTopic"/>
                                </xsl:when>
                                <xsl:when test="@code='y'">
                                        <xsl:call-template name="subjectTemporalY"/>
                                </xsl:when>
                                <xsl:when test="@code='z'">
                                        <xsl:call-template name="subjectGeographicZ"/>
                                </xsl:when>
                        </xsl:choose>
                </xsl:for-each>
        </xsl:template>
        <xsl:template name="specialSubfieldSelect">
                <xsl:param name="anyCodes"/>
                <xsl:param name="axis"/>
                <xsl:param name="beforeCodes"/>
                <xsl:param name="afterCodes"/>
                <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:text> </xsl:text>
                                </xsl:if>
                        </xsl:for-each>
                </xsl:variable>
                <xsl:value-of select="substring($str,1,string-length($str)-1)"/>
        </xsl:template>

        <!-- 3.2 change tmee 6xx $v genre -->
        <xsl:template match="marc:datafield[@tag=600]">
                <subject>
                        <xsl:call-template name="subjectAuthority"/>
                        <name type="personal">
                                <xsl:call-template name="termsOfAddress"/>
                                <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 name="affiliation"/>
                                <xsl:call-template name="role"/>
                        </name>
                        <xsl:call-template name="subjectAnyOrder"/>
                </subject>
        </xsl:template>
        <xsl:template match="marc:datafield[@tag=610]">
                <subject>
                        <xsl:call-template name="subjectAuthority"/>
                        <name type="corporate">
                                <xsl:for-each select="marc:subfield[@code='a']">
                                        <namePart>
                                                <xsl:value-of select="."/>
                                        </namePart>
                                </xsl:for-each>
                                <xsl:for-each select="marc:subfield[@code='b']">
                                        <namePart>
                                                <xsl:value-of select="."/>
                                        </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"/>
                        </name>
                        <xsl:call-template name="subjectAnyOrder"/>
                </subject>
        </xsl:template>
        <xsl:template match="marc:datafield[@tag=611]">
                <subject>
                        <xsl:call-template name="subjectAuthority"/>
                        <name type="conference">
                                <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="."/>
                                                </roleTerm>
                                        </role>
                                </xsl:for-each>
                        </name>
                        <xsl:call-template name="subjectAnyOrder"/>
                </subject>
        </xsl:template>
        <xsl:template match="marc:datafield[@tag=630]">
                <subject>
                        <xsl:call-template name="subjectAuthority"/>
                        <titleInfo>
                                <title>
                                        <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>
                                </title>
                                <xsl:call-template name="part"/>
                        </titleInfo>
                        <xsl:call-template name="subjectAnyOrder"/>
                </subject>
        </xsl:template>
        <!-- 1.27 648 tmee-->
        <xsl:template match="marc:datafield[@tag=648]">
                <subject>
                        <xsl:if test="marc:subfield[@code=2]">
                                <xsl:attribute name="authority">
                                        <xsl:value-of select="marc:subfield[@code=2]"/>
                                </xsl:attribute>
                        </xsl:if>
                        <xsl:call-template name="uri"/>

                        <xsl:call-template name="subjectAuthority"/>
                        <temporal>
                                <xsl:call-template name="chopPunctuation">
                                        <xsl:with-param name="chopString">
                                                <xsl:call-template name="subfieldSelect">
                                                        <xsl:with-param name="codes">abcd</xsl:with-param>
                                                </xsl:call-template>
                                        </xsl:with-param>
                                </xsl:call-template>
                        </temporal>
                        <xsl:call-template name="subjectAnyOrder"/>

                </subject>
        </xsl:template>
        <xsl:template match="marc:datafield[@tag=650]">
                <subject>
                        <xsl:call-template name="subjectAuthority"/>
                        <topic>
                                <xsl:call-template name="chopPunctuation">
                                        <xsl:with-param name="chopString">
                                                <xsl:call-template name="subfieldSelect">
                                                        <xsl:with-param name="codes">abcd</xsl:with-param>
                                                </xsl:call-template>
                                        </xsl:with-param>
                                </xsl:call-template>
                        </topic>
                        <xsl:call-template name="subjectAnyOrder"/>
                </subject>
        </xsl:template>
        <xsl:template match="marc:datafield[@tag=651]">
                <subject>
                        <xsl:call-template name="subjectAuthority"/>
                        <xsl:for-each select="marc:subfield[@code='a']">
                                <geographic>
                                        <xsl:call-template name="chopPunctuation">
                                                <xsl:with-param name="chopString" select="."/>
                                        </xsl:call-template>
                                </geographic>
                        </xsl:for-each>
                        <xsl:call-template name="subjectAnyOrder"/>
                </subject>
        </xsl:template>
        <xsl:template match="marc:datafield[@tag=653]">
                <subject>
                        <xsl:for-each select="marc:subfield[@code='a']">
                                <topic>
                                        <xsl:value-of select="."/>
                                </topic>
                        </xsl:for-each>
                </subject>
        </xsl:template>
        <xsl:template match="marc:datafield[@tag=656]">
                <subject>
                        <xsl:if test="marc:subfield[@code=2]">
                                <xsl:attribute name="authority">
                                        <xsl:value-of select="marc:subfield[@code=2]"/>
                                </xsl:attribute>
                        </xsl:if>
                        <occupation>
                                <xsl:call-template name="chopPunctuation">
                                        <xsl:with-param name="chopString">
                                                <xsl:value-of select="marc:subfield[@code='a']"/>
                                        </xsl:with-param>
                                </xsl:call-template>
                        </occupation>
                </subject>
        </xsl:template>
        <xsl:template name="termsOfAddress">
                <xsl:if test="marc:subfield[@code='b' or @code='c']">
                        <namePart type="termsOfAddress">
                                <xsl:call-template name="chopPunctuation">
                                        <xsl:with-param name="chopString">
                                                <xsl:call-template name="subfieldSelect">
                                                        <xsl:with-param name="codes">bc</xsl:with-param>
                                                </xsl:call-template>
                                        </xsl:with-param>
                                </xsl:call-template>
                        </namePart>
                </xsl:if>
        </xsl:template>
        <xsl:template name="displayLabel">
                <xsl:if test="marc:subfield[@code='i']">
                        <xsl:attribute name="displayLabel">
                                <xsl:value-of select="marc:subfield[@code='i']"/>
                        </xsl:attribute>
                </xsl:if>
                <xsl:if test="marc:subfield[@code='3']">
                        <xsl:attribute name="displayLabel">
                                <xsl:value-of select="marc:subfield[@code='3']"/>
                        </xsl:attribute>
                </xsl:if>
        </xsl:template>
        <xsl:template name="isInvalid">
                <xsl:param name="type"/>
                <xsl:if
                        test="marc:subfield[@code='z'] or marc:subfield[@code='y']  or marc:subfield[@code='m']">
                        <identifier>
                                <xsl:attribute name="type">
                                        <xsl:value-of select="$type"/>
                                </xsl:attribute>
                                <xsl:attribute name="invalid">
                                        <xsl:text>yes</xsl:text>
                                </xsl:attribute>
                                <xsl:if test="marc:subfield[@code='z']">
                                        <xsl:value-of select="marc:subfield[@code='z']"/>
                                </xsl:if>
                                <xsl:if test="marc:subfield[@code='y']">
                                        <xsl:value-of select="marc:subfield[@code='y']"/>
                                </xsl:if>
                                <xsl:if test="marc:subfield[@code='m']">
                                        <xsl:value-of select="marc:subfield[@code='m']"/>
                                </xsl:if>
                        </identifier>
                </xsl:if>
        </xsl:template>
        <xsl:template name="subtitle">
                <xsl:if test="marc:subfield[@code='b']">
                        <subTitle>
                                <xsl:call-template name="chopPunctuation">
                                        <xsl:with-param name="chopString">
                                                <xsl:value-of select="marc:subfield[@code='b']"/>
                                                <!--<xsl:call-template name="subfieldSelect">
                                                        <xsl:with-param name="codes">b</xsl:with-param>
                                                </xsl:call-template>-->
                                        </xsl:with-param>
                                </xsl:call-template>
                        </subTitle>
                </xsl:if>
        </xsl:template>
        <xsl:template name="script">
                <xsl:param name="scriptCode"/>
                <xsl:attribute name="script">
                        <xsl:choose>
                                <xsl:when test="$scriptCode='(3'">Arabic</xsl:when>
                                <xsl:when test="$scriptCode='(B'">Latin</xsl:when>
                                <xsl:when test="$scriptCode='$1'">Chinese, Japanese, Korean</xsl:when>
                                <xsl:when test="$scriptCode='(N'">Cyrillic</xsl:when>
                                <xsl:when test="$scriptCode='(2'">Hebrew</xsl:when>
                                <xsl:when test="$scriptCode='(S'">Greek</xsl:when>
                        </xsl:choose>
                </xsl:attribute>
        </xsl:template>
        <xsl:template name="parsePart">
                <!-- assumes 773$q= 1:2:3<4
                     with up to 3 levels and one optional start page
                -->
                <xsl:variable name="level1">
                        <xsl:choose>
                                <xsl:when test="contains(text(),':')">
                                        <!-- 1:2 -->
                                        <xsl:value-of select="substring-before(text(),':')"/>
                                </xsl:when>
                                <xsl:when test="not(contains(text(),':'))">
                                        <!-- 1 or 1<3 -->
                                        <xsl:if test="contains(text(),'&lt;')">
                                                <!-- 1<3 -->
                                                <xsl:value-of select="substring-before(text(),'&lt;')"/>
                                        </xsl:if>
                                        <xsl:if test="not(contains(text(),'&lt;'))">
                                                <!-- 1 -->
                                                <xsl:value-of select="text()"/>
                                        </xsl:if>
                                </xsl:when>
                        </xsl:choose>
                </xsl:variable>
                <xsl:variable name="sici2">
                        <xsl:choose>
                                <xsl:when test="starts-with(substring-after(text(),$level1),':')">
                                        <xsl:value-of select="substring(substring-after(text(),$level1),2)"/>
                                </xsl:when>
                                <xsl:otherwise>
                                        <xsl:value-of select="substring-after(text(),$level1)"/>
                                </xsl:otherwise>
                        </xsl:choose>
                </xsl:variable>
                <xsl:variable name="level2">
                        <xsl:choose>
                                <xsl:when test="contains($sici2,':')">
                                        <!--  2:3<4  -->
                                        <xsl:value-of select="substring-before($sici2,':')"/>
                                </xsl:when>
                                <xsl:when test="contains($sici2,'&lt;')">
                                        <!-- 1: 2<4 -->
                                        <xsl:value-of select="substring-before($sici2,'&lt;')"/>
                                </xsl:when>
                                <xsl:otherwise>
                                        <xsl:value-of select="$sici2"/>
                                        <!-- 1:2 -->
                                </xsl:otherwise>
                        </xsl:choose>
                </xsl:variable>
                <xsl:variable name="sici3">
                        <xsl:choose>
                                <xsl:when test="starts-with(substring-after($sici2,$level2),':')">
                                        <xsl:value-of select="substring(substring-after($sici2,$level2),2)"/>
                                </xsl:when>
                                <xsl:otherwise>
                                        <xsl:value-of select="substring-after($sici2,$level2)"/>
                                </xsl:otherwise>
                        </xsl:choose>
                </xsl:variable>
                <xsl:variable name="level3">
                        <xsl:choose>
                                <xsl:when test="contains($sici3,'&lt;')">
                                        <!-- 2<4 -->
                                        <xsl:value-of select="substring-before($sici3,'&lt;')"/>
                                </xsl:when>
                                <xsl:otherwise>
                                        <xsl:value-of select="$sici3"/>
                                        <!-- 3 -->
                                </xsl:otherwise>
                        </xsl:choose>
                </xsl:variable>
                <xsl:variable name="page">
                        <xsl:if test="contains(text(),'&lt;')">
                                <xsl:value-of select="substring-after(text(),'&lt;')"/>
                        </xsl:if>
                </xsl:variable>
                <xsl:if test="$level1">
                        <detail level="1">
                                <number>
                                        <xsl:value-of select="$level1"/>
                                </number>
                        </detail>
                </xsl:if>
                <xsl:if test="$level2">
                        <detail level="2">
                                <number>
                                        <xsl:value-of select="$level2"/>
                                </number>
                        </detail>
                </xsl:if>
                <xsl:if test="$level3">
                        <detail level="3">
                                <number>
                                        <xsl:value-of select="$level3"/>
                                </number>
                        </detail>
                </xsl:if>
                <xsl:if test="$page">
                        <extent unit="page">
                                <start>
                                        <xsl:value-of select="$page"/>
                                </start>
                        </extent>
                </xsl:if>
        </xsl:template>
        <xsl:template name="getLanguage">
                <xsl:param name="langString"/>
                <xsl:param name="controlField008-35-37"/>
                <xsl:variable name="length" select="string-length($langString)"/>
                <xsl:choose>
                        <xsl:when test="$length=0"/>
                        <xsl:when test="$controlField008-35-37=substring($langString,1,3)">
                                <xsl:call-template name="getLanguage">
                                        <xsl:with-param name="langString" select="substring($langString,4,$length)"/>
                                        <xsl:with-param name="controlField008-35-37" select="$controlField008-35-37"/>
                                </xsl:call-template>
                        </xsl:when>
                        <xsl:otherwise>
                                <language>
                                        <languageTerm authority="iso639-2b" type="code">
                                                <xsl:value-of select="substring($langString,1,3)"/>
                                        </languageTerm>
                                </language>
                                <xsl:call-template name="getLanguage">
                                        <xsl:with-param name="langString" select="substring($langString,4,$length)"/>
                                        <xsl:with-param name="controlField008-35-37" select="$controlField008-35-37"/>
                                </xsl:call-template>
                        </xsl:otherwise>
                </xsl:choose>
        </xsl:template>
        <xsl:template name="isoLanguage">
                <xsl:param name="currentLanguage"/>
                <xsl:param name="usedLanguages"/>
                <xsl:param name="remainingLanguages"/>
                <xsl:choose>
                        <xsl:when test="string-length($currentLanguage)=0"/>
                        <xsl:when test="not(contains($usedLanguages, $currentLanguage))">
                                <language>
                                        <xsl:if test="@code!='a'">
                                                <xsl:attribute name="objectPart">
                                                        <xsl:choose>
                                                                <xsl:when test="@code='b'">summary or subtitle</xsl:when>
                                                                <xsl:when test="@code='d'">sung or spoken text</xsl:when>
                                                                <xsl:when test="@code='e'">libretto</xsl:when>
                                                                <xsl:when test="@code='f'">table of contents</xsl:when>
                                                                <xsl:when test="@code='g'">accompanying material</xsl:when>
                                                                <xsl:when test="@code='h'">translation</xsl:when>
                                                        </xsl:choose>
                                                </xsl:attribute>
                                        </xsl:if>
                                        <languageTerm authority="iso639-2b" type="code">
                                                <xsl:value-of select="$currentLanguage"/>
                                        </languageTerm>
                                </language>
                                <xsl:call-template name="isoLanguage">
                                        <xsl:with-param name="currentLanguage">
                                                <xsl:value-of select="substring($remainingLanguages,1,3)"/>
                                        </xsl:with-param>
                                        <xsl:with-param name="usedLanguages">
                                                <xsl:value-of select="concat($usedLanguages,$currentLanguage)"/>
                                        </xsl:with-param>
                                        <xsl:with-param name="remainingLanguages">
                                                <xsl:value-of
                                                        select="substring($remainingLanguages,4,string-length($remainingLanguages))"
                                                />
                                        </xsl:with-param>
                                </xsl:call-template>
                        </xsl:when>
                        <xsl:otherwise>
                                <xsl:call-template name="isoLanguage">
                                        <xsl:with-param name="currentLanguage">
                                                <xsl:value-of select="substring($remainingLanguages,1,3)"/>
                                        </xsl:with-param>
                                        <xsl:with-param name="usedLanguages">
                                                <xsl:value-of select="concat($usedLanguages,$currentLanguage)"/>
                                        </xsl:with-param>
                                        <xsl:with-param name="remainingLanguages">
                                                <xsl:value-of
                                                        select="substring($remainingLanguages,4,string-length($remainingLanguages))"
                                                />
                                        </xsl:with-param>
                                </xsl:call-template>
                        </xsl:otherwise>
                </xsl:choose>
        </xsl:template>
        <xsl:template name="chopBrackets">
                <xsl:param name="chopString"/>
                <xsl:variable name="string">
                        <xsl:call-template name="chopPunctuation">
                                <xsl:with-param name="chopString" select="$chopString"/>
                        </xsl:call-template>
                </xsl:variable>
                <xsl:if test="substring($string, 1,1)='['">
                        <xsl:value-of select="substring($string,2, string-length($string)-2)"/>
                </xsl:if>
                <xsl:if test="substring($string, 1,1)!='['">
                        <xsl:value-of select="$string"/>
                </xsl:if>
        </xsl:template>
        <xsl:template name="rfcLanguages">
                <xsl:param name="nodeNum"/>
                <xsl:param name="usedLanguages"/>
                <xsl:param name="controlField008-35-37"/>
                <xsl:variable name="currentLanguage" select="."/>
                <xsl:choose>
                        <xsl:when test="not($currentLanguage)"/>
                        <xsl:when
                                test="$currentLanguage!=$controlField008-35-37 and $currentLanguage!='rfc3066'">
                                <xsl:if test="not(contains($usedLanguages,$currentLanguage))">
                                        <language>
                                                <xsl:if test="@code!='a'">
                                                        <xsl:attribute name="objectPart">
                                                                <xsl:choose>
                                                                        <xsl:when test="@code='b'">summary or subtitle</xsl:when>
                                                                        <xsl:when test="@code='d'">sung or spoken text</xsl:when>
                                                                        <xsl:when test="@code='e'">libretto</xsl:when>
                                                                        <xsl:when test="@code='f'">table of contents</xsl:when>
                                                                        <xsl:when test="@code='g'">accompanying material</xsl:when>
                                                                        <xsl:when test="@code='h'">translation</xsl:when>
                                                                </xsl:choose>
                                                        </xsl:attribute>
                                                </xsl:if>
                                                <languageTerm authority="rfc3066" type="code">
                                                        <xsl:value-of select="$currentLanguage"/>
                                                </languageTerm>
                                        </language>
                                </xsl:if>
                        </xsl:when>
                        <xsl:otherwise> </xsl:otherwise>
                </xsl:choose>
        </xsl:template>

    <xsl:template name="datafield">
                <xsl:param name="tag"/>
                <xsl:param name="ind1">
                        <xsl:text> </xsl:text>
                </xsl:param>
                <xsl:param name="ind2">
                        <xsl:text> </xsl:text>
                </xsl:param>
                <xsl:param name="subfields"/>
                <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="$subfields"/>
                </xsl:element>
        </xsl:template>

        <xsl:template name="subfieldSelect">
                <xsl:param name="codes">abcdefghijklmnopqrstuvwxyz</xsl:param>
                <xsl:param name="delimeter">
                        <xsl:text> </xsl:text>
                </xsl:param>
                <xsl:variable name="str">
                        <xsl:for-each select="marc:subfield">
                                <xsl:if test="contains($codes, @code)">
                                        <xsl:value-of select="text()"/>
                                        <xsl:value-of select="$delimeter"/>
                                </xsl:if>
                        </xsl:for-each>
                </xsl:variable>
                <xsl:value-of select="substring($str,1,string-length($str)-string-length($delimeter))"/>
        </xsl:template>

        <xsl:template name="buildSpaces">
                <xsl:param name="spaces"/>
                <xsl:param name="char">
                        <xsl:text> </xsl:text>
                </xsl:param>
                <xsl:if test="$spaces>0">
                        <xsl:value-of select="$char"/>
                        <xsl:call-template name="buildSpaces">
                                <xsl:with-param name="spaces" select="$spaces - 1"/>
                                <xsl:with-param name="char" select="$char"/>
                        </xsl:call-template>
                </xsl:if>
        </xsl:template>

        <xsl:template name="chopPunctuation">
                <xsl:param name="chopString"/>
                <xsl:param name="punctuation">
                        <xsl:text>.:,;/ </xsl:text>
                </xsl:param>
                <xsl:variable name="length" select="string-length($chopString)"/>
                <xsl:choose>
                        <xsl:when test="$length=0"/>
                        <xsl:when test="contains($punctuation, substring($chopString,$length,1))">
                                <xsl:call-template name="chopPunctuation">
                                        <xsl:with-param name="chopString" select="substring($chopString,1,$length - 1)"/>
                                        <xsl:with-param name="punctuation" select="$punctuation"/>
                                </xsl:call-template>
                        </xsl:when>
                        <xsl:when test="not($chopString)"/>
                        <xsl:otherwise>
                                <xsl:value-of select="$chopString"/>
                        </xsl:otherwise>
                </xsl:choose>
        </xsl:template>

        <xsl:template name="chopPunctuationFront">
                <xsl:param name="chopString"/>
                <xsl:variable name="length" select="string-length($chopString)"/>
                <xsl:choose>
                        <xsl:when test="$length=0"/>
                        <xsl:when test="contains('.:,;/[ ', substring($chopString,1,1))">
                                <xsl:call-template name="chopPunctuationFront">
                                        <xsl:with-param name="chopString" select="substring($chopString,2,$length - 1)"
                                        />
                                </xsl:call-template>
                        </xsl:when>
                        <xsl:when test="not($chopString)"/>
                        <xsl:otherwise>
                                <xsl:value-of select="$chopString"/>
                        </xsl:otherwise>
                </xsl:choose>
        </xsl:template>

        <xsl:template name="chopPunctuationBack">
                <xsl:param name="chopString"/>
                <xsl:param name="punctuation">
                        <xsl:text>.:,;/] </xsl:text>
                </xsl:param>
                <xsl:variable name="length" select="string-length($chopString)"/>
                <xsl:choose>
                        <xsl:when test="$length=0"/>
                        <xsl:when test="contains($punctuation, substring($chopString,$length,1))">
                                <xsl:call-template name="chopPunctuation">
                                        <xsl:with-param name="chopString" select="substring($chopString,1,$length - 1)"/>
                                        <xsl:with-param name="punctuation" select="$punctuation"/>
                                </xsl:call-template>
                        </xsl:when>
                        <xsl:when test="not($chopString)"/>
                        <xsl:otherwise>
                                <xsl:value-of select="$chopString"/>
                        </xsl:otherwise>
                </xsl:choose>
        </xsl:template>

        <!-- nate added 12/14/2007 for lccn.loc.gov: url encode ampersand, etc. -->
        <xsl:template name="url-encode">

                <xsl:param name="str"/>

                <xsl:if test="$str">
                        <xsl:variable name="first-char" select="substring($str,1,1)"/>
                        <xsl:choose>
                                <xsl:when test="contains($safe,$first-char)">
                                        <xsl:value-of select="$first-char"/>
                                </xsl:when>
                                <xsl:otherwise>
                                        <xsl:variable name="codepoint">
                                                <xsl:choose>
                                                        <xsl:when test="contains($ascii,$first-char)">
                                                                <xsl:value-of
                                                                        select="string-length(substring-before($ascii,$first-char)) + 32"
                                                                />
                                                        </xsl:when>
                                                        <xsl:when test="contains($latin1,$first-char)">
                                                                <xsl:value-of
                                                                        select="string-length(substring-before($latin1,$first-char)) + 160"/>
                                                                <!-- was 160 -->
                                                        </xsl:when>
                                                        <xsl:otherwise>
                                                                <xsl:message terminate="no">Warning: string contains a character
                                                                        that is out of range! Substituting "?".</xsl:message>
                                                                <xsl:text>63</xsl:text>
                                                        </xsl:otherwise>
                                                </xsl:choose>
                                        </xsl:variable>
                                        <xsl:variable name="hex-digit1"
                                                select="substring($hex,floor($codepoint div 16) + 1,1)"/>
                                        <xsl:variable name="hex-digit2" select="substring($hex,$codepoint mod 16 + 1,1)"/>
                                        <!-- <xsl:value-of select="concat('%',$hex-digit2)"/> -->
                                        <xsl:value-of select="concat('%',$hex-digit1,$hex-digit2)"/>
                                </xsl:otherwise>
                        </xsl:choose>
                        <xsl:if test="string-length($str) &gt; 1">
                                <xsl:call-template name="url-encode">
                                        <xsl:with-param name="str" select="substring($str,2)"/>
                                </xsl:call-template>
                        </xsl:if>
                </xsl:if>
        </xsl:template>
</xsl:stylesheet>$XXXX$ where name = $$mods33$$;



SELECT evergreen.upgrade_deps_block_check('1103', :eg_version);

INSERT INTO config.metabib_field (id, field_class, name, label, browse_field)
    VALUES (45, 'keyword', 'blob', 'All searchable fields', FALSE);

INSERT INTO config.metabib_field (id, field_class, name, format, weight,
    label, xpath, display_field, search_field, browse_field, facet_field)
VALUES (
    53, 'title', 'maintitle', 'marcxml', 10,
    oils_i18n_gettext(53, 'Main Title', 'cmf', 'label'),
    $$//*[@tag='245']/*[@code='a']$$,
    FALSE, TRUE, FALSE, FALSE
);

INSERT INTO config.metabib_field_virtual_map (real, virtual)
    SELECT  id,
            45
      FROM  config.metabib_field
      WHERE search_field
            AND id NOT IN (15, 45, 38, 40) -- keyword|keyword, self, edition, publisher
            AND id NOT IN (SELECT real FROM config.metabib_field_virtual_map);

UPDATE config.metabib_field SET xpath=$$//mods32:mods/mods32:subject[not(descendant::mods32:geographicCode)]$$ WHERE id = 16;

UPDATE config.metabib_field_virtual_map SET weight = -1 WHERE real = 39;
UPDATE config.metabib_field_virtual_map SET weight = 0 WHERE real = 41;
UPDATE config.metabib_field_virtual_map SET weight = 0 WHERE real = 42;
UPDATE config.metabib_field_virtual_map SET weight = 0 WHERE real = 46;
UPDATE config.metabib_field_virtual_map SET weight = 0 WHERE real = 47;
UPDATE config.metabib_field_virtual_map SET weight = 0 WHERE real = 48;
UPDATE config.metabib_field_virtual_map SET weight = 0 WHERE real = 50;
UPDATE config.metabib_field_virtual_map SET weight = 8 WHERE real = 6;
UPDATE config.metabib_field_virtual_map SET weight = 8 WHERE real = 8;
UPDATE config.metabib_field_virtual_map SET weight = 8 WHERE real = 16;
UPDATE config.metabib_field_virtual_map SET weight = 12 WHERE real = 53;

-- Stemming for genre
INSERT INTO config.metabib_field_ts_map (metabib_field, ts_config)
    SELECT 33, 'english_nostop' WHERE NOT EXISTS (
        SELECT 1 FROM config.metabib_field_ts_map WHERE metabib_field = 33 AND ts_config = 'english_nostop'
    )
;


SELECT evergreen.upgrade_deps_block_check('1104', :eg_version);

INSERT INTO config.org_unit_setting_type
( name, grp, label, description, datatype )
VALUES
	('circ.clear_hold_on_checkout',
	 'circ',
 	oils_i18n_gettext('circ.clear_hold_on_checkout',
     		'Clear hold when other patron checks out item',
     		'coust', 'label'),
        oils_i18n_gettext('circ.clear_hold_on_checkout',
            'Default to cancel the hold when patron A checks out item on hold for patron B.',
     		'coust', 'description'),
   	'bool');



SELECT evergreen.upgrade_deps_block_check('1105', :eg_version);

INSERT into config.org_unit_setting_type (name, label, grp, description, datatype) 
values ('webstaff.circ.itemsout_notice_count_excludes_courtesies','Exclude Courtesy Notices from Patrons Itemsout Notices Count',
    'circ', 'Exclude Courtesy Notices from Patron Itemsout Notices Count', 'bool');


SELECT evergreen.upgrade_deps_block_check('1106', :eg_version);

ALTER TABLE money.billing
	ADD COLUMN create_date TIMESTAMP WITH TIME ZONE,
	ADD COLUMN period_start    TIMESTAMP WITH TIME ZONE,
	ADD COLUMN period_end  TIMESTAMP WITH TIME ZONE;

--Disable materialized update trigger
--It takes forever, and doesn't matter yet for what we are doing, as the
--view definition is unchanged (still using billing_ts)
ALTER TABLE money.billing DISABLE TRIGGER mat_summary_upd_tgr;

--Limit to btype=1 / 'Overdue Materials'
--Update day-granular fines first (i.e. 24 hour, 1 day, 2 day, etc., all of which are multiples of 86400 seconds), and simply remove the time portion of timestamp
UPDATE money.billing mb
	SET period_start = date_trunc('day', billing_ts), period_end = date_trunc('day', billing_ts) + (ac.fine_interval - '1 second')
	FROM action.circulation ac
WHERE mb.xact = ac.id
	AND mb.btype = 1
	AND (EXTRACT(EPOCH FROM ac.fine_interval))::integer % 86400 = 0;

--Update fines for non-day intervals
UPDATE money.billing mb
	SET period_start = billing_ts - ac.fine_interval + interval '1 sec', period_end = billing_ts
	FROM action.circulation ac
WHERE mb.xact = ac.id
	AND mb.btype = 1
	AND (EXTRACT(EPOCH FROM ac.fine_interval))::integer % 86400 > 0;

SET CONSTRAINTS ALL IMMEDIATE;
UPDATE money.billing SET create_date = COALESCE(period_start, billing_ts);

--Re-enable update trigger
ALTER TABLE money.billing ENABLE TRIGGER mat_summary_upd_tgr;

ALTER TABLE money.billing ALTER COLUMN create_date SET DEFAULT NOW();
ALTER TABLE money.billing ALTER COLUMN create_date SET NOT NULL;

CREATE INDEX m_b_create_date_idx ON money.billing (create_date);
CREATE INDEX m_b_period_start_idx ON money.billing (period_start);
CREATE INDEX m_b_period_end_idx ON money.billing (period_end);

CREATE OR REPLACE FUNCTION money.maintain_billing_ts () RETURNS TRIGGER AS $$
BEGIN
	NEW.billing_ts := COALESCE(NEW.period_end, NEW.create_date);
	RETURN NEW;
END;
$$ LANGUAGE PLPGSQL;
CREATE TRIGGER maintain_billing_ts_tgr BEFORE INSERT OR UPDATE ON money.billing FOR EACH ROW EXECUTE PROCEDURE money.maintain_billing_ts();

  
SELECT evergreen.upgrade_deps_block_check('1108', :eg_version);

CREATE OR REPLACE FUNCTION asset.merge_record_assets( target_record BIGINT, source_record BIGINT ) RETURNS INT AS $func$
DECLARE
    moved_objects INT := 0;
    source_cn     asset.call_number%ROWTYPE;
    target_cn     asset.call_number%ROWTYPE;
    metarec       metabib.metarecord%ROWTYPE;
    hold          action.hold_request%ROWTYPE;
    ser_rec       serial.record_entry%ROWTYPE;
    ser_sub       serial.subscription%ROWTYPE;
    acq_lineitem  acq.lineitem%ROWTYPE;
    acq_request   acq.user_request%ROWTYPE;
    booking       booking.resource_type%ROWTYPE;
    source_part   biblio.monograph_part%ROWTYPE;
    target_part   biblio.monograph_part%ROWTYPE;
    multi_home    biblio.peer_bib_copy_map%ROWTYPE;
    uri_count     INT := 0;
    counter       INT := 0;
    uri_datafield TEXT;
    uri_text      TEXT := '';
BEGIN

    -- move any 856 entries on records that have at least one MARC-mapped URI entry
    SELECT  INTO uri_count COUNT(*)
      FROM  asset.uri_call_number_map m
            JOIN asset.call_number cn ON (m.call_number = cn.id)
      WHERE cn.record = source_record;

    IF uri_count > 0 THEN
        
        -- This returns more nodes than you might expect:
        -- 7 instead of 1 for an 856 with $u $y $9
        SELECT  COUNT(*) INTO counter
          FROM  oils_xpath_table(
                    'id',
                    'marc',
                    'biblio.record_entry',
                    '//*[@tag="856"]',
                    'id=' || source_record
                ) as t(i int,c text);
    
        FOR i IN 1 .. counter LOOP
            SELECT  '<datafield xmlns="http://www.loc.gov/MARC21/slim"' || 
			' tag="856"' ||
			' ind1="' || FIRST(ind1) || '"'  ||
			' ind2="' || FIRST(ind2) || '">' ||
                        STRING_AGG(
                            '<subfield code="' || subfield || '">' ||
                            regexp_replace(
                                regexp_replace(
                                    regexp_replace(data,'&','&amp;','g'),
                                    '>', '&gt;', 'g'
                                ),
                                '<', '&lt;', 'g'
                            ) || '</subfield>', ''
                        ) || '</datafield>' INTO uri_datafield
              FROM  oils_xpath_table(
                        'id',
                        'marc',
                        'biblio.record_entry',
                        '//*[@tag="856"][position()=' || i || ']/@ind1|' ||
                        '//*[@tag="856"][position()=' || i || ']/@ind2|' ||
                        '//*[@tag="856"][position()=' || i || ']/*/@code|' ||
                        '//*[@tag="856"][position()=' || i || ']/*[@code]',
                        'id=' || source_record
                    ) as t(id int,ind1 text, ind2 text,subfield text,data text);

            -- As most of the results will be NULL, protect against NULLifying
            -- the valid content that we do generate
            uri_text := uri_text || COALESCE(uri_datafield, '');
        END LOOP;

        IF uri_text <> '' THEN
            UPDATE  biblio.record_entry
              SET   marc = regexp_replace(marc,'(</[^>]*record>)', uri_text || E'\\1')
              WHERE id = target_record;
        END IF;

    END IF;

	-- Find and move metarecords to the target record
	SELECT	INTO metarec *
	  FROM	metabib.metarecord
	  WHERE	master_record = source_record;

	IF FOUND THEN
		UPDATE	metabib.metarecord
		  SET	master_record = target_record,
			mods = NULL
		  WHERE	id = metarec.id;

		moved_objects := moved_objects + 1;
	END IF;

	-- Find call numbers attached to the source ...
	FOR source_cn IN SELECT * FROM asset.call_number WHERE record = source_record LOOP

		SELECT	INTO target_cn *
		  FROM	asset.call_number
		  WHERE	label = source_cn.label
            AND prefix = source_cn.prefix
            AND suffix = source_cn.suffix
			AND owning_lib = source_cn.owning_lib
			AND record = target_record
			AND NOT deleted;

		-- ... and if there's a conflicting one on the target ...
		IF FOUND THEN

			-- ... move the copies to that, and ...
			UPDATE	asset.copy
			  SET	call_number = target_cn.id
			  WHERE	call_number = source_cn.id;

			-- ... move V holds to the move-target call number
			FOR hold IN SELECT * FROM action.hold_request WHERE target = source_cn.id AND hold_type = 'V' LOOP
		
				UPDATE	action.hold_request
				  SET	target = target_cn.id
				  WHERE	id = hold.id;
		
				moved_objects := moved_objects + 1;
			END LOOP;
        
            UPDATE asset.call_number SET deleted = TRUE WHERE id = source_cn.id;

		-- ... if not ...
		ELSE
			-- ... just move the call number to the target record
			UPDATE	asset.call_number
			  SET	record = target_record
			  WHERE	id = source_cn.id;
		END IF;

		moved_objects := moved_objects + 1;
	END LOOP;

	-- Find T holds targeting the source record ...
	FOR hold IN SELECT * FROM action.hold_request WHERE target = source_record AND hold_type = 'T' LOOP

		-- ... and move them to the target record
		UPDATE	action.hold_request
		  SET	target = target_record
		  WHERE	id = hold.id;

		moved_objects := moved_objects + 1;
	END LOOP;

	-- Find serial records targeting the source record ...
	FOR ser_rec IN SELECT * FROM serial.record_entry WHERE record = source_record LOOP
		-- ... and move them to the target record
		UPDATE	serial.record_entry
		  SET	record = target_record
		  WHERE	id = ser_rec.id;

		moved_objects := moved_objects + 1;
	END LOOP;

	-- Find serial subscriptions targeting the source record ...
	FOR ser_sub IN SELECT * FROM serial.subscription WHERE record_entry = source_record LOOP
		-- ... and move them to the target record
		UPDATE	serial.subscription
		  SET	record_entry = target_record
		  WHERE	id = ser_sub.id;

		moved_objects := moved_objects + 1;
	END LOOP;

	-- Find booking resource types targeting the source record ...
	FOR booking IN SELECT * FROM booking.resource_type WHERE record = source_record LOOP
		-- ... and move them to the target record
		UPDATE	booking.resource_type
		  SET	record = target_record
		  WHERE	id = booking.id;

		moved_objects := moved_objects + 1;
	END LOOP;

	-- Find acq lineitems targeting the source record ...
	FOR acq_lineitem IN SELECT * FROM acq.lineitem WHERE eg_bib_id = source_record LOOP
		-- ... and move them to the target record
		UPDATE	acq.lineitem
		  SET	eg_bib_id = target_record
		  WHERE	id = acq_lineitem.id;

		moved_objects := moved_objects + 1;
	END LOOP;

	-- Find acq user purchase requests targeting the source record ...
	FOR acq_request IN SELECT * FROM acq.user_request WHERE eg_bib = source_record LOOP
		-- ... and move them to the target record
		UPDATE	acq.user_request
		  SET	eg_bib = target_record
		  WHERE	id = acq_request.id;

		moved_objects := moved_objects + 1;
	END LOOP;

	-- Find parts attached to the source ...
	FOR source_part IN SELECT * FROM biblio.monograph_part WHERE record = source_record LOOP

		SELECT	INTO target_part *
		  FROM	biblio.monograph_part
		  WHERE	label = source_part.label
			AND record = target_record;

		-- ... and if there's a conflicting one on the target ...
		IF FOUND THEN

			-- ... move the copy-part maps to that, and ...
			UPDATE	asset.copy_part_map
			  SET	part = target_part.id
			  WHERE	part = source_part.id;

			-- ... move P holds to the move-target part
			FOR hold IN SELECT * FROM action.hold_request WHERE target = source_part.id AND hold_type = 'P' LOOP
		
				UPDATE	action.hold_request
				  SET	target = target_part.id
				  WHERE	id = hold.id;
		
				moved_objects := moved_objects + 1;
			END LOOP;

		-- ... if not ...
		ELSE
			-- ... just move the part to the target record
			UPDATE	biblio.monograph_part
			  SET	record = target_record
			  WHERE	id = source_part.id;
		END IF;

		moved_objects := moved_objects + 1;
	END LOOP;

	-- Find multi_home items attached to the source ...
	FOR multi_home IN SELECT * FROM biblio.peer_bib_copy_map WHERE peer_record = source_record LOOP
		-- ... and move them to the target record
		UPDATE	biblio.peer_bib_copy_map
		  SET	peer_record = target_record
		  WHERE	id = multi_home.id;

		moved_objects := moved_objects + 1;
	END LOOP;

	-- And delete mappings where the item's home bib was merged with the peer bib
	DELETE FROM biblio.peer_bib_copy_map WHERE peer_record = (
		SELECT (SELECT record FROM asset.call_number WHERE id = call_number)
		FROM asset.copy WHERE id = target_copy
	);

    -- Apply merge tracking
    UPDATE biblio.record_entry 
        SET merge_date = NOW() WHERE id = target_record;

    UPDATE biblio.record_entry
        SET merge_date = NOW(), merged_to = target_record
        WHERE id = source_record;

    -- replace book bag entries of source_record with target_record
    UPDATE container.biblio_record_entry_bucket_item
        SET target_biblio_record_entry = target_record
        WHERE bucket IN (SELECT id FROM container.biblio_record_entry_bucket WHERE btype = 'bookbag')
        AND target_biblio_record_entry = source_record;

    -- Finally, "delete" the source record
    DELETE FROM biblio.record_entry WHERE id = source_record;

	-- That's all, folks!
	RETURN moved_objects;
END;
$func$ LANGUAGE plpgsql;


SELECT evergreen.upgrade_deps_block_check('1109', :eg_version);

INSERT into config.org_unit_setting_type (name, label, grp, description, datatype)
values ('circ.staff_placed_holds_fallback_to_ws_ou','Workstation OU fallback for staff-placed holds',
        'circ', 'For staff-placed holds, in the absence of a patron preferred pickup location, fall back to using the staff workstation OU (rather than patron home OU)', 'bool');


COMMIT;

\qecho 
\qecho Reingesting all records.  This may take a while. 
\qecho This command can be stopped (control-c) and rerun later if needed: 
\qecho 
\qecho DO $FUNC$
\qecho DECLARE
\qecho     same_marc BOOL;
\qecho BEGIN
\qecho     SELECT INTO same_marc enabled FROM config.internal_flag WHERE name = 'ingest.reingest.force_on_same_marc';
\qecho     UPDATE config.internal_flag SET enabled = true WHERE name = 'ingest.reingest.force_on_same_marc';
\qecho     UPDATE biblio.record_entry SET id=id WHERE not deleted AND id > 0;
\qecho     UPDATE config.internal_flag SET enabled = same_marc WHERE name = 'ingest.reingest.force_on_same_marc';
\qecho END;
\qecho $FUNC$;

DO $FUNC$
DECLARE
    same_marc BOOL;
BEGIN
    SELECT INTO same_marc enabled FROM config.internal_flag WHERE name = 'ingest.reingest.force_on_same_marc';
    UPDATE config.internal_flag SET enabled = true WHERE name = 'ingest.reingest.force_on_same_marc';
    UPDATE biblio.record_entry SET id=id WHERE not deleted AND id > 0;
    UPDATE config.internal_flag SET enabled = same_marc WHERE name = 'ingest.reingest.force_on_same_marc';
END;
$FUNC$;
