<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:gmd="http://www.isotc211.org/2005/gmd" xmlns:gmi="http://www.isotc211.org/2005/gmi"
    xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:gco="http://www.isotc211.org/2005/gco" 
    exclude-result-prefixes="xs" version="2.0">
    <!-- Output a list of all ResponsibleParties in a WAF -->
    <xd:doc xmlns:xd="http://www.oxygenxml.com/ns/doc/xsl" scope="stylesheet">
        <xd:desc>
            <xd:p><xd:b>Created on:</xd:b> July 28, 2011</xd:p>
            <xd:p><xd:b>Author:</xd:b>ted.habermann@noaa.gov</xd:p>
            <xd:p/>
        </xd:desc>
    </xd:doc>
    <xsl:variable name="Version" select="'1.1'"/>
    <xsl:output method="xml" indent="yes"/>
    <xsl:strip-space elements="*"/>
    <!-- Define keys  for extracting uniqe elements -->
    <xsl:key name="uniqueMetadataContacts" match="//gmd:contact/gmd:CI_ResponsibleParty" use="concat(gmd:individualName/gco:CharacterString,gmd:organisationName/gco:CharacterString,gmd:role/gmd:CI_RoleCode)"/>
    <xsl:key name="uniqueOriginators" match="//gmd:has/gmi:MI_Metadata/gmd:identificationInfo/gmd:MD_DataIdentification/gmd:citation/gmd:CI_Citation/gmd:citedResponsibleParty/gmd:CI_ResponsibleParty"
        use="concat(gmd:individualName/gco:CharacterString,gmd:organisationName/gco:CharacterString,gmd:role/gmd:CI_RoleCode)"/>
    <xsl:key name="uniquePointsOfContact" match="//gmd:has/gmi:MI_Metadata/gmd:identificationInfo/gmd:MD_DataIdentification/gmd:pointOfContact/gmd:CI_ResponsibleParty"
        use="concat(gmd:individualName/gco:CharacterString,gmd:organisationName/gco:CharacterString,gmd:role/gmd:CI_RoleCode)"/>
    <xsl:key name="uniqueDistributorContacts" match="//gmd:has/gmi:MI_Metadata/gmd:distributionInfo/gmd:MD_Distribution/gmd:distributor/gmd:MD_Distributor/gmd:distributorContact/gmd:CI_ResponsibleParty"
        use="concat(gmd:individualName/gco:CharacterString,gmd:organisationName/gco:CharacterString,gmd:role/gmd:CI_RoleCode)"/>
    <xsl:key name="uniqueProcessSteps" match="//gmd:has/gmi:MI_Metadata/gmd:dataQualityInfo/gmd:DQ_DataQuality/gmd:lineage/gmd:LI_Lineage/gmd:processStep/gmd:LI_ProcessStep" use="normalize-space(gmd:description/gco:CharacterString)"/>


    <xsl:template  xmlns:uuid="java:java.util.UUID" match="/">
        <gmd:DS_Series xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.isotc211.org/2005/gmi http://www.ngdc.noaa.gov/metadata/published/xsd/schema.xsd" xmlns:gco="http://www.isotc211.org/2005/gco"
            xmlns:gmd="http://www.isotc211.org/2005/gmd" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:gml="http://www.opengis.net/gml/3.2" xmlns:gmi="http://www.isotc211.org/2005/gmi" xmlns:gss="http://www.isotc211.org/2005/gss"
            xmlns:gts="http://www.isotc211.org/2005/gts" xmlns:gmx="http://www.isotc211.org/2005/gmx" xmlns:gsr="http://www.isotc211.org/2005/gsr">
            <gmd:composedOf>
                <gmd:DS_DataSet>
                    <xsl:for-each select="/gmd:DS_Series/gmd:composedOf/gmd:DS_DataSet/gmd:has/gmi:MI_Metadata">
                        <!-- Define id for this contact as the id generated for the first CI_ResponsibleParty with this individual and organization -->
                        <gmd:has>
                            <xsl:element name="gmi:MI_Metadata">
                                <xsl:for-each select="./*">
                                    <xsl:choose>
                                        <xsl:when test="name()='gmd:contact'">
                                            <xsl:element name="gmd:contact">
                                                <xsl:attribute name="xlink:href">
                                                    <xsl:value-of
                                                        select="concat('#',generate-id(key('uniqueMetadataContacts',concat(gmd:CI_ResponsibleParty/gmd:individualName/gco:CharacterString,gmd:CI_ResponsibleParty/gmd:organisationName/gco:CharacterString,gmd:CI_ResponsibleParty/gmd:role/gmd:CI_RoleCode))[1]))"
                                                    />
                                                </xsl:attribute>
                                            </xsl:element>
                                        </xsl:when>
                                        <xsl:when test="name()='gmd:identificationInfo'">
                                            <xsl:element name="gmd:identificationInfo">
                                                <xsl:element name="gmd:MD_DataIdentification">
                                                    <xsl:for-each select="gmd:MD_DataIdentification/*">
                                                        <xsl:choose>
                                                            <xsl:when test="name()='gmd:citation'">
                                                                <xsl:element name="gmd:citation">
                                                                    <xsl:element name="gmd:CI_Citation">
                                                                        <xsl:for-each select="./gmd:CI_Citation/*">
                                                                            <xsl:choose>
                                                                                <xsl:when test="name()='gmd:citedResponsibleParty'">
                                                                                    <xsl:element name="gmd:citedResponsibleParty">
                                                                                        <xsl:attribute name="xlink:href">
                                                                                            <xsl:value-of
                                                                                                select="concat('#',generate-id(key('uniqueOriginators',concat(gmd:CI_ResponsibleParty/gmd:individualName/gco:CharacterString,gmd:CI_ResponsibleParty/gmd:organisationName/gco:CharacterString,gmd:CI_ResponsibleParty/gmd:role/gmd:CI_RoleCode))[1]))"
                                                                                            />
                                                                                        </xsl:attribute>
                                                                                    </xsl:element>
                                                                                </xsl:when>
                                                                                <xsl:otherwise>
                                                                                    <xsl:copy-of select="."/>
                                                                                </xsl:otherwise>
                                                                            </xsl:choose>
                                                                        </xsl:for-each>
                                                                    </xsl:element>
                                                                </xsl:element>
                                                            </xsl:when>
                                                            <xsl:when test="name()='gmd:pointOfContact'">
                                                                <xsl:element name="gmd:pointOfContact">
                                                                    <xsl:attribute name="xlink:href">
                                                                        <xsl:value-of
                                                                            select="concat('#',generate-id(key('uniquePointsOfContact',concat(gmd:CI_ResponsibleParty/gmd:individualName/gco:CharacterString,gmd:CI_ResponsibleParty/gmd:organisationName/gco:CharacterString,gmd:CI_ResponsibleParty/gmd:role/gmd:CI_RoleCode))[1]))"
                                                                        />
                                                                    </xsl:attribute>
                                                                </xsl:element>
                                                            </xsl:when>
                                                            <xsl:otherwise>
                                                                <xsl:copy-of select="."/>
                                                            </xsl:otherwise>
                                                        </xsl:choose>
                                                    </xsl:for-each>
                                                </xsl:element>
                                            </xsl:element>
                                        </xsl:when>
                                        <xsl:when test="name()='gmd:distributionInfo'">
                                            <xsl:element name="gmd:distributionInfo">
                                                <xsl:element name="gmd:MD_Distribution">
                                                    <xsl:for-each select="./gmd:MD_Distribution/*">
                                                        <xsl:choose>
                                                            <xsl:when test="name()='gmd:distributor'">
                                                                <xsl:element name="gmd:distributor">
                                                                    <xsl:element name="gmd:MD_Distributor">
                                                                        <xsl:for-each select="./gmd:MD_Distributor/*">
                                                                            <xsl:choose>
                                                                                <xsl:when test="name()='gmd:distributorContact'">
                                                                                    <xsl:element name="gmd:distributorContact">
                                                                                        <xsl:attribute name="xlink:href">
                                                                                            <xsl:value-of
                                                                                                select="concat('#',generate-id(key('uniqueDistributorContacts',concat(gmd:CI_ResponsibleParty/gmd:individualName/gco:CharacterString,gmd:CI_ResponsibleParty/gmd:organisationName/gco:CharacterString,gmd:CI_ResponsibleParty/gmd:role/gmd:CI_RoleCode))[1]))"
                                                                                            />
                                                                                        </xsl:attribute>
                                                                                    </xsl:element>
                                                                                </xsl:when>
                                                                                <xsl:otherwise>
                                                                                    <xsl:copy-of select="."/>
                                                                                </xsl:otherwise>
                                                                            </xsl:choose>
                                                                        </xsl:for-each>
                                                                    </xsl:element>
                                                                </xsl:element>
                                                            </xsl:when>
                                                            <xsl:otherwise>
                                                                <xsl:copy-of select="."/>
                                                            </xsl:otherwise>
                                                        </xsl:choose>
                                                    </xsl:for-each>
                                                </xsl:element>
                                            </xsl:element>
                                        </xsl:when>
                                        <xsl:when test="name()='gmd:dataQualityInfo'">
                                            <xsl:element name="gmd:dataQualityInfo">
                                                <xsl:element name="gmd:DQ_DataQuality">
                                                    <gmd:scope gco:nilReason="unknown"/>
                                                    <xsl:for-each select="./gmd:DQ_DataQuality/gmd:lineage">
                                                        <gmd:lineage>
                                                            <gmd:LI_Lineage>
                                                                <xsl:for-each select="gmd:LI_Lineage/gmd:processStep">
                                                                    <xsl:element name="gmd:processStep">
                                                                        <xsl:attribute name="xlink:href">
                                                                            <xsl:value-of select="concat('#',generate-id(key('uniqueProcessSteps',normalize-space(gmd:LI_ProcessStep/gmd:description/gco:CharacterString))[1]))"/>
                                                                        </xsl:attribute>
                                                                    </xsl:element>
                                                                </xsl:for-each>
                                                            </gmd:LI_Lineage>
                                                        </gmd:lineage>
                                                    </xsl:for-each>
                                                </xsl:element>
                                            </xsl:element>
                                        </xsl:when>
                                        <xsl:when test="name()='gmd:metadataMaintenance'">
                                            <xsl:element name="gmd:metadataMaintenance">
                                                <xsl:element name="gmd:MD_MaintenanceInformation">
                                                    <xsl:for-each select="./gmd:MD_MaintenanceInformation/*">
                                                        <xsl:choose>
                                                            <xsl:when test="name()='gmd:contact'">
                                                                <xsl:element name="gmd:contact">
                                                                    <xsl:attribute name="xlink:href">
                                                                        <xsl:value-of
                                                                            select="concat('#',generate-id(key('uniqueMetadataContacts',concat(gmd:CI_ResponsibleParty/gmd:individualName/gco:CharacterString,gmd:CI_ResponsibleParty/gmd:organisationName/gco:CharacterString,gmd:CI_ResponsibleParty/gmd:role/gmd:CI_RoleCode))[1]))"
                                                                        />
                                                                    </xsl:attribute>
                                                                </xsl:element>
                                                            </xsl:when>
                                                            <xsl:otherwise>
                                                                <xsl:copy-of select="."/>
                                                            </xsl:otherwise>
                                                        </xsl:choose>
                                                    </xsl:for-each>
                                                </xsl:element>
                                            </xsl:element>
                                        </xsl:when>
                                        <xsl:otherwise>
                                            <xsl:copy-of select="."/>
                                        </xsl:otherwise>
                                    </xsl:choose>
                                </xsl:for-each>
                            </xsl:element>
                        </gmd:has>
                    </xsl:for-each>
                </gmd:DS_DataSet>
            </gmd:composedOf>
            <gmd:seriesMetadata>
                <gmi:MI_Metadata>
                    <xsl:element name="gmd:fileIdentifier">
                        <xsl:element name="gco:CharacterString">
                            <xsl:value-of select="/gmd:DS_Series/gmd:seriesMetadata/gmi:MI_Metadata/gmd:fileIdentifier/gco:CharacterString"/>
                        </xsl:element>
                    </xsl:element>
                    <gmd:hierarchyLevel>
                        <gmd:MD_ScopeCode codeList="http://www.isotc211.org/2005/resources/Codelist/gmxCodelists.xml#MD_ScopeCode" codeListValue="series">series</gmd:MD_ScopeCode>
                    </gmd:hierarchyLevel>
                    <!-- List Unique Metadata Contacts -->
                    <xsl:for-each
                        select="//gmd:has/gmi:MI_Metadata/gmd:contact/gmd:CI_ResponsibleParty[generate-id() = 
      generate-id(key('uniqueMetadataContacts',concat(gmd:individualName/gco:CharacterString,gmd:organisationName/gco:CharacterString,gmd:role/gmd:CI_RoleCode))[1])]">
                        <gmd:contact>
                            <gmd:CI_ResponsibleParty>
                                <xsl:attribute name="id">
                                    <xsl:value-of select="generate-id(key('uniqueMetadataContacts',concat(gmd:individualName/gco:CharacterString,gmd:organisationName/gco:CharacterString,gmd:role/gmd:CI_RoleCode))[1])"/>
                                </xsl:attribute>

                                <xsl:attribute name="uuid" select="uuid:randomUUID()"/>

                                <xsl:copy-of select="./*"/>
                            </gmd:CI_ResponsibleParty>
                        </gmd:contact>
                    </xsl:for-each>
                    <gmd:dateStamp>
                        <xsl:value-of select="current-date()"/>
                    </gmd:dateStamp>
                    <gmd:identificationInfo>
                        <gmd:MD_DataIdentification>
                            <gmd:citation>
                                <gmd:CI_Citation>
                                    <gmd:title>
                                        <gco:CharacterString>
                                            <xsl:value-of select="/gmd:DS_Series/gmd:seriesMetadata/gmi:MI_Metadata/gmd:fileIdentifier/gco:CharacterString"/>
                                        </gco:CharacterString>
                                    </gmd:title>
                                    <gmd:date>
                                        <gmd:CI_Date>
                                            <gmd:date>
                                                <gco:Date>
                                                    <xsl:value-of select="current-date()"/>
                                                </gco:Date>
                                            </gmd:date>
                                            <gmd:dateType>
                                                <gmd:CI_DateTypeCode codeList="http://www.isotc211.org/2005/resources/Codelist/gmxCodelists.xml#CI_DateTypeCode" codeListValue="creation">creation</gmd:CI_DateTypeCode>
                                            </gmd:dateType>
                                        </gmd:CI_Date>
                                    </gmd:date>
                                    <!-- List Unique Points of Contact -->
                                    <xsl:for-each
                                        select="//gmd:has/gmi:MI_Metadata/gmd:identificationInfo/gmd:MD_DataIdentification/gmd:citation/gmd:CI_Citation/gmd:citedResponsibleParty/gmd:CI_ResponsibleParty[generate-id() = 
      generate-id(key('uniqueOriginators',concat(gmd:individualName/gco:CharacterString,gmd:organisationName/gco:CharacterString,gmd:role/gmd:CI_RoleCode))[1])]">
                                        <gmd:citedResponsibleParty>
                                            <gmd:CI_ResponsibleParty>
                                                <xsl:attribute name="id">
                                                    <xsl:value-of select="generate-id(key('uniqueOriginators',concat(gmd:individualName/gco:CharacterString,gmd:organisationName/gco:CharacterString,gmd:role/gmd:CI_RoleCode))[1])"/>
                                                </xsl:attribute>

                                                <xsl:attribute name="uuid" select="uuid:randomUUID()"/>

                                                <xsl:copy-of select="./*"/>
                                            </gmd:CI_ResponsibleParty>
                                        </gmd:citedResponsibleParty>
                                    </xsl:for-each>
                                </gmd:CI_Citation>
                            </gmd:citation>
                            <!-- List Unique Points of Contact -->
                            <xsl:for-each
                                select="//gmd:has/gmi:MI_Metadata/gmd:identificationInfo/gmd:MD_DataIdentification/gmd:pointOfContact/gmd:CI_ResponsibleParty[generate-id() = 
      generate-id(key('uniquePointsOfContact',concat(gmd:individualName/gco:CharacterString,gmd:organisationName/gco:CharacterString,gmd:role/gmd:CI_RoleCode))[1])]">
                                <gmd:pointOfContact>
                                    <gmd:CI_ResponsibleParty>
                                        <xsl:attribute name="id">
                                            <xsl:value-of select="generate-id(key('uniquePointsOfContact',concat(gmd:individualName/gco:CharacterString,gmd:organisationName/gco:CharacterString,gmd:role/gmd:CI_RoleCode))[1])"/>
                                        </xsl:attribute>

                                        <xsl:attribute name="uuid" select="uuid:randomUUID()"/>

                                        <xsl:copy-of select="./*"/>
                                    </gmd:CI_ResponsibleParty>
                                </gmd:pointOfContact>
                            </xsl:for-each>
                        </gmd:MD_DataIdentification>
                    </gmd:identificationInfo>
                    <gmd:distributionInfo>
                        <gmd:MD_Distribution>
                            <xsl:for-each
                                select="//gmd:has/gmi:MI_Metadata/gmd:distributionInfo/gmd:MD_Distribution/gmd:distributor/gmd:MD_Distributor/gmd:distributorContact/gmd:CI_ResponsibleParty[generate-id() = 
      generate-id(key('uniqueDistributorContacts',concat(gmd:individualName/gco:CharacterString,gmd:organisationName/gco:CharacterString,gmd:role/gmd:CI_RoleCode))[1])]">
                                <gmd:distributor>
                                    <gmd:MD_Distributor>
                                        <gmd:distributorContact>
                                            <xsl:element name="gmd:CI_ResponsibleParty">
                                                <xsl:attribute name="id">
                                                    <xsl:value-of select="generate-id(key('uniqueDistributorContacts',concat(gmd:individualName/gco:CharacterString,gmd:organisationName/gco:CharacterString,gmd:role/gmd:CI_RoleCode))[1])"/>
                                                </xsl:attribute>

                                                <xsl:attribute name="uuid" select="uuid:randomUUID()"/>

                                                <xsl:copy-of select="./*"/>
                                            </xsl:element>
                                        </gmd:distributorContact>
                                    </gmd:MD_Distributor>
                                </gmd:distributor>
                            </xsl:for-each>
                        </gmd:MD_Distribution>
                    </gmd:distributionInfo>
                    <gmd:dataQualityInfo>
                        <gmd:DQ_DataQuality>
                            <gmd:scope gco:nilReason="unknown"/>
                            <gmd:lineage>
                                <gmd:LI_Lineage>
                                    <!-- List Unique Process Steps -->
                                    <xsl:for-each
                                        select="//gmd:has/gmi:MI_Metadata/gmd:dataQualityInfo/gmd:DQ_DataQuality/gmd:lineage/gmd:LI_Lineage/gmd:processStep/gmd:LI_ProcessStep[generate-id() = 
      generate-id(key('uniqueProcessSteps',normalize-space(gmd:description/gco:CharacterString))[1])]">
                                        <gmd:processStep>
                                            <gmd:LI_ProcessStep>
                                                <xsl:attribute name="id">
                                                    <xsl:value-of select="generate-id(key('uniqueProcessSteps',normalize-space(gmd:description/gco:CharacterString))[1])"/>
                                                </xsl:attribute>

                                                <xsl:attribute name="uuid" select="uuid:randomUUID()"/>

                                                <xsl:copy-of select="."/>
                                            </gmd:LI_ProcessStep>
                                        </gmd:processStep>
                                    </xsl:for-each>
                                </gmd:LI_Lineage>
                            </gmd:lineage>
                        </gmd:DQ_DataQuality>
                    </gmd:dataQualityInfo>
                    <gmd:metadataMaintenance>
                        <gmd:MD_MaintenanceInformation>
                            <xsl:for-each
                                select="//gmd:has/gmi:MI_Metadata/gmd:contact/gmd:CI_ResponsibleParty[generate-id() = 
      generate-id(key('uniqueMetadataContacts',concat(gmd:individualName/gco:CharacterString,gmd:organisationName/gco:CharacterString,gmd:role/gmd:CI_RoleCode))[1])]">
                                <gmd:contact>
                                    <xsl:attribute name="xlink:href">
                                        <xsl:value-of
                                            select="concat('#',generate-id(key('uniqueMetadataContacts',concat(gmd:individualName/gco:CharacterString,gmd:organisationName/gco:CharacterString,gmd:role/gmd:CI_RoleCode))[1]))"
                                        />
                                    </xsl:attribute>
                                </gmd:contact>
                            </xsl:for-each>
                        </gmd:MD_MaintenanceInformation>
                    </gmd:metadataMaintenance>
                </gmi:MI_Metadata>
            </gmd:seriesMetadata>
        </gmd:DS_Series>
    </xsl:template>
</xsl:stylesheet>
