<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:mods="http://www.loc.gov/mods/v3"
                xmlns:OAI-PMH="http://www.openarchives.org/OAI/2.0/" exclude-result-prefixes="OAI-PMH mods">
    <xsl:output method="xml" indent="yes" encoding="UTF-8" omit-xml-declaration="no" media-type="text/xml"/>
    <!-- Transformerar DiVA:s MODS-format till Swepub MODS v 3 format -->
    <!-- Uppsala universitetsbibliotek, 2020 -->
    <xsl:template match="/OAI-PMH:OAI-PMH">
        <OAI-PMH xmlns="http://www.openarchives.org/OAI/2.0/">
            <xsl:copy-of select="OAI-PMH:responseDate"/>
            <xsl:copy-of select="OAI-PMH:request"/>
            <xsl:apply-templates select="OAI-PMH:ListRecords"/>
        </OAI-PMH>
    </xsl:template>
    <xsl:template match="OAI-PMH:ListRecords">
        <xsl:element name="ListRecords" xmlns="http://www.openarchives.org/OAI/2.0/">
            <xsl:apply-templates select="OAI-PMH:record"/>
            <xsl:copy-of select="OAI-PMH:resumptionToken"/>
        </xsl:element>
    </xsl:template>
    <xsl:template match="OAI-PMH:record">
        <xsl:element name="record" xmlns="http://www.openarchives.org/OAI/2.0/">
            <xsl:copy-of select="OAI-PMH:header"/>
            <xsl:element name="metadata" xmlns="http://www.openarchives.org/OAI/2.0/">
                <xsl:apply-templates select="OAI-PMH:metadata"/>
            </xsl:element>
        </xsl:element>
    </xsl:template>
    <xsl:template match="OAI-PMH:metadata">
        <!-- DiVA organisationsnamn -->
        <xsl:param name="org"
                   select="mods:mods/mods:name[@type = 'corporate'][string-length(@authority) &gt; 0][mods:role/mods:roleTerm = 'pbl']/mods:namePart[1]"/>
        <!-- Bibliografisk beskrivning - Swepub - MODS 3.0 -->
        <!-- 2. MODS root element -->
        <mods xmlns="http://www.loc.gov/mods/v3" version="3.7" xmlns:xlink="http://www.w3.org/1999/xlink"
              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
              xsi:schemaLocation="http://www.loc.gov/mods/v3 http://www.loc.gov/standards/mods/v3/mods-3-7.xsd">
            <xsl:for-each select="mods:mods">
                <!-- 3. Record information -->
                <xsl:for-each select="mods:recordInfo">
                    <xsl:element name="recordInfo">
                        <!-- 3.1. Record content source -->
                        <xsl:copy-of select="mods:recordContentSource"/>
                        <!-- 3.2. Record creation date -->
                        <xsl:copy-of select="mods:recordCreationDate"/>
                    </xsl:element>
                </xsl:for-each>
                <!-- 4. Record identifiers + 5. Resource identifiers -->
                <!-- Tar bort isi-id (ej godkänt för spridning) -->
                <!-- Artikel-id till part/detail -->
                <xsl:for-each select="mods:identifier[not(@type = 'isi')][not(@type = 'articleId')]">
                    <xsl:choose>
                        <xsl:when test="@type = 'isbn'">
                            <!-- Tar bort ISBN från kapitel-/delposter, skall inte tas med -->
                            <xsl:if test="not(../mods:relatedItem[@type = 'host'])">
                                <xsl:copy-of select="."/>
                            </xsl:if>
                        </xsl:when>
                        <xsl:otherwise>
                            <xsl:copy-of select="."/>
                        </xsl:otherwise>
                    </xsl:choose>
                </xsl:for-each>
                <!-- Lägg till patent id -->
                <xsl:for-each select="mods:note[@type = 'patent']">
                    <xsl:element name="identifier">
                        <xsl:attribute name="type">
                            <xsl:text>patent_number</xsl:text>
                        </xsl:attribute>
                        <xsl:value-of select="."/>
                    </xsl:element>
                </xsl:for-each>
                <!-- 6. Title and subtitle -->
                <xsl:copy-of select="mods:titleInfo"/>
                <!-- 7. Abstract -->
                <!-- Taggar behålls -->
                <xsl:for-each select="mods:abstract">
                    <xsl:element name="abstract">
                        <xsl:for-each select="@lang">
                            <xsl:attribute name="lang">
                                <xsl:value-of select="../@lang"/>
                            </xsl:attribute>
                        </xsl:for-each>
                        <xsl:value-of select="." disable-output-escaping="no"/>
                    </xsl:element>
                </xsl:for-each>
                <!-- Om XSL version 2.0 funkar rensar vi bort html-taggar
                <xsl:for-each select="mods:abstract">
                    <xsl:element name="abstract">
                        <xsl:for-each select="@lang">
                            <xsl:attribute name="lang"><xsl:value-of select="../@lang"/></xsl:attribute>
                        </xsl:for-each>
                        <xsl:value-of
                            select="replace(.,'&lt;p&gt;|&lt;/p&gt;|&lt;em&gt;|&lt;/em&gt;|&lt;i&gt;|&lt;/i&gt;|&lt;sub&gt;|&lt;/sub&gt;|&lt;sup&gt;|&lt;/sup&gt;|&#xA;|&#xD;|&#x9;', '')"/>
                    </xsl:element>
                </xsl:for-each>
                -->
                <!-- 8. Subjects and keywords-->
                <!-- 8.1. Subject classification according to Swedish research subjects standard -->
                <xsl:for-each select="mods:subject[@authority = 'hsv']">
                    <xsl:element name="subject">
                        <xsl:attribute name="lang">
                            <xsl:value-of select="@lang"/>
                        </xsl:attribute>
                        <xsl:attribute name="authority">
                            <xsl:text>uka.se</xsl:text>
                        </xsl:attribute>
                        <xsl:attribute name="xlink:href">
                            <xsl:value-of select="@xlink:href"/>
                        </xsl:attribute>
                        <xsl:copy-of select="mods:topic"/>
                    </xsl:element>
                </xsl:for-each>
                <!-- 8.2. Keywords -->
                <xsl:for-each select="mods:subject[not(@authority = 'hsv')]">
                    <xsl:copy-of select="."/>
                </xsl:for-each>
                <!-- 9. Language -->
                <xsl:choose>
                    <xsl:when test="mods:language">
                        <xsl:for-each select="mods:language">
                            <!-- Ta bort sammanfattings- och disputationsspråk -->
                            <xsl:if test="not(@objectPart)">
                                <xsl:element name="language">
                                    <!-- 9.1. Language term -->
                                    <xsl:copy-of select="mods:languageTerm"/>
                                </xsl:element>
                            </xsl:if>
                        </xsl:for-each>
                    </xsl:when>
                    <!-- Om språk saknas anges kod und (språk är obligatoriskt element) -->
                    <xsl:otherwise>
                        <xsl:element name="language">
                            <xsl:element name="languageTerm">
                                <xsl:attribute name="type">
                                    <xsl:text>code</xsl:text>
                                </xsl:attribute>
                                <xsl:attribute name="authority">
                                    <xsl:text>iso639-2b</xsl:text>
                                </xsl:attribute>
                                <xsl:text>und</xsl:text>
                            </xsl:element>
                        </xsl:element>
                    </xsl:otherwise>
                </xsl:choose>
                <!-- 10. Resource type -->
                <!-- 10.1. Publication type -->
                <!-- Gamla SVEP-typerna, används inte längre -->
                <!-- 10.2. Output type -->
                <xsl:for-each select="mods:genre[@authority= 'diva'][@type='publicationTypeCode']">
                    <!-- 10.2.1. Output type – code -->
                    <xsl:element name="genre">
                        <xsl:attribute name="authority">
                            <xsl:text>kb.se</xsl:text>
                        </xsl:attribute>
                        <xsl:attribute name="type">
                            <xsl:text>outputType</xsl:text>
                        </xsl:attribute>
                        <xsl:choose>
                            <xsl:when test=". = 'article'">
                                <xsl:choose>
                                    <xsl:when
                                            test="../mods:genre[@authority = 'diva'][@type = 'publicationSubTypeCode' or @type = 'editorialMaterial']">
                                        <xsl:text>publication/editorial-letter</xsl:text>
                                    </xsl:when>
                                    <xsl:when
                                            test="../mods:genre[@authority = 'diva'][@type = 'publicationSubTypeCode' or @type = 'letter']">
                                        <xsl:text>publication/editorial-letter</xsl:text>
                                    </xsl:when>
                                    <xsl:when
                                            test="../mods:genre[@authority = 'diva'][@type = 'publicationSubTypeCode' or @type = 'newsItem']">
                                        <xsl:text>publication/newspaper-article</xsl:text>
                                    </xsl:when>
                                    <xsl:otherwise>
                                        <xsl:choose>
                                            <xsl:when test="../mods:genre[@authority = 'svep'][@type = 'ref']">
                                                <xsl:text>publication/journal-article</xsl:text>
                                            </xsl:when>
                                            <xsl:when test="../mods:genre[@authority = 'svep'][@type = 'vet']">
                                                <xsl:text>publication/magazine-article</xsl:text>
                                            </xsl:when>
                                            <xsl:when test="../mods:genre[@authority = 'svep'][@type = 'pop']">
                                                <xsl:text>publication/newspaper-article</xsl:text>
                                            </xsl:when>
                                            <xsl:otherwise>
                                                <xsl:text>publication/journal-article</xsl:text>
                                            </xsl:otherwise>
                                        </xsl:choose>
                                    </xsl:otherwise>
                                </xsl:choose>
                            </xsl:when>
                            <xsl:when test=". = 'review'">
                                <xsl:text>publication/review-article</xsl:text>
                            </xsl:when>
                            <xsl:when test=". = 'bookReview'">
                                <xsl:text>publication/book-review</xsl:text>
                            </xsl:when>
                            <xsl:when test=". = 'book'">
                                <xsl:text>publication/book</xsl:text>
                            </xsl:when>
                            <xsl:when test=". = 'dataset'">
                                <xsl:text>other/data-set</xsl:text>
                            </xsl:when>
                            <xsl:when test=". = 'dissertation'">
                                <xsl:text>publication/doctoral-thesis</xsl:text>
                            </xsl:when>
                            <xsl:when test=". = 'monographDoctoralThesis'">
                                <xsl:text>publication/doctoral-thesis</xsl:text>
                            </xsl:when>
                            <xsl:when test=". = 'comprehensiveDoctoralThesis'">
                                <xsl:text>publication/doctoral-thesis</xsl:text>
                            </xsl:when>
                            <xsl:when test=". = 'chapter'">
                                <xsl:text>publication/book-chapter</xsl:text>
                            </xsl:when>
                            <xsl:when test=". = 'conferencePaper'">
                                <xsl:choose>
                                    <xsl:when
                                            test="../mods:genre[@authority = 'diva'][@type = 'publicationSubTypeCode' or @type = 'meetingAbstract']">
                                        <xsl:text>conference/other</xsl:text>
                                    </xsl:when>
                                    <xsl:when
                                            test="../mods:genre[@authority= 'diva'][@type ='publicationSubTypeCode' or @type = 'poster']">
                                        <xsl:text>conference/poster</xsl:text>
                                    </xsl:when>
                                    <xsl:when
                                            test="../mods:genre[@authority= 'diva'][@type ='publicationSubTypeCode' or @type = 'presentation']">
                                        <xsl:text>conference/other</xsl:text>
                                    </xsl:when>
                                    <xsl:otherwise>
                                        <xsl:text>conference/paper</xsl:text>
                                    </xsl:otherwise>
                                </xsl:choose>
                            </xsl:when>
                            <xsl:when test=". = 'monographLicentiateThesis'">
                                <xsl:text>publication/licentiate-thesis</xsl:text>
                            </xsl:when>
                            <xsl:when test=". = 'comprehensiveLicentiateThesis'">
                                <xsl:text>publication/licentiate-thesis</xsl:text>
                            </xsl:when>
                            <xsl:when test=". = 'manuscript'">
                                <xsl:text>publication/preprint</xsl:text>
                            </xsl:when>
                            <xsl:when test=". = 'patent'">
                                <xsl:text>intellectual-property/patent</xsl:text>
                            </xsl:when>
                            <xsl:when test=". = 'conferenceProceedings'">
                                <xsl:text>conference/proceeding</xsl:text>
                            </xsl:when>
                            <xsl:when test=". = 'report'">
                                <xsl:text>publication/report</xsl:text>
                            </xsl:when>
                            <xsl:when test=". = 'collection'">
                                <xsl:text>publication/edited-book</xsl:text>
                            </xsl:when>
                            <xsl:when test=". = 'other'">
                                <xsl:text>publication/other</xsl:text>
                            </xsl:when>
                            <!-- 10.2.2. Output type – artistic -->
                            <xsl:when test=". = 'artisticOutput'">
                                <xsl:text>artistic-work</xsl:text>
                            </xsl:when>
                        </xsl:choose>
                    </xsl:element>
                </xsl:for-each>
                <!-- 10.2.2. Output type – artistic, som komplement till annan outputtyp -->
                <xsl:for-each select="mods:genre[@authority = 'svep'][@type ='publicationType']">
                    <xsl:if test=". = 'kfa'">
                        <xsl:element name="genre">
                            <xsl:attribute name="authority">
                                <xsl:text>kb.se</xsl:text>
                            </xsl:attribute>
                            <xsl:attribute name="type">
                                <xsl:text>outputType</xsl:text>
                            </xsl:attribute>
                            <xsl:text>artistic-work</xsl:text>
                        </xsl:element>
                    </xsl:if>
                </xsl:for-each>
                <!-- 11. Content type -->
                <xsl:for-each select="mods:genre[@authority = 'svep'][@type ='contentType']">
                    <xsl:copy-of select="."/>
                </xsl:for-each>

                <!-- DiVA Content type -->
                <xsl:for-each select="mods:genre[@authority = 'diva'][@type = 'contentTypeCode']">
                    <xsl:if test=". = 'refereedArtisticOutput' and count(mods:genre[@authority = 'svep'][@type = 'contentType'][. = 'ref']) &lt; 1">
                        <xsl:element name="genre">
                            <xsl:attribute name="authority">
                                <xsl:text>svep</xsl:text>
                            </xsl:attribute>
                            <xsl:attribute name="type">
                                <xsl:text>contentType</xsl:text>
                            </xsl:attribute>
                            <xsl:text>ref</xsl:text>
                        </xsl:element>
                    </xsl:if>
                </xsl:for-each>

                <!-- 12. Publication status -->
                <xsl:choose>
                    <xsl:when test="string-length(mods:note[@type = 'publicationStatus']) &gt; 0">
                        <xsl:for-each select="mods:note[@type = 'publicationStatus']">
                            <xsl:choose>
                                <xsl:when test=". = 'Accepted'">
                                    <xsl:element name="note">
                                        <xsl:attribute name="type">
                                            <xsl:text>publicationStatus</xsl:text>
                                        </xsl:attribute>
                                        <xsl:text>Accepted</xsl:text>
                                    </xsl:element>
                                </xsl:when>
                                <xsl:when test=". = 'In press' or . = 'In progress'">
                                    <xsl:element name="note">
                                        <xsl:attribute name="type">
                                            <xsl:text>publicationStatus</xsl:text>
                                        </xsl:attribute>
                                        <xsl:text>In press</xsl:text>
                                    </xsl:element>
                                </xsl:when>
                                <xsl:when test=". = 'Epub ahead of print'">
                                    <xsl:element name="note">
                                        <xsl:attribute name="type">
                                            <xsl:text>publicationStatus</xsl:text>
                                        </xsl:attribute>
                                        <xsl:text>Epub ahead of print/Online first</xsl:text>
                                    </xsl:element>
                                </xsl:when>
                                <xsl:when test=". = 'Submitted'">
                                    <xsl:element name="note">
                                        <xsl:attribute name="type">
                                            <xsl:text>publicationStatus</xsl:text>
                                        </xsl:attribute>
                                        <xsl:text>Submitted</xsl:text>
                                    </xsl:element>
                                </xsl:when>
                                <xsl:otherwise>
                                    <xsl:element name="note">
                                        <xsl:attribute name="type">
                                            <xsl:text>publicationStatus</xsl:text>
                                        </xsl:attribute>
                                        <xsl:text>Published</xsl:text>
                                    </xsl:element>
                                </xsl:otherwise>
                            </xsl:choose>
                        </xsl:for-each>
                    </xsl:when>
                    <xsl:otherwise>
                        <xsl:element name="note">
                            <xsl:attribute name="type">
                                <xsl:text>publicationStatus</xsl:text>
                            </xsl:attribute>
                            <xsl:text>Published</xsl:text>
                        </xsl:element>
                    </xsl:otherwise>
                </xsl:choose>
                <!-- 13. Names and affiliations -->
                <xsl:for-each select="mods:name[@type = 'personal']">
                    <!-- 13.1. Person name, identifier and affiliation -->
                    <!-- Alla roller nu godkända -->
                    <xsl:element name="name">
                        <xsl:for-each select="@type">
                            <xsl:attribute name="type">
                                <xsl:value-of select="../@type"/>
                            </xsl:attribute>
                        </xsl:for-each>
                        <xsl:for-each select="@authority">
                            <xsl:attribute name="authority">
                                <xsl:value-of select="../@authority"/>
                            </xsl:attribute>
                        </xsl:for-each>
                        <!-- 13.1.1. Name part -->
                        <xsl:copy-of select="mods:namePart"/>
                        <!-- 13.1.2. Role -->
                        <xsl:copy-of select="mods:role"/>
                        <!-- 13.1.3. Affiliation -->
                        <xsl:for-each select="mods:affiliation">
                            <xsl:choose>
                                <xsl:when test="starts-with(.,$org) and string-length($org) > 1">
                                    <affiliation lang="swe" authority="kb.se"
                                                 xsi:type="stringPlusLanguagePlusAuthority">
                                        <xsl:attribute name="valueURI">
                                            <xsl:value-of select="../../mods:recordInfo/mods:recordContentSource"/>
                                            <xsl:text>.se</xsl:text>
                                        </xsl:attribute>
                                        <xsl:value-of select="$org"/>
                                    </affiliation>
                                    <xsl:if test="contains(.,', ')">
                                        <affiliation lang="swe" xsi:type="stringPlusLanguagePlusAuthority">
                                            <xsl:attribute name="authority">
                                                <xsl:value-of select="../../mods:recordInfo/mods:recordContentSource"/>
                                                <xsl:text>.se</xsl:text>
                                            </xsl:attribute>
                                            <xsl:attribute name="valueURI">
                                                    <xsl:value-of select="substring-after(.,', ')"/>
                                             </xsl:attribute>
                                            <xsl:value-of select="substring-after(.,', ')"/>
                                        </affiliation>
                                    </xsl:if>
                                </xsl:when>
                                <xsl:otherwise>
                                    <xsl:element name="affiliation">
                                        <xsl:value-of select="."/>
                                    </xsl:element>
                                </xsl:otherwise>
                            </xsl:choose>
                        </xsl:for-each>
                        <!-- 13.1.2. Name identifier -->
                        <xsl:for-each select="@authority">
                            <nameIdentifier>
                                <xsl:attribute name="type">
                                    <xsl:value-of select="."/>
                                </xsl:attribute>
                                <xsl:for-each select="../@xlink:href">
                                    <xsl:value-of select="."/>
                                </xsl:for-each>
                            </nameIdentifier>
                        </xsl:for-each>
                        <xsl:for-each select="mods:description">
                            <xsl:if test="contains(.,'orcid.org')">
                                <nameIdentifier type="orcid">
                                    <xsl:value-of select="substring-after(.,'orcid.org=')"/>
                                </nameIdentifier>
                            </xsl:if>
                        </xsl:for-each>
                    </xsl:element>
                </xsl:for-each>
                <!-- 13.1. Organisation as author -->
                <xsl:for-each select="mods:name[@type = 'corporate'][mods:description = 'Responsible Organisation']">
                    <!-- Ta bort de som inte är det egna lärosätets kontrollerade -->
                    <xsl:element name="name">
                        <xsl:attribute name="type">
                            <xsl:text>corporate</xsl:text>
                        </xsl:attribute>
                        <affiliation authority="kb.se" xsi:type="stringPlusLanguagePlusAuthority">
                            <xsl:attribute name="valueURI">
                                <xsl:value-of select="../mods:recordInfo/mods:recordContentSource"/>
                                <xsl:text>.se</xsl:text>
                            </xsl:attribute>
                            <xsl:value-of select="mods:namePart"/>
                        </affiliation>
                        <xsl:element name="role">
                            <xsl:element name="roleTerm">
                                <xsl:attribute name="type">
                                    <xsl:text>code</xsl:text>
                                </xsl:attribute>
                                <xsl:attribute name="authority">
                                    <xsl:text>marcrelator</xsl:text>
                                </xsl:attribute>
                                <xsl:text>pbl</xsl:text>
                            </xsl:element>
                        </xsl:element>
                    </xsl:element>
                </xsl:for-each>
                <!-- 14. Creator count -->
                <xsl:choose>
                    <xsl:when test="mods:note[@type = 'creatorCount']">
                        <xsl:copy-of select="mods:note[@type = 'creatorCount']"/>
                    </xsl:when>
                    <xsl:otherwise>
                        <xsl:choose>
                            <xsl:when test="mods:genre[@authority= 'svep'][@ type='publicationType'][. = 'sam']">
                                <xsl:choose>
                                    <xsl:when test="count(mods:name[mods:role/mods:roleTerm = 'edt']) &gt; 0">
                                        <xsl:element name="note">
                                            <xsl:attribute name="type">
                                                <xsl:text>creatorCount</xsl:text>
                                            </xsl:attribute>
                                            <xsl:value-of select="count(mods:name[mods:role/mods:roleTerm = 'edt'])"/>
                                        </xsl:element>
                                    </xsl:when>
                                </xsl:choose>
                            </xsl:when>
                            <xsl:when test="mods:genre[@authority= 'svep'][@ type='publicationType'][. = 'pro']">
                                <xsl:choose>
                                    <xsl:when test="count(mods:name[mods:role/mods:roleTerm = 'edt']) &gt; 0">
                                        <xsl:element name="note">
                                            <xsl:attribute name="type">
                                                <xsl:text>creatorCount</xsl:text>
                                            </xsl:attribute>
                                            <xsl:value-of select="count(mods:name[mods:role/mods:roleTerm = 'edt'])"/>
                                        </xsl:element>
                                    </xsl:when>
                                </xsl:choose>
                            </xsl:when>
                            <xsl:otherwise>
                                <xsl:choose>
                                    <xsl:when
                                            test="count(mods:name[mods:role/mods:roleTerm = 'aut' or mods:role/mods:roleTerm = 'cre']) &gt; 0">
                                        <xsl:element name="note">
                                            <xsl:attribute name="type">
                                                <xsl:text>creatorCount</xsl:text>
                                            </xsl:attribute>
                                            <xsl:value-of
                                                    select="count(mods:name[mods:role/mods:roleTerm = 'aut' or mods:role/mods:roleTerm = 'cre'])"/>
                                        </xsl:element>
                                    </xsl:when>
                                </xsl:choose>
                            </xsl:otherwise>
                        </xsl:choose>
                    </xsl:otherwise>
                </xsl:choose>
                <!-- 15. Contracts, projects, programmes and strategic initiatives -->
                <!-- Projekt -->
                <xsl:for-each select="mods:note[@type= 'project']">
                    <xsl:element name="relatedItem">
                        <xsl:attribute name="type">
                            <xsl:text>host</xsl:text>
                        </xsl:attribute>
                        <xsl:element name="genre">
                            <xsl:text>project</xsl:text>
                        </xsl:element>
                        <xsl:element name="titleInfo">
                            <xsl:element name="title">
                                <xsl:value-of select="."/>
                            </xsl:element>
                        </xsl:element>
                    </xsl:element>
                </xsl:for-each>
                <!-- Finansiär/strategiska forskningsprojekt -->
                <xsl:for-each select="mods:note[@type= 'funder']">
                    <xsl:element name="relatedItem">
                        <xsl:attribute name="type">
                            <xsl:text>host</xsl:text>
                        </xsl:attribute>
                        <xsl:element name="genre">
                            <xsl:text>grantAgreement</xsl:text>
                        </xsl:element>
                        <xsl:element name="name">
                            <xsl:attribute name="type">
                                <xsl:text>corporate</xsl:text>
                            </xsl:attribute>
                            <xsl:element name="namePart">
                                <xsl:choose>
                                    <xsl:when test="contains(., '[')">
                                        <xsl:value-of select="substring-before(., ' [')"/>
                                    </xsl:when>
                                    <xsl:otherwise>
                                        <xsl:value-of select="."/>
                                    </xsl:otherwise>
                                </xsl:choose>
                            </xsl:element>
                            <xsl:element name="role">
                                <xsl:element name="roleTerm">
                                    <xsl:attribute name="type">
                                        <xsl:text>code</xsl:text>
                                    </xsl:attribute>
                                    <xsl:attribute name="authority">
                                        <xsl:text>marcrelator</xsl:text>
                                    </xsl:attribute>
                                    <xsl:text>fnd</xsl:text>
                                </xsl:element>
                            </xsl:element>
                        </xsl:element>
                        <xsl:if test="contains(., '[')">
                            <xsl:element name="identifier">
                                <xsl:value-of select="substring-before(substring-after(.,' ['),']')"/>
                            </xsl:element>
                        </xsl:if>
                    </xsl:element>
                </xsl:for-each>
                <!-- 16. Event -->
                <!-- Används ej -->
                <!-- 17. Publication date, publisher and edition -->
                <xsl:for-each select="mods:originInfo">
                    <xsl:element name="originInfo">
                        <!-- 17.1. Publication date -->
                        <!-- Ta bort övriga datum -->
                        <xsl:for-each select="mods:dateIssued">
                            <xsl:element name="dateIssued">
                                <xsl:value-of select="."/>
                            </xsl:element>
                        </xsl:for-each>
                        <!-- 17.2. Publisher -->
                        <xsl:copy-of select="mods:publisher"/>
                        <!-- 17.3. Place -->
                        <xsl:copy-of select="mods:place"/>
                        <!-- 17.4. Edition -->
                        <xsl:copy-of select="mods:edition"/>
                    </xsl:element>
                </xsl:for-each>
                <!-- 18. Source -->
                <!-- 18.1. Journal or book as source -->
                <!-- Värdpublikation -->
                <xsl:for-each select="mods:relatedItem[@type = 'host'][not(mods:genre = 'dataset')]">
                    <!-- Ta bort tomma -->
                    <xsl:if test="string-length(mods:titleInfo/mods:title) &gt; 0">
                        <xsl:element name="relatedItem">
                            <xsl:attribute name="type">
                                <xsl:text>host</xsl:text>
                            </xsl:attribute>
                            <!-- 18.3. Source title -->
                            <xsl:copy-of select="mods:titleInfo"/>
                            <!-- 18.4. Source identifiers -->
                            <xsl:for-each select="mods:identifier[@type = 'issn']">
                                <xsl:element name="identifier">
                                    <xsl:attribute name="type">
                                        <xsl:text>issn</xsl:text>
                                    </xsl:attribute>
                                    <xsl:attribute name="displayLabel">
                                        <xsl:text>ISSN</xsl:text>
                                    </xsl:attribute>
                                    <xsl:value-of select="."/>
                                </xsl:element>
                            </xsl:for-each>
                            <xsl:for-each select="mods:identifier[@type = 'eissn']">
                                <xsl:element name="identifier">
                                    <xsl:attribute name="type">
                                        <xsl:text>issn</xsl:text>
                                    </xsl:attribute>
                                    <xsl:attribute name="displayLabel">
                                        <xsl:text>EISSN</xsl:text>
                                    </xsl:attribute>
                                    <xsl:value-of select="."/>
                                </xsl:element>
                            </xsl:for-each>
                            <xsl:for-each select="mods:part">
                                <xsl:element name="part">
                                    <xsl:copy-of select="mods:detail"/>
                                    <xsl:if test="string-length(../../mods:identifier[@type = 'articleId']) &gt; 0">
                                        <xsl:element name="detail">
                                            <xsl:attribute name="type">
                                                <xsl:text>artNo</xsl:text>
                                            </xsl:attribute>
                                            <xsl:element name="number">
                                                <xsl:value-of select="../../mods:identifier[@type = 'articleId']"/>
                                            </xsl:element>
                                        </xsl:element>
                                    </xsl:if>
                                    <xsl:copy-of select="mods:extent"/>
                                </xsl:element>
                            </xsl:for-each>
                            <xsl:for-each select="../mods:identifier[@type = 'isbn']">
                                <xsl:copy-of select="."/>
                            </xsl:for-each>
                        </xsl:element>
                    </xsl:if>
                </xsl:for-each>
                <!-- 18.2. Series as source -->
                <xsl:for-each select="mods:relatedItem[@type = 'series']">
                    <!-- Ta bort tomma -->
                    <xsl:if test="string-length(mods:titleInfo/mods:title) &gt; 0">
                        <xsl:element name="relatedItem">
                            <xsl:attribute name="type">
                                <xsl:text>series</xsl:text>
                            </xsl:attribute>
                            <!-- 18.3. Source title -->
                            <xsl:element name="titleInfo">
                                <xsl:copy-of select="mods:titleInfo/mods:title"/>
                                <xsl:copy-of select="mods:titleInfo/mods:subTitle"/>
                                <xsl:choose>
                                    <xsl:when test="string-length(mods:titleInfo/mods:partNumber) &gt; 0">
                                        <xsl:copy-of select="mods:titleInfo/mods:partNumber"/>
                                    </xsl:when>
                                    <xsl:otherwise>
                                        <xsl:if test="string-length(mods:identifier[@type = 'issue number']) &gt; 0">
                                            <xsl:element name="partNumber">
                                                <xsl:value-of select="mods:identifier[@type = 'issue number']"/>
                                            </xsl:element>
                                        </xsl:if>
                                    </xsl:otherwise>
                                </xsl:choose>
                                <xsl:copy-of select="mods:titleInfo/mods:partName"/>
                                <xsl:copy-of select="mods:titleInfo/mods:nonSort"/>
                            </xsl:element>
                            <!-- 18.4. Source identifiers -->
                            <xsl:if test="string-length(mods:identifier[@type = 'issn']) &gt; 0">
                                <xsl:element name="identifier">
                                    <xsl:attribute name="type">
                                        <xsl:text>issn</xsl:text>
                                    </xsl:attribute>
                                    <xsl:attribute name="displayLabel">
                                        <xsl:text>ISSN</xsl:text>
                                    </xsl:attribute>
                                    <xsl:value-of select="mods:identifier[@type = 'issn']"/>
                                </xsl:element>
                            </xsl:if>
                            <xsl:if test="string-length(mods:identifier[@type = 'eissn']) &gt; 0">
                                <xsl:element name="identifier">
                                    <xsl:attribute name="type">
                                        <xsl:text>issn</xsl:text>
                                    </xsl:attribute>
                                    <xsl:attribute name="displayLabel">
                                        <xsl:text>EISSN</xsl:text>
                                    </xsl:attribute>
                                    <xsl:value-of select="mods:identifier[@type = 'eissn']"/>
                                </xsl:element>
                            </xsl:if>
                        </xsl:element>
                    </xsl:if>
                </xsl:for-each>
                <!-- 19. Research data -->
                <xsl:for-each select="mods:relatedItem[mods:genre = 'dataset']">
                    <xsl:copy-of select="."/>
                </xsl:for-each>
                <!-- 20. Location and accessibility -->
                <xsl:for-each select="mods:location">
                    <!-- 20.1. URL -->
                    <xsl:element name="location">
                        <xsl:for-each select="mods:url">
                            <xsl:element name="url">
                                <xsl:if test="string-length(@displayLabel) &gt; 0 and @displayLabel = 'fulltext'">
                                    <xsl:if test="contains(.,'diva-portal.org')">
                                        <xsl:attribute name="usage">
                                            <xsl:text>primary</xsl:text>
                                        </xsl:attribute>
                                    </xsl:if>
                                </xsl:if>
                                <xsl:value-of select="."/>
                            </xsl:element>
                        </xsl:for-each>
                    </xsl:element>
                    <!-- 21. Access condition -->
                    <xsl:if test="contains(mods:url/@note, 'free')">
                        <xsl:element name="accessCondition">
                            <xsl:text>gratis</xsl:text>
                        </xsl:element>
                    </xsl:if>
                </xsl:for-each>
                <!-- 21.1. Embargo -->
                <xsl:for-each select="mods:location">
                    <xsl:if test="contains(mods:url/@note, 'free/')">
                        <xsl:element name="accessCondition">
                            <xsl:attribute name="type">
                                <xsl:text>restriction on access</xsl:text>
                            </xsl:attribute>
                            <xsl:attribute name="displayLabel">
                                <xsl:text>embargo</xsl:text>
                            </xsl:attribute>
                            <xsl:value-of select="substring-after(mods:url/@note,'/')"/>
                        </xsl:element>
                    </xsl:if>
                </xsl:for-each>
                <!-- 21.2. License -->
                <!-- Används ej i DiVA -->
                <!-- 22. Physical description -->
                <xsl:for-each select="mods:physicalDescription">
                    <xsl:element name="physicalDescription">
                        <!-- 22.1. Physical form -->
                        <xsl:copy-of select="mods:form "/>
                        <!-- 22.2. Physical extent -->
                        <xsl:for-each select="mods:extent">
                            <xsl:element name="extent">
                                <xsl:value-of select="."/>
                                <xsl:text>s.</xsl:text>
                            </xsl:element>
                        </xsl:for-each>
                    </xsl:element>
                </xsl:for-each>
                <!-- 23. Type of resource -->
                <xsl:copy-of select="mods:typeOfResource "/>
                <!-- 24. Notes -->
                <!-- Ta bort specifika -->
                <xsl:for-each select="mods:note[not(@type)]">
                    <xsl:if test="not(contains(.,'Part of urn:nbn:se:'))">
                        <xsl:element name="note">
                            <xsl:value-of select="."/>
                        </xsl:element>
                    </xsl:if>
                </xsl:for-each>
            </xsl:for-each>
        </mods>
    </xsl:template>
</xsl:stylesheet>