<xsl:stylesheet xmlns:i18n="http://apache.org/cocoon/i18n/2.1"
    xmlns:dri="http://di.tamu.edu/DRI/1.0/" xmlns:mets="http://www.loc.gov/METS/"
    xmlns:xlink="http://www.w3.org/TR/xlink/" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0" xmlns:dim="http://www.dspace.org/xmlns/dspace/dim"
    xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:mods="http://www.loc.gov/mods/v3"
    xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns="http://www.w3.org/1999/xhtml"
    exclude-result-prefixes="i18n dri mets xlink xsl dim xhtml mods dc">

    <xsl:import href="dri2xhtml.xsl"/>

    <!-- This stylesheet contains utility templates expected to be useful for all (or nearly all) Manakin themes
    in use on MINDS@UW. It currently contains:
    
    - a template for the HTML <head> element that adds Google Scholar metadata
    - a template for <dri:xref> that adds a "current" class attribute to the navbar option representing the current page
    - a template for <dri:trail> that adds a "first" class attribute to the first breadcrumb in the list
    - a template for the item-summary listings on browse pages that only shows dc.creator, dc.contributor.editor, and dc.contributor.author as authors
      (rather than dc.contributor.*)
    - a template for the brief metadata view that adds new dc.contributor types, contains more information, makes the view a list 
      rather than a table, links authors and subjects to their corresponding browse pages, and does not display empty metadata fields
    - a template for the "pagination" attribute (on e.g. browse or search results pages) that removes the plural when there is 
      only one item in the listing
    - a template for dri:div/dri:head that puts a community or collection logo inside <h1> on community/collection pages
    - a template for the full-item-metadata page that adds table headers for accessibility
    - a revised buildBitstreamRow template that uses lists instead of tables and de-verbosifies the display
    - a template that turns table heads into HTML <caption> elements
    - a template for Context Objects in Spans (COinS)
    - an English-language date-munger that turns yyyy-mm-dd into 1 Jan 2008
    - a revamp of the form templates that puts help text above the control in question
    - RefWorks-related templates, for our homegrown RefWorks exporter
    - a disgusting hack to solve the MIME-type-to-user-friendly-description problem
    - another disgusting hack for the collection-membership link on item pages such that it links to the title browse instead of the useless collection splash page    
-->


    <!-- Helps distinguish the test server (handle 1960) from the production server (handle 1793) -DS -->
    <xsl:variable name="handle-prefix"
        select="/dri:document/dri:meta/dri:repositoryMeta/dri:repository/@repositoryID"/>

    <!--  Whether the current page is a community or collection home page. -DS -->
    <xsl:variable name="is_comm"
        select="boolean(/dri:document/dri:body/dri:div[@n='community-home'])"/>
    <xsl:variable name="is_coll"
        select="boolean(/dri:document/dri:body/dri:div[@n='collection-home'])"/>
    <xsl:variable name="is_item" select="boolean(/dri:document/dri:body/dri:div[@n='item-view'])"/>

    <xsl:variable name="refworksLink"
        select="/dri:document/dri:body/dri:div/dri:p[@rend='refworks-export']/dri:xref/@target"/>

    <!-- if context-path plus current page request URI = the value of the xref target attribute, we're on the same page and can style that correctly.
         Includes any query string, because that can be important for determining which browse page you're on. -DS -->
    <xsl:variable name="currentpage">
        <xsl:value-of select="$context-path"/>
        <xsl:text>/</xsl:text>
        <xsl:value-of
            select="/dri:document/dri:meta/dri:pageMeta/dri:metadata[@element='request' and @qualifier='URI']"/>
        <xsl:if
            test="/dri:document/dri:meta/dri:pageMeta/dri:metadata[@element='request' and @qualifier='queryString']">
            <xsl:text>?</xsl:text>
            <xsl:value-of
                select="/dri:document/dri:meta/dri:pageMeta/dri:metadata[@element='request' and @qualifier='queryString']"
            />
        </xsl:if>
    </xsl:variable>

    <xsl:template name="buildHead">
        <xsl:param name="dim"/>
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
            <!-- tell IE8 not to screw everything up -DS -->
            <meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7"/>
            <!-- Add stylesheets -->
            <xsl:for-each
                select="/dri:document/dri:meta/dri:pageMeta/dri:metadata[@element='stylesheet']">
                <link rel="stylesheet" type="text/css">
                    <xsl:attribute name="media">
                        <xsl:value-of select="@qualifier"/>
                    </xsl:attribute>
                    <xsl:attribute name="href">
                        <xsl:value-of
                            select="/dri:document/dri:meta/dri:pageMeta/dri:metadata[@element='contextPath'][not(@qualifier)]"/>
                        <xsl:text>/themes/</xsl:text>
                        <xsl:value-of
                            select="/dri:document/dri:meta/dri:pageMeta/dri:metadata[@element='theme'][@qualifier='path']"/>
                        <xsl:text>/</xsl:text>
                        <xsl:value-of select="."/>
                    </xsl:attribute>
                </link>
            </xsl:for-each>

            <!-- Add syndication feeds -->
            <xsl:for-each select="/dri:document/dri:meta/dri:pageMeta/dri:metadata[@element='feed']">
                <link rel="alternate" type="application">
                    <xsl:attribute name="type">
                        <xsl:text>application/</xsl:text>
                        <xsl:value-of select="@qualifier"/>
                    </xsl:attribute>
                    <xsl:attribute name="href">
                        <xsl:value-of select="."/>
                    </xsl:attribute>
                </link>
            </xsl:for-each>

            <!-- The following javascript removes the default text of empty text areas when they are focused on or submitted -->
            <!-- There is also javascript to disable submitting a form when the 'enter' key is pressed. -->
            <script type="text/javascript"> //Clear default text of emty text areas on focus
                function tFocus(element) { if (element.value ==
                    '<i18n:text>xmlui.dri2xhtml.default.textarea.value</i18n:text>'){element.value='';}
                } //Clear default text of emty text areas on submit function tSubmit(form) { var
                defaultedElements = document.getElementsByTagName("textarea"); for (var i=0; i !=
                defaultedElements.length; i++){ if (defaultedElements[i].value ==
                    '<i18n:text>xmlui.dri2xhtml.default.textarea.value</i18n:text>'){
                defaultedElements[i].value='';}} } //Disable pressing 'enter' key to submit a form
                (otherwise pressing 'enter' causes a submission to start over) function
                disableEnterKey(e) { var key; if(window.event) key = window.event.keyCode;
                //Internet Explorer else key = e.which; //Firefox and Netscape if(key == 13) //if
                "Enter" pressed, then disable! return false; else return true; } </script>

            <!-- Add javascipt  -->
            <xsl:for-each
                select="/dri:document/dri:meta/dri:pageMeta/dri:metadata[@element='javascript']">
                <script type="text/javascript">
                    <xsl:attribute name="src">
                        <xsl:value-of
                            select="/dri:document/dri:meta/dri:pageMeta/dri:metadata[@element='contextPath'][not(@qualifier)]"/>
                        <xsl:text>/themes/</xsl:text>
                        <xsl:value-of
                            select="/dri:document/dri:meta/dri:pageMeta/dri:metadata[@element='theme'][@qualifier='path']"/>
                        <xsl:text>/</xsl:text>
                        <xsl:value-of select="."/>
                    </xsl:attribute> &#160; </script>
            </xsl:for-each>
            <!-- Add the title in -->
            <xsl:variable name="page_title"
                select="/dri:document/dri:meta/dri:pageMeta/dri:metadata[@element='title']"/>
            <title>
                <xsl:choose>
                    <xsl:when test="not($page_title)">
                        <xsl:text>MINDS@UW</xsl:text>
                    </xsl:when>
                    <xsl:otherwise>
                        <xsl:copy-of select="$page_title/node()"/>
                    </xsl:otherwise>
                </xsl:choose>
            </title>
        </head>
    </xsl:template>

    <!-- Changed to add the "current" class. -DS -->
    <xsl:template match="dri:xref" priority="10">
        <a>
            <xsl:attribute name="href">
                <xsl:value-of select="@target"/>
            </xsl:attribute>
            <xsl:if test="($currentpage)=(@target)">
                <xsl:attribute name="class">
                    <xsl:text>current</xsl:text>
                </xsl:attribute>
            </xsl:if>
            <xsl:apply-templates/>
        </a>
    </xsl:template>

    <!-- Changed to add class="first-link" to the first breadcrumb in the list. -DS -->
    <xsl:template match="dri:trail">
        <li>
            <xsl:attribute name="class">
                <xsl:text>ds-trail-link </xsl:text>
                <xsl:if test="position()=1">
                    <!--  fixed -DS -->
                    <xsl:text>first-link </xsl:text>
                </xsl:if>
                <xsl:if test="position()=last()">
                    <xsl:text>last-link</xsl:text>
                </xsl:if>
            </xsl:attribute>
            <!-- Determine whether we are dealing with a link or plain text trail link -->
            <xsl:choose>
                <xsl:when test="./@target">
                    <a>
                        <xsl:attribute name="href">
                            <xsl:value-of select="./@target"/>
                        </xsl:attribute>
                        <xsl:apply-templates/>
                    </a>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:apply-templates/>
                </xsl:otherwise>
            </xsl:choose>
            <!-- add caret -DS -->
            <xsl:choose>
                <xsl:when test="position()!=last()">
                    <xsl:text> &gt; </xsl:text>
                </xsl:when>
                <xsl:otherwise>
                    <!-- do nothing, to fix doubling problem? -DS -->
                </xsl:otherwise>
            </xsl:choose>
        </li>
    </xsl:template>

    <!-- Its name, for the record: <xsl:template match="dim:dim" mode="itemSummaryList-DIM"> -->
    <!-- Altered to turn author names into browse links. -DS -->
    <xsl:template match="dim:dim" mode="itemSummaryList-DIM">
        <xsl:variable name="itemWithdrawn" select="@withdrawn"/>

        <div class="artifact-description">
            <div class="artifact-title">
                <a>
                    <xsl:attribute name="href">
                        <xsl:choose>
                            <xsl:when test="$itemWithdrawn">
                                <xsl:value-of select="ancestor::mets:METS/@OBJEDIT"/>
                            </xsl:when>
                            <xsl:otherwise>
                                <xsl:value-of select="ancestor::mets:METS/@OBJID"/>
                            </xsl:otherwise>
                        </xsl:choose>
                    </xsl:attribute>
                    <xsl:choose>
                        <xsl:when test="dim:field[@element='title']">
                            <xsl:value-of select="dim:field[@element='title'][1]/child::node()"/>
                        </xsl:when>
                        <xsl:otherwise>
                            <i18n:text>xmlui.dri2xhtml.METS-1.0.no-title</i18n:text>
                        </xsl:otherwise>
                    </xsl:choose>
                </a>
            </div>

            <!-- FIXME: This template should pull out dc.contributor.author and dc.creator, then make a list of all of them.
                 Instead, it picks EITHER dc.contributor.author OR dc.creator OR other-labeled dc.contributor. 
                 Should probably have better way to cope with other dc.contributor as well. The trick is that some dc.contribs
                 feel "author-y" (composers, maybe editors) and others don't (e.g. translators, advisors).
                 Three Minute Hate on Dublin Core. -DS -->
            <div class="artifact-info">
                <xsl:call-template name="COinS">
                    <xsl:with-param name="dim" select="."/>
                </xsl:call-template>
                <span class="author">
                    <xsl:choose>
                        <xsl:when test="dim:field[@element='contributor'][@qualifier='author']">
                            <xsl:for-each
                                select="dim:field[@element='contributor'][@qualifier='author']">
                                <a>
                                    <xsl:attribute name="href">
                                        <xsl:value-of
                                            select="concat($context-path,'/browse?type=author&amp;value=')"/>
                                        <xsl:value-of select="./text()"/>
                                    </xsl:attribute>
                                    <xsl:value-of select="text()"/>
                                </a>
                                <xsl:if
                                    test="count(following-sibling::dim:field[@element='contributor'][@qualifier='author']) != 0">
                                    <xsl:text>; </xsl:text>
                                </xsl:if>
                            </xsl:for-each>
                        </xsl:when>
                        <xsl:when test="dim:field[@element='creator']">
                            <xsl:for-each select="dim:field[@element='creator']">
                                <a>
                                    <xsl:attribute name="href">
                                        <xsl:value-of
                                            select="concat($context-path,'/browse?type=author&amp;value=')"/>
                                        <xsl:value-of select="./text()"/>
                                    </xsl:attribute>
                                    <xsl:value-of select="text()"/>
                                </a>
                                <xsl:if
                                    test="count(following-sibling::dim:field[@element='creator']) != 0">
                                    <xsl:text>; </xsl:text>
                                </xsl:if>
                            </xsl:for-each>
                        </xsl:when>
                        <!-- adding editor because I can; again, not ideal, but should handle a lot of edge cases now being handled VERY badly -DS -->
                        <xsl:when test="dim:field[@element='contributor'][@qualifier='editor']">
                            <xsl:for-each
                                select="dim:field[@element='contributor'][@qualifier='editor']">
                                <a>
                                    <xsl:attribute name="href">
                                        <xsl:value-of
                                            select="concat($context-path,'/browse?type=author&amp;value=')"/>
                                        <xsl:value-of select="./text()"/>
                                    </xsl:attribute>
                                    <xsl:value-of select="text()"/>
                                </a>
                                <xsl:text> (editor)</xsl:text>
                                <xsl:if
                                    test="count(following-sibling::dim:field[@element='contributor'][@qualifier='editor']) != 0">
                                    <xsl:text>; </xsl:text>
                                </xsl:if>
                            </xsl:for-each>
                        </xsl:when>
                        <!-- adding "researcher" and "respondent" for folklore -DS -->
                        <xsl:when
                            test="dim:field[@element='contributor'][@qualifier='respondent'] or dim:field[@element='contributor'][@qualifier='researcher'] or dim:field[@element='contributor'][@qualifier='interviewer'] or dim:field[@element='contributor'][@qualifier='interviewee']">
                            <xsl:for-each
                                select="dim:field[@element='contributor'][@qualifier='researcher' or @qualifier='interviewer']">
                                <a>
                                    <xsl:attribute name="href">
                                        <xsl:value-of
                                            select="concat($context-path,'/browse?type=author&amp;value=')"/>
                                        <xsl:value-of select="./text()"/>
                                    </xsl:attribute>
                                    <xsl:value-of select="text()"/>

                                </a>
                                <xsl:text> (researcher)</xsl:text>
                                <xsl:if
                                    test="count(following-sibling::dim:field[@element='contributor'][@qualifier='researcher' or @qualifier='interviewer']) != 0 or following-sibling::dim:field[@element='contributor'][@qualifier='respondent' or @qualifier='interviewee'] != 0">
                                    <xsl:text>; </xsl:text>
                                </xsl:if>
                            </xsl:for-each>
                            <xsl:for-each
                                select="dim:field[@element='contributor'][@qualifier='respondent' or @qualifier='interviewee']">
                                <a>
                                    <xsl:attribute name="href">
                                        <xsl:value-of
                                            select="concat($context-path,'/browse?type=author&amp;value=')"/>
                                        <xsl:value-of select="./text()"/>
                                    </xsl:attribute>
                                    <xsl:value-of select="text()"/>
                                </a>
                                <xsl:text> (respondent)</xsl:text>
                                <xsl:if
                                    test="count(following-sibling::dim:field[@element='contributor'][@qualifier='respondent' or @qualifier='interviewee']) != 0">
                                    <xsl:text>; </xsl:text>
                                </xsl:if>
                            </xsl:for-each>
                        </xsl:when>
                        <xsl:when test="dim:field[@element='contributor'][@qualifier='other']">
                            <xsl:for-each
                                select="dim:field[@element='contributor'][@qualifier='other']">
                                <a>
                                    <xsl:attribute name="href">
                                        <xsl:value-of
                                            select="concat($context-path,'/browse?type=author&amp;value=')"/>
                                        <xsl:value-of select="./text()"/>
                                    </xsl:attribute>
                                    <xsl:value-of select="./text()"/>
                                </a>
                                <xsl:if
                                    test="count(following-sibling::dim:field[@element='contributor']) != 0">
                                    <xsl:text>; </xsl:text>
                                </xsl:if>
                            </xsl:for-each>
                        </xsl:when>
                        <xsl:when test="dim:field[@element='contributor'][not(@qualifier)]">
                            <xsl:for-each
                                select="dim:field[@element='contributor'][not(@qualifier)]">
                                <a>
                                    <xsl:attribute name="href">
                                        <xsl:value-of
                                            select="concat($context-path,'/browse?type=author&amp;value=')"/>
                                        <xsl:value-of select="./text()"/>
                                    </xsl:attribute>
                                    <xsl:value-of select="./text()"/>
                                </a>
                                <xsl:if
                                    test="count(following-sibling::dim:field[@element='contributor']) != 0">
                                    <xsl:text>; </xsl:text>
                                </xsl:if>
                            </xsl:for-each>
                        </xsl:when>
                        <xsl:otherwise>
                            <i18n:text>xmlui.dri2xhtml.METS-1.0.no-author</i18n:text>
                        </xsl:otherwise>
                    </xsl:choose>
                </span>
                <xsl:text> </xsl:text>
                <xsl:if
                    test="dim:field[@element='date' and @qualifier='issued'] or dim:field[@element='publisher']">
                    <span class="publisher-date">
                        <xsl:text>(</xsl:text>
                        <xsl:if test="dim:field[@element='publisher']">
                            <span class="publisher">
                                <xsl:for-each select="dim:field[@element='publisher']">
                                    <xsl:value-of select="./text()"/>
                                    <xsl:if
                                        test="count(following-sibling::dim:field[@element='publisher']) != 0">
                                        <xsl:text>, </xsl:text>
                                    </xsl:if>
                                </xsl:for-each>
                            </span>
                            <xsl:text>; </xsl:text>
                        </xsl:if>
                        <span class="date">
                            <xsl:call-template name="mungeDate">
                                <xsl:with-param name="date">
                                    <xsl:value-of
                                        select="substring(dim:field[@element='date' and @qualifier='issued']/child::node(),1,10)"
                                    />
                                </xsl:with-param>
                            </xsl:call-template>

                        </span>
                        <xsl:text>)</xsl:text>
                    </span>
                </xsl:if>
            </div>
        </div>

    </xsl:template>

    <!-- An item rendered in the summaryView pattern. This is the default way to view a DSpace item in Manakin. -->
    <!-- Altered to move file info on top -DS -->
    <xsl:template name="itemSummaryView-DIM">
        <xsl:variable name="context" select="."/>
        <!-- Moved up over metadata. Changed from table into list. -DS -->
        <h2 id="fileshead">
            <i18n:text>xmlui.dri2xhtml.METS-1.0.item-files-head</i18n:text>
        </h2>
        <div id="filelist">
            <xsl:choose>
                <xsl:when test="not(./mets:fileSec/mets:fileGrp[@USE='CONTENT'])">
                    <!-- when there are no files -DS -->
                    <p>
                        <i18n:text>xmlui.dri2xhtml.METS-1.0.item-no-files</i18n:text>
                    </p>
                </xsl:when>
                <xsl:otherwise>
                    <ul class="file-list">
                        <!-- First, figure out if there is a primary bitstream -->
                        <xsl:variable name="primary"
                            select="./mets:structMap[@TYPE='LOGICAL']/mets:div[@TYPE='DSpace Item']/mets:fptr/@FILEID"/>
                        <xsl:choose>
                            <!-- If one exists and it's of text/html MIME type, only display the primary bitstream -->
                            <xsl:when
                                test="./mets:fileSec/mets:fileGrp[@USE='CONTENT']/mets:file[@ID=$primary]/@MIMETYPE='text/html'">
                                <xsl:call-template name="buildBitstreamRow">
                                    <xsl:with-param name="context" select="$context"/>
                                    <xsl:with-param name="file"
                                        select="./mets:fileSec/mets:fileGrp[@USE='CONTENT']/mets:file[@ID=$primary]"
                                    />
                                </xsl:call-template>
                            </xsl:when>
                            <!-- Otherwise, iterate over and display all of them -->
                            <xsl:otherwise>
                                <xsl:for-each
                                    select="./mets:fileSec/mets:fileGrp[@USE='CONTENT']/mets:file">
                                    <xsl:sort select="./mets:FLocat[@LOCTYPE='URL']/@xlink:title"/>
                                    <xsl:call-template name="buildBitstreamRow">
                                        <xsl:with-param name="context" select="$context"/>
                                    </xsl:call-template>
                                </xsl:for-each>
                            </xsl:otherwise>
                        </xsl:choose>
                    </ul>
                </xsl:otherwise>
            </xsl:choose>
            <!-- Generate the bitstream information from the file section -->
        </div>

        <!-- Generate the info about the item from the metadata section -->
        <xsl:apply-templates
            select="./mets:dmdSec/mets:mdWrap[@OTHERMDTYPE='DIM']/mets:xmlData/dim:dim"
            mode="itemSummaryView-DIM"/>

        <!-- Generate the license information from the file section -->
        <!-- CC licensing only on the summary page -DS -->
        <xsl:apply-templates select="./mets:fileSec/mets:fileGrp[@USE='CC-LICENSE']"/>

    </xsl:template>

    <!-- Utility template used by the item's summary view to list each bitstream -->
    <!-- NO CAPES! -Edna Mode. NO TABLES! -DS -->
    <xsl:template name="buildBitstreamRow">
        <xsl:param name="context" select="."/>
        <xsl:param name="file" select="."/>
        <li>
            <xsl:if
                test="$context/mets:METS/mets:fileSec/mets:fileGrp[@USE='THUMBNAIL']/
                mets:file[@GROUP_ID=current()/@GROUP_ID]">
                <a class="image-link">
                    <xsl:attribute name="href">
                        <xsl:value-of select="$file/mets:FLocat[@LOCTYPE='URL']/@xlink:href"/>
                    </xsl:attribute>
                    <img alt="Thumbnail">
                        <xsl:attribute name="src">
                            <xsl:value-of
                                select="$context/mets:METS/mets:fileSec/mets:fileGrp[@USE='THUMBNAIL']/
                                mets:file[@GROUP_ID=current()/@GROUP_ID]/mets:FLocat[@LOCTYPE='URL']/@xlink:href"
                            />
                        </xsl:attribute>
                    </img>
                </a>
            </xsl:if>
            <a>
                <xsl:attribute name="href">
                    <xsl:value-of select="$file/mets:FLocat[@LOCTYPE='URL']/@xlink:href"/>
                </xsl:attribute>
                <xsl:attribute name="title">
                    <xsl:value-of select="$file/mets:FLocat[@LOCTYPE='URL']/@xlink:title"/>
                </xsl:attribute>
                <xsl:choose>
                    <!-- use the bitstream label instead of the filename as the link text, when available -DS -->
                    <xsl:when test="$file/mets:FLocat[@LOCTYPE='URL']/@xlink:label">
                        <xsl:value-of select="$file/mets:FLocat[@LOCTYPE='URL']/@xlink:label"/>
                    </xsl:when>
                    <xsl:when
                        test="string-length($file/mets:FLocat[@LOCTYPE='URL']/@xlink:title) > 50">
                        <xsl:variable name="title_length"
                            select="string-length($file/mets:FLocat[@LOCTYPE='URL']/@xlink:title)"/>
                        <xsl:value-of
                            select="substring($file/mets:FLocat[@LOCTYPE='URL']/@xlink:title,1,15)"/>
                        <xsl:text> ... </xsl:text>
                        <xsl:value-of
                            select="substring($file/mets:FLocat[@LOCTYPE='URL']/@xlink:title,$title_length - 25,$title_length)"
                        />
                    </xsl:when>
                    <xsl:otherwise>
                        <xsl:value-of select="$file/mets:FLocat[@LOCTYPE='URL']/@xlink:title"/>
                    </xsl:otherwise>
                </xsl:choose>
            </a>
            <xsl:text> (</xsl:text>
            <!-- File size always comes in bytes and thus needs conversion -->
            <xsl:choose>
                <xsl:when test="$file/@SIZE &lt; 1000">
                    <xsl:value-of select="$file/@SIZE"/>
                    <i18n:text>xmlui.dri2xhtml.METS-1.0.size-bytes</i18n:text>
                </xsl:when>
                <xsl:when test="$file/@SIZE &lt; 1000000">
                    <xsl:value-of select="substring(string($file/@SIZE div 1000),1,5)"/>
                    <i18n:text>xmlui.dri2xhtml.METS-1.0.size-kilobytes</i18n:text>
                </xsl:when>
                <xsl:when test="$file/@SIZE &lt; 1000000000">
                    <xsl:value-of select="substring(string($file/@SIZE div 1000000),1,5)"/>
                    <i18n:text>xmlui.dri2xhtml.METS-1.0.size-megabytes</i18n:text>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:value-of select="substring(string($file/@SIZE div 1000000000),1,5)"/>
                    <i18n:text>xmlui.dri2xhtml.METS-1.0.size-gigabytes</i18n:text>
                </xsl:otherwise>
            </xsl:choose>
            <xsl:text> </xsl:text>
            <!-- Currently format carries forward the mime type. In the original DSpace, this
                would get resolved to an application via the Bitstream Registry, but we are
                constrained by the capabilities of METS and can't really pass that info through. 
                Lo, here is a Big Ugly Hack. -DS -->
            <xsl:call-template name="getFileTypeDesc">
                <xsl:with-param name="mimetype">
                    <xsl:value-of select="substring-before($file/@MIMETYPE,'/')"/>
                    <xsl:text>/</xsl:text>
                    <xsl:value-of select="substring-after($file/@MIMETYPE,'/')"/>
                </xsl:with-param>
            </xsl:call-template>
            <xsl:text>)</xsl:text>
        </li>
    </xsl:template>
    <!--
        Display an overview of an item, this is used on the /handle/xxxx/yyyy pages for items.         
        
        Heavily altered. -DS
    -->
    <xsl:template match="dim:dim" mode="itemSummaryView-DIM">

        <dl class="ds-includeSet">
            <!-- Title only if we're on a workflow page. -DS -->
            <xsl:if test="dim:field[@element='title'][not(@qualifier)]">
                <xsl:if test="substring-before($currentpage,'workflow') !=''">
                    <dt>
                        <i18n:text>xmlui.dri2xhtml.METS-1.0.item-title</i18n:text>
                    </dt>
                    <dd>
                        <xsl:value-of select="dim:field[@element='title'][not(@qualifier)]"/>
                    </dd>
                </xsl:if>
            </xsl:if>
            <xsl:if
                test="dim:field[@element='contributor'][@qualifier='author'] or dim:field[@element='creator']">
                <dt>
                    <i18n:text>xmlui.dri2xhtml.METS-1.0.item-author</i18n:text>
                </dt>
                <dd>
                    <span class="author">
                        <xsl:choose>
                            <xsl:when test="dim:field[@element='contributor'][@qualifier='author']">
                                <xsl:for-each
                                    select="dim:field[@element='contributor'][@qualifier='author']">
                                    <a>
                                        <xsl:attribute name="href">
                                            <xsl:value-of
                                                select="concat($context-path,'/browse?type=author&amp;value=')"/>
                                            <xsl:value-of select="./text()"/>
                                        </xsl:attribute>
                                        <xsl:value-of select="./text()"/>
                                    </a>
                                    <xsl:if
                                        test="count(following-sibling::dim:field[@element='contributor'][@qualifier='author']) != 0">
                                        <xsl:text>; </xsl:text>
                                    </xsl:if>
                                </xsl:for-each>
                            </xsl:when>
                            <xsl:when test="dim:field[@element='creator']">
                                <xsl:for-each select="dim:field[@element='creator']">
                                    <a>
                                        <xsl:attribute name="href">
                                            <xsl:value-of
                                                select="concat($context-path,'/browse?type=author&amp;value=')"/>
                                            <xsl:value-of select="./text()"/>
                                        </xsl:attribute>
                                        <xsl:value-of select="./text()"/>
                                    </a>
                                    <xsl:if
                                        test="count(following-sibling::dim:field[@element='creator']) != 0">
                                        <xsl:text>; </xsl:text>
                                    </xsl:if>
                                </xsl:for-each>
                            </xsl:when>
                            <xsl:when test="dim:field[@element='contributor'][not(@qualifier)]">
                                <xsl:for-each
                                    select="dim:field[@element='contributor'][not(@qualifier)]">
                                    <a>
                                        <xsl:attribute name="href">
                                            <xsl:value-of
                                                select="concat($context-path,'/browse?type=author&amp;value=')"/>
                                            <xsl:value-of select="./text()"/>
                                        </xsl:attribute>
                                        <xsl:value-of select="./text()"/>
                                    </a>
                                    <xsl:if
                                        test="count(following-sibling::dim:field[@element='contributor'][not(@qualifier)]) != 0">
                                        <xsl:text>; </xsl:text>
                                    </xsl:if>
                                </xsl:for-each>
                            </xsl:when>
                            <xsl:otherwise>
                                <i18n:text>xmlui.dri2xhtml.METS-1.0.no-author</i18n:text>
                            </xsl:otherwise>
                        </xsl:choose>
                    </span>
                </dd>
            </xsl:if>
            <xsl:if test="dim:field[@element='contributor'][@qualifier='translator']">
                <dt>
                    <i18n:text>xmlui.dri2xhtml-METS-1.0.translator</i18n:text>
                </dt>
                <dd>
                    <xsl:for-each
                        select="dim:field[@element='contributor'][@qualifier='translator']">
                        <a>
                            <xsl:attribute name="href">
                                <xsl:value-of
                                    select="concat($context-path,'/browse?type=author&amp;value=')"/>
                                <xsl:value-of select="./text()"/>
                            </xsl:attribute>
                            <xsl:value-of select="./text()"/>
                        </a>
                        <xsl:if
                            test="count(following-sibling::dim:field[@element='contributor'][@qualifier='translator']) != 0">
                            <xsl:text>; </xsl:text>
                        </xsl:if>
                    </xsl:for-each>
                </dd>
            </xsl:if>
            <xsl:if test="dim:field[@element='contributor'][@qualifier='advisor']">
                <dt>
                    <i18n:text>xmlui.dri2xhtml-METS-1.0.advisor</i18n:text>
                </dt>
                <dd>
                    <xsl:for-each select="dim:field[@element='contributor'][@qualifier='advisor']">
                        <a>
                            <xsl:attribute name="href">
                                <xsl:value-of
                                    select="concat($context-path,'/browse?type=author&amp;value=')"/>
                                <xsl:value-of select="./text()"/>
                            </xsl:attribute>
                            <xsl:value-of select="./text()"/>
                        </a>
                        <xsl:if
                            test="count(following-sibling::dim:field[@element='contributor'][@qualifier='advisor']) != 0">
                            <xsl:text>; </xsl:text>
                        </xsl:if>
                    </xsl:for-each>
                </dd>
            </xsl:if>
            <xsl:if test="dim:field[@element='contributor'][@qualifier='editor']">
                <dt>
                    <i18n:text>xmlui.dri2xhtml-METS-1.0.editor</i18n:text>
                </dt>
                <dd>
                    <xsl:for-each select="dim:field[@element='contributor'][@qualifier='editor']">
                        <a>
                            <xsl:attribute name="href">
                                <xsl:value-of
                                    select="concat($context-path,'/browse?type=author&amp;value=')"/>
                                <xsl:value-of select="./text()"/>
                            </xsl:attribute>
                            <xsl:value-of select="./text()"/>
                        </a>
                        <xsl:if
                            test="count(following-sibling::dim:field[@element='contributor'][@qualifier='editor']) != 0">
                            <xsl:text>; </xsl:text>
                        </xsl:if>
                    </xsl:for-each>
                </dd>
            </xsl:if>
            <xsl:if test="dim:field[@element='contributor'][@qualifier='researcher' or @qualifier='interviewer']">
                <dt>
                    <xsl:text>Researcher</xsl:text>
                </dt>
                <dd>
                    <xsl:for-each
                        select="dim:field[@element='contributor'][@qualifier='researcher' or @qualifier='interviewer']">
                        <a>
                            <xsl:attribute name="href">
                                <xsl:value-of
                                    select="concat($context-path,'/browse?type=author&amp;value=')"/>
                                <xsl:value-of select="./text()"/>
                            </xsl:attribute>
                            <xsl:value-of select="./text()"/>
                        </a>
                        <xsl:if
                            test="count(following-sibling::dim:field[@element='contributor'][@qualifier='researcher' or @qualifier='interviewer']) != 0">
                            <xsl:text>; </xsl:text>
                        </xsl:if>
                    </xsl:for-each>
                </dd>
            </xsl:if>
            <xsl:if test="dim:field[@element='contributor'][@qualifier='respondent' or @qualifier='interviewee']">
                <dt>
                    <xsl:text>Respondent</xsl:text>
                </dt>
                <dd>
                    <xsl:for-each
                        select="dim:field[@element='contributor'][@qualifier='respondent' or @qualifier='interviewee']">
                        <a>
                            <xsl:attribute name="href">
                                <xsl:value-of
                                    select="concat($context-path,'/browse?type=author&amp;value=')"/>
                                <xsl:value-of select="./text()"/>
                            </xsl:attribute>
                            <xsl:value-of select="./text()"/>
                        </a>
                        <xsl:if
                            test="count(following-sibling::dim:field[@element='contributor'][@qualifier='respondent' or @qualifier='interviewee']) != 0">
                            <xsl:text>; </xsl:text>
                        </xsl:if>
                    </xsl:for-each>
                </dd>
            </xsl:if>
            <xsl:if test="dim:field[@element='course'][@qualifier='number']">
                <!-- added for folklore; not repeatable -DS -->
                <dt>
                    <xsl:text>Course #</xsl:text>
                </dt>
                <dd>
                    <xsl:value-of select="dim:field[@element='course'][@qualifier='number']"/>
                </dd>
            </xsl:if>
            <xsl:if test="dim:field[@element='degree'][@qualifier='level']">
                <dt>
                    <xsl:text>Degree</xsl:text>
                </dt>
                <dd>
                    <xsl:value-of select="dim:field[@element='degree'][@qualifier='level']"/>
                    <xsl:if test="dim:field[@element='degree'][@qualifier='discipline']">
                        <xsl:text>, </xsl:text>
                        <xsl:value-of select="dim:field[@element='degree'][@qualifier='discipline']"
                        />
                    </xsl:if>
                </dd>
            </xsl:if>
            <xsl:if test="dim:field[@element='publisher']">
                <dt>
                    <xsl:text>Publisher</xsl:text>
                </dt>
                <dd>
                    <xsl:for-each select="dim:field[@element='publisher']">
                        <xsl:value-of select="./text()"/>
                        <xsl:if
                            test="count(following-sibling::dim:field[@element='publisher']) != 0">
                            <xsl:text>; </xsl:text>
                        </xsl:if>
                    </xsl:for-each>
                </dd>
            </xsl:if>
            <xsl:if test="dim:field[@element='identifier'][@qualifier='citation']">
                <dt>
                    <i18n:text>xmlui.dri2xhtml-METS-1.0.citation</i18n:text>
                </dt>
                <dd>
                    <xsl:value-of
                        select="dim:field[@element='identifier'][@qualifier='citation']/text()"/>
                </dd>
            </xsl:if>
            <xsl:if test="dim:field[@element='date' and @qualifier='issued']">
                <dt>
                    <i18n:text>xmlui.dri2xhtml.METS-1.0.item-date</i18n:text>
                </dt>
                <dd>
                    <xsl:for-each select="dim:field[@element='date' and @qualifier='issued']">
                        <xsl:call-template name="mungeDate">
                            <xsl:with-param name="date">
                                <xsl:value-of select="substring(./node(),1,10)"/>
                            </xsl:with-param>
                        </xsl:call-template>
                        <xsl:if
                            test="count(following-sibling::dim:field[@element='date' and @qualifier='issued']) != 0">
                            <xsl:text>; </xsl:text>
                        </xsl:if>
                    </xsl:for-each>
                </dd>
            </xsl:if>
            <!-- not separating out CVs yet, but could in future -DS -->
            <xsl:if test="dim:field[@element='subject']">
                <dt>
                    <i18n:text>xmlui.dri2xhtml-METS-1.0.subject</i18n:text>
                </dt>
                <dd>
                    <xsl:for-each select="dim:field[@element='subject']">
                        <a>
                            <xsl:attribute name="href">
                                <xsl:value-of
                                    select="concat($context-path,'/browse?type=subject&amp;value=')"/>
                                <xsl:value-of select="./text()"/>
                            </xsl:attribute>
                            <xsl:value-of select="./text()"/>
                        </a>
                        <xsl:if test="count(following-sibling::dim:field[@element='subject']) != 0">
                            <xsl:text>; </xsl:text>
                        </xsl:if>
                    </xsl:for-each>
                </dd>
            </xsl:if>
            <!-- Kill the fields if they're empty. -DS -->
            <xsl:if test="dim:field[@element='relation'][@qualifier='ispartofseries']">
                <xsl:for-each select="dim:field[@element='relation'][@qualifier='ispartofseries']">
                    <dt>
                        <i18n:text>xmlui.dri2xhtml-METS-1.0.ispartofseries</i18n:text>
                    </dt>
                    <dd>
                        <xsl:value-of select="./text()"/>
                        <xsl:if
                            test="count(following-sibling::dim:field[@element='relation' and @qualifier='ispartofseries']) != 0">
                            <xsl:text>; </xsl:text>
                        </xsl:if>
                    </dd>
                </xsl:for-each>
            </xsl:if>
            <xsl:if test="dim:field[@element='relation' and @qualifier='ispartof']">
                <dt>
                    <i18n:text>xmlui.dri2xhtml-METS-1.0.ispartof</i18n:text>
                </dt>
                <dd>
                    <xsl:for-each select="dim:field[@element='relation' and @qualifier='ispartof']">
                        <xsl:value-of select="./text()"/>
                        <xsl:if
                            test="count(following-sibling::dim:field[@element='relation' and @qualifier='ispartof']) != 0">
                            <xsl:text>; </xsl:text>
                        </xsl:if>
                    </xsl:for-each>
                </dd>
            </xsl:if>

            <xsl:if test="dim:field[@element='description'][@qualifier='abstract']">
                <dt>
                    <i18n:text>xmlui.dri2xhtml.METS-1.0.item-abstract</i18n:text>
                </dt>
                <dd>
                    <xsl:value-of
                        select="dim:field[@element='description' and @qualifier='abstract']/text()"
                    />
                </dd>
            </xsl:if>
            <xsl:if test="dim:field[@element='description'][@qualifier='tableofcontents']">
                <dt>
                    <i18n:text>xmlui.dri2xhtml.METS-1.0.item-toc</i18n:text>
                </dt>
                <dd>
                    <xsl:value-of
                        select="dim:field[@element='description' and @qualifier='tableofcontents']/text()"
                    />
                </dd>
            </xsl:if>
            <xsl:if test="dim:field[@element='description'][not(@qualifier)]">
                <dt>
                    <i18n:text>xmlui.dri2xhtml.METS-1.0.item-description</i18n:text>
                </dt>
                <dd>
                    <xsl:if
                        test="count(dim:field[@element='description' and not(@qualifier)]) &gt; 1 and not(count(dim:field[@element='description' and @qualifier='abstract']) &gt; 1)">
                        <hr class="metadata-seperator"/>
                    </xsl:if>
                    <xsl:for-each select="dim:field[@element='description' and not(@qualifier)]">
                        <xsl:value-of select="./text()"/>
                        <xsl:if
                            test="count(following-sibling::dim:field[@element='description' and not(@qualifier)]) != 0">
                            <hr class="metadata-seperator"/>
                        </xsl:if>
                    </xsl:for-each>
                    <xsl:if
                        test="count(dim:field[@element='description' and not(@qualifier)]) &gt; 1">
                        <hr class="metadata-seperator"/>
                    </xsl:if>
                </dd>
            </xsl:if>
            <xsl:if test="dim:field[@element='description'][@qualifier='sponsorship']">
                <dt>
                    <i18n:text>xmlui.dri2xhtml.METS-1.0.item-sponsor</i18n:text>
                </dt>
                <dd>
                    <xsl:value-of
                        select="dim:field[@element='description' and @qualifier='sponsorship']/text()"
                    />
                </dd>
            </xsl:if>
            <xsl:if test="dim:field[@element='identifier' and @qualifier='uri']">
                <xsl:for-each select="dim:field[@element='identifier' and @qualifier='uri']">
                    <dt>
                        <i18n:text>xmlui.dri2xhtml.METS-1.0.item-uri</i18n:text>
                    </dt>
                    <dd>

                        <a>
                            <xsl:attribute name="href">
                                <xsl:value-of select="./text()"/>
                            </xsl:attribute>
                            <xsl:value-of select="./text()"/>
                        </a>
                        <xsl:text>&#160;</xsl:text>
                    </dd>
                </xsl:for-each>
            </xsl:if>
            <!-- TODO: fixme -DS -->
            <!--<dt><i18n:text>xmlui.ArtifactBrowser.ItemViewer.head_parent_collections</i18n:text></dt>
            <dd>&#0160;</dd>-->
            <dt>Export</dt>
            <dd>
                <a href="{$refworksLink}">
                    <xsl:comment>
                        <xsl:value-of select="$refworksLink"/>
                    </xsl:comment>
                    <i18n:text>xmlui.ArtifactBrowser.ItemViewer.refworks_export</i18n:text>
                </a>
                <xsl:text>&#160;</xsl:text>
            </dd>
        </dl>
        <!-- Putting COinS here -DS -->
        <xsl:call-template name="COinS">
            <xsl:with-param name="dim" select="."/>
        </xsl:call-template>
    </xsl:template>

    <xsl:template match="/dri:document/dri:body/dri:div/dri:p[@rend='refworks-export']"
        ><!-- do nothing -DS --></xsl:template>

    <!-- Fixing pagination 
        Fixed to eliminate plural on 1 item. -DS -->
    <xsl:template match="@pagination">
        <xsl:param name="position"/>
        <xsl:choose>
            <xsl:when test=". = 'simple'">
                <div class="pagination {$position}">
                    <xsl:if test="parent::node()/@previousPage">
                        <a class="previous-page-link">
                            <xsl:attribute name="href">
                                <xsl:value-of select="parent::node()/@previousPage"/>
                            </xsl:attribute>
                            <i18n:text>xmlui.dri2xhtml.structural.pagination-previous</i18n:text>
                        </a>
                    </xsl:if>
                    <p class="pagination-info">
                        <xsl:choose>
                            <xsl:when
                                test="parent::node()/@firstItemIndex=parent::node()/@lastItemIndex">
                                <i18n:text>xmlui.dri2xhtml.structural.pagination-one</i18n:text>
                                <!-- Showing 1 item. -->
                            </xsl:when>
                            <xsl:otherwise>
                                <i18n:translate>
                                    <i18n:text>xmlui.dri2xhtml.structural.pagination-info</i18n:text>
                                    <i18n:param>
                                        <xsl:value-of select="parent::node()/@firstItemIndex"/>
                                    </i18n:param>
                                    <i18n:param>
                                        <xsl:value-of select="parent::node()/@lastItemIndex"/>
                                    </i18n:param>
                                    <i18n:param>
                                        <xsl:value-of select="parent::node()/@itemsTotal"/>
                                    </i18n:param>
                                </i18n:translate>
                            </xsl:otherwise>
                        </xsl:choose>
                        <!--
                            <xsl:text>Now showing items </xsl:text>
                            <xsl:value-of select="parent::node()/@firstItemIndex"/>
                            <xsl:text>-</xsl:text>
                            <xsl:value-of select="parent::node()/@lastItemIndex"/>
                            <xsl:text> of </xsl:text>
                            <xsl:value-of select="parent::node()/@itemsTotal"/>
                        -->
                    </p>
                    <xsl:if test="parent::node()/@nextPage">
                        <a class="next-page-link">
                            <xsl:attribute name="href">
                                <xsl:value-of select="parent::node()/@nextPage"/>
                            </xsl:attribute>
                            <i18n:text>xmlui.dri2xhtml.structural.pagination-next</i18n:text>
                        </a>
                    </xsl:if>
                </div>
            </xsl:when>
            <xsl:when test=". = 'masked'">
                <div class="pagination-masked {$position}">
                    <xsl:if
                        test="not(parent::node()/@firstItemIndex = 0 or parent::node()/@firstItemIndex = 1)">
                        <a class="previous-page-link">
                            <xsl:attribute name="href">
                                <xsl:value-of
                                    select="substring-before(parent::node()/@pageURLMask,'{pageNum}')"/>
                                <xsl:value-of select="parent::node()/@currentPage - 1"/>
                                <xsl:value-of
                                    select="substring-after(parent::node()/@pageURLMask,'{pageNum}')"
                                />
                            </xsl:attribute>
                            <i18n:text>xmlui.dri2xhtml.structural.pagination-previous</i18n:text>
                        </a>
                    </xsl:if>
                    <p class="pagination-info">
                        <i18n:translate>
                            <i18n:text>xmlui.dri2xhtml.structural.pagination-info</i18n:text>
                            <i18n:param>
                                <xsl:value-of select="parent::node()/@firstItemIndex"/>
                            </i18n:param>
                            <i18n:param>
                                <xsl:value-of select="parent::node()/@lastItemIndex"/>
                            </i18n:param>
                            <i18n:param>
                                <xsl:value-of select="parent::node()/@itemsTotal"/>
                            </i18n:param>
                        </i18n:translate>
                    </p>
                    <ul class="pagination-links">
                        <xsl:if test="(parent::node()/@currentPage - 4) &gt; 0">
                            <li class="first-page-link">
                                <a>
                                    <xsl:attribute name="href">
                                        <xsl:value-of
                                            select="substring-before(parent::node()/@pageURLMask,'{pageNum}')"/>
                                        <xsl:text>1</xsl:text>
                                        <xsl:value-of
                                            select="substring-after(parent::node()/@pageURLMask,'{pageNum}')"
                                        />
                                    </xsl:attribute>
                                    <xsl:text>1</xsl:text>
                                </a>
                                <xsl:text> . . . </xsl:text>
                            </li>
                        </xsl:if>
                        <xsl:call-template name="offset-link">
                            <xsl:with-param name="pageOffset">-3</xsl:with-param>
                        </xsl:call-template>
                        <xsl:call-template name="offset-link">
                            <xsl:with-param name="pageOffset">-2</xsl:with-param>
                        </xsl:call-template>
                        <xsl:call-template name="offset-link">
                            <xsl:with-param name="pageOffset">-1</xsl:with-param>
                        </xsl:call-template>
                        <xsl:call-template name="offset-link">
                            <xsl:with-param name="pageOffset">0</xsl:with-param>
                        </xsl:call-template>
                        <xsl:call-template name="offset-link">
                            <xsl:with-param name="pageOffset">1</xsl:with-param>
                        </xsl:call-template>
                        <xsl:call-template name="offset-link">
                            <xsl:with-param name="pageOffset">2</xsl:with-param>
                        </xsl:call-template>
                        <xsl:call-template name="offset-link">
                            <xsl:with-param name="pageOffset">3</xsl:with-param>
                        </xsl:call-template>
                        <xsl:if
                            test="(parent::node()/@currentPage + 4) &lt;= (parent::node()/@pagesTotal)">
                            <li class="last-page-link">
                                <xsl:text> . . . </xsl:text>
                                <a>
                                    <xsl:attribute name="href">
                                        <xsl:value-of
                                            select="substring-before(parent::node()/@pageURLMask,'{pageNum}')"/>
                                        <xsl:value-of select="parent::node()/@pagesTotal"/>
                                        <xsl:value-of
                                            select="substring-after(parent::node()/@pageURLMask,'{pageNum}')"
                                        />
                                    </xsl:attribute>
                                    <xsl:value-of select="parent::node()/@pagesTotal"/>
                                </a>
                            </li>
                        </xsl:if>
                    </ul>
                    <xsl:if test="not(parent::node()/@lastItemIndex = parent::node()/@itemsTotal)">
                        <a class="next-page-link">
                            <xsl:attribute name="href">
                                <xsl:value-of
                                    select="substring-before(parent::node()/@pageURLMask,'{pageNum}')"/>
                                <xsl:value-of select="parent::node()/@currentPage + 1"/>
                                <xsl:value-of
                                    select="substring-after(parent::node()/@pageURLMask,'{pageNum}')"
                                />
                            </xsl:attribute>
                            <i18n:text>xmlui.dri2xhtml.structural.pagination-next</i18n:text>
                        </a>
                    </xsl:if>
                </div>
            </xsl:when>
        </xsl:choose>
    </xsl:template>

    <!-- Changing not to display at all if no content. -DS -->
    <xsl:template match="dri:div" priority="1">
        <xsl:variable name="divtype">
            <xsl:value-of select="@n"/>
        </xsl:variable>
        <!--<xsl:comment>@n=<xsl:value-of select="@n"/> @id=<xsl:value-of select="@id"/></xsl:comment>-->
        <xsl:choose>
            <!--  does this element have any children -->
            <xsl:when test="not(child::node())"/>
            <xsl:otherwise>
                <xsl:choose>
                    <xsl:when test="$divtype='collection-view'">
                        <xsl:variable name="data"
                            select="./mets:METS/mets:dmdSec/mets:mdWrap/mets:xmlData/dim:dim"/>
                        <xsl:choose>
                            <xsl:when
                                test="string-length(dim:field[@element='description'][not(@qualifier)]&gt;0) or string-length(dim:field[@element='rights'][not(@qualifier)])&gt;0">
                                <xsl:apply-templates/>
                            </xsl:when>
                            <xsl:otherwise><!-- DO NOTHING, damn it, or there will be an empty element that screws up layouts in Firefox -DS --></xsl:otherwise>
                        </xsl:choose>
                    </xsl:when>
                    <xsl:when test="$divtype='collection-recent-submission'">
                        <xsl:choose>
                            <xsl:when test="count(child::node())=1"/>
                            <!-- just a head node, no content, so ignore it -DS -->
                            <xsl:otherwise>
                                <!-- behave normally -DS -->
                                <xsl:apply-templates select="dri:head"/>
                                <xsl:apply-templates select="@pagination">
                                    <xsl:with-param name="position">top</xsl:with-param>
                                </xsl:apply-templates>
                                <div>
                                    <xsl:call-template name="standardAttributes">
                                        <xsl:with-param name="class">ds-static-div</xsl:with-param>
                                    </xsl:call-template>
                                    <xsl:apply-templates select="*[not(name()='head')]"/>
                                </div>
                                <xsl:apply-templates select="@pagination">
                                    <xsl:with-param name="position">bottom</xsl:with-param>
                                </xsl:apply-templates>
                            </xsl:otherwise>
                        </xsl:choose>
                    </xsl:when>
                    <!-- FIXME: it may be better to make an xsl:when that tests ALL divs to see if they have a single child node
                        and if that node is a dri:head. If so, don't display. -DS -->
                    <xsl:otherwise>
                        <!-- behave normally -DS -->
                        <xsl:apply-templates select="dri:head"/>
                        <xsl:apply-templates select="@pagination">
                            <xsl:with-param name="position">top</xsl:with-param>
                        </xsl:apply-templates>
                        <div>
                            <xsl:call-template name="standardAttributes">
                                <xsl:with-param name="class">ds-static-div</xsl:with-param>
                            </xsl:call-template>
                            <xsl:apply-templates select="*[not(name()='head')]"/>
                        </div>
                        <xsl:apply-templates select="@pagination">
                            <xsl:with-param name="position">bottom</xsl:with-param>
                        </xsl:apply-templates>
                    </xsl:otherwise>
                </xsl:choose>
            </xsl:otherwise>
        </xsl:choose>

    </xsl:template>

    <xsl:template match="dri:div/dri:head">
        <xsl:variable name="head_count" select="count(ancestor::dri:div)"/>
        <xsl:element name="h{$head_count}">
            <xsl:call-template name="standardAttributes">
                <xsl:with-param name="class">ds-div-head</xsl:with-param>
            </xsl:call-template>
            <!-- on h1 on comm/coll page, check for logo -DS -->
            <xsl:if test="$head_count=1 and ($is_coll or $is_comm)">
                <!-- External Metadata URL: cocoon://metadata/handle/1760/6794/mets.xml -->
                <!-- go get the fileSec from the METS -DS -->
                <xsl:variable name="externalMetadataURL">
                    <xsl:text>cocoon://metadata/</xsl:text>
                    <xsl:value-of
                        select="/dri:document/dri:meta/dri:pageMeta/dri:metadata[@element='request'][@qualifier='URI']"/>
                    <xsl:text>/mets.xml</xsl:text>
                </xsl:variable>
                <xsl:variable name="logoURL">
                    <xsl:apply-templates select="document($externalMetadataURL)" mode="getLogo"/>
                </xsl:variable>
                <xsl:comment>
                    <xsl:value-of select="$externalMetadataURL"/>
                    <xsl:text> </xsl:text>
                    <xsl:value-of select="$logoURL"/>
                </xsl:comment>
                <xsl:if test="contains($logoURL, 'bitstream')">
                    <img>
                        <xsl:attribute name="class">logo</xsl:attribute>
                        <xsl:attribute name="id">commcollogo</xsl:attribute>
                        <xsl:choose>
                            <xsl:when test="$is_comm">
                                <xsl:attribute name="alt"
                                    >xmlui.dri2xhtml.METS-1.0.community-logo-alt</xsl:attribute>
                                <xsl:attribute name="attr"
                                    namespace="http://apache.org/cocoon/i18n/2.1"
                                >alt</xsl:attribute>
                            </xsl:when>
                            <xsl:when test="$is_coll">
                                <xsl:attribute name="alt"
                                    >xmlui.dri2xhtml.METS-1.0.collection-logo-alt</xsl:attribute>
                                <xsl:attribute name="attr"
                                    namespace="http://apache.org/cocoon/i18n/2.1"
                                >alt</xsl:attribute>
                            </xsl:when>
                        </xsl:choose>
                        <xsl:attribute name="src">
                            <xsl:value-of select="$logoURL"/>
                        </xsl:attribute>
                    </img>
                </xsl:if>
            </xsl:if>
            <span id="headtext">
                <xsl:apply-templates/>
            </span>
        </xsl:element>
    </xsl:template>

    <xsl:template match="mets:METS" mode="getLogo">
        <xsl:value-of select="$context-path"/>
        <xsl:value-of select="./mets:fileSec/mets:fileGrp/mets:file/mets:FLocat/@xlink:href"/>
    </xsl:template>
    <!--
        Show a detailed view of a collection. This is used on the collection home page. It
        adds in the full collection description. Notice that the collection's news and side bar
        fields are not displayed.
        Changed to get rid of logo. -DS
        Changed to make sure this is never an empty element, because Firefox breaks the layout
        if so. -DS
    -->
    <xsl:template name="collectionDetailView-DIM">
        <div class="detail-view">&#160; <!-- Generate the logo, if present, from the file section -->
            <!--<xsl:apply-templates select="./mets:fileSec/mets:fileGrp[@USE='LOGO']"/>-->
            <!-- Generate the info about the collections from the metadata section -->
            <xsl:apply-templates
                select="./mets:dmdSec/mets:mdWrap[@OTHERMDTYPE='DIM']/mets:xmlData/dim:dim"
                mode="collectionDetailView-DIM"/>
        </div>
    </xsl:template>

    <!-- A community rendered in the detailView pattern; default way of viewing a community. -->
    <!-- Logo killed. It's in the h1 instead. -DS -->
    <xsl:template name="communityDetailView-DIM">
        <div class="detail-view">&#160; <!-- Generate the logo, if present, from the file section -->
            <!--<xsl:apply-templates select="./mets:fileSec/mets:fileGrp[@USE='LOGO']"/>-->
            <!-- Generate the info about the collections from the metadata section -->
            <xsl:apply-templates
                select="./mets:dmdSec/mets:mdWrap[@OTHERMDTYPE='DIM']/mets:xmlData/dim:dim"
                mode="communityDetailView-DIM"/>
        </div>
    </xsl:template>
    <!-- An item rendered in the detailView pattern, the "full item record" view of a DSpace item in Manakin. -->
    <xsl:template name="itemDetailView-DIM">

        <!-- Generate the bitstream information from the file section -->
        <!-- Table eliminated. -DS -->
        <xsl:choose>
            <xsl:when test="not(./mets:fileSec/mets:fileGrp[@USE='CONTENT'])">
                <h2>
                    <i18n:text>xmlui.dri2xhtml.METS-1.0.item-files-head</i18n:text>
                </h2>
                <p>
                    <i18n:text>xmlui.dri2xhtml.METS-1.0.item-no-files</i18n:text>
                </p>
            </xsl:when>
            <xsl:otherwise>
                <xsl:apply-templates select="./mets:fileSec/mets:fileGrp[@USE='CONTENT']">
                    <xsl:with-param name="context" select="."/>
                    <xsl:with-param name="primaryBitstream"
                        select="./mets:structMap[@TYPE='LOGICAL']/mets:div[@TYPE='DSpace Item']/mets:fptr/@FILEID"
                    />
                </xsl:apply-templates>
            </xsl:otherwise>
        </xsl:choose>

        <!-- Output all of the metadata about the item from the metadata section -->
        <xsl:apply-templates
            select="mets:dmdSec/mets:mdWrap[@OTHERMDTYPE='DIM']/mets:xmlData/dim:dim"
            mode="itemDetailView-DIM"/>

        <!-- Generate the license information from the file section -->
        <xsl:apply-templates select="mets:fileSec/mets:fileGrp[@USE='CC-LICENSE' or @USE='LICENSE']"
            mode="itemSummaryView"/>

    </xsl:template>

    <!-- The block of templates used to render the complete DIM contents of a DRI object -->
    <!-- table header added -DS -->
    <xsl:template match="dim:dim" mode="itemDetailView-DIM">
        <xsl:apply-templates
            select="mets:dmdSec/mets:mdWrap[@OTHERMDTYPE='DIM']/mets:xmlData/dim:dim"
            mode="itemDetailView-DIM"/>
        <table class="ds-includeSet-table">
            <thead>
                <tr>
                    <th>
                        <i18n:text>xmlui.dri2xhtml.METS-1.0.fullmetadata.label</i18n:text>
                    </th>
                    <th>
                        <i18n:text>xmlui.dri2xhtml.METS-1.0.fullmetadata.value</i18n:text>
                    </th>
                    <th>
                        <i18n:text>xmlui.dri2xhtml.METS-1.0.fullmetadata.language</i18n:text>
                    </th>
                </tr>
            </thead>
            <tbody>
                <xsl:apply-templates mode="itemDetailView-DIM"/>
            </tbody>
        </table>
    </xsl:template>

    <!-- changed to get rid of even/odd class, as I don't bother with zebra-striping tables -DS -->
    <xsl:template match="dim:field" mode="itemDetailView-DIM">
        <tr>
            <td>
                <xsl:value-of select="./@mdschema"/>
                <xsl:text>.</xsl:text>
                <xsl:value-of select="./@element"/>
                <xsl:if test="./@qualifier">
                    <xsl:text>.</xsl:text>
                    <xsl:value-of select="./@qualifier"/>
                </xsl:if>
            </td>
            <td>
                <xsl:value-of select="./text()"/>
            </td>
            <td>
                <xsl:value-of select="./@language"/>
            </td>
        </tr>
    </xsl:template>



    <!-- The second case is the header on tables, which always creates an HTML h3 element -->
    <!-- should be caption, not h3 -DS -->
    <xsl:template match="dri:table/dri:head" priority="2">
        <caption>
            <xsl:call-template name="standardAttributes">
                <xsl:with-param name="class">ds-table-head</xsl:with-param>
            </xsl:call-template>
            <xsl:apply-templates/>
        </caption>
    </xsl:template>

    <xsl:template name="mungeDate">
        <!-- This template takes dates in the form yyyy-mm-dd and turns them into the form 'Jan 1, 2008'. -DS -->
        <xsl:param name="date"/>
        <xsl:variable name="year" select="substring($date, 1, 4)"/>
        <xsl:variable name="month" select="substring($date, 6, 7)"/>
        <xsl:variable name="day" select="substring($date, 9)"/>
        <!--<xsl:comment>
            <xsl:value-of select="$date"/>
            <xsl:text>/year: </xsl:text>
            <xsl:value-of select="$year"/>
            <xsl:text>/month: </xsl:text>
            <xsl:value-of select="$month"/>
            <xsl:text>/day: </xsl:text>
            <xsl:value-of select="$day"/>
            <xsl:text>/month? </xsl:text>
            <xsl:value-of select="boolean(normalize-space($month))"/>
        </xsl:comment>-->

        <xsl:if test="normalize-space($month)">
            <xsl:choose>
                <!-- Using starts-with because for some reason there's a bug in the $month
                     definition such that it returns mm-dd instead of just mm. Three-Minute
                     Hate on XSLT. -DS -->
                <xsl:when test="starts-with($month, '01')">Jan</xsl:when>
                <xsl:when test="starts-with($month, '02')">Feb</xsl:when>
                <xsl:when test="starts-with($month, '03')">Mar</xsl:when>
                <xsl:when test="starts-with($month, '04')">Apr</xsl:when>
                <xsl:when test="starts-with($month, '05')">May</xsl:when>
                <xsl:when test="starts-with($month, '06')">Jun</xsl:when>
                <xsl:when test="starts-with($month, '07')">Jul</xsl:when>
                <xsl:when test="starts-with($month, '08')">Aug</xsl:when>
                <xsl:when test="starts-with($month, '09')">Sep</xsl:when>
                <xsl:when test="starts-with($month, '10')">Oct</xsl:when>
                <xsl:when test="starts-with($month, '11')">Nov</xsl:when>
                <xsl:when test="starts-with($month, '12')">Dec</xsl:when>
            </xsl:choose>
            <xsl:text> </xsl:text>
        </xsl:if>
        <xsl:if test="$day">
            <xsl:value-of select="normalize-space($day)"/>
            <xsl:text>, </xsl:text>
        </xsl:if>
        <xsl:value-of select="normalize-space($year)"/>
    </xsl:template>

    <xsl:template mode="COinS" name="COinS">
        <!-- This template creates a <span> element conforming to the Context Objects in Spans (COinS) specification.
             The OpenURL KEV value it produces conforms to the book template for items whose Dublin Core type is books 
             and book chapters, the journal template for articles, preprints, postprints, technical reports, and working
             papers, the dissertation template for items marked as theses, and the unqualified Dublin Core template for
             everything else. -DS
        -->
        <xsl:param name="dim"/>

        <xsl:variable name="type" select="dim:field[@element='type']"/>
        <xsl:variable name="isJournal">
            <xsl:choose>
                <xsl:when
                    test="$type='Journal issue' or $type='Article' or $type='Preprint' or $type='Postprint' or $type='Working paper' or $type='Technical report'">
                    <xsl:value-of select="true()"/>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:value-of select="false()"/>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:variable>
        <xsl:variable name="isBook">
            <xsl:choose>
                <xsl:when test="$type='Book' or $type='Book chapter'">
                    <xsl:value-of select="true()"/>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:value-of select="false()"/>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:variable>
        <xsl:variable name="isDissertation">
            <xsl:choose>
                <xsl:when test="$type='Thesis'">
                    <xsl:value-of select="true()"/>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:value-of select="false()"/>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:variable>
        <span class="Z3988">
            <xsl:attribute name="title">
                <xsl:text>url_ver=Z39.88-2004</xsl:text>
                <xsl:choose>
                    <xsl:when test="$isBook='true'"
                        >&amp;rft_val_fmt=info%3Aofi%2Ffmt%3Akev%3Amtx%3Abook</xsl:when>
                    <xsl:when test="$isJournal='true'"
                        >&amp;rft_val_fmt=info%3Aofi%2Ffmt%3Akev%3Amtx%3Ajournal</xsl:when>
                    <xsl:otherwise>&amp;rft_val_fmt=info%3Aofi%2Ffmt%3Akev%3Amtx%3Adc</xsl:otherwise>
                </xsl:choose>
                <xsl:text>&amp;ctx_ver=Z39.88-2004</xsl:text>
                <!-- item's handle as URI -->
                <xsl:text>&amp;rft_id=</xsl:text>
                <xsl:value-of
                    select="dim:field[@element='identifier'][@qualifier='uri']/child::node()"/>
                <!-- DOI if any -->
                <xsl:if test="dim:field[@element='identifier'][@qualifier='doi']">
                    <xsl:text>&amp;rft_id=</xsl:text>
                    <xsl:value-of
                        select="dim:field[@element='identifier'][@qualifier='doi']/child::node()"/>
                </xsl:if>
                <!-- ISSN if any -->
                <xsl:if test="dim:field[@element='identifier'][@qualifier='issn']">
                    <xsl:text>&amp;rft.issn=</xsl:text>
                    <xsl:value-of
                        select="dim:field[@element='identifier'][@qualifier='issn']/child::node()"/>
                </xsl:if>
                <!-- ISBN if any -->
                <xsl:if test="dim:field[@element='identifier'][@qualifier='isbn']">
                    <xsl:text>&amp;rft.isbn=</xsl:text>
                    <xsl:value-of
                        select="dim:field[@element='identifier'][@qualifier='isbn']/child::node()"/>
                </xsl:if>
                <!-- publisher if any -->
                <xsl:if test="dim:field[@element='publisher'][not(@qualifier)]">
                    <xsl:text>&amp;rft.pub=</xsl:text>
                    <xsl:value-of
                        select="dim:field[@element='publisher'][not(@qualifier)]/child::node()"/>
                </xsl:if>
                <!-- title -->
                <xsl:choose>
                    <xsl:when test="$isBook='true'">
                        <xsl:choose>
                            <xsl:when test="$type='Book chapter'">
                                <xsl:text>&amp;rft.atitle=</xsl:text>
                                <xsl:value-of
                                    select="dim:field[@element='title'][not(@qualifier)]/child::node()"
                                />
                            </xsl:when>
                            <xsl:when test="$type='Book'">
                                <xsl:text>&amp;rft.title=</xsl:text>
                                <xsl:value-of
                                    select="dim:field[@element='title'][not(@qualifier)]/child::node()"
                                />
                            </xsl:when>
                        </xsl:choose>
                    </xsl:when>
                    <xsl:when test="$isJournal='true'">
                        <xsl:choose>
                            <xsl:when test="$type='Journal issue'">
                                <xsl:text>&amp;rft.jtitle=</xsl:text>
                                <xsl:value-of
                                    select="dim:field[@element='title'][not(@qualifier)]/child::node()"
                                />
                            </xsl:when>
                            <xsl:otherwise>
                                <xsl:text>&amp;rft.atitle=</xsl:text>
                                <xsl:value-of
                                    select="dim:field[@element='title'][not(@qualifier)]/child::node()"
                                />
                            </xsl:otherwise>
                        </xsl:choose>
                    </xsl:when>
                    <xsl:otherwise>
                        <xsl:text>&amp;rft.title=</xsl:text>
                        <xsl:value-of
                            select="dim:field[@element='title'][not(@qualifier)]/child::node()"/>
                    </xsl:otherwise>
                </xsl:choose>
                <!-- date -->
                <xsl:if test="dim:field[@element='date'][@qualifier='issued']">
                    <xsl:text>&amp;rft.date=</xsl:text>
                    <xsl:value-of
                        select="dim:field[@element='date'][@qualifier='issued']/child::node()"/>
                </xsl:if>
                <!-- series membership -->
                <xsl:if
                    test="$isBook='true' and dim:field[@element='relation'][qualifier='ispartofseries']">
                    <xsl:text>&amp;rft.series=</xsl:text>
                    <xsl:value-of
                        select="dim:field[@element='relation'][qualifier='ispartofseries']/child::node()"
                    />
                </xsl:if>
                <!-- "genre" -->
                <xsl:choose>
                    <xsl:when test="$isJournal='true'">
                        <xsl:choose>
                            <xsl:when test="$type='Journal issue'">
                                <xsl:text>&amp;rft.genre=issue</xsl:text>
                            </xsl:when>
                            <xsl:when test="$type='Article'">
                                <xsl:text>&amp;rft.genre=article</xsl:text>
                            </xsl:when>
                            <xsl:when
                                test="$type='Preprint' or $type='Postprint' or $type='Working paper'">
                                <xsl:text>&amp;rft.genre=preprint</xsl:text>
                            </xsl:when>
                            <xsl:otherwise>
                                <xsl:text>&amp;rft.genre=unknown</xsl:text>
                            </xsl:otherwise>
                        </xsl:choose>
                    </xsl:when>
                    <xsl:when test="$isBook='true'">
                        <xsl:choose>
                            <xsl:when test="$type='Book'">
                                <xsl:text>&amp;rft.genre=book</xsl:text>
                            </xsl:when>
                            <xsl:when test="$type='Book chapter'">
                                <xsl:text>&amp;rft.genre=bookitem</xsl:text>
                            </xsl:when>
                        </xsl:choose>
                    </xsl:when>
                    <xsl:when test="$isDissertation='true'"><!-- do nothing? --></xsl:when>
                    <xsl:otherwise>&amp;rft.genre=unknown</xsl:otherwise>
                </xsl:choose>
                <!-- authors -->
                <xsl:for-each select="dim:field[@element='contributor'][@qualifier='author']">
                    <xsl:text>&amp;rft.au=</xsl:text>
                    <xsl:value-of select="./child::node()"/>
                </xsl:for-each>
            </xsl:attribute>
            <!-- non-breaking space to keep Firefox from screwing up layouts -DS -->
            <xsl:text>&#8203;</xsl:text>
        </span>
    </xsl:template>

    <!-- The handling of component fields, that is fields that are part of a composite field type -->
    <!-- Changed to get inputs outside of labels, because they look INSANE that way. -DS -->
    <xsl:template match="dri:field" mode="compositeComponent">
        <xsl:choose>
            <xsl:when test="@type = 'checkbox'  or @type='radio'">
                <xsl:apply-templates select="dri:label" mode="compositeComponent"/>
                <xsl:apply-templates select="." mode="normalField"/>
                <br/>
            </xsl:when>
            <xsl:otherwise>
                <label class="ds-composite-component">
                    <xsl:if test="position()=last()">
                        <xsl:attribute name="class">ds-composite-component last</xsl:attribute>
                    </xsl:if>
                    <xsl:apply-templates select="dri:label" mode="compositeComponent"/>
                </label>
                <xsl:apply-templates select="." mode="normalField"/>
                <br/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>

    <xsl:template match="dri:field" mode="normalField">
        <!-- Altered so that radiobuttons and checkboxes don't have their own <fieldset> elements,
             because nobody else in the known world does things that way, and it messes up the
             display something fierce. -DS -->
        <xsl:choose>
            <!-- TODO: this has changed drammatically (see form3.xml) -->
            <xsl:when test="@type= 'select'">
                <select>
                    <xsl:call-template name="fieldAttributes"/>
                    <xsl:apply-templates/>
                </select>
            </xsl:when>
            <xsl:when test="@type= 'textarea'">
                <textarea>
                    <xsl:call-template name="fieldAttributes"/>

                    <!--  
                        if the cols and rows attributes are not defined we need to call
                        the tempaltes for them since they are required attributes in strict xhtml
                    -->
                    <xsl:choose>
                        <xsl:when test="not(./dri:params[@cols])">
                            <xsl:call-template name="textAreaCols"/>
                        </xsl:when>
                    </xsl:choose>
                    <xsl:choose>
                        <xsl:when test="not(./dri:params[@rows])">
                            <xsl:call-template name="textAreaRows"/>
                        </xsl:when>
                    </xsl:choose>

                    <xsl:apply-templates/>
                    <xsl:choose>
                        <xsl:when test="./dri:value[@type='raw']">
                            <xsl:value-of select="./dri:value[@type='raw']/text()"/>
                        </xsl:when>
                        <xsl:otherwise>
                            <xsl:value-of select="./dri:value[@type='default']/text()"/>
                        </xsl:otherwise>
                    </xsl:choose>
                    <xsl:if test="string-length(./dri:value) &lt; 1">
                        <i18n:text>xmlui.dri2xhtml.default.textarea.value</i18n:text>
                    </xsl:if>

                </textarea>
            </xsl:when>
            <!-- This is changing drammatically -->
            <!-- Changed to get label out of <legend> and remove <fieldset>. -DS -->
            <xsl:when test="@type= 'checkbox' or @type= 'radio'">
                <xsl:call-template name="standardAttributes">
                    <xsl:with-param name="class">
                        <xsl:text>ds-</xsl:text>
                        <xsl:value-of select="@type"/>
                        <xsl:text>-field </xsl:text>
                        <xsl:if test="dri:error">
                            <xsl:text>error </xsl:text>
                        </xsl:if>
                    </xsl:with-param>
                </xsl:call-template>
                <xsl:attribute name="id">
                    <xsl:value-of select="generate-id()"/>
                </xsl:attribute>
                <!-- check the first radiobutton option by default -DS -->
                <xsl:if test="@type='radio' and position()=1">
                    <xsl:attribute name="checked">checked</xsl:attribute>
                </xsl:if>
                <xsl:apply-templates/>
                <xsl:if test="dri:label">
                    <label>
                        <xsl:apply-templates select="dri:label" mode="compositeComponent"/>
                    </label>
                </xsl:if>
                <br/>
            </xsl:when>
            <!--
                <input>
                <xsl:call-template name="fieldAttributes"/>
                <xsl:if test="dri:value[@checked='yes']">
                <xsl:attribute name="checked">checked</xsl:attribute>
                </xsl:if>
                <xsl:apply-templates/>
                </input>
            -->
            <xsl:when test="@type= 'composite'">
                <!-- TODO: add error and help stuff on top of the composite -->
                <xsl:apply-templates select="dri:field/dri:help" mode="compositeComponent"/>
                <span class="ds-composite-field">
                    <xsl:apply-templates select="dri:field" mode="compositeComponent"/>
                </span>
                <xsl:apply-templates select="dri:field/dri:error" mode="compositeComponent"/>
                <xsl:apply-templates select="dri:error" mode="compositeComponent"/>
                <!--<xsl:apply-templates select="dri:help" mode="compositeComponent"/>-->
            </xsl:when>
            <!-- text, password, file, and hidden types are handled the same. 
                Buttons: added the xsl:if check which will override the type attribute button
                with the value 'submit'. No reset buttons for now...
            -->
            <xsl:otherwise>
                <input>
                    <xsl:call-template name="fieldAttributes"/>
                    <xsl:if test="@type='button'">
                        <xsl:attribute name="type">submit</xsl:attribute>
                    </xsl:if>
                    <xsl:attribute name="value">
                        <xsl:choose>
                            <xsl:when test="./dri:value[@type='raw']">
                                <xsl:value-of select="./dri:value[@type='raw']"/>
                            </xsl:when>
                            <xsl:otherwise>
                                <xsl:value-of select="./dri:value[@type='default']"/>
                            </xsl:otherwise>
                        </xsl:choose>
                    </xsl:attribute>
                    <xsl:if test="dri:value/i18n:text">
                        <xsl:attribute name="i18n:attr">value</xsl:attribute>
                    </xsl:if>
                    <xsl:apply-templates/>
                </input>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>

    <!-- Fieldset (instanced) field stuff, in the case of non-composites -->
    <xsl:template match="dri:field[dri:field/dri:instance | dri:params/@operations]" priority="2">
        <!-- Moving the help again. -DS -->
        <xsl:apply-templates select="dri:help" mode="help"/>
        <xsl:apply-templates select="dri:error" mode="error"/>
        <!-- Create the first field normally -->
        <xsl:apply-templates select="." mode="normalField"/>
        <!-- Follow it up with an ADD button if the add operation is specified. This allows 
            entering more than one value for this field. -->
        <xsl:if test="contains(dri:params/@operations,'add')">
            <input type="submit" value="Add another" name="{concat(@n,'_add')}"
                class="ds-button-field"/>
        </xsl:if>
        <xsl:if test="dri:instance">
            <div class="ds-previous-values">
                <!-- Iterate over the dri:instance elements contained in this field. The instances contain
                    stored values as either "interpreted", "raw", or "default" values. -->
                <xsl:call-template name="simpleFieldIterator">
                    <xsl:with-param name="position">1</xsl:with-param>
                </xsl:call-template>
                <!-- Conclude with a DELETE button if the delete operation is specified. This allows 
                    removing one or more values stored for this field. -->
                <xsl:if test="contains(dri:params/@operations,'delete') and dri:instance">
                    <input type="submit" value="Remove selected" name="{concat(@n,'_delete')}"
                        class="ds-button-field"/>
                </xsl:if>
                <!-- Behind the scenes, add hidden fields for every instance set. This is to make sure that
                    the form still submits the information in those instances, even though they are no 
                    longer encoded as HTML fields. The DRI Reference should contain the exact attributes
                    the hidden fields should have in order for this to work properly. -->
                <xsl:apply-templates select="dri:instance" mode="hiddenInterpreter"/>
            </div>
        </xsl:if>
    </xsl:template>

    <xsl:template match="dri:field[@type='composite']" mode="formComposite">
        <!-- Moving the help again. -DS -->
        <div class="ds-form-content">
            <xsl:apply-templates select="dri:error" mode="compositeComponent"/>
            <xsl:apply-templates select="dri:help" mode="compositeComponent"/>
            <xsl:apply-templates select="dri:field" mode="compositeComponent"/>
            <div class="spacer">&#160;</div>
            <xsl:apply-templates select="dri:field/dri:error" mode="compositeComponent"/>
        </div>
    </xsl:template>

    <!-- The hadling of the special case of instanced composite fields under "form" lists -->
    <xsl:template
        match="dri:field[@type='composite'][dri:field/dri:instance | dri:params/@operations]"
        mode="formComposite" priority="2">
        <!-- Moving the help again. -DS -->
        <div class="ds-form-content">
            <xsl:apply-templates select="dri:field/dri:error" mode="compositeComponent"/>
            <xsl:apply-templates select="dri:error" mode="compositeComponent"/>
            <xsl:apply-templates select="dri:help" mode="compositeComponent"/>
            <xsl:apply-templates select="dri:field" mode="compositeComponent"/>
            <xsl:if test="contains(dri:params/@operations,'add')">
                <input type="submit" value="Add another" name="{concat(@n,'_add')}"
                    class="ds-button-field"/>
            </xsl:if>
            <div class="spacer">&#160;</div>
            <xsl:if test="dri:instance or dri:field/dri:instance">
                <div class="ds-previous-values">
                    <xsl:call-template name="fieldIterator">
                        <xsl:with-param name="position">1</xsl:with-param>
                    </xsl:call-template>
                    <xsl:if
                        test="contains(dri:params/@operations,'delete') and (dri:instance or dri:field/dri:instance)">
                        <input type="submit" value="Remove selected" name="{concat(@n,'_delete')}"
                            class="ds-button-field"/>
                    </xsl:if>
                    <xsl:for-each select="dri:field">
                        <xsl:apply-templates select="dri:instance" mode="hiddenInterpreter"/>
                    </xsl:for-each>
                </div>
            </xsl:if>
        </div>
    </xsl:template>
    <!--<xsl:template name="refworks" priority="2">
        <xsl:comment><xsl:value-of select="/dri:document/dri:body/dri:div/dri:p[@rend='refworks-export']/dri:xref/@target"/></xsl:comment>
        <a>
            <xsl:attribute name="href">
                <xsl:value-of
                    select="./dri:xref/@dri:target"
                />
            </xsl:attribute>
            <xsl:text>Export to Refworks</xsl:text>
        </a>
    </xsl:template>-->

    <xsl:template match="dri:field">
        <!-- Moving the help again. -DS -->
        <xsl:apply-templates select="dri:help" mode="help"/>
        <xsl:apply-templates select="dri:error" mode="error"/>
        <xsl:apply-templates select="." mode="normalField"/>
        <xsl:if test="not(@type='composite') and ancestor::dri:list[@type='form']">
            <!--
                <xsl:if test="not(@type='checkbox') and not(@type='radio') and not(@type='button')">
                <br/>
                </xsl:if>
            -->
        </xsl:if>
    </xsl:template>

    <!-- copying this over to see if it fixes a problem -DS -->
    <xsl:template match="dri:div[@interactive='yes']" priority="2">
        <xsl:apply-templates select="dri:head"/>
        <xsl:apply-templates select="@pagination">
            <xsl:with-param name="position">top</xsl:with-param>
        </xsl:apply-templates>
        <form>
            <xsl:call-template name="standardAttributes">
                <xsl:with-param name="class">ds-interactive-div</xsl:with-param>
            </xsl:call-template>
            <xsl:attribute name="action">
                <xsl:value-of select="@action"/>
            </xsl:attribute>
            <xsl:attribute name="method">
                <xsl:value-of select="@method"/>
            </xsl:attribute>
            <xsl:if test="@method='multipart'">
                <xsl:attribute name="method">post</xsl:attribute>
                <xsl:attribute name="enctype">multipart/form-data</xsl:attribute>
            </xsl:if>
            <xsl:attribute name="onsubmit">javascript:tSubmit(this);</xsl:attribute>
            <!--For Item Submission process, disable ability to submit a form by pressing 'Enter'-->
            <xsl:if test="starts-with(@n,'submit')">
                <xsl:attribute name="onkeydown">javascript:return
                disableEnterKey(event);</xsl:attribute>
            </xsl:if>
            <xsl:apply-templates select="*[not(name()='head')]"/>

        </form>
        <xsl:apply-templates select="@pagination">
            <xsl:with-param name="position">bottom</xsl:with-param>
        </xsl:apply-templates>
    </xsl:template>

    <!-- Disgusting hack to provide user-friendly file descriptions.
         Add a new <xsl:when> whenever a new file type is added to
         the database. -DS -->

    <xsl:template name="getFileTypeDesc">
        <xsl:param name="mimetype"/>
        <xsl:choose>
            <xsl:when test="$mimetype='application/pdf'">
                <xsl:text>PDF</xsl:text>
            </xsl:when>
            <xsl:when test="$mimetype='application/marc'">
                <xsl:text>MARC record</xsl:text>
            </xsl:when>
            <xsl:when test="$mimetype='application/mathematica'">
                <xsl:text>Mathematica</xsl:text>
            </xsl:when>
            <xsl:when test="$mimetype='application/msword'">
                <xsl:text>Microsoft Word</xsl:text>
            </xsl:when>
            <xsl:when test="$mimetype='application/octet-stream'">
                <xsl:text>Unknown</xsl:text>
            </xsl:when>
            <xsl:when test="$mimetype='application/postscript'">
                <xsl:text>Postscript</xsl:text>
            </xsl:when>
            <xsl:when test="$mimetype='application/sgml'">
                <xsl:text>SGML</xsl:text>
            </xsl:when>
            <xsl:when test="$mimetype='application/vnd.ms-excel'">
                <xsl:text>Microsoft Excel</xsl:text>
            </xsl:when>
            <xsl:when test="$mimetype='application/vnd.ms-powerpoint'">
                <xsl:text>Microsoft PowerPoint</xsl:text>
            </xsl:when>
            <xsl:when test="$mimetype='application/vnd.ms-project'">
                <xsl:text>Microsoft Project</xsl:text>
            </xsl:when>
            <xsl:when test="$mimetype='application/vnd.visio'">
                <xsl:text>Microsoft Visio</xsl:text>
            </xsl:when>
            <xsl:when test="$mimetype='application/wordperfect5.1'">
                <xsl:text>WordPerfect</xsl:text>
            </xsl:when>
            <xsl:when test="$mimetype='application/x-dvi'">
                <xsl:text>TeX DVI</xsl:text>
            </xsl:when>
            <xsl:when test="$mimetype='application/x-filemaker'">
                <xsl:text>FileMaker Pro 3</xsl:text>
            </xsl:when>
            <xsl:when test="$mimetype='application/x-java-applet'">
                <xsl:text>Java applet</xsl:text>
            </xsl:when>
            <xsl:when test="$mimetype='application/x-latex'">
                <xsl:text>LaTeX</xsl:text>
            </xsl:when>
            <xsl:when test="$mimetype='application/x-photoshop'">
                <xsl:text>Photoshop</xsl:text>
            </xsl:when>
            <xsl:when test="$mimetype='application/x-tex'">
                <xsl:text>TeX</xsl:text>
            </xsl:when>
            <xsl:when test="$mimetype='audio/mpeg'">
                <xsl:text>mp3 file</xsl:text>
            </xsl:when>
            <xsl:when test="$mimetype='audio/x-aiff'">
                <xsl:text>AIFF</xsl:text>
            </xsl:when>
            <xsl:when test="$mimetype='audio/x-mpeg'">
                <xsl:text>MPEG Audio</xsl:text>
            </xsl:when>
            <xsl:when test="$mimetype='audio/x-pn-realaudio'">
                <xsl:text>RealAudio</xsl:text>
            </xsl:when>
            <xsl:when test="$mimetype='audio/x-wav'">
                <xsl:text>WAV</xsl:text>
            </xsl:when>
            <xsl:when test="$mimetype='image/gif'">
                <xsl:text>GIF</xsl:text>
            </xsl:when>
            <xsl:when test="$mimetype='image/jpeg'">
                <xsl:text>JPEG</xsl:text>
            </xsl:when>
            <xsl:when test="$mimetype='image/png'">
                <xsl:text>PNG</xsl:text>
            </xsl:when>
            <xsl:when test="$mimetype='image/tiff'">
                <xsl:text>TIFF</xsl:text>
            </xsl:when>
            <xsl:when test="$mimetype='image/x-ms-bmp'">
                <xsl:text>BMP</xsl:text>
            </xsl:when>
            <xsl:when test="$mimetype='image/x-photo-cd'">
                <xsl:text>Photo CD</xsl:text>
            </xsl:when>
            <xsl:when test="$mimetype='text/css'">
                <xsl:text>CSS file</xsl:text>
            </xsl:when>
            <xsl:when test="$mimetype='text/html'">
                <xsl:text>HTML</xsl:text>
            </xsl:when>
            <xsl:when test="$mimetype='text/plain'">
                <xsl:text>Text file</xsl:text>
            </xsl:when>
            <xsl:when test="$mimetype='text/richtext'">
                <xsl:text>RTF file</xsl:text>
            </xsl:when>
            <xsl:when test="$mimetype='text/xml'">
                <xsl:text>XML file</xsl:text>
            </xsl:when>
            <xsl:when test="$mimetype='video/avi'">
                <xsl:text>AVI file</xsl:text>
            </xsl:when>
            <xsl:when test="$mimetype='video/mpeg'">
                <xsl:text>MPEG video</xsl:text>
            </xsl:when>
            <xsl:when test="$mimetype='video/quicktime'">
                <xsl:text>QuickTime video</xsl:text>
            </xsl:when>
            <xsl:when test="$mimetype='application/zip'">
                <xsl:text>ZIP file</xsl:text>
            </xsl:when>
            <xsl:when
                test="$mimetype='application/vnd.openxmlformats-officedocument.wordprocessingml.document'">
                <xsl:text>Microsoft Word 2008</xsl:text>
            </xsl:when>
            <!-- if no match, shoot back the MIME type; it's better than nothing -DS -->
            <xsl:otherwise>
                <xsl:value-of select="$mimetype"/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
    <!-- Not ready for prime time yet. -DS -->
    <!--<xsl:template match="dri:referenceSet[@id='aspect.artifactbrowser.ItemViewer.referenceSet.collection-viewer']/dri:reference/dri:referenceSet"></xsl:template>-->


    <!-- Fixing this so that "Part of [collection]" goes to the collection's title browse instead of the useless splash page. Thanks to Kerry Kresse for the bug report. -DS -->
    <xsl:template match="dri:reference" mode="detailList">
        <xsl:variable name="externalMetadataURL">
            <xsl:text>cocoon:/</xsl:text>
            <xsl:value-of select="@url"/>
            <!-- No options selected, render the full METS document -->
        </xsl:variable>
        <xsl:comment> External Metadata URL: <xsl:value-of select="$externalMetadataURL"/>
        </xsl:comment>
        <xsl:choose>
            <xsl:when test="./@type='DSpace Collection'">
                <xsl:variable name="dim" select="document($externalMetadataURL)"/>
                <a>
                    <xsl:attribute name="href">
                        <xsl:text>http://minds.wisconsin.edu/handle/</xsl:text>
                        <xsl:value-of
                            select="substring-after(substring-before(@url, '/mets.xml'), 'handle/')"/>
                        <xsl:text>/browse?type=title</xsl:text>
                    </xsl:attribute>
                    <xsl:value-of select="$dim/mets:METS/mets:dmdSec/mets:mdWrap/mets:xmlData/dim:dim/dim:field[@element='title']/text()"/>
                </a>
            </xsl:when>
            <xsl:otherwise>
                <xsl:apply-templates select="document($externalMetadataURL)" mode="summaryView"/>
                <xsl:apply-templates/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>

</xsl:stylesheet>

