<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
	exclude-result-prefixes="xsl"
	xmlns:cdm="http://cdm-fr.fr/2006/CDM-frSchema"
	xsi:schemaLocation="http://cdm-fr.fr/2006/CDM-frSchema http://cdm-fr.fr/2006/schemas/CDM-fr.xsd">
	<!-- traitement par défaut : copie des éléments -->
	<xsl:template match="*" mode="custom">
		<xsl:element name="cdm:{name()}">
			<xsl:apply-templates select="@* | comment() | text()"
				mode="custom" />
			<xsl:apply-templates select="*" mode="custom" />
		</xsl:element>
	</xsl:template>
	<!-- traitement par défaut : copie des attributs et éléments txt et comment -->
	<xsl:template match="@* | comment() | text()" mode="custom">
		<xsl:copy>
			<xsl:apply-templates select="@* | comment() | text()"
				mode="custom" />
		</xsl:copy>
	</xsl:template>
	<xsl:template match="* | @* | text() | comment()">
		<xsl:copy>
			<xsl:apply-templates select="*|@* | comment() | text()" />
		</xsl:copy>
	</xsl:template>
	<xsl:variable name="programID">
		<xsl:choose>
			<xsl:when
				test="not(key('formation', $oid)/infoBlock/extension/unrnpdc/lov[@info='typeDiplome'][@code!=''])">
				<xsl:value-of select="key('formation', $oid)/../@id" />
			</xsl:when>
			<xsl:when
				test="key('formation', $oid)/infoBlock/extension/unrnpdc/lov[@info='typeDiplome'][@code!='']">
				<xsl:value-of select="$oid" />
			</xsl:when>
		</xsl:choose>
	</xsl:variable>
	<!-- PRISE EN CHARGE DU TEMPLATE CUSTOM POINT D'ENTREE -->
	<!-- DEFINITION DU CONTENU DU RECORD OAI POUR LE PREFIXE CDM -->
	<xsl:template name="cdm_template">
		<cdm:CDM xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
			xmlns:cdm="http://cdm-fr.fr/2006/CDM-frSchema"
			xsi:schemaLocation="http://cdm-fr.fr/2006/CDM-frSchema http://cdm-fr.fr/2006/schemas/CDM-fr.xsd">
			<xsl:copy-of select="/CDM/@*" />
			<cdm:orgUnit>
				<xsl:attribute name="id"><xsl:value-of
						select="/CDM/orgUnit[1]/@id" />
				</xsl:attribute>
				<xsl:apply-templates
					select="/CDM/orgUnit[1]/orgUnitName" mode="custom" />
			</cdm:orgUnit>
			<cdm:program id="{$oid}" ident="{$oid}">
				<!-- PROGRAMNAME -->
				<xsl:apply-templates
						select="key('formation', $programID)/programName" mode="custom" />
				<!-- PROGRAMCODE -->
				<!-- copie eventuelle des programcode du parent-->
				<xsl:if test="$oid!=$programID">
					<xsl:apply-templates
						select="key('formation', $programID)/programCode[@codeSet='erasmus']"
						mode="custom" />
				</xsl:if>
				<!-- copie eventuelle des lov erasmus du parent-->
				<xsl:if test="$oid!=$programID">
					<xsl:apply-templates
						select="key('formation', $programID)/infoBlock/extension/unrnpdc/lov[@info='erasmus'][@code!='']" />
				</xsl:if>
				<!-- copie des programcode de l'objet -->
				<xsl:apply-templates
					select="key('formation', $oid)/programCode[@codeSet='erasmus']"
					mode="custom" />
				<xsl:apply-templates
					select="key('formation', $oid)/programCode[@codeSet='rome']"
					mode="custom" />
				<!-- PROGRAMDESCRIPTION -->
				<xsl:if test="$oid!=$programID">
					<xsl:apply-templates
								select="key('formation', $programID)/programDescription" mode='custom'/>
				</xsl:if>
				<xsl:apply-templates
							select="key('formation', $oid)/programDescription" mode='custom' />
				<!-- QUALIFICATION -->
				<xsl:variable name="importDegree">
					<xsl:choose>
						<xsl:when test="$oid!=$programID">
							<xsl:value-of
								select="key('formation', $programID)/infoBlock/extension/unrnpdc/lov[@info='typeDiplome']/@code" />
						</xsl:when>
						<xsl:otherwise>
							<xsl:value-of
								select="key('formation', $oid)/infoBlock/extension/unrnpdc/lov[@info='typeDiplome']/@code" />
						</xsl:otherwise>
					</xsl:choose>
				</xsl:variable>

				<cdm:qualification>
					<xsl:if
						test="$codes/typeDiplome/data[@import=$importDegree]">
						<cdm:degree
							degree="{$codes/typeDiplome/data[@import=$importDegree]/@oai}">
							<xsl:value-of
								select="key('formation', $programID)/infoBlock/extension/unrnpdc/lov[@info='typeDiplome']" />
						</cdm:degree>
					</xsl:if>
					<xsl:if
						test="count($codes/typeDiplome/data[import=$importDegree]/@oai)=1">
						<cdm:degree
							degree="{$codes/typeDiplome/data[import=$importDegree]/@oai}">
							<xsl:value-of
								select="key('formation', $programID)/infoBlock/extension/unrnpdc/lov[@info='typeDiplome']" />
						</cdm:degree>
					</xsl:if>
					<xsl:apply-templates
								select="key('formation', $oid)/qualification/profession" mode='custom' />
				</cdm:qualification>

				<!-- LEARNINGOBJECTIVES -->
				<xsl:apply-templates
							select="key('formation', $oid)/learningObjectives" mode='custom' />
				<!-- INFOBLOCK -->
				<cdm:infoBlock>
					<cdm:extension>
						<cdm:unrnpdc>
							<xsl:apply-templates select="key('formation', $oid)/infoBlock/extension/unrnpdc/typeObjet" mode="custom"/>
							<xsl:apply-templates select="key('formation', $oid)/infoBlock/extension/unrnpdc/labelCourant" mode="custom"/>
							<cdm:lov code="{$codes/typeDiplome/data[@import=$importDegree]/@oai}" info="typeDiplome" name="TYPEDIPLOME"/>
							<xsl:apply-templates select="key('formation', $oid)/infoBlock/extension/unrnpdc/lov[@info!='typeDiplome']" mode="custom"/>
							<xsl:apply-templates select="key('formation', $oid)/infoBlock/extension/unrnpdc/orioai-search" mode="custom"/>
							<xsl:apply-templates select="key('formation', $oid)/infoBlock/extension/unrnpdc/link" mode="custom"/>
						</cdm:unrnpdc>
					</cdm:extension>
				</cdm:infoBlock>
				<!-- SEACRHWORD -->
				<xsl:if test="$oid!=$programID">
					<xsl:apply-templates
						select="key('formation', $programID)/searchword" mode="custom" />
				</xsl:if>
				<xsl:apply-templates
					select="key('formation', $oid)/searchword" mode="custom" />
			</cdm:program>
		</cdm:CDM>
	</xsl:template>
	<!-- traitement du code erasmus -->
	<xsl:template match="lov[@info='erasmus']">
		<cdm:programCode codeSet="erasmus">
			<xsl:value-of select="@code" />
		</cdm:programCode>
	</xsl:template>
	<!-- traitement du lien -->
	<xsl:template match="link" mode="custom">
		<cdm:link>
			<xsl:call-template name="url" />
		</cdm:link>
	</xsl:template>
	<xsl:template match="header" mode="custom" />
	<xsl:template match="lov/labelCourant" mode="custom">
		<xsl:value-of select="normalize-space(.)" />
	</xsl:template>

	<!-- TRAITEMENT URL -->
	<xsl:template name="url">
		<xsl:value-of select="$url" />/ws?_cmd=getFormation&amp;_oid=<xsl:value-of select="/CDM/program[1]/@id" />&amp;_oidSpecialite=<xsl:value-of select="$oid" />&amp;_codAnuProgram=<xsl:value-of select="$codAnu" />&amp;_redirect=fiche&amp;_lang=fr-FR
	</xsl:template>
	<!-- TRAITEMENT ELEMENTS AVEC INFOBLOCK -->
	<xsl:template match="programDescription">
		<xsl:apply-templates select="* | text()" mode="custom" />
	</xsl:template>
	<xsl:template match="programDescription/subBlock" />
	<xsl:template match="subBlock[@userDefined]" mode="custom" />
	<xsl:template match="profession">
		<xsl:apply-templates select="* | text()" mode="custom" />
	</xsl:template>
	<xsl:template match="profession/subBlock" />
	<xsl:template match="learningObjectives">
		<xsl:apply-templates select="* | text()" mode="custom" />
	</xsl:template>
	<xsl:template match="learningObjectives/subBlock" />
</xsl:stylesheet>
