Newer
Older
web-services / loterre-resolvers / v1 / annotate.xsl
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:exslt="http://exslt.org/common"
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
    xmlns:skos="http://www.w3.org/2004/02/skos/core#"
    xmlns:dc="http://purl.org/dc/elements/1.1/"
    xmlns:dcterms="http://purl.org/dc/terms/" 
    xmlns:dct="http://purl.org/dc/terms/" 
    xmlns:file="http://expath.org/ns/file"
    xmlns:xd="http://www.oxygenxml.com/ns/doc/xsl"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
    xmlns:isothes="http://purl.org/iso25964/skos-thes#"
    xmlns:cc="http://creativecommons.org/ns#"
    xmlns:tei="http://www.tei-c.org/ns/1.0"
    xmlns:fn="http://www.w3.org/2005/xpath-functions"
    version="3.0">
    <xsl:output  method="xml" version="1.0" encoding="UTF-8" indent="yes" omit-xml-declaration="yes"/>
      
    <xsl:param name="inputFile" select="unparsed-text('input.json')"/>
    <xsl:param name="lang" select="'en'"/>
    
    <!-- chemin du répertoire de travail (dans lequel est cette feuille de transformation) -->
    <xsl:variable name="folderURI" select="resolve-uri('.', base-uri(document('')))"/>
    <!-- nom de la feuile de style -->
    <xsl:variable name="fds" select="substring-before(tokenize(base-uri(document('')), '/')[last()], '.')" />
    <!--  table de correspondance "Nom du fichier json"/"Nom du corpus"-->
 
    <!-- Contenu des Dicos -->
    <!--<xsl:variable name="DICO">
        <xsl:copy-of select="collection(concat($folderURI,'?select=*.skos'))/*"/>
    </xsl:variable>-->
    <xsl:param name="vocabulary" select="'/home/thouvenin/devel/web-services/loterre-resolvers/v1/QX8.skos'"/>
    <xsl:variable name="DICO" select="document(unparsed-text($vocabulary))/rdf:RDF"/>
     
    <!--<xsl:variable name="DICO_var">
        <xsl:copy-of select="collection(concat($folderURI,'?select=*var.skos'))/*"/>
    </xsl:variable>-->
    
    <!--table de correspondance pour le renommage des balises-->
    <xsl:variable name="KEYS">
        <key>
            <old>articleId</old>
            <new>Identifiant article</new>
        </key>
        
        <key>
            <old>paragraphCount</old>
            <new>Nombre paragraphes</new>
        </key>
  
        <key>
            <old>sectionLevels</old>
            <new>Niveaux section</new>
        </key>
        <key>
            <old>level</old>
            <new>Niveau</new>
        </key>
        <key>
            <old>sectionCount</old>
            <new>Nombre sections</new>
        </key>
        <key>
            <old>sectionDepth</old>
            <new>Profondeur section</new>
        </key>
        <key>
            <old>imageCount</old>
            <new>Nombre images</new>
        </key>
        <key>
            <old>tableCount</old>
            <new>Nombre tableaux</new>
        </key>
        <key>
            <old>refCount</old>
            <new>Nombre références</new>
        </key>
        <key>
            <old>tdmReady</old>
            <new>Prêt TDM</new>
        </key>
        <key>
            <old>score</old>
            <new>Score qualité</new>
        </key>
        <key>
            <old>pdfWordCount</old>
            <new>Nombre mots PDF</new>
        </key>
        <key>
            <old>pdfCharCount</old>
            <new>Nombre caractères PDF</new>
        </key>
        <key>
            <old>pdfVersion</old>
            <new>Version PDF</new>
        </key>
        <key>
            <old>pdfPageCount</old>
            <new>Nombre pages PDF</new>
        </key>
        <key>
            <old>pdfPageSize</old>
            <new>Taille page PDF</new>
        </key>
        <key>
            <old>pdfWordsPerPage</old>
            <new>Nombre mots par page PDF</new>
        </key>
        <key>
            <old>pdfText</old>
            <new>PDF texte</new>
        </key>
        <key>
            <old>refBibsNative</old>
            <new>Références bibliographiques origine</new>
        </key>
        <key>
            <old>abstractWordCount</old>
            <new>Nombre mots résumé</new>
        </key>
        <key>
            <old>abstractCharCount</old>
            <new>Nombre caractères résumé</new>
        </key>
        <key>
            <old>keywordCount</old>
            <new>Nombre mots-clés</new>
        </key>
        <key>
            <old>hasFormula</old>
            <new>Formule</new>
        </key>
        <key>
            <old>refBibs</old>
            <new>Références bibliographiques</new>
        </key>
        <key>
            <old>title</old>
            <new>Titre</new>
        </key>
        <key>
            <old>author</old>
            <new>Auteur(s)</new>
        </key>
        <key>
            <old>name</old>
            <new>Nom</new>
        </key>
        <key>
            <old>host</old>
            <new>Source</new>
        </key>
        <key>
            <old>pages</old>
            <new>Pages</new>
        </key>
        <key>
            <old>first</old>
            <new>Première</new>
        </key>
        <key>
            <old>last</old>
            <new>Dernière</new>
        </key>
        <key>
            <old>publicationDate</old>
            <new>Date publication</new>
        </key>
        <key>
            <old>volume</old>
            <new>Volume</new>
        </key>
        <key>
            <old>editor</old>
            <new>Éditeur</new>
        </key>
        
        <key>
            <old>publisher</old>
            <new>Publisher</new>
        </key>
        
        <key>
            <old>issn</old>
            <new>ISSN</new>
        </key>
        <key>
            <old>eissn</old>
            <new>e-ISSN</new>
        </key>
        <key>
            <old>journalId</old>
            <new>Identifiant journal</new>
        </key>

        <key>
            <old>issue</old>
            <new>Numéro</new>
        </key>
        
        <key>
            <old>genre</old>
            <new>Genre</new>
        </key>
        <key>
            <old>subject</old>
            <new>Sujet</new>
        </key>
        
        <key>
            <old>namedEntities</old>
            <new>Entités nommées</new>
        </key>
        <key>
            <old>unitex</old>
            <new>Unitex</new>
        </key>
        <key>
            <old>date</old>
            <new>Date</new>
        </key>
        <key>
      <old>geogName</old>
      <new>Nom géographique</new>
   </key>
        <key>
            <old>orgName</old>
            <new>Nom organisme</new>
        </key>
        <key>
            <old>persName</old>
            <new>Nom personne</new>
        </key>
        <key>
            <old>placeName</old>
            <new>Nom lieu</new>
        </key>
        <key>
            <old>copyrightDate</old>
            <new>Date copyright</new>
        </key>
        <key>
            <old>enrichments</old>
            <new>Enrichissements</new>
        </key>
        <key>
            <old>type</old>
            <new>Type</new>
        </key>
        
        
        <key>
            <old>frequency</old>
            <new>Fréquence</new>
        </key>
        
        <key>
            <old>keywords</old>
            <new>Mots-clés</new>
        </key>
        <key>
            <old>arkIstex</old>
            <new>ARK</new>
        </key>
        <!--<key>
      <old>originalGenre</old>
      <new>Type de document</new>
   </key>-->
        <key>
            <old>qualityIndicators</old>
            <new>Indicateurs qualité</new>
        </key>
        <!--<key>
      <old>hasStructuredBody</old>
      <new>Corps structuré</new>
   </key>-->
        <key>
            <old>teiSource</old>
            <new>Source TEI</new>
        </key>
        <key>
            <old>xmlStats</old>
            <new>Statistiques XML</new>
        </key>
    </xsl:variable>
    
   
<!--===============================================================
0- traitement du thésaurus : création d'un index avec les prefs,  les synos et les hiddens triés  
========================================================================-->
           <xsl:variable name="FRE">
                <xsl:for-each select="$DICO//skos:Concept/skos:prefLabel[@xml:lang='fr']">
                       <!--liste d'exclusion-->
                    <xsl:if test="not(.=('SOI', 'SOC', 'climat', 'K', 'B','N,','S','Si','D','Cl','H'))">
                            <terme>
                               <xsl:attribute name="id">
                                   <xsl:value-of select="../@rdf:about"/>
                               </xsl:attribute>
                               <xsl:attribute name="lang">fr</xsl:attribute>
                               <xsl:attribute name="status">pref</xsl:attribute>
                               <xsl:value-of select="normalize-space(.)"/> 
                           </terme>
                        </xsl:if>
               </xsl:for-each>
               <xsl:for-each select="$DICO//skos:Concept/skos:altLabel[@xml:lang='fr' and not(contains(.,'?')) and not(.='')] | $DICO//skos:Concept/skos:hiddenLabel[@xml:lang='fr'  and not(contains(.,'?')) and not(.='')]">  <!--liste d'exclusion-->
                   <!--liste d'exclusion-->
                   <xsl:if test="not(.=('SOI', 'SOC', 'climat', 'K', 'B','N,','S','Si','D','Cl','H'))">
                       <terme>
                       <xsl:attribute name="id">
                           <xsl:value-of select="../@rdf:about"/>
                       </xsl:attribute>
                       <xsl:attribute name="lang">fr</xsl:attribute>
                       <xsl:attribute name="status">syn</xsl:attribute>
                       <xsl:attribute name="renvoi"><xsl:value-of select="../skos:prefLabel[@xml:lang='fr']"/></xsl:attribute>
                       <xsl:value-of select="normalize-space(.)"/> 
                   </terme>
                   </xsl:if>
               </xsl:for-each>
           </xsl:variable>
           <xsl:variable name="ENG">
           <xsl:for-each select="$DICO//skos:Concept/skos:prefLabel[@xml:lang='en']">
                       <!--liste d'exclusion-->
                   <xsl:if test="not(.=('SOI', 'SOC', 'climate', 'K', 'B','N,','S','Si','D','Cl','H'))">
                           <terme>
                              <xsl:attribute name="id">
                                  <xsl:value-of select="../@rdf:about"/>
                              </xsl:attribute>
                              <xsl:attribute name="lang">en</xsl:attribute>
                              <xsl:attribute name="status">pref</xsl:attribute>
                              <xsl:value-of select="normalize-space(.)"/> 
                          </terme>
                       </xsl:if>
               </xsl:for-each>
 
                   <xsl:for-each select="$DICO//skos:Concept/skos:altLabel[@xml:lang='en' and not(.='')] | $DICO//skos:Concept/skos:hiddenLabel[@xml:lang='en' and not(.='')]">    <!-- liste d'exclusion -->
                   <!--liste d'exclusion-->
                   <xsl:if test="not(.=('SOI', 'SOC', 'climate', 'K', 'B','N,','S','Si','D','Cl','H'))">
                       <terme>
                       <xsl:attribute name="id">
                           <xsl:value-of select="../@rdf:about"/>
                       </xsl:attribute>
                       <xsl:attribute name="lang">en</xsl:attribute>
                       <xsl:attribute name="status">syn</xsl:attribute>
                       <xsl:attribute name="renvoi"><xsl:value-of select="../skos:prefLabel[@xml:lang='en']"/></xsl:attribute>
                       <xsl:value-of select="normalize-space(.)"/> 
                   </terme>
                   </xsl:if>
        
               </xsl:for-each>
               
               
           </xsl:variable>
           <xsl:variable name="dicoName">
               <xsl:for-each select="$DICO//skos:ConceptScheme">
                   <dico>
                       <xsl:attribute name="uri" select="@rdf:about"/>
                      <xsl:choose>
                         <xsl:when test="dct:title[@xml:lang='fr']">
                            <xsl:value-of select="dct:title[@xml:lang='fr']"/>
                         </xsl:when>
                         <xsl:otherwise>
                            <xsl:value-of select="dct:title[@xml:lang='en']"/>
                         </xsl:otherwise>
                      </xsl:choose>
                    </dico>
               </xsl:for-each>
           </xsl:variable>
     
    <!-- =================================================================================================
    1- transformation du fichier d'entrée json en XML
    =====================================================================================================-->
	<xsl:template match="/" name="main">
		<xsl:variable name="PASS1">
			<pass1>
				<xsl:copy>
					<xsl:apply-templates select="@*|node()" mode="input"/>
				</xsl:copy>
			</pass1>
		</xsl:variable>
		<xsl:apply-templates select="exslt:node-set($PASS1)/pass1"/>
	</xsl:template>
	<xsl:template match="@*|node()" mode="input">
		<xsl:copy>
			<xsl:apply-templates select="@*|node()" mode="input"/>
		</xsl:copy>
    </xsl:template>

    <!-- =================================================================================================
    2- annotation de chaque article
    =====================================================================================================-->
    
       <xsl:template match="pass1">
        <xsl:variable name="PASS2">
            <pass2>
            <array  xmlns="http://www.w3.org/2005/xpath-functions">
<!--                 <xsl:for-each select="//fn:map[child::fn:string[@key='id']][position() le 100]"><!-\- les 100 premiers documents -\->-->
                   <xsl:for-each select="//fn:map[child::fn:string[@key='id']]"> <!-- tous les documents -->
                 <map xmlns="http://www.w3.org/2005/xpath-functions">
                        <!--<xsl:variable name="id">
                            <xsl:value-of select="descendant::fn:string[@key='id']"/>
                        </xsl:variable>
                        <xsl:variable name="docLanguage">
                            <xsl:value-of select="descendant::fn:array[@key='language']/fn:string"/>
                        </xsl:variable>
                        <xsl:variable name="title">
                            <xsl:value-of select="descendant::fn:string[@key='title']"/>
                        </xsl:variable>
                        <xsl:variable name="abstract">
                            <xsl:value-of select="descendant::fn:string[@key='abstract']"/>
                        </xsl:variable>
                     <xsl:variable name="nativeKeywords">
                         <xsl:for-each select="descendant::fn:array[@key='subject']/descendant::fn:string[@key='value']">
                             <xsl:text> </xsl:text><xsl:value-of select="."/><xsl:text> </xsl:text>
                         </xsl:for-each>
                     </xsl:variable>-->
                     <!--<xsl:variable name="fullText">
                        <xsl:value-of select="$CORPUS//tei:TEI[descendant::tei:idno=$id]/normalize-space(string-join(descendant-or-self::text(), ' '))"/>
                     </xsl:variable>-->
                    <!-- titre + résumé + MCA -->
                   <!-- <xsl:variable name="TitleAbstractKeywords">
                       <!-\-<xsl:value-of select="concat(fn:string[@key='title'], fn:string[@key='abstract'], fn:map[@key='keywords'])"/>-\-><!-\- keywords = teeft -\->
                        <xsl:value-of select="concat(' ',descendant::fn:string[@key='title'], ' ', descendant::fn:string[@key='abstract'], ' ', $nativeKeywords, ' ')"/>
                    </xsl:variable>-->
                     
                    <!-- tous les contenus textuels (même les identifiants !) dans une variable-->
                   <!-- <xsl:variable name="docText" select="normalize-space(string-join(descendant-or-self::text(), ' '))"/>-->
                     <xsl:variable name="docText" select="normalize-space(string-join(descendant::fn:string[@key='value'], ' '))"/>
                    
                       <xsl:copy-of select="node()"/>
<!--=====================================Annotation proprement dite================================-->
                    <xsl:for-each select="$dicoName/dico">
                            <xsl:variable name="uriDico" select="@uri"/>
                            <array>
                                <xsl:attribute name="key">annotations</xsl:attribute>
<!--                                <xsl:if test="$docLanguage=('fr', 'fre', 'fra', 'FR', 'FRE', 'FRA') ">-->
                                    <xsl:if test="$lang=('fr', 'fre', 'fra', 'FR', 'FRE', 'FRA') "> 
                                    <!-- annotation avec les termes français de la ressource terminologique dont l'uri correspond à $uriDico-->
                                    <xsl:for-each select="$FRE/terme[substring-before(@id, '-')=$uriDico]">
                                        <xsl:variable name="keyword" select="."/>
                                        <xsl:variable name="id" select="@id"/>
                                        <xsl:variable name="status" select="@status"/>
                                        <xsl:variable name="renvoi" select="@renvoi"/>
                                        <!-- word boundary "\W" ne tient pas compte des lettres accentuées ("début" et "d'eau" génèrent "Deutérium" dont l'altLabel est "D"; 
                                            la variable "nonWordChar" intègre les lettres accentuées (\u00C0 jusqu'à \u017F) et les 3 formes d'apostrophe(\u0027, \u02BC, \u2019)-->
                                        <xsl:variable name="nonWordChar" select="'[^0-9A-Za-z\u00C0-\u017F\u0027\u02BC\u2019]'"/>
<!--                                        <xsl:variable name="nonWordChar" select="'[^\\p{L}+$]'"/>-->
                                        <!-- variable keyword2 : intègre les word boundaries pour ne prendre que les termes entiers ; échappe les caractères RegExp réservés-->
                                        <xsl:variable name="keyword2" select="concat($nonWordChar, replace(replace(replace(replace(replace(replace(replace(replace(replace($keyword, ',','&#x2C;'), '+', '\\+' , '!iq'), '(','\\(', '!iq'), ')', '\\)','!iq'), '[', '\\[' , '!iq'), ']', '\\]' , '!iq'), '{', '\\{' , '!iq'), '}', '\\}' , '!iq'), '|', '\\|' , '!iq'),$nonWordChar)"/>
                                        <xsl:analyze-string select="concat('\W',$docText,'\W')" regex="{$keyword2}" flags="!i" >
<!--                                           <xsl:analyze-string select="concat('\W',$TitleAbstractKeywords,'\W')" regex="{$keyword2}" flags="!i" >-->
 <!--                                              <xsl:analyze-string select="concat('\W',$fullText,'\W')" regex="{$keyword2}" flags="!i" >-->
                                            <xsl:matching-substring>
                                                <map>
                                                    <array key="lang">
                                                        <string>fre</string>
                                                    </array>
                                                    <string key="value">
                                                        <!--on ne sort que le préférentiel-->
                                                        <xsl:choose>
                                                            <xsl:when test="$status='pref'">
                                                                <xsl:value-of select="$keyword"/>
                                                            </xsl:when>
                                                            <xsl:otherwise>
                                                                <xsl:value-of select="$renvoi"/>
                                                            </xsl:otherwise>
                                                        </xsl:choose>
                                                    </string>
                                                    <string key="matchingText">
                                                        <xsl:value-of select="."/>
                                                    </string>
                                                    <string key="conceptUri">
                                                        <xsl:value-of select="$id"/>
                                                    </string>
 
                                                    <string key="status">
                                                        <xsl:value-of select="$status"/>
                                                    </string>
                                                    <string key="renvoi">
                                                        <xsl:value-of select="$renvoi"/>
                                                    </string>
                                                </map>
                                            </xsl:matching-substring>
                                            <xsl:non-matching-substring>
                                                <!-- on ne sort pas le reste du texte pour alléger le résultat-->
                                            </xsl:non-matching-substring>
                                        </xsl:analyze-string>
                                    </xsl:for-each>
                                </xsl:if>
                                
                                <xsl:if test="$lang=('en', 'eng','EN','ENG','de','ger','dut','unknown')">
                                    <!-- annotation avec les termes anglais de la ressource terminologique -->
                                    <xsl:for-each select="$ENG/terme[substring-before(@id, '-')=$uriDico]">
                                        <xsl:variable name="keyword" select="."/>
                                        <xsl:variable name="id" select="@id"/>
                                        <xsl:variable name="status" select="@status"/>
                                        <xsl:variable name="renvoi" select="@renvoi"/>
                                        <!-- variable keyword2 : intègre les word boundaries pour ne prendre que les termes entiers ; échappe les caractères RegExp réservés-->
                                        <xsl:variable name="keyword2" select="concat('\W', replace(replace(replace(replace(replace(replace(replace(replace(replace(replace($keyword, ',','&#x2C;'), '+', '\\+' , '!iq'), '(','\\(', '!iq'), ')', '\\)','!iq'), '[', '\\[' , '!iq'), ']', '\\]' , '!iq'), '{', '\\{' , '!iq'), '}', '\\}' , '!iq'), '|', '\\|' , '!iq'), '-', '\\‐' , '!iq'),'\W')"/>
                                                                               <xsl:analyze-string select="concat('\W',$docText,'\W')" regex="{$keyword2}" flags="!i" >
<!--                                       <xsl:analyze-string select="concat('\W',$TitleAbstractKeywords,'\W')" regex="{$keyword2}" flags="!i" >-->
<!--                                          <xsl:analyze-string select="concat('\W',$fullText,'\W')" regex="{$keyword2}" flags="!i" >-->
                                            <xsl:matching-substring>
                                                <map>
                                                    <array key="lang">
                                                        <string>eng</string>
                                                    </array>
                                                    <string key="value">
                                                        <!--on ne sort que le préférentiel-->
                                                        <xsl:choose>
                                                            <xsl:when test="$status='pref'">
                                                                <xsl:value-of select="$keyword"/>
                                                            </xsl:when>
                                                            <xsl:otherwise>
                                                                <xsl:value-of select="$renvoi"/>
                                                            </xsl:otherwise>
                                                        </xsl:choose>
                                                    </string>
                                                    <string key="matchingText">
                                                        <xsl:value-of select="."/>
                                                    </string>
                                                    <string key="conceptUri">
                                                        <xsl:value-of select="$id"/>
                                                    </string>
                                                      <string key="status">
                                                        <xsl:value-of select="$status"/>
                                                    </string>
                                                    <string key="renvoi">
                                                        <xsl:value-of select="$renvoi"/>
                                                    </string>
                                                  </map>
                                            </xsl:matching-substring>
                                            <xsl:non-matching-substring>
                                                <!-- on ne sort pas le reste du texte pour alléger le résultat-->
                                            </xsl:non-matching-substring>
                                        </xsl:analyze-string>
                                    </xsl:for-each>
                                </xsl:if>
                                
                            </array>
                        </xsl:for-each>
                </map>
                </xsl:for-each>
            </array>
            </pass2>
        </xsl:variable>
        <xsl:apply-templates select="exslt:node-set($PASS2)/pass2"/>
    </xsl:template>
    <!-- ===================================================================
   3- regroupement des annotations par vocabulaire puis par terme
=======================================================================   -->
    
    <xsl:template match="pass2">
        <xsl:variable name="PASS3">
            <pass3>
        <map xmlns="http://www.w3.org/2005/xpath-functions">
                <xsl:copy-of select="namespace::*"/>
                <number key="total">
                    <xsl:value-of select="count(//fn:map[child::fn:string[@key='id']])"/>
                </number>
                <array key="data">
                      <xsl:for-each select="//fn:map[child::fn:string[@key='id']]">
                <!-- début de chaque document -->
               <map>
                    <xsl:copy-of select="fn:string[@key='id']"/>
                   <map>
                       <xsl:attribute name="key">value</xsl:attribute>
                       <!--on recopie tout sauf les annotations-->
<!--                                         <xsl:copy-of select="fn:map[@key='value']/*[not(@key='annotations')]"/>-->
 
                           <array key="Annotations">
                            <!-- on regroupe les annotations et on factorise sur l'identifiant ark (scheme) et sur le terme-->
<!--                              <xsl:for-each-group select="descendant::fn:map[parent::fn:array[@key='annotations']]" group-by="fn:string[@key='conceptUri'], fn:array[@key='lang']/fn:string" composite="1">-->
                               <!-- on regroupe les annotations et on factorise sur le terme + langue-->
                                  <xsl:for-each-group select="descendant::fn:map[parent::fn:array[@key='annotations']]" group-by="fn:string[@key='value'], fn:array[@key='lang']/fn:string" composite="1">
                                         <xsl:variable name="freq" select="count(current-group()) "/>
 
                                    <map>
                                        <!--<array key="lang">
                                            <string><xsl:value-of select="current-grouping-key()[2]"/></string>
                                        </array>-->
                                       <string key="lang">
                                          <xsl:value-of select="current-grouping-key()[2]"/>
                                       </string>
                                       <string key="Nom"><xsl:value-of select="fn:string[@key='value'][1]"/></string>
                                       <array key="Termes reconnus">
                                           <xsl:for-each-group select="current-group()/fn:string[@key='matchingText']" group-by=".">
                                           <string>
                                               <xsl:value-of select="."/>
                                           </string>
                                           </xsl:for-each-group>
                                       </array>
                                         <array key="Termes génériques">                                   
                                            <xsl:for-each-group select="current-group()/fn:string[@key='conceptUri']" group-by=".">
                                                <xsl:for-each select="$DICO//skos:Concept[@rdf:about=current-grouping-key()]/skos:broader">

                                                         <xsl:variable name="génériques">
                                                            <xsl:call-template name="classif">
                                                                <xsl:with-param name="tg" select="@rdf:resource"/>
                                                                <xsl:with-param name="position">1</xsl:with-param>
                                                            </xsl:call-template>
                                                         </xsl:variable>
                                                        
                                                        <!-- génériques dans une variable pour pouvroir les renuméroter dans le sens décroissant-->
                                                        <xsl:variable name="génériques_num">
                                                         <xsl:for-each select="$génériques/fn:string[starts-with(.,'1')]">
                                                            <xsl:variable name="position">1</xsl:variable>
                                                            <xsl:variable name="string1" select="."/>
                                                            <xsl:choose>
                                                                <xsl:when test="following::fn:array[starts-with(@key, 'TG_')]">
                                                                    <xsl:for-each select="following::fn:array[starts-with(@key, 'TG_') and not(child::fn:array)]">
                                                                        <xsl:variable name="niveau" select="number(substring-after(@key, 'TG_'))"/>
                                                                        <xsl:variable name="num">1</xsl:variable>
                                                                        <array>
                                                                            <string>
                                                                                <xsl:value-of select="fn:string"/>
                                                                            </string>
                                                                            <xsl:call-template name="previousTG">
                                                                                <xsl:with-param name="tg" select="parent::fn:array[last()]/fn:string"/>
                                                                                <xsl:with-param name="niveau" select="$niveau -1"/>
                                                                                <xsl:with-param name="num" select="$num +1"/>
                                                                            </xsl:call-template>
                                                                             <string>
                                                                                <xsl:value-of select="$string1"/>
                                                                            </string>
                                                                        </array>
                                                                    </xsl:for-each>
                                                                </xsl:when>
                                                                <xsl:otherwise>
                                                                    <array>
                                                                        <string>
                                                                            <xsl:value-of select="$string1"/>
                                                                        </string>
                                                                    </array>
                                                                </xsl:otherwise>
                                                            </xsl:choose>
                                                            
                                                        
                                                        </xsl:for-each>
                                                        </xsl:variable>
                                                        
                                                        <!-- renumérotation -->
                                                        <xsl:variable name="génériques_Renum">
                                                        <xsl:for-each select="$génériques_num/fn:array">
                                                            <array xmlns="http://www.w3.org/2005/xpath-functions">
                                                                   <xsl:for-each select="fn:string">
                                                                    <string xmlns="http://www.w3.org/2005/xpath-functions">
                                                                        <xsl:value-of select="concat(position(), ' - ',substring(.,3))"/>
                                                                    </string>
                                                                </xsl:for-each>
                                                            </array>
                                                        </xsl:for-each>
                                                        </xsl:variable>
                                                        <!-- regroupement des strings -->
                                                        <!--<array xmlns="http://www.w3.org/2005/xpath-functions">
                                                            <xsl:attribute name="key">Arborescence</xsl:attribute>-->
                                                        <xsl:for-each select="$génériques_Renum/fn:array">
                                                            <string xmlns="http://www.w3.org/2005/xpath-functions">
                                                                 <xsl:for-each select="fn:string">
                                                                    <xsl:choose>
                                                                        <xsl:when test="position()=last()">
                                                                            <xsl:value-of select="."/>
                                                                        </xsl:when>
                                                                        <xsl:otherwise>
                                                                            <xsl:value-of select="."/>
                                                                            <xsl:text>, </xsl:text>
                                                                        </xsl:otherwise>
                                                                    </xsl:choose>
                                                                </xsl:for-each>
                                                            </string>
                                                        </xsl:for-each>
                                                        <!--</array>-->
                                                    <!--</map>-->
                                                </xsl:for-each>
                                            </xsl:for-each-group>
                                        </array>
 
                                       <string key="Outils">Loterre_Annot</string>
                                         <xsl:for-each-group select="current-group()/fn:string[@key='conceptUri']" group-by=".">
                                            <string>
                                                <xsl:attribute name="key">
                                                            <!--<xsl:value-of select="concat('Concept_Uri_', position())"/>-->
<!--                                                            <xsl:value-of select="concat('Concept_Uri_', substring-before(tokenize(current-grouping-key(), '/')[6], '-'))"/>-->
                                                    <xsl:value-of select="'Concept_Uri'"/>
                                                  </xsl:attribute>
                                                <xsl:value-of select="current-grouping-key()"/>
                                            </string>
                                        </xsl:for-each-group>
                                        <number key="frequency">
                                            <xsl:value-of select="$freq "/>
                                        </number>
                                        
                                    </map> 
                            </xsl:for-each-group>
                                </array>
               </map>
               </map>
            </xsl:for-each>
                </array>
            </map>
 
        </pass3>
        </xsl:variable>
        <xsl:apply-templates select="exslt:node-set($PASS3)/pass3"/>
    </xsl:template>
  
<!-- ++++++++++++++++++++++++++++++++++++++++++++++ -->
    <xsl:template name="previousTG">
        <xsl:param name="tg"/>
        <xsl:param name="niveau"/>
        <xsl:param name="num"/>
         <xsl:for-each select="parent::fn:array[fn:string=$tg]">
            <string xmlns="http://www.w3.org/2005/xpath-functions">
            <xsl:value-of select="$tg"/>
            </string>
              <xsl:call-template name="previousTG">
                <!--<xsl:with-param name="tg" select="parent::fn:array[child::fn:array/fn:string=$tg]"/>-->
                  <xsl:with-param name="tg" select="parent::fn:array[last()]/fn:string"/> 
                  <xsl:with-param name="niveau" select="$niveau -1"/>
                  <xsl:with-param name="num" select="$num -1"/>
            </xsl:call-template>
        </xsl:for-each>
    </xsl:template>

    <!-- ===================================================================
  TG 
=======================================================================   -->
    <xsl:template name="classif">
        <xsl:param name="tg"/>
        <xsl:param name="position"/>
        
        <xsl:if test="$DICO//skos:Concept[@rdf:about=$tg]">
            <string xmlns="http://www.w3.org/2005/xpath-functions">
                <!--on rajoute des chiffres pour le tri descendant qui seront enlevés après-->
                <!--on rajoute des chiffres pour le tri descendant qui seront enlevés après-->
                <!--           <xsl:value-of select="replace(replace(replace(replace(replace(replace(concat($DICO//skos:Concept[@rdf:about=$tg]/rdfs:comment, ' : ', $DICO//skos:Concept[@rdf:about=$tg]/skos:prefLabel[@xml:lang='fr']), 'genus','1-Genre'),'family','2-Famille'),'order','3-Ordre'),'class','4-Classe'),'phylum','5-Embranchement'),'kingdom','6-Règne')"/>-->
                
                <!-- <xsl:value-of select="$DICO//skos:Concept[@rdf:about=$tg]/skos:prefLabel[@xml:lang='en']"/>
             <string xmlns="http://www.w3.org/2005/xpath-functions">
                 <xsl:value-of select="concat('Terme générique : ',$DICO//skos:Concept[@rdf:about=$uri]/skos:prefLabel[@xml:lang='en'])"/>
             </string>-->
                <!--             <xsl:value-of select="concat('Terme Générique : ', $DICO//skos:Concept[@rdf:about=$tg]/skos:prefLabel[@xml:lang='en'])"/>-->
                <xsl:value-of select="concat($position, '-',$DICO//skos:Concept[@rdf:about=$tg]/skos:prefLabel[@xml:lang='en'])"/>
                
            </string>
           <!-- <xsl:choose>
                <xsl:when test="count($DICO//skos:Concept[@rdf:about=$tg]/skos:broader) = 1">
                    <xsl:call-template name="classif">
                        <xsl:with-param name="tg" select="."/>
                        <xsl:with-param name="position">
                            <xsl:value-of select="$position +1"/>
                        </xsl:with-param>
                    </xsl:call-template>
                </xsl:when>
                <xsl:otherwise>-->
            
                    <xsl:for-each select="$DICO//skos:Concept[@rdf:about=$tg]/skos:broader/@rdf:resource">
                        <array xmlns="http://www.w3.org/2005/xpath-functions">
                            <xsl:attribute name="key">
                                <xsl:value-of select="concat('TG_',$position +1)"/>
                            </xsl:attribute>
<!--                                    <map xmlns="http://www.w3.org/2005/xpath-functions"> -->
                                        <!--<string>
                                            <xsl:value-of select="."/>
                                        </string>-->
                       <!-- <xsl:call-template name="pref2">
                    <xsl:with-param name="tg" select="."/>
                    <xsl:with-param name="position">
                        <xsl:value-of select="$position +1"/>
                    </xsl:with-param>
                </xsl:call-template> -->
                        
                        <!-- <xsl:call-template name="classif2">
                    <xsl:with-param name="uri" select="."/>
                    <xsl:with-param name="position">1</xsl:with-param>
                </xsl:call-template>-->
                        <xsl:call-template name="classif99">
                            <xsl:with-param name="tg" select="."/>
                            <xsl:with-param name="position">
                                <xsl:value-of select="$position +1"/>
                            </xsl:with-param>
                        </xsl:call-template>
                        <!--</map>-->
                        </array> 
                    </xsl:for-each>
                    
                <!--</xsl:otherwise>
            </xsl:choose>-->
           
        </xsl:if> 
        
        
        
    </xsl:template>
    <!-- ===================================================================
  TG99 
=======================================================================   -->
    <xsl:template name="classif99">
        <xsl:param name="tg"/>
        <xsl:param name="position"/>
        
        <xsl:if test="$DICO//skos:Concept[@rdf:about=$tg]">
            <string xmlns="http://www.w3.org/2005/xpath-functions">
                <!--on rajoute des chiffres pour le tri descendant qui seront enlevés après-->
                <!--on rajoute des chiffres pour le tri descendant qui seront enlevés après-->
                <!--           <xsl:value-of select="replace(replace(replace(replace(replace(replace(concat($DICO//skos:Concept[@rdf:about=$tg]/rdfs:comment, ' : ', $DICO//skos:Concept[@rdf:about=$tg]/skos:prefLabel[@xml:lang='fr']), 'genus','1-Genre'),'family','2-Famille'),'order','3-Ordre'),'class','4-Classe'),'phylum','5-Embranchement'),'kingdom','6-Règne')"/>-->
                
                <!-- <xsl:value-of select="$DICO//skos:Concept[@rdf:about=$tg]/skos:prefLabel[@xml:lang='en']"/>
             <string xmlns="http://www.w3.org/2005/xpath-functions">
                 <xsl:value-of select="concat('Terme générique : ',$DICO//skos:Concept[@rdf:about=$uri]/skos:prefLabel[@xml:lang='en'])"/>
             </string>-->
                <!--             <xsl:value-of select="concat('Terme Générique : ', $DICO//skos:Concept[@rdf:about=$tg]/skos:prefLabel[@xml:lang='en'])"/>-->
                <xsl:value-of select="concat($position, '-',$DICO//skos:Concept[@rdf:about=$tg]/skos:prefLabel[@xml:lang='en'])"/>
                
            </string>
            <!-- <xsl:choose>
                <xsl:when test="count($DICO//skos:Concept[@rdf:about=$tg]/skos:broader) = 1">
                    <xsl:call-template name="classif">
                        <xsl:with-param name="tg" select="."/>
                        <xsl:with-param name="position">
                            <xsl:value-of select="$position +1"/>
                        </xsl:with-param>
                    </xsl:call-template>
                </xsl:when>
                <xsl:otherwise>-->
            
            <xsl:for-each select="$DICO//skos:Concept[@rdf:about=$tg]/skos:broader/@rdf:resource">
                <array xmlns="http://www.w3.org/2005/xpath-functions">
                    <xsl:attribute name="key">
                        <xsl:value-of select="concat('TG_',$position+1)"/>
                    </xsl:attribute>
                    <!--                                    <map xmlns="http://www.w3.org/2005/xpath-functions"> -->
                    <!--<string>
                                            <xsl:value-of select="."/>
                                        </string>-->
                    <!-- <xsl:call-template name="pref2">
                    <xsl:with-param name="tg" select="."/>
                    <xsl:with-param name="position">
                        <xsl:value-of select="$position +1"/>
                    </xsl:with-param>
                </xsl:call-template> -->
                    
                    <!-- <xsl:call-template name="classif2">
                    <xsl:with-param name="uri" select="."/>
                    <xsl:with-param name="position">1</xsl:with-param>
                </xsl:call-template>-->
                    <xsl:call-template name="classif99">
                        <xsl:with-param name="tg" select="."/>
                        <xsl:with-param name="position">
                            <xsl:value-of select="$position +1"/>
                        </xsl:with-param>
                    </xsl:call-template>
                    <!--</map>-->
                </array> 
            </xsl:for-each>
            
            <!--</xsl:otherwise>
            </xsl:choose>-->
            
        </xsl:if> 
        
        
    </xsl:template>
    <!-- ===================================================================
  TG2 
=======================================================================   -->
    <xsl:template name="classif2">
        <xsl:param name="uri"/>
        <xsl:param name="position"/>
        
        <xsl:for-each select="$DICO//skos:Concept[@rdf:about=$uri]/skos:broader/@rdf:resource">
            
            <xsl:call-template name="pref2">
                <xsl:with-param name="tg" select="."/>
                <xsl:with-param name="position" select="$position+1"/>
            </xsl:call-template> 
            
            <xsl:call-template name="classif2">
                <xsl:with-param name="uri" select="."/>
                <xsl:with-param name="position" select="$position+1"/>
            </xsl:call-template>
            
        </xsl:for-each>
        
        
    </xsl:template>
    <!-- ===================================================================
  pref 
=======================================================================   -->
    <xsl:template name="pref">
        <xsl:param name="uri"/>
        <xsl:if test="$DICO//skos:Concept[@rdf:about=$uri]">
            <xsl:value-of select="$DICO//skos:Concept[@rdf:about=$uri]/skos:prefLabel[@xml:lang='en']"/>
        </xsl:if>
        
    </xsl:template>
    <!-- ===================================================================
  pref 2
=======================================================================   -->
    <xsl:template name="pref2">
        <xsl:param name="tg"/>
        <xsl:param name="position"/>
        <xsl:if test="$DICO//skos:Concept[@rdf:about=$tg]">
            <string xmlns="http://www.w3.org/2005/xpath-functions">
                <!--on rajoute des chiffres pour le tri descendant qui seront enlevés après-->
                 <!--on rajoute des chiffres pour le tri descendant qui seront enlevés après-->
                <!--           <xsl:value-of select="replace(replace(replace(replace(replace(replace(concat($DICO//skos:Concept[@rdf:about=$tg]/rdfs:comment, ' : ', $DICO//skos:Concept[@rdf:about=$tg]/skos:prefLabel[@xml:lang='fr']), 'genus','1-Genre'),'family','2-Famille'),'order','3-Ordre'),'class','4-Classe'),'phylum','5-Embranchement'),'kingdom','6-Règne')"/>-->
                
                <!-- <xsl:value-of select="$DICO//skos:Concept[@rdf:about=$tg]/skos:prefLabel[@xml:lang='en']"/>
             <string xmlns="http://www.w3.org/2005/xpath-functions">
                 <xsl:value-of select="concat('Terme générique : ',$DICO//skos:Concept[@rdf:about=$uri]/skos:prefLabel[@xml:lang='en'])"/>
             </string>-->
                <!--             <xsl:value-of select="concat('Terme Générique : ', $DICO//skos:Concept[@rdf:about=$tg]/skos:prefLabel[@xml:lang='en'])"/>-->
                <xsl:value-of select="concat($position, '-',$DICO//skos:Concept[@rdf:about=$tg]/skos:prefLabel[@xml:lang='en'])"/>
                
            </string>
        </xsl:if>
        
    </xsl:template>
    
    <!-- ===================================================================
   4ème traitement : renommage des étiquettes
=======================================================================   -->
    <xsl:template match="pass3">
        <xsl:variable name="PASS4">
            <pass4>
        <map xmlns="http://www.w3.org/2005/xpath-functions">
            <xsl:copy-of select="namespace::*"/>
            <number key="total">
                <xsl:value-of select="count(//fn:map[child::fn:string[@key='id']])"/>
            </number>
            <array key="data">
                <!--                   <xsl:for-each select="//fn:map[child::fn:string[@key='corpusName']][descendant::node()[@key='_id']='0CC1E440A99F479B3551C506521B86B67EB26C7D']">-->
                    <xsl:apply-templates select="node() | @*"/>
                    
            </array>
        </map>
            </pass4>
        </xsl:variable>
        <xsl:apply-templates select="exslt:node-set($PASS4)/pass4"/>
    </xsl:template>
    
    <!-- ================================================================
    renommage des @key
    ===================================================================-->
    <xsl:template match="node()|@*">
        <xsl:copy>
            <xsl:apply-templates select="node()|@*"/>
        </xsl:copy>
    </xsl:template>
    
    <xsl:template match="@key">
        <xsl:variable name="key" select="."/>

        <xsl:attribute name="key">
            <xsl:choose>
                <xsl:when test="$KEYS/key[old=$key]">
                <xsl:value-of select="$KEYS/key[old=$key]/new"/>
            </xsl:when>
                <xsl:otherwise>
                    <xsl:value-of select="$key"/>
                </xsl:otherwise>
            </xsl:choose>
  
        </xsl:attribute>
    </xsl:template>
    
 
    
    <!-- ===================================================================
   5- création du fichier de sortie en json
=======================================================================   -->
    <xsl:template match="pass4">
        <xsl:variable name="racine" select="."/>
                <xsl:text>{
 "total": </xsl:text><xsl:value-of select="count($racine//fn:map[child::fn:string[@key='id']])"/><xsl:text>,
 "data": [</xsl:text>
                       <xsl:for-each select="//fn:map[child::fn:string[@key='id'] ]">
                <!-- pour ne prendre que les articles ayant une annotation -->
<!--                <xsl:for-each select="$racine//fn:map[child::fn:string[@key='id'] and not(descendant::fn:array[contains(@key,'nnotations')]='')">-->
                            <xsl:choose>
                                <xsl:when test="position()=last()">
                                    <xsl:copy-of select="xml-to-json(., map {'indent': true()})"/>
                                </xsl:when>
                                <xsl:otherwise>
                                    <xsl:copy-of select="xml-to-json(., map {'indent': true()})"/>
                                    <xsl:text>,</xsl:text>
                                </xsl:otherwise>
                            </xsl:choose>
                        </xsl:for-each>
                <xsl:text>]</xsl:text>
                <xsl:text>}</xsl:text>
    </xsl:template>
</xsl:stylesheet>