<xsl:stylesheet xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0" xmlns:table="urn:oasis:names:tc:opendocument:xmlns:table:1.0" xmlns:svg="urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0" xmlns:draw="urn:oasis:names:tc:opendocument:xmlns:drawing:1.0" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.0">
    <xsl:import href="scenarioNomenclature.xsl"/>
    <xsl:import href="scenarioXmlExample.xsl"/>
    <xsl:import href="scenarioODF.xsl"/>
    <xsl:import href="common.xsl"/>
    <xsl:param name="imagePrefixURL"/>
	<!-- URL des fichiersXML contenant les CodeType -->
    <xsl:param name="urlPrefixCodeType" select="'http://tomnux.rnde.tm.fr:8080/grcsandre/data/'"/>
    <xsl:param name="cfCodeList" select="'cf nomenclature de code Sandre '"/>	
	
	<!-- URL du fichier source Schema du scenario -->
    <xsl:param name="sourceURL"/>
 	
 	<!--  URL du fichier referentiel des code liste -->
    <xsl:param name="referentielURL"/>
 	
 	<!--  REST URI pour exploiter les chemins relatifs dans les schemas importes -->
    <xsl:param name="dburi"/> 
 	<!--  select="'http://localhost:8090/eXist-1.2.6-rev9165/rest'" /> -->
 	<!--  select="'../../../../db' -->
 	<!-- en prod: http://xml.sandre.eaufrance.fr -->
    <xsl:output method="xml" encoding="UTF-8" omit-xml-declaration="yes"/>

	<!-- Variables spécifiques au Scenario -->
    <xsl:variable name="titreBaliseRacineVarName">BaliseRacine</xsl:variable>
    <xsl:variable name="titreSchemaNameVarName">SchemaName</xsl:variable>
    <xsl:variable name="titreSchemaLocationVarName">SchemaURI</xsl:variable>
    <xsl:variable name="schemaFileName">
        <xsl:call-template name="getFileName">
            <xsl:with-param name="path" select="$sourceURL"/>
        </xsl:call-template>
    </xsl:variable>
    <xsl:variable name="schemaFilePath" select="concat($dburi, substring-before( substring-after($sourceURL, '/db'), $schemaFileName))"/>
    <xsl:template match="/">
        <scenario>
            <xsl:call-template name="displayLog">
                <xsl:with-param name="logContent">Début du traitement du scenario d'échanges</xsl:with-param>
            </xsl:call-template>
            <xsl:call-template name="displayLog">
                <xsl:with-param name="logContent">Paramètre dburi: <xsl:value-of select="$dburi"/>
                </xsl:with-param>
            </xsl:call-template>
            <xsl:call-template name="displayLog">
                <xsl:with-param name="logContent">Paramètre sourceURL: <xsl:value-of select="$sourceURL"/>
                </xsl:with-param>
            </xsl:call-template>
            <xsl:call-template name="displayLog">
                <xsl:with-param name="logContent">Variable schemaFileName: <xsl:value-of select="$schemaFileName"/>
                </xsl:with-param>
            </xsl:call-template>
            <xsl:call-template name="displayLog">
                <xsl:with-param name="logContent">Variable schemaFilePath: <xsl:value-of select="$schemaFilePath"/>
                </xsl:with-param>
            </xsl:call-template>
	
			<!--
				RECUPERATION DES ESPACES DE NOMMAGE
			-->
            <namespaces>
                <table:table table:style-name="{$nsTableStyleName}">
                    <xsl:call-template name="displayNsHeaders"/>
                    <xsl:variable name="currentNamespace" select="/*[local-name()='schema']/namespace::*[local-name(.)='']"/>
                    <xsl:call-template name="displayOneNamespace">
                        <xsl:with-param name="nsUrl" select="string($currentNamespace)"/>
                    </xsl:call-template>
                    <xsl:for-each select="/*[local-name()='schema']/namespace::*[.!='']">
                        <xsl:variable name="ns" select="."/>
                        <xsl:if test="starts-with(local-name($ns), $sandreNsPrefixStartsWith)">
                            <xsl:variable name="nsUrl" select="string(.)"/>
                            <xsl:variable name="relativeFileName" select="string(//*[local-name()='import'][@namespace=$nsUrl]/@schemaLocation)"/>
                            <xsl:variable name="fileName">
                                <xsl:call-template name="getFileName">
                                    <xsl:with-param name="path" select="$relativeFileName"/>
                                </xsl:call-template>
                            </xsl:variable>
                            <xsl:variable name="filePath" select="substring-before($relativeFileName, $fileName)"/>
                            <xsl:call-template name="displayLog">
                                <xsl:with-param name="logContent">Récupération document <xsl:value-of select="local-name($ns)"/> schemaLocation <xsl:value-of select="$relativeFileName"/>
                                </xsl:with-param>
                            </xsl:call-template>
                            <xsl:if test="$fileName!=''">
                                <xsl:call-template name="displayOneNamespace">
                                    <xsl:with-param name="nsUrl" select="$nsUrl"/>
                                    <xsl:with-param name="nsPrefix" select="local-name(.)"/>
                                    <xsl:with-param name="nsTitle" select="document(concat($schemaFilePath, $relativeFileName))/*[local-name()='schema']/*[local-name()='annotation'][position()=1]/*[local-name()='documentation'][@source='Titre']"/>
                                </xsl:call-template>
                            </xsl:if>
                        </xsl:if>
                    </xsl:for-each>
                </table:table>
                <xsl:call-template name="displayEmptyLine"/>
            </namespaces>
	
			<!-- 
				TRAITEMENT DES NOMENCLATURES
			-->
            <nomenclatures>
                <xsl:call-template name="computeNomenclature"/>
                <xsl:call-template name="displayEmptyLine"/>
            </nomenclatures>
	
			<!-- 
				RECUPERATION DES VARIABLES DU TEMPLATE OPEN OFFICE 
			-->
            <templates>
                <xsl:call-template name="computeTemplateVariables">
                    <xsl:with-param name="annotation" select="/*[local-name()='schema']/*[local-name()='annotation'][position()=1]"/>
                </xsl:call-template>
				
				<!-- Variables spécifiques au Scenario -->
                <xsl:variable name="VarBaliseRacine" select="//*[local-name()='element'][count(ancestor::*[local-name()='element']) = 0]/@name"/>
                <xsl:call-template name="displayOneTemplateVariable">
                    <xsl:with-param name="templateVarName" select="$titreBaliseRacineVarName"/>
                    <xsl:with-param name="templateVarValue">
                        <xsl:value-of select="$VarBaliseRacine"/>
                    </xsl:with-param>
                </xsl:call-template>
                <xsl:variable name="targetNamespace" select="/*[local-name()='schema']/@targetNamespace"/>
<!-- DEBUG
				<xsl:message>sourceURL <xsl:value-of select="$sourceURL" /></xsl:message>
 -->
                <xsl:variable name="xsiSchemaLocation">
                    <xsl:call-template name="getFileName">
                        <xsl:with-param name="path" select="$sourceURL"/>
                    </xsl:call-template>
                </xsl:variable>
                <xsl:call-template name="displayOneTemplateVariable">
                    <xsl:with-param name="templateVarName" select="$titreSchemaLocationVarName"/>
                    <xsl:with-param name="templateVarValue">
                        <xsl:value-of select="concat($targetNamespace, '/', $xsiSchemaLocation)"/>
                    </xsl:with-param>
                </xsl:call-template>
            </templates>

				<!-- 
					RECUPERATION DES EVOLUTIONS DU SCENARIO D'ECHANGES
				-->
            <evolutions>
                <xsl:variable name="evolutions" select="/*[local-name()='schema']/*[local-name()='annotation'][position()=1]/*[local-name()='documentation'][@source=$evolutionSchemeVarName]"/>
                <xsl:choose>
                    <xsl:when test="$evolutions">
                        <xsl:call-template name="displayEvolutions">
                            <xsl:with-param name="evolutions" select="$evolutions"/>
                        </xsl:call-template>
                    </xsl:when>
                    <xsl:otherwise>
                        <xsl:call-template name="displayEvolutions">
                            <xsl:with-param name="evolutions" select="$undefinedLabel"/>
                        </xsl:call-template>
                    </xsl:otherwise>
                </xsl:choose>
            </evolutions>

			<!-- 
				TRAITEMENT DE LA BALISE RACINE
			-->
            <xsl:for-each select="//*[local-name()='element'][count(ancestor::*[local-name()='element']) = 0]">
				<!--  PREMIERE LIGNE -->
                <rootline>
                    <xsl:call-template name="computeRootElementLine">
                        <xsl:with-param name="element" select="."/>
                    </xsl:call-template>
                </rootline>
				
				<!--  TABLEAU DESCRIPTION -->
                <root>
				<!-- <xsl:call-template name="displayRootElementChapter"/>  -->
                    <xsl:call-template name="computeRootElement">
                        <xsl:with-param name="element" select="."/>
                    </xsl:call-template>
                </root>
            </xsl:for-each>

			<!-- 
				TRAITEMENT DE L'ELEMENT SCENARIO
				Tous les éléments qui décrivent le scénario ?
			-->

<!-- L'élément Scenario seul
//*[(local-name()='element') and @name='Scenario'][count(ancestor::*[local-name()='element']) = 1] 
-->
<!-- L'élément Scenario et ses sous-éléments
//*[(local-name()='element')][ancestor-or-self::*[(local-name()='element') and (@name='Scenario')]]
//*[(local-name()='element')][ancestor-or-self::*[(local-name()='element') and (@name='Scenario')]]
 -->
            <subroot>
			
<!--
 			<xsl:call-template name="displaySubRootElementChapter"/>
 -->
                <xsl:for-each select="//*[(local-name()='element') and @name='Scenario'][count(ancestor::*[local-name()='element']) = 1]">
					<!-- Teste si l'élément peut faire l'objet d'un tableau -->
                    <xsl:variable name="isValid">
                        <xsl:call-template name="hasScndLevelElements">
                            <xsl:with-param name="element" select="."/>
                            <xsl:with-param name="level" select="3"/>
                        </xsl:call-template>
                    </xsl:variable>
                    <xsl:if test="string($isValid)=string(true())">
                        <xsl:call-template name="displayChildElementChapter">
                            <xsl:with-param name="elementName" select="@name"/>
                        </xsl:call-template>
                        <xsl:call-template name="computeSubRootElement">
                            <xsl:with-param name="element" select="."/>
                        </xsl:call-template>
                    </xsl:if>
                </xsl:for-each>
            </subroot>

			<!-- 
				TRAITEMENT DES ELEMENTS METIER
				Tous les éléments sauf NIVEAU 0 et Scenario
			-->
            <children>
                <xsl:call-template name="computeChildElement">
                    <xsl:with-param name="level">1</xsl:with-param>
                </xsl:call-template>  --&gt;
			</children>
            <xsl:call-template name="displayLog">
                <xsl:with-param name="logContent">Fin de traitement du scenario d'échanges</xsl:with-param>
            </xsl:call-template>
        </scenario>
    </xsl:template>


	<!-- 
		TESTE SI UN ELEMENT A DEJA ETE TRAITE
	-->
    <xsl:template name="hasBeenAlreadyComputed">
        <xsl:param name="name"/>
        <xsl:param name="type"/>
        <xsl:param name="level"/>
        <xsl:choose>
            <xsl:when test="//*[local-name()='element'][@name=$name and (not(@type) or @type=$type) and number($level) &gt; count(ancestor::*[local-name()='element'])]">
                <xsl:copy-of select="true()"/>
            </xsl:when>
            <xsl:otherwise>
                <xsl:copy-of select="false()"/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>

	<!-- 
		TESTE SI UN ELEMENT A DES FILS DE SECOND NIVEAU
	-->
    <xsl:template name="hasScndLevelElements">
        <xsl:param name="element"/>
        <xsl:param name="level"/>
        <xsl:choose>
            <xsl:when test="$element/descendant::*[local-name()='element'][count(ancestor::*[local-name()='element']) = $level]">
                <xsl:copy-of select="true()"/>
            </xsl:when>
            <xsl:otherwise>
                <xsl:copy-of select="false()"/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>

	<!-- 
		TESTE SI IL EXISTE DES ELEMENTS DE NIVEAU DONNE
	-->
    <xsl:template name="newLevelExist">
        <xsl:param name="level"/>
        <xsl:choose>
            <xsl:when test="//*[local-name()='element'][count(ancestor::*[local-name()='element']) = $level]">
                <xsl:copy-of select="true()"/>
            </xsl:when>
            <xsl:otherwise>
                <xsl:copy-of select="false()"/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>

	<!--
		TRAITEMENT DES ELEMENTS METIER (NIVEAU >= 1) SAUF ELEMENT SCENARIO
	-->
    <xsl:template name="computeChildElement">
        <xsl:param name="level"/>

		<!-- Parcours des éléments du niveau donné -->
        <xsl:for-each select="//*[(local-name()='element') and (@name!='Scenario')][count(ancestor::*[local-name()='element']) = $level]">
            <xsl:variable name="element" select="."/>
			
			<!-- Teste si l'élément peut faire l'objet d'un tableau -->
            <xsl:variable name="isValid">
                <xsl:call-template name="hasScndLevelElements">
                    <xsl:with-param name="element" select="$element"/>
                    <xsl:with-param name="level" select="$level + 2"/><!-- +2 -->
                </xsl:call-template>
            </xsl:variable>
			
			<!-- Si l'élément peut faire l'objet d'un tableau -->
            <xsl:if test="string($isValid)=string(true())">
			
				<!-- Teste si l'élément n'a pas déjà fait l'objet d'un tableau -->
                <xsl:variable name="isAlreadyComputed">
                    <xsl:call-template name="hasBeenAlreadyComputed">
                        <xsl:with-param name="name" select="$element/@name"/>
                        <xsl:with-param name="type" select="$element/@type"/>
                        <xsl:with-param name="level" select="$level"/>
                    </xsl:call-template>
                </xsl:variable>
				
				<!-- Si l'élément n'a pas deja été traité -->
                <xsl:if test="string($isAlreadyComputed)=string(false())">
					<!--child-->
                    <xsl:call-template name="displayLog">
                        <xsl:with-param name="logContent">Traitement de la balise métier <xsl:value-of select="$element/@name"/>
                        </xsl:with-param>
                    </xsl:call-template>
                    <xsl:call-template name="displayChildElementChapter">
                        <xsl:with-param name="elementName" select="$element/@name"/>
                    </xsl:call-template>
					
					<!-- Traitement de l'élément -->
                    <xsl:call-template name="computeElement">
                        <xsl:with-param name="element" select="$element"/>
                        <xsl:with-param name="first" select="true()"/>
                        <xsl:with-param name="firstLevelChild" select="$level + 1"/>
                        <xsl:with-param name="lastLevelChild" select="$level + 3"/>
                    </xsl:call-template>
					
					<!-- FIXME Images - SUPPRIME EN V2 -->
					<!-- Traitement de l'image associée -->					
					<!-- <xsl:call-template name="computeImage">
						<xsl:with-param name="name" select="$element/@name"/>
					</xsl:call-template> -->
					<!--/child-->
                </xsl:if>
            </xsl:if>
        </xsl:for-each>
		
		<!--Test d'existence des éléments d'un niveau supérieur et appel récursif le cas échéant -->
        <xsl:variable name="newLevelExist">
            <xsl:call-template name="newLevelExist">
                <xsl:with-param name="level" select="$level + 1"/>
            </xsl:call-template>
        </xsl:variable>
        <xsl:if test="string($newLevelExist)=string(true())">
            <xsl:call-template name="computeChildElement">
                <xsl:with-param name="level" select="$level + 1"/>
            </xsl:call-template>
        </xsl:if>
    </xsl:template>

	<!-- 
		TRAITEMENT DES FILS DE LA BALISE RACINE > SEUL L'ELEMENT SCENARIO est traité à ce niveau
	-->
    <xsl:template name="computeSubRootElement">
        <xsl:param name="element"/>	
		<!--subroot-->
        <xsl:call-template name="displayLog">
            <xsl:with-param name="logContent">Traitement de la balise scenario <xsl:value-of select="$element/@name"/>
            </xsl:with-param>
        </xsl:call-template>
        <xsl:call-template name="computeElement">
            <xsl:with-param name="element" select="$element"/>
            <xsl:with-param name="first" select="true()"/>
            <xsl:with-param name="firstLevelChild">2</xsl:with-param>
            <xsl:with-param name="lastLevelChild">4</xsl:with-param>
        </xsl:call-template>
		<!-- FIXME Images - SUPPRIME EN V2 -->
		<!-- <xsl:call-template name="computeImage">
			<xsl:with-param name="name" select="$element/@name"/>
		</xsl:call-template> -->
		<!--/subroot-->
    </xsl:template>

	<!-- 
		TRAITEMENT DE LA BALISE RACINE : PREMIERE LIGNE
	-->
    <xsl:template name="computeRootElementLine">
        <xsl:param name="element"/>
        <xsl:call-template name="displayLog">
            <xsl:with-param name="logContent">Traitement d'affichage de la première ligne de la balise racine <xsl:value-of select="$element/@name"/>
            </xsl:with-param>
        </xsl:call-template>
        <xsl:variable name="targetNamespace" select="/*[local-name()='schema']/@targetNamespace"/>
<!-- DEBUG 
				<xsl:message>computeRootElementLine sourceURL <xsl:value-of select="$sourceURL" /></xsl:message>
 -->
        <xsl:variable name="xsiSchemaLocation">
            <xsl:call-template name="getFileName">
                <xsl:with-param name="path" select="$sourceURL"/>
            </xsl:call-template>
        </xsl:variable>
        <text:p>
            <text:span text:style-name="T23">&lt;<xsl:value-of select="$element/@name"/>
            </text:span>
            <text:span text:style-name="T23"> xmlns=“<xsl:value-of select="$targetNamespace"/>“ </text:span>
            <text:span text:style-name="T23"> xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance“</text:span>
            <text:span text:style-name="T23"> xsi:schemalocation=“<xsl:value-of select="$targetNamespace"/>
                <xsl:value-of select="concat(' ',$targetNamespace, '/', $xsiSchemaLocation)"/>“&gt;</text:span>
        </text:p>
    </xsl:template>

	<!-- 
		TRAITEMENT DE LA BALISE RACINE
	-->
    <xsl:template name="computeRootElement">
        <xsl:param name="element"/>

		<!--root-->
        <xsl:call-template name="displayLog">
            <xsl:with-param name="logContent">Traitement de la balise racine <xsl:value-of select="$element/@name"/>
            </xsl:with-param>
        </xsl:call-template>
        <xsl:call-template name="computeElement">
            <xsl:with-param name="element" select="$element"/>
            <xsl:with-param name="first" select="true()"/>
            <xsl:with-param name="isRoot" select="true()"/>
            <xsl:with-param name="firstLevelChild">1</xsl:with-param>
            <xsl:with-param name="lastLevelChild">3</xsl:with-param>
        </xsl:call-template>
	
			<!-- FIXME Images - SUPPRIME EN V2 -->
			<!-- <xsl:call-template name="computeImage">
				<xsl:with-param name="name" select="$element/@name"/>
			</xsl:call-template> -->
    </xsl:template>


	<!-- 
		TRAITEMENT D'UN ELEMENT
	-->
    <xsl:template name="computeElement">
        <xsl:param name="element"/>
        <xsl:param name="first"/>
        <xsl:param name="isRoot"/>
        <xsl:param name="firstLevelChild"/>
        <xsl:param name="lastLevelChild"/>
        <xsl:param name="isSimple"/>
        <xsl:param name="isOneLevel"/>
        <xsl:param name="isTwoLevel"/>
        <xsl:param name="mode"/>		
		
		<!-- Détermination du type de l'élément -->
        <xsl:variable name="type">
            <xsl:variable name="typeNs" select="substring-before($element/@type, ':')"/>
            <xsl:choose>
                <xsl:when test="$typeNs">
                    <xsl:value-of select="$typeNs"/>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:variable name="tmpTypeNs" select="substring-before($element/child::*[local-name()='complexType']/child::*[local-name()='simpleContent']/child::*[local-name() = 'restriction' or local-name() = 'extension']/@base, ':')"/>
                    <xsl:choose>
                        <xsl:when test="$tmpTypeNs">
                            <xsl:value-of select="$tmpTypeNs"/>
                        </xsl:when>
                        <xsl:otherwise>
                            <xsl:value-of select="$defaultType"/>
                        </xsl:otherwise>
                    </xsl:choose>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:variable>

		<!-- Récupération de la définition de l'élément dans le dictionnaire des données -->
        <xsl:variable name="ns" select="//*[local-name()='schema']/namespace::*[local-name()=$type]"/>
        <xsl:variable name="relativeFileName" select="string(//*[local-name()='import'][@namespace=$ns]/@schemaLocation)"/>
        <xsl:variable name="fileName">
            <xsl:call-template name="getFileName">
                <xsl:with-param name="path" select="//*[local-name()='import'][@namespace=$ns]/@schemaLocation"/>
            </xsl:call-template>
        </xsl:variable>	

<!-- DEBUG
<xsl:message>computeElement type <xsl:value-of select="$type" /> ns <xsl:value-of select="$ns" /> filename <xsl:value-of select="$fileName" /></xsl:message>		
 -->
        <xsl:variable name="elementDef" select="document(concat($schemaFilePath, $relativeFileName))//*[local-name()='complexType'][@name=$element/@name]"/>		

<!-- <xsl:choose>
<xsl:when test="$fileName">

		<xsl:value-of select="document(concat($ns, '/', $fileName))//*[local-name()='complexType'][@name=$element/@name]"/>		

 		<xsl:value-of select="document(concat($schemaFilePath, $relativeFileName))//*[local-name()='complexType'][@name=$element/@name]"/>		
 
</xsl:when>
<xsl:otherwise>
<xsl:message>elementDef undefined</xsl:message>
<xsl:value-of select="''"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
 -->
        <xsl:if test="$type = $defaultType">
            <xsl:call-template name="displayLogWarning">
                <xsl:with-param name="logContent">    Cet élément n'a pas de type défini</xsl:with-param>
            </xsl:call-template>
        </xsl:if>		

		<!-- Traitement de la colonne "Nom de l'élément" -->
		<!-- Traitement des attributs non obligatoires -->
        <xsl:variable name="othersAtt">
            <xsl:choose>
                <xsl:when test="string($isRoot) = string(true())">
<!--
 					<xsl:variable name="xsNamespace" select="/*[local-name()='schema']/namespace::*[contains(., 'XMLSchema')]"/>
					<xsl:variable name="targetNamespace" select="/*[local-name()='schema']/@targetNamespace"/>
					<xsl:variable name="xsNamespaceName" select="local-name($xsNamespace)"/>				
					<xsl:value-of select="concat(' xmlns=', $targetNamespace,  ' xmlns:', $xsNamespaceName,  '=',  $xsNamespace)"/>							
 -->
                </xsl:when>
                <xsl:otherwise>
                    <xsl:choose>
                        <xsl:when test="$type != $defaultType">
                            <xsl:for-each select="$elementDef/descendant::*[local-name()='attribute'][@fixed and @use!='required']">
                                <xsl:value-of select="concat(' ', @name, '=&#34;', @fixed, '&#34;')"/>
                            </xsl:for-each>
                            <xsl:for-each select="$elementDef/descendant::*[local-name()='attribute'][@use!='required' and descendant::*[local-name()='enumeration']]">
                                <xsl:value-of select="concat(' ', @name, '=&#34;[')"/>
                                <xsl:for-each select="descendant::*[local-name()='enumeration']">
                                    <xsl:choose>
                                        <xsl:when test="position() != last()">
                                            <xsl:value-of select="concat(@value, ';')"/>
                                        </xsl:when>
                                        <xsl:otherwise>
                                            <xsl:value-of select="@value"/>
                                        </xsl:otherwise>
                                    </xsl:choose>
                                </xsl:for-each>
                                <xsl:value-of select="']&#34;'"/>
                            </xsl:for-each>
                        </xsl:when>
                    </xsl:choose>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:variable>
		

		<!-- Traitement des attributs  obligatoires -->
        <xsl:variable name="mndAtt">
            <xsl:choose>
                <xsl:when test="$type != $defaultType">
                    <xsl:for-each select="$elementDef/descendant::*[local-name()='attribute'][@fixed and @use='required']">
                        <xsl:value-of select="concat(' ', @name, '=&#34;', @fixed, '&#34;')"/>
                    </xsl:for-each>
                    <xsl:for-each select="$elementDef/descendant::*[local-name()='attribute'][@use='required' and descendant::*[local-name()='enumeration']]">
                        <xsl:value-of select="concat(' ', @name, '=&#34;[')"/>
                        <xsl:for-each select="descendant::*[local-name()='enumeration']">
                            <xsl:choose>
                                <xsl:when test="position() != last()">
                                    <xsl:value-of select="concat(@value, ';')"/>
                                </xsl:when>
                                <xsl:otherwise>
                                    <xsl:value-of select="@value"/>
                                </xsl:otherwise>
                            </xsl:choose>
                        </xsl:for-each>
                        <xsl:value-of select="']&#34;'"/>
                    </xsl:for-each>
                </xsl:when>
            </xsl:choose>
        </xsl:variable>
        <xsl:variable name="minOccurs" select="$element/@minOccurs"/>
        <xsl:variable name="maxOccurs" select="$element/@maxOccurs"/>

		<!-- Traitement de la colonne "Caractère obligatoire de l'élément" -->
        <xsl:variable name="mandatoryCaracteristic">
            <xsl:choose>
                <xsl:when test="string($minOccurs)= '' or number($minOccurs)=1">
                    <xsl:value-of select="$elementMandatoryCaracteristic"/>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:value-of select="$elementNotMandatoryCaracteristic"/>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:variable>		

		<!-- Traitement de la colonne "Nombre d'occurrence de l'élément" -->
        <xsl:variable name="minOccurences">
            <xsl:choose>
                <xsl:when test="string($minOccurs) = '' ">
                    <xsl:value-of select="'1'"/>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:value-of select="$minOccurs"/>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:variable>
        <xsl:variable name="maxOccurences">
            <xsl:choose>
                <xsl:when test="string($maxOccurs) = '' ">
                    <xsl:value-of select="'1'"/>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:value-of select="$maxOccurs"/>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:variable>

		<!-- Traitement de la colonne "Format" -->
        <xsl:variable name="format">
            <xsl:choose>
                <xsl:when test="$type != $defaultType">
                    <xsl:variable name="tmpFmt" select="substring-after($elementDef/child::*[local-name()='simpleContent' or local-name()='complexContent']/child::*[local-name()='restriction' or local-name()='extension']/@base, ':' )"/>
                    <xsl:choose>
                        <xsl:when test="string($tmpFmt) = '' ">
                            <xsl:value-of select="$defaultFormat"/>
                        </xsl:when>
                        <xsl:otherwise>
                            <xsl:value-of select="$tmpFmt"/>
                        </xsl:otherwise>
                    </xsl:choose>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:value-of select="$defaultFormat"/>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:variable>

		<!-- Traitement de la colonne "Longueur maximale" -->
        <xsl:variable name="maxLength">
            <xsl:choose>
                <xsl:when test="$type != $defaultType">
                    <xsl:variable name="tmpMxLngth" select="$elementDef/child::*[local-name()='simpleContent']/child::*[local-name()='restriction']/child::*[local-name()='maxLength']/@value"/>
                    <xsl:choose>
                        <xsl:when test="string($tmpMxLngth) = '' ">
                            <xsl:value-of select="$defaultMaxLength"/>
                        </xsl:when>
                        <xsl:otherwise>
                            <xsl:value-of select="$tmpMxLngth"/>
                        </xsl:otherwise>
                    </xsl:choose>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:value-of select="$defaultMaxLength"/>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:variable>

		<!-- Test si le code est de type 'CodeType' -->
        <xsl:variable name="code">
            <xsl:if test="$format = 'CodeType' ">
                <xsl:value-of select="$elementDef/child::*[local-name()='simpleContent' or local-name()='complexContent']/child::*[local-name()='restriction']/child::*[local-name()='attribute'][@name='listID']/@fixed"/>
            </xsl:if>
        </xsl:variable>

		<!-- Traitement de la colonne "Commentaires / Valeur" -->
        <xsl:variable name="typeComment">
            <xsl:if test="$type != $defaultType ">
                <xsl:variable name="tmpComment" select="$elementDef/child::*[local-name()='annotation']/child::*[local-name()='documentation'][@source='Libelle']"/>
                <xsl:choose>
                    <xsl:when test="contains($tmpComment, $nomenclatureStartStr) and contains($tmpComment, $nomenclatureEndStr)">
                        <xsl:value-of select="concat( substring-before ($tmpComment, $nomenclatureStartStr) , substring-after ($tmpComment, $nomenclatureEndStr))"/>
                    </xsl:when>
                    <xsl:otherwise>
                        <xsl:value-of select="$tmpComment"/>
                    </xsl:otherwise>
                </xsl:choose>
            </xsl:if>
        </xsl:variable>
        <xsl:choose>
            <xsl:when test="string($first)=string(true())">
                <table:table table:style-name="{$elementTableStyleName}">
                    <xsl:call-template name="displayElementTabHeader"/>
                    <xsl:call-template name="displayElementValues">
                        <xsl:with-param name="name" select="$element/@name"/>
                        <xsl:with-param name="mndAtt" select="$mndAtt"/>
                        <xsl:with-param name="othersAtt" select="$othersAtt"/>
                        <xsl:with-param name="type" select="$type"/>
                        <xsl:with-param name="mandatoryCaracteristic" select="$mandatoryCaracteristic"/>
                        <xsl:with-param name="minOccurences" select="$minOccurences"/>
                        <xsl:with-param name="maxOccurences" select="$maxOccurences"/>
                        <xsl:with-param name="format" select="$format"/>
                        <xsl:with-param name="value" select="$maxLength"/>
                        <xsl:with-param name="typeComment" select="$typeComment"/>
                        <xsl:with-param name="elmntComment" select="$element/child::*[local-name()='annotation']/child::*[local-name()='documentation'][@source='Libelle']"/>
                        <xsl:with-param name="code" select="$code"/>
                        <xsl:with-param name="maxLength" select="$maxLength"/>
                        <xsl:with-param name="first" select="true()"/>
                    </xsl:call-template>

					<!-- Traitement des sous-éléments -->
                    <xsl:call-template name="computeSubElements">
                        <xsl:with-param name="parentElement" select="$element"/>
                        <xsl:with-param name="firstLevelChild" select="$firstLevelChild"/>
                        <xsl:with-param name="lastLevelChild" select="$lastLevelChild"/>
                    </xsl:call-template>
                </table:table>
				
				<!-- 
					V2 - EXEMPLE CONTENANT XML 
				-->
                <xsl:call-template name="displayXmlExample">
                    <xsl:with-param name="elementToDisplay" select="$element"/>
                    <xsl:with-param name="firstLevelChild" select="$firstLevelChild"/>
                    <xsl:with-param name="lastLevelChild" select="$lastLevelChild"/>
                    <xsl:with-param name="first" select="$first"/>
                    <xsl:with-param name="isSimple" select="$isSimple"/>
                    <xsl:with-param name="isOneLevel" select="$isOneLevel"/>
                    <xsl:with-param name="isTwoLevel" select="$isTwoLevel"/>
                </xsl:call-template>
            </xsl:when>
            <xsl:otherwise>
                <xsl:call-template name="displayElementValues">
                    <xsl:with-param name="name" select="$element/@name"/>
                    <xsl:with-param name="mndAtt" select="$mndAtt"/>
                    <xsl:with-param name="othersAtt" select="$othersAtt"/>
                    <xsl:with-param name="type" select="$type"/>
                    <xsl:with-param name="mandatoryCaracteristic" select="$mandatoryCaracteristic"/>
                    <xsl:with-param name="minOccurences" select="$minOccurences"/>
                    <xsl:with-param name="maxOccurences" select="$maxOccurences"/>
                    <xsl:with-param name="format" select="$format"/>
                    <xsl:with-param name="value" select="$maxLength"/>
                    <xsl:with-param name="typeComment" select="$typeComment"/>
                    <xsl:with-param name="elmntComment" select="$element/child::*[local-name()='annotation']/child::*[local-name()='documentation'][@source='Libelle']"/>
                    <xsl:with-param name="code" select="$code"/>
                    <xsl:with-param name="maxLength" select="$maxLength"/>
                    <xsl:with-param name="isSimple" select="$isSimple"/>
                    <xsl:with-param name="isOneLevel" select="$isOneLevel"/>
                    <xsl:with-param name="isTwoLevel" select="$isTwoLevel"/>
                    <xsl:with-param name="first" select="false()"/>
                </xsl:call-template>
			
				<!-- Traitement des sous-éléments -->
                <xsl:if test="string($isOneLevel)=string(true())">
                    <xsl:call-template name="computeSubElements">
                        <xsl:with-param name="parentElement" select="$element"/>
                        <xsl:with-param name="firstLevelChild" select="$firstLevelChild"/>
                        <xsl:with-param name="lastLevelChild" select="$lastLevelChild"/>
                    </xsl:call-template>	
					
				<!-- 
					EXEMPLE CONTENANT XML 
				-->
                    <xsl:call-template name="displayXmlExample">
                        <xsl:with-param name="elementToDisplay" select="$element"/>
                        <xsl:with-param name="firstLevelChild" select="$firstLevelChild"/>
                        <xsl:with-param name="lastLevelChild" select="$lastLevelChild"/>
                        <xsl:with-param name="first" select="$first"/>
                        <xsl:with-param name="isSimple" select="$isSimple"/>
                        <xsl:with-param name="isOneLevel" select="$isOneLevel"/>
                        <xsl:with-param name="isTwoLevel" select="$isTwoLevel"/>
                    </xsl:call-template>
                </xsl:if>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
	
	
	
	<!-- 
		TRAITEMENT DES ELEMENTS FILS D'UN
		ELEMENT FAISANT L'OBJET D'UN TABLEAU
	-->
    <xsl:template name="computeSubElements">
        <xsl:param name="parentElement"/>
        <xsl:param name="firstLevelChild"/>
        <xsl:param name="lastLevelChild"/>
	
		<!-- Parcours des éléments fils -->
        <xsl:for-each select="$parentElement/descendant::*[local-name()='element'][count(ancestor::*[local-name()='element']) = $firstLevelChild]">
            <xsl:variable name="childElement" select="."/>
            <xsl:call-template name="displayLog">
                <xsl:with-param name="logContent">    Traitement du sous-élément <xsl:value-of select="$childElement/@name"/>
                </xsl:with-param>
            </xsl:call-template>

			<!-- Teste si l'élément est le premier d'un choice -->
            <xsl:if test="$childElement/parent::*[local-name()='choice'] and position()=1">
                <xsl:call-template name="displayStartChoice"/>
            </xsl:if>
            <xsl:choose>
                <xsl:when test="not($childElement/descendant::*[local-name()='element'])">
                    <xsl:call-template name="computeSimpleElement">
                        <xsl:with-param name="element" select="$childElement"/>
                    </xsl:call-template>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:choose>
                        <xsl:when test="not($childElement/descendant::*[local-name()='element'][count(ancestor::*[local-name()='element']) = $lastLevelChild])">
                            <xsl:call-template name="computeOneLevelElement">
                                <xsl:with-param name="element" select="$childElement"/>
                                <xsl:with-param name="level" select="$firstLevelChild"/>
                            </xsl:call-template>
                        </xsl:when>
                        <xsl:otherwise>
                            <xsl:call-template name="computeTwoLevelElement">
                                <xsl:with-param name="element" select="$childElement"/>
                            </xsl:call-template>
                        </xsl:otherwise>
                    </xsl:choose>
                </xsl:otherwise>
            </xsl:choose>
			
			<!-- Teste si l'élément est le dernier d'un choice -->
            <xsl:if test="$childElement/parent::*[local-name()='choice'] and position()=last()">
                <xsl:call-template name="displayEndChoice"/>
            </xsl:if>
        </xsl:for-each>
    </xsl:template>

	<!-- 
		????
	-->
    <xsl:template name="computeTwoLevelElement">
        <xsl:param name="element"/>

		<!--twoLevelElement-->
        <xsl:call-template name="computeElement">
            <xsl:with-param name="element" select="$element"/>
            <xsl:with-param name="isTwoLevel" select="true()"/>
        </xsl:call-template>			
		<!--/twoLevelElement-->
    </xsl:template>

	<!-- 
		????
	-->
    <xsl:template name="computeOneLevelElement">
        <xsl:param name="element"/>
        <xsl:param name="level"/>		

		<!--oneLevelElement-->
        <xsl:call-template name="computeElement">
            <xsl:with-param name="element" select="$element"/>
            <xsl:with-param name="isOneLevel" select="true()"/>
            <xsl:with-param name="firstLevelChild" select="$level + 1"/>
            <xsl:with-param name="lastLevelChild" select="$level + 3"/>
        </xsl:call-template>
			
		<!--/oneLevelElement-->
    </xsl:template>

	<!-- 
		????
	-->
    <xsl:template name="computeSimpleElement">
        <xsl:param name="element"/>

		<!--simpleElement-->
        <xsl:call-template name="computeElement">
            <xsl:with-param name="element" select="$element"/>
            <xsl:with-param name="isSimple" select="true()"/>
        </xsl:call-template>
		<!--/simpleElement-->
    </xsl:template>


	<!-- FIXME IMAGES - SUPPRIME EN V2 -->
	<!-- 
		TRAITEMENT D'UNE IMAGE
	-->
	<!--
	 <xsl:template name="computeImage">
		<xsl:param name="name"/>
		
		<xsl:call-template name="displayImage">
			<xsl:with-param name="imageFileName" select="concat('Image_', $name, '.gif')"/>
			<xsl:with-param name="imageName" select="concat('Image_', $name, '.gif')"/>
			<xsl:with-param name="prefixURL" select="$imagePrefixURL"/>			
		</xsl:call-template>
	</xsl:template> 
	-->

	<!-- 
		TRAITEMENT DU FORMAT DE L'ELEMENT
	
	<xsl:template name="computeElementFormat">
		<xsl:param name="format"/>
		<xsl:param name="cellStyleName"/>
		<xsl:param name="textStyleName"/>				
		<xsl:param name="textSpanStyleName"/>				

		<xsl:variable name="formatElmt">
			<xsl:choose>
				<xsl:when test=" $format = 'TextType' ">
					<xsl:value-of select="$texteFormat"/>
				</xsl:when>			
				<xsl:when test="$format='NumericType'">			
					<xsl:value-of select="$numericFormat"/>					
				</xsl:when>			
				<xsl:when test="$format='DateType'">
					<xsl:value-of select="$dateFormat"/>
				</xsl:when>		
				<xsl:when test="$format='TimeType'">
					<xsl:value-of select="$timeFormat"/>					
				</xsl:when>			
				<xsl:when test="$format='DateTimeType'">
					<xsl:value-of select="$dateTimeFormat"/>					
				</xsl:when>			
				<xsl:when test="$format='IndicatorType'">
					<xsl:value-of select="$indicatorFormat"/>					
				</xsl:when>		
				<xsl:when test="$format='BinaryObjectType'">
					<xsl:value-of select="$binaryFormat"/>					
				</xsl:when>		
				<xsl:when test="$format='IdentifierType'">
					<xsl:value-of select="$identifierFormat"/>					
				</xsl:when>
				<xsl:when test="$format='CodeType'">
					<xsl:value-of select="$codeFormat"/>					
				</xsl:when>
				<xsl:otherwise>
					<xsl:value-of select="$format"/>
				</xsl:otherwise>
			</xsl:choose>
		</xsl:variable>			
		<xsl:call-template name="displayFormat">
			<xsl:with-param name="format" select="$formatElmt"/>					
			<xsl:with-param name="cellStyleName" select="$cellStyleName"/>
			<xsl:with-param name="textStyleName" select="$textStyleName" />				
			<xsl:with-param name="textSpanStyleName" select="$textStyleName" />							
		</xsl:call-template>
	</xsl:template>
-->
</xsl:stylesheet>