<?xml version="1.0" encoding="UTF-8"?>
<!--
This file is part of IREL, "Instruments de Recherche En Ligne",
<http://www.archivesnationales.culture.gouv.fr/caom/fr/index_irel.html>.
Copyright © 2008, 2009 : ANOM, PASS-TECH

Archives nationales d’outre-mer (ANOM, France)
29, chemin du moulin de Testas
13090 Aix-en-Provence
<http://www.archivesnationales.culture.gouv.fr/caom/fr/>
anom.aix@culture.gouv.fr

PASS-TECH
26, rue Louis Braille
F-75012 Paris
<http://pass-tech.fr/>
contact@pass-tech.fr

IREL is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

IREL is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with IREL application; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

$Date: 2008-09-06 17:25:29 +0200 (sam., 06 sept. 2008) $:
$Author: frederic $:
$Rev: 350 $:

-->
<!--
XML EAC Encoded Archival Context
<http://jefferson.village.virginia.edu/eac>

Transformation vers EAC-CPF.
-->
<xsl:transform
	version="1.1"
	xmlns="urn:isbn:1-931666-33-4"
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
	xmlns:xlink="http://www.w3.org/1999/xlink"
	exclude-result-prefixes=""
>

	<!-- la déclaration de sortie -->
	<xsl:output method="xml" indent="yes" encoding="UTF-8" />

	<!-- Racine -->
	<xsl:template match="/eac">
		<eac-cpf xmlns="urn:isbn:1-931666-33-4">
			<control>
				<!--
				<recordId><xsl:value-of select="@id"/></recordId>
				-->
				<recordId>
					<xsl:call-template name="convertid">
						<xsl:with-param name="id" select="@id" />
					</xsl:call-template>
				</recordId>

				<!-- Control -->
				<xsl:apply-templates select="eacheader" />
			</control>

			<!-- cpfDescription -->
			<xsl:apply-templates select="condesc" />
		</eac-cpf>
	</xsl:template>

	<!-- EAC header -->
	<xsl:template match="eacheader">

		<!-- Dans les documents EAC des ANOM, on a seulement la valeur "edited"
		     que l'on traduit ici en "revised". On aurait également pu
			 utiliser "derived" => question à poser aux ANOM -->
		<maintenanceStatus>revised</maintenanceStatus>

		<maintenanceAgency>
			<agencyCode>FR-ANOM</agencyCode>
			<agencyName>Archives nationales d'outre-mer</agencyName>
		</maintenanceAgency>

		<xsl:apply-templates select="languagedecl"/>

		<xsl:apply-templates select="ruledecl/rule" />	

		<!--
		<xsl:apply-templates select="eacid/@countrycode" />
		<xsl:apply-templates select="@status" mode="localTypeDeclaration"/>
		-->

		<xsl:apply-templates select="@detaillevel" />

		<xsl:apply-templates select="mainhist" />

		<xsl:apply-templates select="sourcedecl" />
	</xsl:template>

	<!-- Langue du document -->
	<xsl:template match="languagedecl">
		<languageDeclaration>
			<xsl:apply-templates select="language" />
            <script scriptCode="Latn">Latin</script>
		</languageDeclaration>
	</xsl:template>

	<xsl:template match="language">
		<language>
			<xsl:copy-of select="*[name() != 'languagecode']" />
			<xsl:if test="@languagecode">
				<xsl:attribute name="languageCode"><xsl:value-of select="@languagecode" /></xsl:attribute>
			</xsl:if>
			<xsl:apply-templates />
		</language>
	</xsl:template>

	<!-- Règles -->
	<xsl:template match="ruledecl/rule">
		<conventionDeclaration>
			<xsl:if test="@id">
				<abbreviation><xsl:value-of select="@id" /></abbreviation>
			</xsl:if>
			<citation><xsl:value-of select="." /></citation>
		</conventionDeclaration>
	</xsl:template>

	<!-- Nationalité -->
	<xsl:template match="eacid/@countrycode">
        <localTypeDeclaration>
            <abbreviation>nationality</abbreviation>
            <citation xlink:href="http://www.iso.org/iso/french_country_names_and_code_elements" xlink:type="simple" lastDateTimeVerified="2010-05-25">Notation du pays de nationalité suivant la codification ISO 3166-1-alpha-2</citation>
        </localTypeDeclaration>
	</xsl:template>

	<!-- Statut d'édition -->
	<xsl:template match="@status" mode="localTypeDeclaration">
		<localTypeDeclaration>
			<abbreviation>editing_status</abbreviation>
			<citation><xsl:value-of select="." /></citation>
		</localTypeDeclaration>
	</xsl:template>

	<!-- Niveau de détail -->
	<xsl:template match="@detaillevel">
		<localControl localType="detailLevel">
			<!-- Toujours partial dans les documents EAC des ANOM -->
			<term>partial</term>
		</localControl>
	</xsl:template>

	<!-- Historique de maintenance -->
	<xsl:template match="mainhist">
		<maintenanceHistory>
			<xsl:apply-templates select="mainevent" />
		</maintenanceHistory>
	</xsl:template>

	<!-- Événement de maintenance -->
	<xsl:template match="mainevent">
		<maintenanceEvent>
			<!-- TODO : vraie correspondance entre les types d'événements -->
			<eventType><xsl:value-of select="@maintype"/>d</eventType>
			<xsl:apply-templates select="maindate" />
			<agentType>human</agentType>
			<xsl:apply-templates select="maindesc" />
		</maintenanceEvent>
	</xsl:template>

	<!-- Date d'événement de maintenance -->
	<xsl:template match="maindate">
		<eventDateTime standardDateTime="{.}">
			<!-- Jour -->
			<xsl:value-of select="concat(substring(., 9),' ')" />

			<!-- Mois -->
			<xsl:variable name="mois" select="substring(., 6, 2)" />
			<xsl:if test="$mois='01'">janvier</xsl:if>
			<xsl:if test="$mois='02'">février</xsl:if>
			<xsl:if test="$mois='03'">mars</xsl:if>
			<xsl:if test="$mois='04'">avril</xsl:if>
			<xsl:if test="$mois='05'">mai</xsl:if>
			<xsl:if test="$mois='06'">juin</xsl:if>
			<xsl:if test="$mois='07'">juillet</xsl:if>
			<xsl:if test="$mois='08'">août</xsl:if>
			<xsl:if test="$mois='09'">septembre</xsl:if>
			<xsl:if test="$mois='10'">octobre</xsl:if>
			<xsl:if test="$mois='11'">novembre</xsl:if>
			<xsl:if test="$mois='12'">décembre</xsl:if>
			<xsl:text> </xsl:text>

			<!-- Année -->
			<xsl:value-of select="substring(., 1, 4)" />
		</eventDateTime>
	</xsl:template>

	<!-- Description d'événement de maintenance -->
	<xsl:template match="maindesc">
		<xsl:if test="contains(., 'par ')">
			<agent>
				<xsl:value-of select="substring-after(.,'par ')" />
			</agent>
		</xsl:if>
		<eventDescription>
			<xsl:value-of select="." />
		</eventDescription>
	</xsl:template>

	<!-- Sources -->
	<xsl:template match="sourcedecl">
		<sources>
			<xsl:apply-templates select="source" />
		</sources>
	</xsl:template>

	<!-- Source -->
	<xsl:template match="source">
		<source>
			<sourceEntry><xsl:value-of select="." /></sourceEntry>
		</source>
	</xsl:template>

	<!-- cpfDescription -->
	<xsl:template match="condesc">
		<cpfDescription>

			<xsl:apply-templates select="identity" />
			<xsl:apply-templates select="desc" />

			<xsl:if test="eacrels or resourcerels">
				<relations>
					<xsl:apply-templates select="eacrels" />
					<xsl:apply-templates select="resourcerels" />
				</relations>
			</xsl:if>

		</cpfDescription>
	</xsl:template>

	<!-- Identity -->
	<xsl:template match="identity">
		<identity>

			<entityType>
				<xsl:if test="/eac/@type='persname'">person</xsl:if>	
				<xsl:if test="/eac/@type='corpname'">corporateBody</xsl:if>	
			</entityType>

			<xsl:apply-templates select=".//pershead" />
		</identity>
	</xsl:template>

	<!-- Pershead -->
	<xsl:template match="pershead">
		<!-- TODO: que faire de @rule ? -->
		<nameEntry>
			<xsl:apply-templates select="part" />
			<xsl:choose>
				<xsl:when test="@authorized">
					<authorizedForm>FRANOM</authorizedForm>
				</xsl:when>
				<xsl:otherwise>
					<alternativeForm>FRANOM</alternativeForm>
				</xsl:otherwise>
			</xsl:choose>
		</nameEntry>
	</xsl:template>

	<xsl:template match="part">
		<part>
			<xsl:if test="@type">
				<xsl:attribute name="localType"><xsl:value-of select="@type" /></xsl:attribute>
			</xsl:if>
			<xsl:value-of select="." />
		</part>
	</xsl:template>

	<!-- Description -->
	<xsl:template match="desc">
		<description>
		
			<xsl:apply-templates select="*/existdesc" />
			<xsl:apply-templates select="*/funactdesc" />
			<xsl:apply-templates select="bioghist" />

		</description>
	</xsl:template>

	<!-- Dates et lieux -->
	<xsl:template match="existdesc">

		<xsl:if test="existdate">
			<existDates>
				<xsl:choose>
					<xsl:when test="count(existdate)=2
									and existdate[@type='date_naissance'] and existdate[@type='date  deces']
									and not(contains(existdate[@type='date_naissance']/@normal,'/'))
									and not(contains(existdate[@type='date  deces']/@normal,'/'))">
						<xsl:variable name="from" select="existdate[@type='date_naissance']" />
						<xsl:variable name="to" select="existdate[@type='date  deces']" />
						<dateRange>
							<fromDate standardDate="{$from/@normal}"><xsl:value-of select="$from" /></fromDate>
							<toDate standardDate="{$to/@normal}"><xsl:value-of select="$to" /></toDate>
						</dateRange>
					</xsl:when>
					<xsl:when test="count(existdate) &gt; 1">
						<dateSet>
							<xsl:apply-templates select="existdate" />
						</dateSet>
					</xsl:when>
					<xsl:otherwise>
						<xsl:apply-templates select="existdate" />
					</xsl:otherwise>
				</xsl:choose>
			</existDates>
		</xsl:if>

		<xsl:if test="place">
			<places>
				<xsl:apply-templates select="place" />
			</places>
		</xsl:if>
	</xsl:template>

	<!-- Dates d'existence / date -->
	<xsl:template match="existdate|date">

		<xsl:choose>
			<xsl:when test="contains(@normal, '/') or string-length(@normal) = 8">
				<dateRange>
					<xsl:if test="@type">
						<xsl:attribute name="localType"><xsl:value-of select="@type" /></xsl:attribute>
					</xsl:if>
					<xsl:variable name="from">
						<xsl:choose>
							<xsl:when test="contains(@normal,'/')">
								<xsl:value-of select="substring-before(@normal,'/')" />
							</xsl:when>
							<xsl:otherwise>
								<xsl:value-of select="substring(@normal,1,4)" />
							</xsl:otherwise>
						</xsl:choose>
					</xsl:variable>
					<xsl:variable name="fromtext">
						<xsl:if test="contains(.,'-')">
							<xsl:value-of select="normalize-space(substring-before(.,'-'))" />
						</xsl:if>
					</xsl:variable>
					<xsl:variable name="to">
						<xsl:choose>
							<xsl:when test="contains(@normal,'/')">
								<xsl:value-of select="substring-after(@normal,'/')" />
							</xsl:when>
							<xsl:otherwise>
								<xsl:value-of select="substring(@normal,5,8)" />
							</xsl:otherwise>
						</xsl:choose>
					</xsl:variable>
					<xsl:variable name="totext">
						<xsl:if test="contains(.,'-')">
							<xsl:value-of select="normalize-space(substring-after(.,'-'))" />
						</xsl:if>
					</xsl:variable>
					<fromDate standardDate="{$from}"><xsl:value-of select="$fromtext" /></fromDate>
					<toDate standardDate="{$to}"><xsl:value-of select="$totext" /></toDate>
				</dateRange>
			</xsl:when>
			<xsl:otherwise>
				<date>
					<xsl:if test="@normal and @normal != ''">
						<xsl:attribute name="standardDate"><xsl:value-of select="@normal" /></xsl:attribute>
					</xsl:if>
					<xsl:if test="@type">
						<xsl:attribute name="localType"><xsl:value-of select="@type" /></xsl:attribute>
					</xsl:if>
					<xsl:value-of select="." />
				</date>
			</xsl:otherwise>
		</xsl:choose>

	</xsl:template>

	<!-- Lieux -->
	<xsl:template match="place">
		<place>
			<xsl:if test="@type">
				<placeRole><xsl:value-of select="@type" /></placeRole>
			</xsl:if>
			<placeEntry><xsl:value-of select="."/></placeEntry>

			<!-- Associe-t-on la date de naissance ? -->
			<xsl:if test="@type = 'lieu naissance' and parent::*/existdate[@type='date_naissance']">
				<xsl:variable name="date" select="parent::*/existdate[@type='date_naissance'][1]" />
				<date localType="date naissance">
					<xsl:if test="@normal and @normal != ''">
						<xsl:attribute name="standardDate"><xsl:value-of select="@normal" /></xsl:attribute>
					</xsl:if>
					<xsl:value-of select="$date" />
				</date>
			</xsl:if>

			<!-- Associe-t-on la date de décès ? -->
			<xsl:if test="@type = 'lieu deces' and parent::*/existdate[@type='date  deces']">
				<xsl:variable name="date" select="parent::*/existdate[@type='date  deces'][1]" />
				<date localType="date  deces">
					<xsl:if test="@normal and @normal != ''">
						<xsl:attribute name="standardDate"><xsl:value-of select="@normal" /></xsl:attribute>
					</xsl:if>
					<xsl:value-of select="$date" />
				</date>
			</xsl:if>
		</place>
	</xsl:template>

	<!-- bioghist -->
	<xsl:template match="bioghist">
		<biogHist>
			<xsl:apply-templates select="p" />
		</biogHist>
	</xsl:template>

	<!-- Fonctions, activités -->
	<xsl:template match="funactdesc">
		<functions>
			<xsl:apply-templates select="chronlist/chronitem" />
		</functions>
	</xsl:template>

	<!-- ChronItem -->
	<xsl:template match="chronitem">
		<function>
			<xsl:apply-templates select="event"/>
			<xsl:apply-templates select="place" mode="placeentry"/>
			<xsl:apply-templates select="date" />
		</function>
	</xsl:template>

	<!-- Lieu -->
	<xsl:template match="place" mode="placeentry">
		<xsl:if test=". != ''">
			<placeEntry localType="{@type}"><xsl:value-of select="." /></placeEntry>
		</xsl:if>
	</xsl:template>

	<!-- Événement -->
	<xsl:template match="event">
		<term><xsl:value-of select="normalize-space(.)"/></term>
	</xsl:template>

	<!-- Copie générique -->
	<xsl:template match="*">
		<xsl:element name="{name()}">
			<xsl:copy-of select="@*" />
			<xsl:apply-templates />
		</xsl:element>
	</xsl:template>

	<!-- eacrels -->
	<xsl:template match="eacrels">
		<xsl:apply-templates select="eacrel" />
	</xsl:template>

	<!-- eacrel -->
	<xsl:template match="eacrel">
		<cpfRelation>
			<xsl:if test="@reltype">
				<xsl:attribute name="cpfRelationType">
					<xsl:choose>
						<xsl:when test="@reltype='earlier'">temporal-earlier</xsl:when>
						<xsl:when test="@reltype='later'">temporal-later</xsl:when>
						<xsl:when test="@reltype='child'">hierarchical-child</xsl:when>
						<xsl:when test="@reltype='parent'">hierarchical-parent</xsl:when>
						<xsl:when test="@reltype='superior'">hierarchical</xsl:when>
						<xsl:when test="@reltype='associative'">associative</xsl:when>
					</xsl:choose>
				</xsl:attribute>
			</xsl:if>
			<xsl:if test="@syskey">
				<xsl:attribute name="xlink:href">
					<xsl:call-template name="convertid">
						<xsl:with-param name="id" select="@syskey" />
					</xsl:call-template>
				</xsl:attribute>
				<xsl:attribute name="xlink:type">simple</xsl:attribute>
			</xsl:if>
			<xsl:apply-templates select="persname" mode="relation" />
			<xsl:apply-templates select="descnote/date" />
			<xsl:apply-templates select="descnote" />
		</cpfRelation>
	</xsl:template>

	<!-- Personne dans les relations -->
	<xsl:template match="persname" mode="relation">
		<relationEntry><xsl:value-of select="." /></relationEntry>
	</xsl:template>

	<!-- Note descriptive -->
	<xsl:template match="descnote">
		<descriptiveNote><p><xsl:value-of select="." /></p></descriptiveNote>
	</xsl:template>

	<!-- resourcerels -->
	<xsl:template match="resourcerels">
		<xsl:apply-templates select="resourcerel" />
	</xsl:template>

	<!-- resourcerel -->
	<xsl:template match="resourcerel">
		<resourceRelation>
			<xsl:if test="@reltype">
				<xsl:attribute name="resourceRelationType">
					<xsl:choose>
						<xsl:when test="@reltype='other'">other</xsl:when>
						<xsl:when test="@reltype='subject'">subjectOf</xsl:when>
						<xsl:when test="@reltype='origination'">creatorOf</xsl:when> 
						<xsl:when test="@reltype='causa'">other</xsl:when> 
					</xsl:choose>
				</xsl:attribute>
			</xsl:if>
			<xsl:if test="@syskey">
				<xsl:attribute name="xlink:href"><xsl:value-of select="@syskey" /></xsl:attribute>
				<xsl:attribute name="xlink:type">simple</xsl:attribute>
			</xsl:if>
			<descriptiveNote>
				<p>
					<xsl:value-of select="archunit/unittitle" />
				</p>
			</descriptiveNote>
		</resourceRelation>
	</xsl:template>

	<!-- Conversion id EAC -> id EAC_CPF -->
	<xsl:template name="convertid">
		<xsl:param name="id" />

		<xsl:variable name="prefix">FRDAFANOM_EACP</xsl:variable>

		<xsl:choose>
			<xsl:when test="contains($id, $prefix)">
				<xsl:value-of select="concat(substring-before($id, $prefix),'FRANOM_EAC-CPF_',substring-after($id, $prefix))"/>
			</xsl:when>
			<xsl:otherwise>
				<xsl:value-of select="$id" />
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>

</xsl:transform>
