<?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/cdm/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()" mode="custom"/>
			<xsl:apply-templates select="* | text()" mode="custom"/>
		</xsl:element>
	</xsl:template>
	<!--<xsl:template match="text()" mode="custom">
		<xsl:value-of select="translate(.,'&#x2019;&#180;&#92;',&quot;'''&quot;)"/>
	</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>
	<!-- 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/cdm/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}">
				<cdm:programName>
					<cdm:text language="fr-FR">
						<xsl:apply-templates select="key('formation', $oid)/programName/text" mode="custom"/>
					</cdm:text>
				</cdm:programName>
				<xsl:choose>
					<xsl:when test="key('formation', $oid)/programCode[@codeSet='erasmus']">
						<xsl:apply-templates select="key('formation', $oid)/programCode[@codeSet='erasmus']" mode="custom"/>
					</xsl:when>
					<xsl:when test="key('formation', $oid)/infoBlock/extension/unrnpdc/lov[@info='erasmus'][@code!='']">
						<cdm:programCode codeSet="erasmus">
							<xsl:value-of select="key('formation', $oid)/infoBlock/extension/unrnpdc/lov[@info='erasmus'][@code!='']/@code"/>
						</cdm:programCode>
					</xsl:when>
					<xsl:otherwise/>
				</xsl:choose>
				<xsl:choose>
					<xsl:when test="key('formation', $oid)/programCode[@codeSet='rome']">
						<xsl:apply-templates select="key('formation', $oid)/programCode[@codeSet='rome']" mode="custom"/>
					</xsl:when>
				</xsl:choose>
				<xsl:apply-templates select="key('formation', $oid)/programDescription" mode="custom"/>
				<xsl:variable name="importDegree" select="key('formation', $oid)/infoBlock/extension/unrnpdc/lov[@info='typeDiplome']/@code"/>
				<xsl:if test="$codes/typeDiplome/data[@import=$importDegree]">
					<cdm:qualification>
						<cdm:degree degree="{$codes/typeDiplome/data[@import=$importDegree]/@oai}"/>
						<cdm:profession>
							<cdm:subBlock blockLang="fr-FR">
								<xsl:apply-templates
									select="key('formation', $oid)/qualification/profession" />
							</cdm:subBlock>
						</cdm:profession>
					</cdm:qualification>
				</xsl:if>
				<!-- LEARNINGOBJECTIVES -->
				<cdm:learningObjectives>
					<cdm:subBlock blockLang="fr-FR">
						<xsl:apply-templates
							select="key('formation', $oid)/learningObjectives" />
					</cdm:subBlock>
				</cdm:learningObjectives>
				<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>
				<xsl:apply-templates select="key('formation', $oid)/searchword" mode="custom"/>
			</cdm:program>
		</cdm:CDM>
	</xsl:template>
	<!-- traitement du type de diplôme -->
	<xsl:template match="lov[@info='typeDiplome']/@code">
		<xsl:variable name="code" select="."/>
		<xsl:for-each select="$codes/typeDiplome/data[@import=$code]">
			<xsl:attribute name="code"><xsl:value-of select="@oai"/></xsl:attribute>
		</xsl:for-each>
	</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"/>
	<!-- specifique lille1: TRAITEMENT DE L'INTITULE -->
	<xsl:template match="programName/text" mode="custom">
		<xsl:variable name="titre" select="normalize-space(.)"/>
		<xsl:choose>
			<xsl:when test="contains($titre, ' Sciences et Technologies')">
			<xsl:value-of select="substring-before($titre, ' Sciences et Technologies' )"/>
			<xsl:call-template name="finalite"/>
			</xsl:when>
			<xsl:when test="contains($titre, ' Sciences Economiques et de Gestion')">
			<xsl:value-of select="substring-before($titre, ' Sciences Economiques et de Gestion' )"/>
			<xsl:call-template name="finalite"/>
			</xsl:when>
			<xsl:when test="contains($titre, ' Sciences Humaines et Sociales')">
			<xsl:value-of select="substring-before($titre, ' Sciences Humaines et Sociales' )"/>
			<xsl:call-template name="finalite"/>
			</xsl:when>
			<xsl:when test="contains($titre, 'Ecoles doctorales') or (contains($titre, 'Ecole doctorale') and position()=1)"/>
			<xsl:when test="not(starts-with($titre,'Secteur') or starts-with($titre,'Profil'))">
				<xsl:if test="preceding-sibling::text and not(contains($titre, 'Ecole doctorale'))"> - </xsl:if>
				<xsl:value-of select="$titre"/>
			</xsl:when>
			<xsl:otherwise/>
		</xsl:choose>
	</xsl:template>
	<xsl:template name="finalite">
		<xsl:choose>
			<xsl:when test="key('formation', $oid)/programCode[@codeSet='uniform.finalite']='R'"> Recherche</xsl:when>
			<xsl:when test="key('formation', $oid)/programCode[@codeSet='uniform.finalite']='P'"> Professionel</xsl:when>
			<xsl:when test="key('formation', $oid)/programCode[@codeSet='uniform.finalite']='RP'"></xsl:when>
		</xsl:choose>
	</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;_oidProgramAnnexe=<xsl:value-of select="$oid"/>&amp;_codAnu=<xsl:value-of select="$codAnu"/>&amp;_redirect=voir_fiche_program&amp;_lang=fr-FR</xsl:template>
	<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>
