VI - Les instructions de programmation
        Instruction xsl:if
                Bande-annonce
                Syntaxe
                Règle XSLT typique
                Sémantique
                Exemple
        Instruction xsl:choose
                Bande-annonce
                Syntaxe
                Règle XSLT typique
                Sémantique
                Exemple
        Instruction xsl:variable
                Bande-annonce
                Syntaxe
                Règle XSLT typique
                Sémantique
                Variables globales et locales
                Utilisation d'une variable
                        Dans une expression XPath
                        Dans un motif
                        Exemple d'utilisation d'une variable
                Evaluation d'une variable globale
                        Exemple
                Temporary Source Tree
                        TST obtenu littéralement
                        TST calculé
                        TST-texte
                        Opérations sur un TST (XSLT¬1.1 ou plus)
                        Exemples d'utilisation d'un TST (XSLT1.1 ou plus)
                Result Tree Fragment (XSLT 1.0)
                        Opérations sur un RTF (XSLT 1.0)
                Règles de visibilité
                        Règles de visibilité pour les variables globales
                        Règles de visibilité pour les variables locales
                Conflits de noms de variables
        Instruction xsl:param
                Bande-annonce
                Syntaxe
                Sémantique
                        Utilisation d'un paramètre global
                        Utilisation d'un paramètre local
        Instruction xsl:template
                Bande-annonce
                Syntaxe
                Modèle nommé typique
                Sémantique
                Exemple
        Instruction xsl:call-template
                Bande-annonce
                Syntaxe
                Sémantique
                Exemple
        Instruction xsl:apply-templates
                Syntaxe
                Sémantique
        Instruction xsl:message
                Syntaxe
                Sémantique
        Instruction xsl:key
                Bande-annonce
                Syntaxe
                Sémantique
                        Construction et exploitation de la table associative
                        Variante
                Exemples


Chapitre VI - Les instructions de programmation

Instruction xsl:if

Bande-annonce

<xsl:template match="Compositeurs">
    <H3 align="center"> Oeuvres de <br/> <xsl:apply-templates/> </H3>
</xsl:template>

<xsl:template match="Compositeur">
    <xsl:value-of select="."/>
    <xsl:if test="not(position() = last())">, </xsl:if>
</xsl:template>

Syntaxe

xsl:if
<xsl:if test=" ... expression XPath ... ">
    <!-- modèle de transformation -->
    ... texte ou instructions XSLT ...
    <!-- fin du modèle de transformation -->
</xsl:if>

Règle XSLT typique

<xsl:template match="... motif (pattern) ...">
    <xsl:if test=" ... expression XPath ... ">
        <!-- modèle de transformation -->
        ... texte ou instructions XSLT ...
        <!-- fin du modèle de transformation -->
    </xsl:if>
</xsl:template> 

Sémantique

Exemple

Concert.xml
<?xml version="1.0" encoding="UTF-16" standalone="yes"?>

<Concert>
    
    <Entête> "Les Concerts d'Anacréon" </Entête>
    <Date>Jeudi 17 Janvier 2002, 20H30</Date>
    <Lieu>Chapelle des Ursules</Lieu>

    <Ensemble> "A deux violes esgales" </Ensemble>

    <Interprète>
        <Nom> Jonathan Dunford </Nom>
        <Instrument>Basse de viole</Instrument>
    </Interprète>

    <Interprète>
        <Nom> Sylvia Abramowicz </Nom>
        <Instrument>Basse de viole</Instrument>
    </Interprète>

    <Interprète>
        <Nom> Benjamin Perrot </Nom>
        <Instrument>Théorbe et Guitare baroque</Instrument>
    </Interprète>
    
    <TitreConcert>
        Folies d'Espagne et diminutions d'Italie
    </TitreConcert>
    
    <Compositeurs>
        <Compositeur>M. Marais</Compositeur>
        <Compositeur>D. Castello</Compositeur>
        <Compositeur>F. Rognoni</Compositeur>
    </Compositeurs>
    
</Concert>
<xsl:template match="Compositeur">
    ...
</xsl:template>
Concert.xsl
<?xml version="1.0" encoding="UTF-16"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output   method='html' encoding='ISO-8859-1' />

    <xsl:strip-space elements='Compositeurs'/>

    <xsl:template match="/">
        <html>
            <head>
                <title><xsl:value-of select="/Concert/Entête"/></title>
            </head>
            <body bgcolor="white" text="black">
                <xsl:apply-templates/>
            </body>
        </html>
    </xsl:template>
    
    <xsl:template match="Entête">
        <p> <xsl:value-of select="."/> présentent </p>
    </xsl:template>
    
    <xsl:template match="Date">
        <H1 align="center"> Concert du <xsl:value-of select="."/> </H1>
    </xsl:template>
    
    <xsl:template match="Lieu">
        <H4 align="center"> <xsl:value-of select="."/> </H4>
    </xsl:template>
    
    <xsl:template match="Ensemble">
        <H2 align="center"> Ensemble <xsl:value-of select="."/></H2>
    </xsl:template>
    
    <xsl:template match="Compositeurs">
        <H3 align="center"> Oeuvres de <br/> <xsl:apply-templates/> </H3>
    </xsl:template>
    
    <xsl:template match="Compositeur">
        <xsl:value-of select="."/>
        <xsl:if test="not(position() = last())">, </xsl:if>
    </xsl:template>
    
    <xsl:template match="text()"/>
    
</xsl:stylesheet>
Concert.html
<html>
   <head>
      <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
   
      <title> Les Concerts d&#8217;Anacr&eacute;on </title>
   </head>
   <body bgcolor="white" text="black">
      <p> Les Concerts d&#8217;Anacr&eacute;on  pr&eacute;sentent </p>
      <H1 align="center"> Concert du Jeudi 17 Janvier 2002, 20H30</H1>
      <H4 align="center">Chapelle des Ursules</H4>
      <H2 align="center"> Ensemble  &laquo;A deux violes esgales&raquo; </H2>
      <H3 align="center"> Oeuvres de <br>M. Marais, D. Castello, F. Rognoni
      </H3>
   </body>
</html>

Instruction xsl:choose

Bande-annonce

<xsl:choose>

    <xsl:when test="count(./Interprète) = 1 ">
        <xsl:value-of select="Interprète"/>
    </xsl:when>

    <xsl:when test="count(./Interprète) = 2 ">
        <xsl:value-of select="Interprète[1]"/> et <xsl:value-of 
                      select="Interprète[2]"/>
    </xsl:when>

    <xsl:otherwise>
        <xsl:for-each select="Interprète">
            <xsl:value-of select="."/><xsl:if test="not(position() =
                          last())">, </xsl:if>
        </xsl:for-each>
    </xsl:otherwise>
    
</xsl:choose>

Syntaxe

xsl:choose
<xsl:choose>
    
    <!-- autant de xsl:when que l'on veut, mais au moins 1 en tout -->
    
    <xsl:when test=" ... expression XPath ... ">
    <!-- modèle de transformation -->
    ... texte ou instructions XSLT ...
    <!-- fin du modèle de transformation -->
    </xsl:when>
    
    <xsl:when test=" ... expression XPath ... ">
    <!-- modèle de transformation -->
    ... texte ou instructions XSLT ...
    <!-- fin du modèle de transformation -->
    </xsl:when>
    
    ...
    
    <!-- l'élément xsl:otherwise est facultatif -->
    
    <xsl:otherwise>
    <!-- modèle de transformation -->
    ... texte ou instructions XSLT ...
    <!-- fin du modèle de transformation -->
    </xsl:otherwise>
    
</xsl:choose>

Règle XSLT typique

<xsl:template match="... motif (pattern) ...">
    <xsl:choose>
    
        <xsl:when test=" ... expression XPath ... ">
        <!-- modèle de transformation -->
        ... texte ou instructions XSLT ...
        <!-- fin du modèle de transformation -->
        </xsl:when>
        
        <xsl:otherwise>
        <!-- modèle de transformation -->
        ... texte ou instructions XSLT ...
        <!-- fin du modèle de transformation -->
        </xsl:otherwise>
        
    </xsl:choose>
</xsl:template>

Sémantique

Exemple

ProgrammeConcert.xml
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>

<ProgrammeConcert>

    <PageTitre>
    
        <Entête>
            "Les Concerts d'Anacréon"
            <Date>Samedi 9 Octobre 1999, 20H30</Date>
            <Lieu>Chapelle des Ursules</Lieu>
        </Entête>
    
        <Ensemble>
            <Nom>
                La Cetra d'Orfeo
            </Nom>
            <Direction>
                Michel Keustermans
            </Direction>
        </Ensemble>
    
        <Interprètes>
            <Role>
                ténor
                <Interprète> Yvan Goossens </Interprète>
            </Role>
            <Role>
                basse
                <Interprète> Conor Biggs </Interprète>
            </Role>
            <Role>
                flûte à bec
                <Interprète> Michel Keustermans </Interprète>
                <Interprète> Laura Pok </Interprète>
            </Role>
            <Role>
                viole d'amour
                <Interprète> Vinciane Baudhuin </Interprète>
            </Role>
            <Role>
                oboe da caccia
                <Interprète> Blai Justo </Interprète>
            </Role>
            <Role>
                viole de gambe
                <Interprète> Rika Murata </Interprète>
                <Interprète> Martin Bauer </Interprète>
                <Interprète> Sophie Watillon </Interprète>
            </Role>
            <Role>
                violone
                <Interprète> Benoit vanden Bemden </Interprète>
            </Role>
            <Role>
                orgue positif et clavecin
                <Interprète> Jacques Willemijns </Interprète>
            </Role>
        </Interprètes>
        
        <TitreConcert>
            Cantates allemandes
        </TitreConcert>
        
        <Compositeurs>
            <Compositeur> Bach </Compositeur>
            <Compositeur> Telemann </Compositeur>
        </Compositeurs>

    </PageTitre>
    
</ProgrammeConcert>
ProgrammeConcert.xsl
<?xml version="1.0" encoding="UCS-2"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

    <xsl:output  method='text' encoding='ISO-8859-1' />

    <xsl:template match="Role">
        
        <xsl:value-of select="./child::text()"/> :
        <xsl:choose>
        
            <xsl:when test="count(./Interprète) = 1 ">
                <xsl:value-of select="Interprète"/>
            </xsl:when>
        
            <xsl:when test="count(./Interprète) = 2 ">
                <xsl:value-of select="Interprète[1]"/> et <xsl:value-of 
                              select="Interprète[2]"/>
            </xsl:when>
        
            <xsl:otherwise>
                <xsl:for-each select="Interprète">
                    <xsl:value-of select="."/><xsl:if test="not(position() =
                                  last())">, </xsl:if>
                </xsl:for-each>
            </xsl:otherwise>
            
        </xsl:choose>
        
    </xsl:template>
    
    <xsl:template match="text()"/>
            
</xsl:stylesheet>
Résultat
                flûte à bec
                 :
         Michel Keustermans  et  Laura Pok 
                viole d'amour
                 :
         Vinciane Baudhuin 
                oboe da caccia
                 :
         Blai Justo 
                viole de gambe
                 :
         Rika Murata ,  Martin Bauer ,  Sophie Watillon 
                violone
                 :
         Benoit vanden Bemden 
                orgue positif et clavecin
                 :
         Jacques Willemijns 

Instruction xsl:variable

Bande-annonce

<xsl:variable name="nombreInterprètes" select="count(./Interprète)" />
<xsl:choose>

    <xsl:when test="$nombreInterprètes = 1 ">
        <xsl:value-of select="Interprète"/>
    </xsl:when>

    <xsl:when test="$nombreInterprètes = 2 ">
        <xsl:value-of select="Interprète[1]"/> et <xsl:value-of 
                      select="Interprète[2]"/>
    </xsl:when>

    <xsl:otherwise>
        <xsl:for-each select="Interprète">
            <xsl:value-of select="."/><xsl:if test="not(position() =
                          last())">, </xsl:if>
        </xsl:for-each>
    </xsl:otherwise>
    
</xsl:choose>
<xsl:variable name="maison">
    <RDC>
        <cuisine surface='12m2'>
            Evier inox. Mobilier encastré.
        </cuisine>
        <WC>
            Lavabo. Cumulus 200L.
        </WC>
        <séjour surface='40m2'>
            Cheminée en pierre. Poutres au plafond. 
            Carrelage terre cuite. Grande baie vitrée.
        </séjour>
        <bureau surface='15m2'>
            Bibliothèque encastrée.
        </bureau>
        <jardin surface='150m2'>
            Palmier en zinc figurant le désert.
        </jardin>
        <garage/>
    </RDC>
</xsl:variable>
<xsl:variable name="mouvement">
    <insert>
        <Ensemble>
            <Nom><xsl:value-of select="NomEnsemble"/></Nom>
            <Direction><xsl:value-of select="Chef"/></Direction>
        </Ensemble>
        <Concert>
            <Date><xsl:value-of select="Date"/></Date>
            <Ville><xsl:value-of select="Ville"/></Ville>
            <Lieu><xsl:value-of select="Salle"/></Lieu>
            <Titre><xsl:value-of select="TitreConcert"/></Titre>
        </Concert>
    </insert>
</xsl:variable>

Syntaxe

xsl:variable
<xsl:variable name="..." select=" ... expression XPath ... "/>
xsl:variable
<xsl:variable name="...">
    <!-- modèle de transformation -->
    ... texte ou instructions XSLT ...
    <!-- fin du modèle de transformation -->
</xsl:variable>

Règle XSLT typique

<?xml version="1.0" encoding="UCS-2"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

    <xsl:output    method='...' encoding='ISO-8859-1' />
    
    <xsl:variable    name='...' select='...' />

    <xsl:template match="...">
        <xsl:variable    name='...'>
            ...
        </xsl:variable>
        ...
    </xsl:template>
    
    <xsl:variable    name='...'>
        ...
    </xsl:variable>

    <xsl:template match="...">
        <xsl:variable    name='...' select='...' />
        ...
    </xsl:template>
    ...         
</xsl:stylesheet>

Sémantique

<?xml version="1.0" encoding="UCS-2"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

    <xsl:template match="...">
        <!-- modèle de transformation propre à la règle -->
        ...
        <xsl:variable    name='...'>
            <!-- modèle de transformation propre à la variable -->
            ...
            <!-- fin du modèle de transformation propre à la variable -->
        </xsl:variable>
        ...
        <!-- fin du modèle de transformation propre à la règle -->
    </xsl:template>
    ...         
</xsl:stylesheet>

Variables globales et locales

Utilisation d'une variable

Dans une expression XPath

Dans un motif

Exemple d'utilisation d'une variable

ProgrammeConcert.xsl
<?xml version="1.0" encoding="UCS-2"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

    <xsl:output  method='text' encoding='ISO-8859-1' />

    <xsl:template match="Role">
        
        *<xsl:value-of select="normalize-space(./child::text())"/> :
        <xsl:choose>
        
            <xsl:when test="count(./Interprète) = 1 ">
                <xsl:value-of select="Interprète"/>
            </xsl:when>
        
            <xsl:when test="count(./Interprète) = 2 ">
                <xsl:value-of select="Interprète[1]"/> et <xsl:value-of 
                              select="Interprète[2]"/>
            </xsl:when>
        
            <xsl:otherwise>
                <xsl:for-each select="Interprète">
                    <xsl:value-of select="."/><xsl:if test="not(position() =
                                  last())">, </xsl:if>
                </xsl:for-each>
            </xsl:otherwise>
            
        </xsl:choose>
        
    </xsl:template>
    
    <xsl:template match="text()"/>
            
</xsl:stylesheet>
ProgrammeConcert.xsl
<?xml version="1.0" encoding="UCS-2"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

    <xsl:output  method='text' encoding='ISO-8859-1' />

    <xsl:template match="Role">
        
        *<xsl:value-of select="normalize-space(./child::text())"/> :
        <xsl:variable name="nombreInterprètes" select="count(./Interprète)" />
        <xsl:choose>
        
            <xsl:when test="$nombreInterprètes = 1 ">
                <xsl:value-of select="Interprète"/>
            </xsl:when>
        
            <xsl:when test="$nombreInterprètes = 2 ">
                <xsl:value-of select="Interprète[1]"/> et <xsl:value-of 
                              select="Interprète[2]"/>
            </xsl:when>
        
            <xsl:otherwise>
                <xsl:for-each select="Interprète">
                    <xsl:value-of select="."/><xsl:if test="not(position() =
                                  last())">, </xsl:if>
                </xsl:for-each>
            </xsl:otherwise>
            
        </xsl:choose>
        
    </xsl:template>
    
    <xsl:template match="text()"/>
            
</xsl:stylesheet>

Evaluation d'une variable globale

Exemple

BaseProduits.xml
<?xml version="1.0" encoding="UCS-2" standalone="yes"?>

<BaseProduits>

    <LesProduits>
    
        <Livre ref="vernes1" NoISBN="193335" gamme="roman" media="papier">
            <refOeuvre>
                <Ref valeur="20000lslm"/>
            </refOeuvre>
            <Prix valeur="40.5" monnaie="FF"/>
            <Prix valeur="5" monnaie="£"/>
        </Livre>
    
        <Livre ref="boileaunarcejac1" NoISBN="533791" gamme="roman" 
                                                      media="papier">
            <refOeuvre>
                <Ref valeur="liatlc.bn"/>
            </refOeuvre>
            <Prix valeur="30" monnaie="FF"/>
            <Prix valeur="3" monnaie="£"/>
        </Livre>
    
        <Enregistrement ref="marais1" RefEditeur="LC000280" 
                                             gamme="violedegambe" media="CD">
            ... sans intérêt pour l'exemple
        </Enregistrement>
    
        <Matériel ref="HarKar1" refConstructeur="XL-FZ158BK" gamme="lecteurCD" 
                                                            marque="HarKar">
            ... sans intérêt pour l'exemple
        </Matériel>
    
        <Livre ref="phbeaussant1" NoISBN="138301" gamme="essai" media="papier">
            <refOeuvre>
                <Ref valeur="vadb.phb"/>
            </refOeuvre>
            <Prix valeur="60" monnaie="FF"/>
            <Prix valeur="8" monnaie="£"/>
        </Livre>
        
    </LesProduits>
    
    <LesOeuvres>
        <Oeuvre ref="20000lslm">
            <Titre> Vingt mille lieues sous les mers </Titre>
            <refAuteurs>
                <Ref valeur="JVernes"/>
            </refAuteurs>
        </Oeuvre>
        <Oeuvre ref="marais.folies">
            <Titre> Les Folies d'Espagne </Titre>
            <refAuteurs>
                <Ref valeur="MMarais"/>
            </refAuteurs>
        </Oeuvre>
        <Oeuvre ref="vadb.phb">
            <Titre> Vous avez dit baroque ? </Titre>
            <refAuteurs>
                <Ref valeur="PhBeaussant"/>
            </refAuteurs>
        </Oeuvre>
        <Oeuvre ref="marais.pieces1685">
            <Titre> Pièces de viole en manuscrit </Titre>
            <refAuteurs>
                <Ref valeur="MMarais"/>
            </refAuteurs>
        </Oeuvre>
        <Oeuvre ref="liatlc.bn">
            <Titre> L'ingénieur aimait trop les chiffres </Titre>
            <refAuteurs>
                <Ref valeur="PBoileau"/>
                <Ref valeur="ThNarcejac"/>
            </refAuteurs>
        </Oeuvre>
    </LesOeuvres>
    
    <!-- ... suite du fichier sans intérêt pour l'exemple ... -->
    
</BaseProduits>
BaseProduits.xsl
<?xml version="1.0" encoding="UCS-2"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

    <xsl:output  method='text' encoding='ISO-8859-1' />

    <xsl:variable name="livresAuCatalogue" 
                  select="/BaseProduits/LesProduits/Livre"/>
    
    <xsl:template match="Oeuvre">
    
        <xsl:variable name="oeuvreCourante" select="."/>
        
        <xsl:for-each select="$livresAuCatalogue">
            <xsl:if test="$oeuvreCourante/@ref = ./refOeuvre/Ref/@valeur">
                - <xsl:value-of select="$oeuvreCourante/Titre"/>
            </xsl:if>
        </xsl:for-each>
    </xsl:template>
    
    
    <xsl:template match="text()"/>
    
</xsl:stylesheet>
Résultat
                -  Vingt mille lieues sous les mers 
                -  Vous avez dit baroque ? 
                -  L'ingénieur aimait trop les chiffres 
BaseProduits.xsl
<?xml version="1.0" encoding="UTF-16"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

    <xsl:output  method='text' encoding='ISO-8859-1' />

    <xsl:variable name="lesOeuvres" select="BaseProduits/LesOeuvres/Oeuvre"/>
    
    <xsl:template match="Livre">
        <xsl:variable name="reference" select="./refOeuvre/Ref/@valeur"/>
        - <xsl:value-of 
            select="$lesOeuvres/Titre[ 
                parent::Oeuvre[ 
                    @ref = $reference 
                ] 
            ]"/>
    </xsl:template>
    
    <xsl:template match="text()"/>
    
</xsl:stylesheet>
<xsl:template match="Livre">
    - <xsl:value-of 
        select="/BaseProduits/LesOeuvres/Oeuvre/Titre[ 
            parent::Oeuvre[ 
                @ref = current()/refOeuvre/Ref/@valeur 
            ] 
        ]"/>
</xsl:template>

Temporary Source Tree

TST obtenu littéralement

<xsl:variable name="maison">
    <RDC>
        <cuisine surface='12m2'>
            Evier inox. Mobilier encastré.
        </cuisine>
        <WC>
            Lavabo. Cumulus 200L.
        </WC>
        <séjour surface='40m2'>
            Cheminée en pierre. Poutres au plafond. 
            Carrelage terre cuite. Grande baie vitrée.
        </séjour>
        <bureau surface='15m2'>
            Bibliothèque encastrée.
        </bureau>
        <jardin surface='150m2'>
            Palmier en zinc figurant le désert.
        </jardin>
        <garage/>
    </RDC>
</xsl:variable>
<xsl:variable name="instrument">
    viole de gambe
    <Interprète> Rika Murata </Interprète>
    <Interprète> Martin Bauer </Interprète>
    <Interprète> Sophie Watillon </Interprète>
</xsl:variable>

TST calculé

<xsl:variable name="mouvement">
    <insert>
        <Ensemble>
            <Nom><xsl:value-of select="NomEnsemble"/></Nom>
            <Direction><xsl:value-of select="Chef"/></Direction>
        </Ensemble>
        <Concert>
            <Date><xsl:value-of select="Date"/></Date>
            <Ville><xsl:value-of select="Ville"/></Ville>
            <Lieu><xsl:value-of select="Salle"/></Lieu>
            <Titre><xsl:value-of select="TitreConcert"/></Titre>
        </Concert>
    </insert>
</xsl:variable>

TST-texte

<xsl:variable name="instrument">saqueboute</xsl:variable>
<xsl:variable name="instrument" select="'saqueboute'"/>
<xsl:variable name="instrument" select="saqueboute"/> <!-- faux ! -->
<xsl:variable name="result">
    <xsl:value-of select='concat( $replacement, .)'/>
</xsl:variable>
<xsl:variable name="result">
    <xsl:variable name="begin" select="$fragments[position() = 1]"/>
    
    <xsl:choose>
        <xsl:when test="starts-with( $unTexte, $givenChar )">
            <xsl:value-of select="$replacement"/>
            <xsl:value-of select="$begin"/>
        </xsl:when>
        <xsl:otherwise>
            <xsl:value-of select="$begin"/>
        </xsl:otherwise>
    </xsl:choose>
    
    <xsl:for-each select="$fragments[position() &gt; 1]">
        <xsl:value-of select='concat( $replacement, .)'/>
    </xsl:for-each>
    
</xsl:variable>

Opérations sur un TST (XSLT¬1.1 ou plus)

Utilisation transparente d'un TST avec Saxon 6.5
<xsl:stylesheet 
   xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
   version="1.1">

Exemples d'utilisation d'un TST (XSLT1.1 ou plus)

Utilisation d'un TST obtenu littéralement (XSLT1.1 ou plus)
<xsl:variable name="entête">
    <hautDePage>
        <gauche>
            musique
        </gauche>
        <centre>
            Anacréon
        </centre>
        <droite>
            baroque
        </droite>
    </hautDePage>
</xsl:variable>
Concerts.xsl
<?xml version="1.0" encoding="UTF-16"?>
<xsl:stylesheet 
   xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
   version="1.1">

   <xsl:output  method='html' encoding='ISO-8859-1' />

   <xsl:variable name="entête">
      <hautDePage>
         <gauche>
            musique
         </gauche>
         <centre>
            Anacréon
         </centre>
         <droite>
            baroque
         </droite>
      </hautDePage>
   </xsl:variable>
    
   <xsl:template match="/">
      <html>
         <head>
            <title><xsl:value-of select="/Concert/Entête"/></title>
         </head>
         <body bgcolor="white" text="black">
         <TABLE valign="top" width="100%" height="2%" BORDER="0" >
            <TR>
            <TD align="left">
               <xsl:value-of 
               select="$entête/hautDePage/gauche"/>
            </TD>
            <TD  align="center">
               <xsl:value-of 
               select="$entête/hautDePage/centre"/>
            </TD>
         
            <TD  align="right">
               <xsl:value-of 
               select="$entête/hautDePage/droite"/>
            </TD>
         
            </TR>
         </TABLE>
         <xsl:apply-templates/>
         </body>
      </html>
   </xsl:template>
    
   <xsl:template match="Entête">
      <p> <xsl:value-of select="."/> présentent </p>
   </xsl:template>
   
   <xsl:template match="Date">
      <H1 align="center"> Concert du <xsl:value-of select="."/> </H1>
   </xsl:template>
   
   <xsl:template match="Lieu">
      <H4 align="center"> <xsl:value-of select="."/> </H4>
   </xsl:template>
   
   <xsl:template match="Ensemble">
      <H2 align="center"> Ensemble <xsl:value-of select="."/></H2>
   </xsl:template>
   
   <xsl:template match="Compositeurs">
      <H3 align="center"> Oeuvres de <br/> <xsl:value-of select="."/> </H3>
   </xsl:template>
     
   <xsl:template match="text()"/>
</xsl:stylesheet>
Concerts.xml
<?xml version="1.0" encoding="UTF-16" standalone="yes"?>

<Concert>
    
    <Entête> Les Concerts d'Anacréon </Entête>
    <Date>Jeudi 17 Janvier 2002, 20H30</Date>
    <Lieu>Chapelle des Ursules</Lieu>

    <Ensemble> "A deux violes esgales" </Ensemble>

    <Interprète>
        <Nom> Jonathan Dunford </Nom>
        <Instrument>Basse de viole</Instrument>
    </Interprète>

    <Interprète>
        <Nom> Sylvia Abramowicz </Nom>
        <Instrument>Basse de viole</Instrument>
    </Interprète>

    <Interprète>
        <Nom> Benjamin Perrot </Nom>
        <Instrument>Théorbe et Guitare baroque</Instrument>
    </Interprète>
    
    <TitreConcert>
        Folies d'Espagne et diminutions d'Italie
    </TitreConcert>
    
    <Compositeurs>
        <Compositeur>M. Marais</Compositeur>
        <Compositeur>D. Castello</Compositeur>
        <Compositeur>F. Rognoni</Compositeur>
    </Compositeurs>
    
</Concert>
Concerts.html
<html xmlns:xalan="http://xml.apache.org/xalan">
<head>
<META http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title> Les Concerts d&rsquo;Anacr&eacute;on </title>
</head>
<body text="black" bgcolor="white">
<TABLE BORDER="0" height="2%" width="100%" valign="top">
<TR>
<TD align="left">
                musique
            </TD><TD align="center">
                Anacr&eacute;on
            </TD><TD align="right">
                baroque
            </TD>
</TR>
</TABLE>
<H1 align="center"> Concert du Jeudi 17 Janvier 2002, 20H30</H1>
</body>
</html>
Utilisation d'un TST calculé (XSLT1.1 ou plus)
rawInsert.xml
<?xml version="1.0" encoding="UTF-16" standalone="yes"?>
<insertions>
    <Concert>
        <NomEnsemble>A deux violes esgales</NomEnsemble>
        <Chef>-</Chef>
        <Date>Jeudi 17 Janvier 2002, 20H30</Date>
        <Ville>Angers</Ville>
        <Salle>Chapelle des Ursules</Salle>
        <TitreConcert>
            Folies d'Espagne et diminutions d'Italie
        </TitreConcert>
    </Concert>
    <Concert>
        <NomEnsemble>La Cetra d'Orfeo</NomEnsemble>
        <Chef>Michel Keustermans</Chef>
        <Date>Mercredi 20 Mars 2002, 20H30</Date>
        <Ville>Bordeaux</Ville>
        <Salle>Théâtre</Salle>
        <TitreConcert>
            Habendmusiken
        </TitreConcert>
    </Concert>
    <Concert>
        <NomEnsemble>Suonare e cantare</NomEnsemble>
        <Chef>Jean Gaillard</Chef>
        <Date>Vendredi 26 Octobre 2001, 20H30</Date>
        <Ville>Nantes</Ville>
        <Salle>Musée des Beaux-Arts</Salle>
        <TitreConcert>
            Madrigali e altre musiche concertate
        </TitreConcert>
    </Concert>
</insertions>
insert.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<Mouvements>
   <insert>
      <Ensemble>
         <Nom>A deux violes esgales</Nom>
         <Direction>-</Direction>
      </Ensemble>
      <Concert>
         <Date>Jeudi 17 Janvier 2002, 20H30</Date>
         <Ville>Angers</Ville>
         <Lieu>Chapelle des Ursules</Lieu>
         <Titre>
            Folies d'Espagne et diminutions d'Italie
        </Titre>
      </Concert>
   </insert>
   <insert>
      <Ensemble>
         <Nom>La Cetra d'Orfeo</Nom>
         <Direction>Michel Keustermans</Direction>
      </Ensemble>
      <Concert>
         <Date>Mercredi 20 Mars 2002, 20H30</Date>
         <Ville>Bordeaux</Ville>
         <Lieu>Théâtre</Lieu>
         <Titre>
            Habendmusiken
        </Titre>
      </Concert>
   </insert>
   <insert>
      <Ensemble>
         <Nom>Suonare e cantare</Nom>
         <Direction>Jean Gaillard</Direction>
      </Ensemble>
      <Concert>
         <Date>Vendredi 26 Octobre 2001, 20H30</Date>
         <Ville>Nantes</Ville>
         <Lieu>Musée des Beaux-Arts</Lieu>
         <Titre>
            Madrigali e altre musiche concertate
        </Titre>
      </Concert>
   </insert>
</Mouvements>
prepareInsert.xsl
<?xml version="1.0" encoding="UTF-16"?>
<xsl:stylesheet 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    version="1.0">

    <xsl:output  method='xml' encoding='ISO-8859-1'  />
  
    <xsl:template match="/">
        <Mouvements>
        <xsl:apply-templates/>
        </Mouvements>
    </xsl:template>
    
    <xsl:template match="Concert">
        <xsl:variable name="mouvement">
            <insert>
                <Ensemble>
                    <Nom><xsl:value-of select="NomEnsemble"/></Nom>
                    <Direction><xsl:value-of select="Chef"/></Direction>
                </Ensemble>
                <Concert>
                    <Date><xsl:value-of select="Date"/></Date>
                    <Ville><xsl:value-of select="Ville"/></Ville>
                    <Lieu><xsl:value-of select="Salle"/></Lieu>
                    <Titre><xsl:value-of select="TitreConcert"/></Titre>
                </Concert>
            </insert>
        </xsl:variable>
        <xsl:copy-of select="$mouvement"/>
    </xsl:template>
        
    <xsl:template match="text()"/>
</xsl:stylesheet>
insert.xml
<?xml version="1.0" encoding="ISO-8859-1"?><Mouvements><insert><Ensemble><Nom>A deux violes esgales</Nom><Direction>-</Direction></Ensemble><Concert><Date>Jeudi 17 Janvier 2002, 20H30</Date><Ville>Angers</Ville><Lieu>Chapelle des Ursules</Lieu><Titre>
            Folies d'Espagne et diminutions d'Italie
        </Titre></Concert></insert><insert><Ensemble><Nom>La Cetra d'Orfeo</Nom><Direction>Michel Keustermans</Direction></Ensemble><Concert><Date>Mercredi 20 Mars 2002, 20H30</Date><Ville>Bordeaux</Ville><Lieu>Théâtre</Lieu><Titre>
            Habendmusiken
        </Titre></Concert></insert><insert><Ensemble><Nom>Suonare e cantare</Nom><Direction>Jean Gaillard</Direction></Ensemble><Concert><Date>Vendredi 26 Octobre 2001, 20H30</Date><Ville>Nantes</Ville><Lieu>Musée des Beaux-Arts</Lieu><Titre>
            Madrigali e altre musiche concertate
        </Titre></Concert></insert></Mouvements>
<xsl:copy-of select="$mouvement/insert/Ensemble"/>
<xsl:copy-of select="$mouvement"/>
Utilisation d'un TST-texte pour le calcul de la valeur par défaut d'un attribut
produits.xml
<?xml version="1.0" encoding="UTF-16"?>
<LesProduits>
    <Livre ref="vernes1" NoISBN="193335" gamme="roman" media="papier">
        <refOeuvre>
            <Ref valeur="20000lslm"/>
        </refOeuvre>
        <Prix valeur="40.5" monnaie="FF"/>
        <Prix valeur="5" monnaie="£"/>
    </Livre>

    <Livre ref="boileaunarcejac1" NoISBN="533791" gamme="roman" media="papier">
        <refOeuvre>
            <Ref valeur="liatlc.bn"/>
        </refOeuvre>
        <Prix valeur="30"/>
    </Livre>
</LesProduits>
produits.xsl
<?xml version="1.0" encoding="UTF-16"?>
<xsl:stylesheet 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    version="1.0">

    <xsl:output  method='text' encoding='ISO-8859-1' />
  
    <xsl:template match="Livre">
        <xsl:variable name="monnaie">
            <xsl:choose>
                <xsl:when test="Prix/@monnaie">
                    <xsl:value-of select="Prix/@monnaie"/>
                </xsl:when>
                <xsl:otherwise>FF</xsl:otherwise>
            </xsl:choose>
        </xsl:variable>
        --
        ref: <xsl:value-of select="@ref"/> 
        prix: <xsl:value-of 
              select="Prix/@valeur"/> <xsl:value-of select="$monnaie"/>
    </xsl:template>
    
        
    <xsl:template match="text()"/>
</xsl:stylesheet>
Résultat
        --
        ref: vernes1 
        prix: 40.5FF
        --
        ref: boileaunarcejac1 
        prix: 30FF

Result Tree Fragment (XSLT 1.0)

<xsl:variable name="maison">
    <RDC>
        <cuisine surface='12m2'>
            Evier inox. Mobilier encastré.
        </cuisine>
        <WC>
            Lavabo. Cumulus 200L.
        </WC>
        <séjour surface='40m2'>
            Cheminée en pierre. Poutres au plafond. 
            Carrelage terre cuite. Grande baie vitrée.
        </séjour>
        <bureau surface='15m2'>
            Bibliothèque encastrée.
        </bureau>
        <jardin surface='150m2'>
            Palmier en zinc figurant le désert.
        </jardin>
        <garage/>
    </RDC>
</xsl:variable>

Opérations sur un RTF (XSLT 1.0)

Concerts.xsl
<?xml version="1.0" encoding="UCS-2"?>
<xsl:stylesheet 
   xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
   xmlns:xalan="http://xml.apache.org/xalan"
   version="1.0">

   <xsl:output  method='html' encoding='ISO-8859-1' />

   <xsl:variable name="entête">
      <hautDePage>
         <gauche>
            musique
         </gauche>
         <centre>
            Anacréon
         </centre>
         <droite>
            baroque
         </droite>
      </hautDePage>
   </xsl:variable>
    
   <xsl:template match="/">
      <html>
         <head>
            <title><xsl:value-of select="/Concert/Entête"/></title>
         </head>
         <body bgcolor="white" text="black">
         <TABLE valign="top" width="100%" height="2%" BORDER="0" >
            <TR>
            <TD align="left">
               <xsl:value-of 
               select="xalan:nodeset($entête)/hautDePage/gauche"/>
            </TD>
            <TD  align="center">
               <xsl:value-of 
               select="xalan:nodeset($entête)/hautDePage/centre"/>
            </TD>
         
            <TD  align="right">
               <xsl:value-of 
               select="xalan:nodeset($entête)/hautDePage/droite"/>
            </TD>
         
            </TR>
         </TABLE>
         <xsl:apply-templates/>
         </body>
      </html>
   </xsl:template>
    
   <xsl:template match="Entête">
      <p> <xsl:value-of select="."/> présentent </p>
   </xsl:template>
   
   <xsl:template match="Date">
      <H1 align="center"> Concert du <xsl:value-of select="."/> </H1>
   </xsl:template>
   
   <xsl:template match="Lieu">
      <H4 align="center"> <xsl:value-of select="."/> </H4>
   </xsl:template>
   
   <xsl:template match="Ensemble">
      <H2 align="center"> Ensemble <xsl:value-of select="."/></H2>
   </xsl:template>
   
   <xsl:template match="Compositeurs">
      <H3 align="center"> Oeuvres de <br/> <xsl:value-of select="."/> </H3>
   </xsl:template>
     
   <xsl:template match="text()"/>
</xsl:stylesheet>

Règles de visibilité

Règles de visibilité pour les variables globales

<?xml version="1.0" encoding="UCS-2"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

    <xsl:output  method='text' encoding='ISO-8859-1' />

    <xsl:variable name="valeur">
        <xsl:value-of select="$valeur"/> <!-- interdit ! -->
    </xsl:variable>
    
    <xsl:template match="TitreConcert" >
        ...
    </xsl:template>
    
    <xsl:template match="Concert">
        ...
    </xsl:template>

</xsl:stylesheet>
<?xml version="1.0" encoding="UCS-2"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

    <xsl:output  method='text' encoding='ISO-8859-1' />

    <xsl:variable name="truc">
        <xsl:apply-templates />
    </xsl:variable>
    
    <xsl:template match="TitreConcert" >
        ...
    </xsl:template>
    
    <xsl:template match="Concert">
        <xsl:value-of select="$valeur"/>
    </xsl:template>
    
</xsl:stylesheet>
<?xml version="1.0" encoding="UCS-2"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

   <xsl:output  method='text' encoding='ISO-8859-1' />

   <xsl:variable name="amorce">
      <xsl:apply-templates />
   </xsl:variable>
   
    <xsl:template match="$amorce/child::Interprète/child::Nom" >
    ...
    </xsl:template>
    
    <xsl:template match="..." >
    ...
    </xsl:template>
    
</xsl:stylesheet>

Règles de visibilité pour les variables locales

Conflits de noms de variables

Instruction xsl:param

Bande-annonce

Début d'une feuille de style
<?xml version="1.0" encoding="UTF-16"?>
<xsl:stylesheet 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    version="1.0">

    <xsl:output  method='text' encoding='ISO-8859-1' />
  
    <xsl:param name="monnaieUsuelle" select="'FF'"/>
    
    ...
        <xsl:value-of select="$monnaieUsuelle"/>
    ...
Lancement du processeur XSLT
java com.icl.saxon.StyleSheet 
                    -o produits.txt produits.xml produits.xsl monnaieUsuelle=DM

Syntaxe

xsl:param
<xsl:param name="..." select=" ... expression XPath ... "/>
xsl:param
<xsl:param name="...">
    <!-- modèle de transformation -->
    ... texte ou instructions XSLT ...
    <!-- fin du modèle de transformation -->
</xsl:param>

Sémantique

Utilisation d'un paramètre global

produits.xsl
<?xml version="1.0" encoding="UTF-16"?>
<xsl:stylesheet 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    version="1.0">

    <xsl:output  method='text' encoding='ISO-8859-1' />
  
    <xsl:param name="monnaieUsuelle" select="'FF'"/>
  
    <xsl:template match="Livre">
        <xsl:variable name="monnaie">
            <xsl:choose>
                <xsl:when test="Prix/@monnaie">
                    <xsl:value-of select="Prix/@monnaie"/>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:value-of select="$monnaieUsuelle"/>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:variable>
        --
        ref: <xsl:value-of select="@ref"/> 
        prix: <xsl:value-of 
              select="Prix/@valeur"/> <xsl:value-of select="$monnaie"/>
    </xsl:template>
    
        
    <xsl:template match="text()"/>
</xsl:stylesheet>

Résultat
        --
        ref: vernes1 
        prix: 40.5FF
        --
        ref: boileaunarcejac1 
        prix: 30FF
Using Saxon from the Command Line
java com.icl.saxon.StyleSheet [options] source-document stylesheet [params...]
    
The options must come first, then the two file names, then the params.

A param takes the form name=value, name being the name of the parameter, and value the value of the parameter. These parameters are accessible within the stylesheet as normal variables, using the $name syntax, provided they are declared using a top-level xsl:param element. If there is no such declaration, the supplied parameter value is silently ignored.
Ligne de commande
java com.icl.saxon.StyleSheet -o produits.txt produits.xml produits.xsl monnaieUsuelle=DM
Résultat
        --
        ref: vernes1 
        prix: 40.5FF
        --
        ref: boileaunarcejac1 
        prix: 30DM
Ligne de commande
java org.apache.xalan.xslt.Process -IN produits.xml -XSL produits.xsl -OUT produits.txt -PARAM monnaieUsuelle DM

Utilisation d'un paramètre local

Instruction xsl:template

Bande-annonce

Nom.xsl
<?xml version="1.0" encoding="UTF-16"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

    <xsl:output  method='text' encoding='ISO-8859-1' />
    
    <xsl:template name="instancierTexteAvecTiret"> 
        <xsl:param name="texte"/>
        - <xsl:value-of select="$texte" />
    </xsl:template>
    
    <xsl:template match="Nom">
        <xsl:call-template name="instancierTexteAvecTiret">
            <xsl:with-param name="texte" select="."/>
        </xsl:call-template>
    </xsl:template>
    
    <xsl:template match="text()"/>
    
</xsl:stylesheet>
Concert.xml
<?xml version="1.0" encoding="UTF-16" standalone="yes"?>

<Concert>

    <Entête> Les Concerts d'Anacréon </Entête>
    <Date>Jeudi 17 Janvier 2002, 20H30</Date>
    <Lieu>Chapelle des Ursules</Lieu>

    <Ensemble> "A deux violes esgales" </Ensemble>

    <Interprète>
        <Nom> Jonathan Dunford </Nom>
        <Instrument>Basse de viole</Instrument>
    </Interprète>

    <Interprète>
        <Nom> Sylvia Abramowicz </Nom>
        <Instrument>Basse de viole</Instrument>
    </Interprète>

    <Interprète>
        <Nom> Benjamin Perrot </Nom>
        <Instrument>Théorbe et Guitare baroque</Instrument>
    </Interprète>
    
    <TitreConcert>
        Folies d'Espagne et diminutions d'Italie
    </TitreConcert>
    
    <Compositeurs>
        <Compositeur>M. Marais</Compositeur>
        <Compositeur>D. Castello</Compositeur>
        <Compositeur>F. Rognoni</Compositeur>
    </Compositeurs>
    
</Concert>
Résultat
        -  Jonathan Dunford 
        -  Sylvia Abramowicz 
        -  Benjamin Perrot 

Syntaxe

Modèle nommé xsl:template
<xsl:template name="...">
    <!-- arguments formels du modèle nommé -->
    ...
    <!-- fin des arguments formels du modèle nommé -->
    <!-- corps du modèle de transformation -->
    ... instructions ...
    <!-- fin du corps du modèle de transformation -->
</xsl:template>
Argument formel du modèle nommé
<xsl:param name="..."/>
Argument formel du modèle nommé (avec valeur par défaut)
<xsl:param name="..." select="...expression XPath.../>
Variante d'argument formel du modèle nommé (avec valeur par défaut)
<xsl:param name="...">
    <!-- modèle de transformation du paramètre -->
    ...
    <!-- fin du modèle de transformation du paramètre -->
</xsl:param>

Modèle nommé typique

<xsl:template name="...">
    <xsl:param name="truc"/>
    <xsl:param name="machin"/>
    <!-- corps du modèle de transformation -->
    ... référence à $truc ... référence à $machin ...
    <!-- fin du corps du modèle de transformation -->
</xsl:template>

Sémantique

Exemple

annonces.xml
<?xml version="1.0" encoding="UTF-16" standalone="yes"?>

<Annonces>
    <Annonce>
        
        <Entête> "Les Concerts d'Anacréon" </Entête>
        <Date>
            <Jour id="jeu"/>
            <Quantième>17</Quantième>
            <Mois id="jnv"/>
            <Année>2002</Année>
            <Heure>20H30</Heure>
        </Date>
        <Lieu>Chapelle des Ursules</Lieu>
    
        <Ensemble> "A deux violes esgales" </Ensemble>
    
        <Interprète>
            <Nom> Jonathan Dunford </Nom>
            <Instrument id="bvl"/>
        </Interprète>
    
        <Interprète>
            <Nom> Sylvia Abramowicz </Nom>
            <Instrument id="bvl"/>
        </Interprète>
    
        <Interprète>
            <Nom> Benjamin Perrot </Nom>
            <Instrument id="thb"/>
        </Interprète>
    
        <Interprète>
            <Nom> Freddy Eichelberger </Nom>
            <Instrument id="clv"/>
        </Interprète>
        
        <Concert>
            <TitreConcert lang="fr">
                Folies d'Espagne, Bourrasque et Tourbillon
                Gavotte La Ferme - Chaconne de Rougeville
            </TitreConcert>
            <TitreConcert lang="en">
                Spanish folias, Squall and Whirlwind
                Gavotte Shut Up - Redtown's Sillycate
            </TitreConcert>
        </Concert>
        
        <Compositeurs>
            <Compositeur>Marin Marais</Compositeur>
            <Compositeur>Jean de Sainte Colombe</Compositeur>
        </Compositeurs>
        
    </Annonce>
    
    <!-- autres annonces ... -->
    
</Annonces>
annonces.xsl
<?xml version="1.0" encoding="UTF-16"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
                version="1.1"> <!-- compatibilité Saxon 6.5 -->

   <xsl:output  method='text' encoding='ISO-8859-1' />
   
   <xsl:param name="langueCible">fr</xsl:param>
   
   <xsl:variable name="Dictionnaire">

      <mot id="bvl">
         <traduction lang="fr">Basse de viole</traduction>
         <traduction lang="en">Bass viol</traduction>
      </mot>
      
      <mot id="vdg">
         <traduction lang="fr">Viole de gambe</traduction>
         <traduction lang="en">Viola da gamba</traduction>
      </mot>
      
      <mot id="lth">
         <traduction lang="fr">Luth</traduction>
         <traduction lang="en">Lute</traduction>
      </mot>
      
      <mot id="clv">
         <traduction lang="fr">Clavecin</traduction>
         <traduction lang="en">Harpsichord</traduction>
      </mot>
      
      <mot id="flt">
         <traduction lang="fr">Flûte à bec</traduction>
         <traduction lang="en">Recorder</traduction>
      </mot>
      
      <mot id="thb">
         <traduction lang="fr">Théorbe</traduction>
         <traduction lang="en">Theorbo</traduction>
      </mot>
      
      <mot id="lun">
         <traduction lang="fr">lundi</traduction>
         <traduction lang="en">monday</traduction>
      </mot>
      
      <!-- etc. (les autres jours de la semaine) -->
      
      <mot id="dim">
         <traduction lang="fr">dimanche</traduction>
         <traduction lang="en">sunday</traduction>
      </mot>
      
      <mot id="jnv">
         <traduction lang="fr">janvier</traduction>
         <traduction lang="en">january</traduction>
      </mot>
      
      <!-- etc. (les autres mois de l'année) -->
      
      <mot id="dcb">
         <traduction lang="fr">décembre</traduction>
         <traduction lang="en">december</traduction>
      </mot>
      
   </xsl:variable>
   
   <xsl:template name="traduction">
      <xsl:param name="motId"/>
      
      <xsl:variable 
         name="motTrouvé" 
         select="$Dictionnaire/mot[@id=$motId]" />
         
      <xsl:variable 
         name="saTraduction" 
         select="$motTrouvé/traduction[@lang=$langueCible]" />
         
      <xsl:value-of select="$saTraduction" />
   </xsl:template>
   
   <xsl:template match="Date">
      <xsl:call-template name="traduction">
         <xsl:with-param name="motId" select="./Mois/@id" />
      </xsl:call-template> 
   </xsl:template>
   
   <xsl:template match="Interprète">
         
         -<xsl:value-of select="./Nom"/> :
               <xsl:call-template name="traduction">
                  <xsl:with-param name="motId" select="./Instrument/@id" />
               </xsl:call-template>
   </xsl:template>
   
   <xsl:template match="text()" />
   
</xsl:stylesheet>
annonces.xsl
<?xml version="1.0" encoding="UTF-16"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
                version="1.0">

    <xsl:output  method='text' encoding='ISO-8859-1' />
    
    <xsl:param name="langueCible">fr</xsl:param>
    <xsl:param name="dicoFileRef">dictionnaire.xml</xsl:param>
    
    <xsl:variable name="Dictionnaire" 
                  select="document($dicoFileRef)/Dictionnaire"/>
        
    <xsl:template name="traduction">
        <xsl:param name="motId"/>
        
        <xsl:variable 
            name="motTrouvé" 
            select="$Dictionnaire/mot[@id=$motId]" />
            
        <xsl:variable 
            name="saTraduction" 
            select="$motTrouvé/traduction[@lang=$langueCible]" />
            
        <xsl:value-of select="$saTraduction" />
    </xsl:template>
    
    <xsl:template match="Date">
         <xsl:call-template name="traduction">
            <xsl:with-param name="motId" select="./Mois/@id" />
        </xsl:call-template> 
    </xsl:template>
    
    <xsl:template match="Interprète">
            
            -<xsl:value-of select="./Nom"/> :
                <xsl:call-template name="traduction">
                    <xsl:with-param name="motId" select="./Instrument/@id"/>
                </xsl:call-template>
    </xsl:template>
    
    <xsl:template match="text()" />
    
</xsl:stylesheet>

dictionnaire.xml
<?xml version="1.0" encoding="UTF-16" standalone="yes"?>

<Dictionnaire>
    <mot id="bvl">
        <traduction lang="fr">Basse de viole</traduction>
        <traduction lang="en">Bass viol</traduction>
    </mot>
    
    <mot id="vdg">
        <traduction lang="fr">Viole de gambe</traduction>
        <traduction lang="en">Viola da gamba</traduction>
    </mot>
    
    <!-- etc. ... (comme avant) -->
    
    <mot id="dcb">
        <traduction lang="fr">décembre</traduction>
        <traduction lang="en">december</traduction>
    </mot>
</Dictionnaire>

Instruction xsl:call-template

Bande-annonce

Syntaxe

xsl:call-template
<xsl:call-template name="...">
    <!-- arguments effectifs pour l'appel -->
    ...
    <!-- fin des arguments effectifs pour l'appel -->
</xsl:call-template>/>
Argument effectif
<xsl:with-param name="..." select="... expression XPath ..." />
Variante d'argument effectif
<xsl:with-param name="...">
    <!-- modèle de transformation de l'argument effectif -->
    ...
    <!-- fin du modèle de transformation de l'argument effectif -->
</xsl:with-param>

Sémantique

Exemple

Ligne de commande
java com.icl.saxon.StyleSheet -o annonces.txt annonces.xml annonces.xsl 
Résultat
janvier
            
            - Jonathan Dunford  :
                    Basse de viole
            
            - Sylvia Abramowicz  :
                    Basse de viole
            
            - Benjamin Perrot  :
                    Théorbe
            
            - Freddy Eichelberger  :
                    Clavecin
Ligne de commande (une seule ligne d'un seul tenant)
java com.icl.saxon.StyleSheet -o annonces.txt annonces.xml annonces.xsl langueCible=en
Résultat
january
            
            - Jonathan Dunford  :
                    Bass viol
            
            - Sylvia Abramowicz  :
                    Bass viol
            
            - Benjamin Perrot  :
                    Theorbo
            
            - Freddy Eichelberger  :
                    Harpsichord

Instruction xsl:apply-templates

Syntaxe

xsl:apply-templates
<xsl:apply-templates select="..." mode="...">
    <!-- arguments effectifs pour l'appel -->
    ...
    <!-- fin des arguments effectifs pour l'appel -->
</xsl:apply-templates>/>
Argument effectif
<xsl:with-param name="..." select="... expression XPath ..." />
Variante d'argument effectif
<xsl:with-param name="...">
    <!-- modèle de transformation de l'argument effectif -->
    ...
    <!-- fin du modèle de transformation de l'argument effectif -->
</xsl:with-param>

Sémantique

Instruction xsl:message

Syntaxe

xsl:message
<xsl:message>
    <!-- modèle de transformation -->
    ...
    <!-- fin du modèle de transformation -->
</xsl:message>/>
xsl:message (variante syntaxique)
<xsl:message terminate="yes">
    <!-- modèle de transformation -->
    ...
    <!-- fin du modèle de transformation -->
</xsl:message>/>

Sémantique

Instruction xsl:key

Bande-annonce

Dictionnaire.xml
<?xml version="1.0" encoding="UTF-16" standalone="yes"?>
<Dictionnaire>
    <mot id="dim">
        <traduction lang="fr">dimanche</traduction>
        <traduction lang="en">sunday</traduction>
    </mot>
    <mot id="lun">
        <traduction lang="fr">lundi</traduction>
        <traduction lang="en">monday</traduction>
    </mot>
</Dictionnaire>
Déclaration de clé
<xsl:key name="traduction" match="mot" use="attribute::id" />

Syntaxe

xsl:key
<xsl:key name="..." match="...motif..." use="...expression..."/>

Sémantique

Construction et exploitation de la table associative

Une association d'une valeur à un nœud
Déclaration de clé
<xsl:key name="traduction" match="mot" use="attribute::id" />
Dictionnaire.xml
<?xml version="1.0" encoding="UTF-16"?>
<Dictionnaire>
    <mot id="dim">
        <traduction lang="fr">dimanche</traduction>
        <traduction lang="en">sunday</traduction>
    </mot>
</Dictionnaire>
traduction.xsl
<?xml version="1.0" encoding="UTF-16"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

    <xsl:output  method='text' encoding='ISO-8859-1' />

    <xsl:key name="traduction" match="mot" use="attribute::id" />
    
    <xsl:template name="afficher">
        <xsl:param name="code"/>
        <xsl:variable name="mot" select="key('traduction', $code)"/>
        
        <xsl:for-each select="$mot/traduction">
            <xsl:value-of select="$code"/><xsl:text> ( </xsl:text>
            <xsl:value-of select="@lang"/><xsl:text> ) : </xsl:text> 
            <xsl:value-of select="."/>
            <xsl:text>
</xsl:text>
        </xsl:for-each>
   </xsl:template>
    
    <xsl:template match="/">
        <xsl:call-template name="afficher">
            <xsl:with-param name="code" select="'dim'"/>
        </xsl:call-template>
    </xsl:template>
    
    <xsl:template match="text()"/>
    
</xsl:stylesheet>
Résultat
dim ( fr ) : dimanche
dim ( en ) : sunday
Deux associations, avec une valeur et un nœud par association
Dictionnaire.xml
<?xml version="1.0" encoding="UTF-16" standalone="yes"?>
<Dictionnaire>
    <mot id="dim">
        <traduction lang="fr">dimanche</traduction>
        <traduction lang="en">sunday</traduction>
    </mot>
    <mot id="lun">
        <traduction lang="fr">lundi</traduction>
        <traduction lang="en">monday</traduction>
    </mot>
</Dictionnaire>
traduction.xsl
    ... idem ...
    <xsl:template match="/">
        <xsl:call-template name="afficher">
            <xsl:with-param name="code" select="'dim'"/>
        </xsl:call-template>
        <xsl:call-template name="afficher">
            <xsl:with-param name="code" select="'lun'"/>
        </xsl:call-template>
    </xsl:template>
    ... idem ...
Résultat
dim ( fr ) : dimanche
dim ( en ) : sunday
lun ( fr ) : lundi
lun ( en ) : monday
Deux associations, avec deux valeurs et un nœud par association
Déclaration de clé
<xsl:key name="traduction" match="mot" use="traduction" />
./child::traduction
où "." est le <mot> courant.
traduction.xsl
<?xml version="1.0" encoding="UTF-16"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

    <xsl:output  method='text' encoding='ISO-8859-1' />

    <xsl:key name="traduction" match="mot" use="traduction" />
    
    <xsl:template name="afficher">
        <xsl:param name="unMot"/>
        <xsl:variable name="motTrouvé" select="key('traduction', $unMot)"/>
        <xsl:variable name="sonCode" select="$motTrouvé/@id"/>
        
        <xsl:for-each select="$motTrouvé/traduction">
            <xsl:value-of select="$sonCode"/><xsl:text> ( </xsl:text>
            <xsl:value-of select="@lang"/><xsl:text> ) : </xsl:text> 
            <xsl:value-of select="."/>
            <xsl:text>
</xsl:text>
        </xsl:for-each>
   </xsl:template>
    
    <xsl:template match="/">
        <xsl:call-template name="afficher">
            <xsl:with-param name="unMot" select="'dimanche'"/>
        </xsl:call-template>
        <xsl:call-template name="afficher">
            <xsl:with-param name="unMot" select="'monday'"/>
        </xsl:call-template>
    </xsl:template>
    
    <xsl:template match="text()"/>
    
</xsl:stylesheet>
Résultat
dim ( fr ) : dimanche
dim ( en ) : sunday
lun ( fr ) : lundi
lun ( en ) : monday
Deux associations, avec une valeur et deux nœuds par association
Déclaration de clé
<xsl:key name="traduction" match="traduction" use="@lang" />
traduction.xsl
<?xml version="1.0" encoding="UTF-16"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

    <xsl:output  method='text' encoding='ISO-8859-1' />

    <xsl:key name="traduction" match="traduction" use="@lang" />
    
    <xsl:template name="afficher">
        <xsl:param name="langue"/>
        <xsl:variable name="traductionsTrouvées" select="key('traduction', $langue)"/>
        
        <xsl:text> 
</xsl:text>
        <xsl:text>Langue = </xsl:text><xsl:value-of select="$langue"/><xsl:text> : 
    </xsl:text>
        
        <xsl:for-each select="$traductionsTrouvées">
            <xsl:value-of select="."/> 
            <xsl:text> ( code : </xsl:text>
            <xsl:value-of select="../@id"/>  
            <xsl:text> ) </xsl:text>
            <xsl:text>
    </xsl:text>
        </xsl:for-each>
    </xsl:template>
    
    <xsl:template match="/">
        <xsl:call-template name="afficher">
            <xsl:with-param name="langue" select="'fr'"/>
        </xsl:call-template>
        <xsl:call-template name="afficher">
            <xsl:with-param name="langue" select="'en'"/>
        </xsl:call-template>
    </xsl:template>
    
    <xsl:template match="text()"/>
    
</xsl:stylesheet>
Résultat
 
Langue = fr : 
    dimanche ( code : dim ) 
    lundi ( code : lun ) 
     
Langue = en : 
    sunday ( code : dim ) 
    monday ( code : lun ) 
    
Construction d'une table à partir d'un document XML auxiliaire
traduction.xsl
<?xml version="1.0" encoding="UTF-16"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

    <xsl:output  method='text' encoding='ISO-8859-1' />

    <xsl:key name="traduction" match="mot" use="attribute::id" />
    
    <xsl:template name="afficher">
        <xsl:param name="code"/>
        
        <xsl:for-each select="document('Dictionnaire.xml')">
            <xsl:variable name="mot" select="key('traduction', $code)"/>
            
            <xsl:for-each select="$mot/traduction">
                <xsl:value-of select="$code"/><xsl:text> ( </xsl:text>
                <xsl:value-of select="@lang"/><xsl:text> ) : </xsl:text> 
                <xsl:value-of select="."/>
                <xsl:text>
</xsl:text>
            </xsl:for-each>
        </xsl:for-each>
   </xsl:template>
    
    <xsl:template match="/">
        <xsl:call-template name="afficher">
            <xsl:with-param name="code" select="'dim'"/>
        </xsl:call-template>
        <xsl:call-template name="afficher">
            <xsl:with-param name="code" select="'lun'"/>
        </xsl:call-template>
    </xsl:template>
    
    <xsl:template match="text()"/>
    
</xsl:stylesheet>
Résultat
dim ( fr ) : dimanche
dim ( en ) : sunday
lun ( fr ) : lundi
lun ( en ) : monday

Variante

Exemples

preceding-sibling::*[1] Table following-sibling::*[1]