<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:jj="http://www.it.ojp.gov/jxdm/3.0.3"
    xmlns:gjxdm="http://www.search.org/GJXDMtoNIEM/GJXDM"
    xmlns:j="http://niem.gov/niem/domains/jxdm/4.0"
    xmlns:s="http://niem.gov/niem/structures/2.0"
    xmlns:nc="http://niem.gov/niem/niem-core/2.0"
    xmlns:niem="http://www.search.org/GJXDMtoNIEM/NIEM"
    exclude-result-prefixes="jj gjxdm">
    
    <!-- A stylesheet to transform the GJXDM instance (Appendix A) into an equivalent
        NIEM 2.0 instance (Appendix B) -->
    
	<!--
		1.	In order for each included instance to validate correctly you must extract the archived files while preserving the existing folder structure, else you must alter the xsi:schemaLocation attribute in each XML instance. 

		2.	When you use this style sheet to transform the included GJXDM instance to a NIEM instance the newly created file does not indicate the physical location of the schema document that is to be used 		 
			for validation.  If you would like to include the physical location of a schema used for validation you will need to manually edit the transformed XML instance to declare the XSI namespace 
			(xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”) and set the xsi:schemaLocation attribute.
	-->
    <xsl:output indent="yes" method="xml"/>
    
    <xsl:template match="/gjxdm:gjxdm">
        <niem:niem>
            <xsl:apply-templates select="descendant::jj:EnforcementOfficial"/>
            <xsl:apply-templates select="descendant::jj:PersonName"/>
            <xsl:apply-templates mode="write-it" select="descendant::jj:PrimaryContactInformation"/>
            <xsl:apply-templates mode="associate-it" select="descendant::jj:PrimaryContactInformation"/>
            <xsl:apply-templates select="//@jj:reportingOrganizationText"/>
        </niem:niem>
    </xsl:template>
    
    <xsl:template match="jj:PersonName">
        <nc:Person>
            <xsl:attribute name="s:id">
                <xsl:value-of select="generate-id(..)"/>
            </xsl:attribute>
            <xsl:apply-templates select="../jj:PersonBirthDate"/>
            <nc:PersonName>
                <xsl:attribute name="s:metadata">
                    <xsl:value-of select="generate-id(@jj:reportingOrganizationText)"/>
                </xsl:attribute>
                <nc:PersonGivenName>
                    <xsl:value-of select="jj:PersonGivenName"/>
                </nc:PersonGivenName>
                <nc:PersonSurName>
                    <xsl:value-of select="jj:PersonSurName"/>
                </nc:PersonSurName>
            </nc:PersonName>
        </nc:Person>
    </xsl:template>
    
    <xsl:template match="jj:PersonBirthDate">
        <nc:PersonBirthDate>
            <xsl:attribute name="s:metadata">
                <xsl:value-of select="generate-id(@jj:reportingOrganizationText)"/>
            </xsl:attribute>
            <nc:Date>
				<xsl:value-of select="."/>
			</nc:Date>
        </nc:PersonBirthDate>
    </xsl:template>
    
    <xsl:template match="@jj:reportingOrganizationText">
        <nc:Metadata>
            <xsl:attribute name="s:id">
                <xsl:value-of select="generate-id()"/>
            </xsl:attribute>
            <nc:ReportingOrganizationText>
                <xsl:value-of select="."/>
            </nc:ReportingOrganizationText>
        </nc:Metadata>
    </xsl:template>
    
    <xsl:template match="jj:PrimaryContactInformation" mode="write-it">
        <nc:ContactInformation>
            <xsl:attribute name="s:id">
                <xsl:value-of select="generate-id()"/>
            </xsl:attribute>
            <nc:ContactMailingAddress>
                <nc:StructuredAddress>
                    <nc:LocationStreet>
                        <nc:StreetFullText>
                            <xsl:value-of select="jj:ContactMailingAddress/jj:LocationStreet/jj:StreetFullText"/>
                        </nc:StreetFullText>
                    </nc:LocationStreet>
                    <nc:LocationCityName>
                        <xsl:value-of select="jj:ContactMailingAddress/jj:LocationCityName"/>
                    </nc:LocationCityName>
                    <nc:LocationStateName>
                        <xsl:value-of select="jj:ContactMailingAddress/jj:LocationStateName"/>
                    </nc:LocationStateName>
                    <nc:LocationPostalCode>
                        <xsl:value-of select="jj:ContactMailingAddress/jj:LocationPostalCode"/>
                    </nc:LocationPostalCode>
                </nc:StructuredAddress>
            </nc:ContactMailingAddress>
        </nc:ContactInformation>
    </xsl:template>
    
    <xsl:template match="jj:PrimaryContactInformation" mode="associate-it">
        <nc:PersonContactInformationAssociation>
            <nc:PersonReference>
                <xsl:attribute name="s:ref">
                    <xsl:value-of select="generate-id(..)"/>
                </xsl:attribute>
            </nc:PersonReference>
            <nc:ContactInformationReference>
                <xsl:attribute name="s:ref">
                    <xsl:value-of select="generate-id()"/>
                </xsl:attribute>
            </nc:ContactInformationReference>
            <nc:ContactInformationIsPrimaryIndicator>
                <xsl:text>true</xsl:text>
            </nc:ContactInformationIsPrimaryIndicator>
        </nc:PersonContactInformationAssociation>
    </xsl:template>
    
    <xsl:template match="jj:EnforcementOfficial">
        <j:EnforcementOfficial>
            <nc:RoleOfPersonReference>
                <xsl:attribute name="s:ref">
                    <xsl:value-of select="generate-id()"/>
                </xsl:attribute>
            </nc:RoleOfPersonReference>
            <j:EnforcementOfficialBadgeIdentification>
                <nc:IdentificationID>
                    <xsl:value-of select="jj:EnforcementOfficialBadgeID/jj:ID"/>
                </nc:IdentificationID>
            </j:EnforcementOfficialBadgeIdentification>
        </j:EnforcementOfficial>
    </xsl:template>
    
</xsl:stylesheet>
