Les solutions aux exercices XSLT

Solution au exercice 1a:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output encoding="iso-8859-1" version="1.0"/>
  <xsl:template match="recueilPoemes">
    <html>
      <body><h1>Recueil de poèmes</h1> 
        <xsl:apply-templates/>
      </body>
    </html>
  </xsl:template>
  <xsl:template match="auteur">
    <br/>
    <h4>
      <xsl:apply-templates/>
    </h4>
  </xsl:template>
  <xsl:template match="prenom">
    <xsl:apply-templates/>
    <xsl:text> </xsl:text>
  </xsl:template>
  <xsl:template match="nomFamille">
    <xsl:apply-templates/>
  </xsl:template>
  <xsl:template match="titre">
    <h2>
      <xsl:apply-templates/>
    </h2>
  </xsl:template>
  <xsl:template match="strophe">
    <p>
      <xsl:apply-templates/>
    </p>
  </xsl:template>
  <xsl:template match="vers">
    <xsl:apply-templates/>
    <br/>
  </xsl:template>
  <xsl:template match="poeme">
    <xsl:apply-templates/>
  </xsl:template>
  <xsl:template match="gras">
    <b>
      <xsl:apply-templates/>
    </b>
  </xsl:template>
  <xsl:template match="sous-titre">
    <h4>
      <xsl:apply-templates/>
    </h4>
  </xsl:template>
</xsl:stylesheet>

Solution au exercice 1b:

Avec l'aide d'élément <xsl:apply-templates> plus de templates sont cherchés qui s'appliquent aux sous-éléments. Au cas où aucun template est trouvé, le contenu (texte) sera copié.

Solution au exercice 1c:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output encoding="iso-8859-1" version="1.0"/>
  <xsl:template match="recueilPoemes">
    <html>
      <body><h1>Recueil de poèmes</h1> 
        <xsl:apply-templates/>
      </body>
    </html>
  </xsl:template>
  <xsl:template match="auteur">
    <br/>
    <h4><!-- VotrePrenom VotreNomFamille --></h4>
  </xsl:template>
  <xsl:template match="prenom">
    <xsl:apply-templates/>
    <xsl:text> </xsl:text>
  </xsl:template>
  <xsl:template match="nomFamille">
    <xsl:apply-templates/>
  </xsl:template>
  <xsl:template match="titre">
    <h2>
      <xsl:apply-templates/>
    </h2>
  </xsl:template>
  <xsl:template match="strophe">
    <p>
      <xsl:apply-templates/>
    </p>
  </xsl:template>
  <xsl:template match="vers">
    <xsl:apply-templates/>
    <br/>
  </xsl:template>
  <xsl:template match="poeme">
    <xsl:apply-templates/>
  </xsl:template>
  <xsl:template match="gras">
    <b>
      <xsl:apply-templates/>
    </b>
  </xsl:template>
  <xsl:template match="sous-titre">
    <h4>
      <xsl:apply-templates/>
    </h4>
  </xsl:template>
</xsl:stylesheet>

Solution au exercice 2a:

Avec l'attribut select seulement une certaine part de l'arbre a été sélectionnée. Tous les autres sous-éléments sont ignorés.

Solution au exercice 2b:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output encoding="iso-8859-1" version="1.0"/>
  <xsl:template match="recueilPoemes">
    <html>
      <body>
        <h1>Recueil de poèmes</h1>
        <p>
          <xsl:call-template name="titre"/>
          <xsl:call-template name="nom"/>
        </p>
      </body>
    </html>
  </xsl:template>
  <xsl:template name="titre">
    <br/>
Titre: <xsl:apply-templates select="poeme/titre"/>
  </xsl:template>
  <xsl:template match="titre">
    <h4>
      <xsl:apply-templates/>
    </h4>
  </xsl:template>
  <xsl:template name="nom">
    <br/>
Auteur: <xsl:apply-templates select="poeme/auteur/prenom | poeme/auteur/nomFamille"/>
  </xsl:template>
  <xsl:template match="titre">
    <h4>
      <xsl:apply-templates/>
    </h4>
  </xsl:template>
  <xsl:template match="prenom">
    <br/>
    <xsl:apply-templates/>
    <xsl:text> </xsl:text>
  </xsl:template>
  <xsl:template match="nomFamille">
    <xsl:apply-templates/>
  </xsl:template>
</xsl:stylesheet>

Solution au exercice 2c:

Éléments qui ne sont pas représentés dans la liste des éléments sélectionnés ne sont pas adopté au document cible.

Solution au exercice 3:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output encoding="iso-8859-1" version="1.0"/>
  <xsl:template match="recueilPoemes">
    <html>
      <body>
        <xsl:apply-templates/>
      </body>
    </html>
  </xsl:template>
  <xsl:template match="auteur">
    <br/>
    <h4>
      <xsl:apply-templates/>
    </h4>
  </xsl:template>
  <xsl:template match="prenom">
    <xsl:apply-templates/>
    <xsl:text> </xsl:text>
  </xsl:template>
  <xsl:template match="nomFamille">
    <xsl:apply-templates/>
  </xsl:template>
  <xsl:template match="titre">
    <h2>
      <xsl:apply-templates/>
    </h2>
  </xsl:template>
  <xsl:template match="strophe">
    <p>
      <xsl:apply-templates/>
    </p>
  </xsl:template>
  <xsl:template match="vers">
    <xsl:apply-templates/>
    <br/>
  </xsl:template>
  <xsl:template match="poeme">
  Année de parution: 
    <xsl:value-of select="@anneeParution"/>
    <br/><xsl:apply-templates/>
  </xsl:template>
</xsl:stylesheet>

Solution au exercice 4:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output encoding="iso-8859-1" version="1.0"/>
  <xsl:template match="recueilPoemes">
    <html>
      <body>
        <xsl:apply-templates/>
      </body>
    </html>
  </xsl:template>
  <xsl:template match="auteur">
    <br/>
    <h4>
      <xsl:apply-templates/>
    </h4>
  </xsl:template>
  <xsl:template match="prenom">
    <xsl:apply-templates/>
    <xsl:text> </xsl:text>
  </xsl:template>
  <xsl:template match="nomFamille">
    <xsl:apply-templates/>
  </xsl:template>
  <xsl:template match="titre">
    <h2>
      <xsl:apply-templates/>
    </h2>
  </xsl:template>
  <xsl:template match="strophe">
    <p>
      <xsl:apply-templates/>
    </p>
  </xsl:template>
  <xsl:template match="vers">
    <xsl:apply-templates/>
    <br/>
  </xsl:template>
  <xsl:template match="poeme">
    <br/>
    <xsl:if test="@langue='el'">Le poème est en langue grec.
    <xsl:apply-templates/>
    </xsl:if>
  </xsl:template>
</xsl:stylesheet>

Solution au exercice 5a:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output encoding="iso-8859-1" version="1.0"/>
  <xsl:template match="/">
    <xsl:for-each select="//poeme">
      <xsl:sort order="ascending" data-type="number" select="@anneeParution"/>
      <xsl:sort order="ascending" data-type="text" select="auteur/nomFamille"/>
      <xsl:apply-templates/>
    </xsl:for-each>
  </xsl:template>
  <xsl:template match="auteur">
    <br/>
    <h4>
      <xsl:apply-templates/>
    </h4>
  </xsl:template>
  <xsl:template match="prenom">
    <xsl:apply-templates/>
    <xsl:text> </xsl:text>
  </xsl:template>
  <xsl:template match="nomFamille">
    <xsl:apply-templates/>
  </xsl:template>
  <xsl:template match="titre">
    <h2>
        <xsl:apply-templates/>
    </h2>
    <p>Année de parution: <xsl:value-of select="parent::poeme/@anneeParution"/></p>
  </xsl:template>
  <xsl:template match="strophe">
    <p>
      <xsl:apply-templates/>
    </p>
  </xsl:template>
  <xsl:template match="vers">
    <xsl:apply-templates/>
    <br/>
  </xsl:template>
  <xsl:template match="poeme">
    <xsl:apply-templates/>
  </xsl:template>
</xsl:stylesheet>
Vue du browser à la solution du exercice 5a (extrait):

vue du browser: solution 5a

Solution au exercice 5b:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output encoding="iso-8859-1" version="1.0"/>
  <xsl:template match="/">
    <h1>Table des matières</h1>
    <p>
      <xsl:for-each select="//poeme">
        <xsl:sort order="ascending" data-type="number" select="@anneeParution"/>
        <xsl:sort order="ascending" data-type="text" select="auteur/nomFamille"/>
        <a href="#{titre}">
          <xsl:value-of select="titre"/>
        </a><br/>
      </xsl:for-each>
    </p>
    <xsl:for-each select="//poeme">
      <xsl:sort order="ascending" data-type="number" select="@anneeParution"/>
      <xsl:sort order="ascending" data-type="text" select="auteur/nomFamille"/>
      <xsl:apply-templates/>
    </xsl:for-each>
  </xsl:template>
  <xsl:template match="auteur">
    <br/>
    <h4>
      <xsl:apply-templates/>
    </h4>
  </xsl:template>
  <xsl:template match="prenom">
    <xsl:apply-templates/>
    <xsl:text> </xsl:text>
  </xsl:template>
  <xsl:template match="nomFamille">
    <xsl:apply-templates/>
  </xsl:template>
  <xsl:template match="titre">
    <h2>
      <a name="{.}"/>
      <xsl:apply-templates/>
    </h2>
    <p>Année de parution: <xsl:value-of select="parent::poeme/@anneeParution"/></p>
  </xsl:template>
  <xsl:template match="strophe">
    <p>
      <xsl:apply-templates/>
    </p>
  </xsl:template>
  <xsl:template match="vers">
    <xsl:apply-templates/>
    <br/>
  </xsl:template>
  <xsl:template match="poeme">
    <xsl:apply-templates/>
  </xsl:template>
</xsl:stylesheet>
Vue du browser à la solution du exercice 5b:

vue du browser: solution 5b

Solution au exercice 6a:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output encoding="iso-8859-1" version="1.0"/>
  <xsl:template match="recueilPoemes">
    <html>
      <body>
        <xsl:apply-templates/>
      </body>
    </html>
  </xsl:template>
  <xsl:template match="auteur">
    <br/>
    <h4>
      <xsl:apply-templates/>
    </h4>
  </xsl:template>
  <xsl:template match="prenom">
    <xsl:apply-templates/>
    <xsl:text> </xsl:text>
  </xsl:template>
  <xsl:template match="nomFamille">
    <xsl:apply-templates/>
  </xsl:template>
  <xsl:template match="titre">
    <h2>
      <xsl:number format="I" level="any"/>
      <xsl:text> </xsl:text>
      <xsl:apply-templates/>
    </h2>
  </xsl:template>
  <xsl:template match="strophe">
    <p>
      <b>Strophe <xsl:number/>
      </b>
      <br/>
      <xsl:apply-templates/>
    </p>
  </xsl:template>
  <xsl:template match="vers">
    <b>Vers <xsl:number level="multiple" count="//poeme | //poeme/strophe | //poeme/strophe/vers" format="I.1.1"/></b>
    <br/>
    <xsl:apply-templates/>
    <br/>
  </xsl:template>
  <xsl:template match="poeme">
    <br/>
    <xsl:apply-templates/>
  </xsl:template>
</xsl:stylesheet>
Vue du browser à la solution du exercice 6a:

vue du browser: solution 6a

Solution du exercice 6b:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output encoding="iso-8859-1" version="1.0"/>
  <xsl:template match="recueilPoemes">
    <html>
      <body>
        <xsl:apply-templates/>
      </body>
    </html>
  </xsl:template>
  <xsl:template match="auteur">
    <br/>
    <table>
      <col width="30px"/>
      <tbody>
        <tr>
          <td style="color:#G9G9G9; font-size:9pt">
            <xsl:number count="//titre | //vers | //auteur | //sous-titre" format="1" level="any"/>
          </td>
          <td style="font-size:12pt; font-weight:bold">
            <xsl:apply-templates/>
          </td>
        </tr>
      </tbody>
    </table>
  </xsl:template>
  <xsl:template match="prenom">
    <xsl:apply-templates/>
    <xsl:text> </xsl:text>
  </xsl:template>
  <xsl:template match="nomFamille">
    <xsl:apply-templates/>
  </xsl:template>
  <xsl:template match="titre">
    <table>
      <col width="30px"/>
      <tbody>
        <tr>
          <td style="color:#G9G9G9; font-size:9pt">
            <xsl:number count="//titre | //vers | //auteur | //sous-titre" format="1" level="any"/>
          </td>
          <td style="font-size:18pt; font-weight:bold">
            <xsl:apply-templates/>
          </td>
        </tr>
      </tbody>
    </table>
  </xsl:template>
  <xsl:template match="sous-titre">
    <table>
      <col width="30px"/>
      <tbody>
        <tr>
          <td style="color:#G9G9G9; font-size:9pt">
            <xsl:number count="//titre | //vers | //auteur | //sous-titre" format="1" level="any"/>
          </td>
          <td style="font-size:14pt; font-weight:bold">
            <xsl:apply-templates/>
          </td>
        </tr>
      </tbody>
    </table>
  </xsl:template>
  <xsl:template match="strophe">
    <p>
      <xsl:apply-templates/>
    </p>
  </xsl:template>
  <xsl:template match="vers">
    <table>
      <col width="30px"/>
      <tbody>
        <tr>
          <td style="color:#G9G9G9; font-size:9pt">
            <xsl:number count="//titre | //vers | //auteur | //sous-titre" format="1" level="any"/>
          </td>
          <td style="font-size:10pt">
            <xsl:apply-templates/>
          </td>
        </tr>
      </tbody>
    </table>
  </xsl:template>
  <xsl:template match="poeme">
    <br/>
    <xsl:apply-templates/>
  </xsl:template>
</xsl:stylesheet>
Vue du browser à la solution du exercice 6b:

vue du browser: solution 6b

Solution au exercice 7:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output encoding="iso-8859-1" version="1.0"/>
  <xsl:template match="recueilPoemes">
    <html>
      <body>
        <xsl:apply-templates/>
      </body>
    </html>
  </xsl:template>
  <xsl:template match="auteur">
    <br/>
    <h4>
      <xsl:apply-templates/>
    </h4>
  </xsl:template>
  <xsl:template match="prenom">
    <xsl:apply-templates/>
    <xsl:text> </xsl:text>
  </xsl:template>
  <xsl:template match="nomFamille">
    <xsl:apply-templates/>
  </xsl:template>
  <xsl:template match="titre">
    <h2>
      <xsl:apply-templates/>
    </h2>
  </xsl:template>
  <xsl:template match="strophe">
    <p>
      <xsl:apply-templates/>
    </p>
  </xsl:template>
  <xsl:template match="vers">
    <xsl:apply-templates/>
    <br/>
  </xsl:template>
  <xsl:template match="poeme">
    <xsl:variable name="annee">
      <xsl:value-of select="starts-with(@anneeParution,'-')"/>
    </xsl:variable>
    <div>
      <xsl:if test="$annee='true'">
        <xsl:attribute name="style">background-color:red</xsl:attribute>
      </xsl:if>
      <br/>
      <xsl:apply-templates/>
    </div>
  </xsl:template>
</xsl:stylesheet>
Vue du browser à la solution du exercice 7 (extrait):

vue du browser: solution 7

Solution au exercice 8a:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:import href="Solution8b.xslt"/>  
  <xsl:output encoding="iso-8859-1" version="1.0"/>
  <xsl:template match="recueilPoemes">
    <html>
      <body>
        <xsl:apply-templates/>
      </body>
    </html>
  </xsl:template>
</xsl:stylesheet>

Solution au exercice 8b:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output encoding="iso-8859-1" version="1.0"/>

  <xsl:template match="auteur">
    <br/>
    <h4>
      <xsl:apply-templates/>
    </h4>
  </xsl:template>
  <xsl:template match="prenom">
    <xsl:apply-templates/>
    <xsl:text> </xsl:text>
  </xsl:template>
  <xsl:template match="nomFamille">
    <xsl:apply-templates/>
  </xsl:template>
  <xsl:template match="titre">
    <h2>
      <xsl:apply-templates/>
    </h2>
  </xsl:template>
  <xsl:template match="strophe">
    <p>
      <xsl:apply-templates/>
    </p>
  </xsl:template>
  <xsl:template match="vers">
    <xsl:apply-templates/>
    <br/>
  </xsl:template>
  <xsl:template match="poeme">
    <xsl:apply-templates/>
  </xsl:template>
</xsl:stylesheet>

Copyright © dpunkt.verlag GmbH 2004
Vous pouvez imprimer cette version en ligne pour un usage privé. Par ailleurs, ce chapitre du livre "XSL-FO in der Praxis" est soumis aux mêmes clauses prévues pour la version papier: L'intégralité de l'œuvre est protégée par les droits d'auteurs. Tous droits réservés y compris la copie, la traduction, la reproduction sur microfilm, tout comme l'enregistrement et le traitement dans des systèmes électroniques.

dpunkt.verlag GmbH, Ringstraße 19B, 69115 Heidelberg, téléphone +49 (0)6221-14830, fax +49 (0)6221-148399, hallo(at)dpunkt.de