<?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:ns2="http://cdm-fr.fr/2011/CDM-frSchema"
	xmlns:cdm="http://cdm-fr.fr/2006/CDM-frSchema/"
	cdm:schemaLocation="http://cdm-fr.fr/2006/CDM-frSchema/ http://cdm-fr.fr/cdm/2006/schemas/CDM-fr.xsd"
	xmlns:java="java.lang.String"
	exclude-result-prefixes="xsi xsl">
	<xsl:variable name="rome2DP"
		select="document('../rome/rome.xml')/ROME" />
	<xsl:variable name="qualifications"
		select="document('../rome/qualifications.xml')/qualifications" />

	<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="rome" select="text()" />
		<!-- COPIE DE L'ELEMENT programCode -->
		<xsl:copy>
			<xsl:apply-templates select="@*|*|text() | comment()" />
		</xsl:copy>
		<!-- PUIS ETABLISSELMENT DES CORRESPONDANCES DP ET FAP -->
		<xsl:apply-templates
			select="$rome2DP/rome[@code=$rome]/fap224">
			<xsl:with-param name="type" select="java:toLowerCase(java:new(/CDM/program[1]/infoBlock/extension/*/lov[@info='typeDiplome']))"/>
		</xsl:apply-templates>
	</xsl:template>
	<xsl:template match="lov[@info='rome']">
		<xsl:variable name="rome" select="@code" />
		<!-- COPIE DE L'ELEMENT programCode -->
		<xsl:copy>
			<xsl:apply-templates select="@*|*|text() | comment()" />
		</xsl:copy>
		<!-- PUIS ETABLISSELMENT DES CORRESPONDANCES DP ET FAP -->
		<xsl:apply-templates select="$rome2DP/rome[@code=$rome]/fap224"
			mode="lov">
			<xsl:with-param name="type" select="java:toLowerCase(java:new(/CDM/program[1]/infoBlock/extension/*/lov[@info='typeDiplome']))"/>
			<xsl:with-param name="poids" select="@poids"/>
		</xsl:apply-templates>
	</xsl:template>
	<xsl:template match="fap224">
		<xsl:param name="type" />
		<xsl:variable name="nbFapQualifie">
			<xsl:for-each select="qualification">
				<xsl:variable name="qualification"
					select="@qualification" />
				<xsl:value-of
					select="count($qualifications/qualification[@qualification=$qualification][typeDiplome=$type])" />
			</xsl:for-each>
		</xsl:variable>
		<xsl:if
			test="count(qualification)=0 or number($nbFapQualifie)>=1">

			<programCode codeSet="fap224">
				<xsl:value-of select="@code" />
			</programCode>
			<xsl:apply-templates select="dp" />
			<!--  <xsl:apply-templates select="dp|pcs" />-->
		</xsl:if>
	</xsl:template>
	<xsl:template match="pcs">
		<programCode codeSet="pcs">
			<xsl:value-of select="@code" />
		</programCode>
	</xsl:template>
	<xsl:template match="nsf">
		<programCode codeSet="nsf">
			<xsl:value-of select="@code" />
		</programCode>
	</xsl:template>
	<xsl:template match="mefac">
		<programCode codeSet="mefac">
			<xsl:value-of select="@code" />
		</programCode>
		<xsl:apply-templates select="nsf" />
	</xsl:template>
	<xsl:template match="dp">
		<programCode codeSet="dp">
			<xsl:value-of select="@code" />
		</programCode>
		<!-- <xsl:apply-templates select="mefac" /> -->
	</xsl:template>

	<xsl:template match="fap224" mode="lov">
		<xsl:param name="type" />
		<xsl:param name="poids" />	
		<xsl:variable name="nbFapQualifie">
			<xsl:for-each select="qualification">
				<xsl:variable name="qualification" select="@qualification"/>
				<xsl:value-of select="count($qualifications/qualification[@qualification=$qualification][typeDiplome=$type])"/>
			</xsl:for-each>
		</xsl:variable>	
		<xsl:choose>
		<xsl:when test="count(qualification)=0 or number($nbFapQualifie)>=1">
		    <lov info="fap224" name="FAP224" code="{@code}" />
			<lov info="dp" name="DP" code="{dp/@code}" />
			<xsl:choose>
				<xsl:when test="$poids='5'">
					<orioai-search info="fap224" code="{@code}"
						rank="{$poids}">
						<xsl:value-of select="@code" />
					</orioai-search>
				</xsl:when>
				<xsl:when test="$poids='4'">
					<orioai-search info="fap224" code="{@code}"
						rank="{$poids}">
						<xsl:value-of select="@code" /> o r</orioai-search>
				</xsl:when>
				<xsl:when test="$poids='3'">
					<orioai-search info="fap224" code="{@code}"
						rank="{$poids}">
						<xsl:value-of select="@code" /> o r i o</orioai-search>
				</xsl:when>
				<xsl:when test="$poids='2'">
					<orioai-search info="fap224" code="{@code}"
						rank="{$poids}">
						<xsl:value-of select="@code" /> o r i o a i</orioai-search>
				</xsl:when>
				<xsl:when test="$poids='1'">
					<orioai-search info="fap224" code="{@code}"
						rank="{$poids}">
						<xsl:value-of select="@code" /> o r i o a i o r</orioai-search>
				</xsl:when>
				<xsl:otherwise>
					<orioai-search info="fap224" code="{@code}"
						rank="5">
						<xsl:value-of select="@code" /></orioai-search>
				</xsl:otherwise>
			</xsl:choose>
			</xsl:when>
			<xsl:otherwise><!--<lov info="fap224" type="{$type}" name="FAP224" code="{@code}"  nbFap224="{$nbFapQualifie}"/>-->
			</xsl:otherwise>
			</xsl:choose>

	</xsl:template>
</xsl:stylesheet>
