<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:cdm="http://cdm-fr.fr/2006/CDM-frSchema/"
	xmlns:java="java.lang.String"
	cdm:schemaLocation="http://cdm-fr.fr/2006/CDM-frSchema/ http://cdm-fr.fr/cdm/2006/schemas/CDM-fr.xsd"
	exclude-result-prefixes="xsi xsl">
	<xsl:variable name="rome"
		select="document('../rome/rome.xml')/ROME" />
	<xsl:variable name="qualifications"
		select="document('../rome/qualifications.xml')/qualifications" />
	<xsl:variable name="erasmus"
		select="document('../erasmus/erasmus.xml')/disciplines" />
		
	<xsl:output method="xml" standalone="yes" />
	<xsl:template match="* | @* | comment() | text()">
		<xsl:copy>
			<xsl:apply-templates select="@*|*|text() | comment()" />
		</xsl:copy>
	</xsl:template>
	<xsl:template match="programCode[@codeSet='rome']">
		<xsl:variable name="code" select="text()" />
		<xsl:variable name="type" select="/CDM/program[1]/infoBlock/extension/*/lov[@info='typeDiplome']/@code" />
		<xsl:for-each select="$rome/rome[v2/@code=$code or @code=$code]/fap224">
			<xsl:variable name="qualification" select="qualification/@qualification" />
			<xsl:if test="count($qualifications/qualification[@qualification=$qualification][typeDiplome=$type])>=1">

				<xsl:choose>
					<xsl:when test="string(number(substring($code,1,1)))='NaN'">
						<programCode codeSet="rome" ><xsl:value-of select="$code"/></programCode>
					</xsl:when>
					<xsl:otherwise>
						<programCode codeSet="rome" ><xsl:value-of select="../@code"/></programCode>
					</xsl:otherwise>
					
				</xsl:choose>
			</xsl:if>
		</xsl:for-each>
		
	</xsl:template>
	<xsl:template match="programCode[@codeSet='erasmus']">
		<xsl:variable name="code" select="text()" />
		<xsl:choose>
			<xsl:when test="not(contains($code,'.'))">
				<xsl:copy>
					<xsl:apply-templates select="@*|*|text() | comment()" />
				</xsl:copy>
			</xsl:when>
			<xsl:otherwise>
					<programCode codeSet="erasmus" ><xsl:value-of select="$erasmus/code[@ancien=$code]/nouveau[1]"/></programCode>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>
	<xsl:template match="lov[@info='rome']">
		<xsl:variable name="code" select="@code" />
		<xsl:variable name="type" select="/CDM/program[1]/infoBlock/extension/*/lov[@info='typeDiplome']/@code" />
		<xsl:for-each select="$rome/rome[v2/@code=$code or @code=$code]/fap224">
			<xsl:variable name="qualification" select="qualification/@qualification" />
			<xsl:if test="count($qualifications/qualification[@qualification=$qualification][typeDiplome=$type])>=1">

				<xsl:choose>
			<xsl:when test="string(number(substring($code,1,1)))='NaN'">
				<lov info="rome" code="{$code}" name="ROME"><xsl:value-of select="$rome/rome[@code=$code]/@intitule"/></lov>
			</xsl:when>
			<xsl:otherwise>
				<lov info="rome" code="{../@code}" name="ROME" ><xsl:value-of select="normalize-space(../@intitule)"/></lov>
			</xsl:otherwise>
		</xsl:choose>
			</xsl:if>
		</xsl:for-each>
		
	</xsl:template>
	<xsl:template match="lov[@info='erasmus']">
		<xsl:variable name="code" select="@code" />
		<xsl:choose>
			<xsl:when test="not(contains($code,'.'))">
				<lov info="erasmus" code="{$code}" name="ERASMUS"><xsl:value-of select="$erasmus/code/nouveau[text()=$code][1]/@intituleFR"/></lov>
			</xsl:when>
			<xsl:otherwise>
				<lov info="erasmus" code="{$erasmus/code[@ancien=$code]/nouveau[1]}" name="ERASMUS"><xsl:value-of select="$erasmus/code[@ancien=$code]/nouveau[1]/@intituleFR"/></lov>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>


</xsl:stylesheet>
