Elaboration du fichier
Reprenons notre fichier XML (inchangé).

<?xml version="1.0"?> <compilation>
<mp3>
<titre>Foule sentimentale</titre>
<artiste>Alain Souchon</artiste> </mp3>
<mp3>
<titre>Solaar pleure</titre> <artiste>MC Solaar</artiste>
</mp3>
<mp3>
<titre>Le baiser</titre> <artiste>Alain Souchon</artiste>
</mp3>
<mp3>
<titre>Pourtant</titre>
<artiste>Vanessa Paradis</artiste> </mp3>
<mp3>
<titre>Chambre avec vue</titre> <artiste>Henri Salvador</artiste>
</mp3>
</compilation> |
Voir le fichier xml (IE 5 et +).
Passons maintenant au fichier XSL

Nous allons reprendre dans notre compilation de mp3 en XML tous les
titres d'Alain Souchon que nous afficherons dans une colonne verte, les
autres seront affichés normalement.
<?xml version='1.0'?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl">
<xsl:template match="/">
<html>
<body>
<table border="1" cellspacing="0" cellpadding="3">
<tr bgcolor="#FFFF00">
<td>Artiste</td>
<td>Titre</td>
</tr>
<xsl:for-each select="compilation/mp3">
<xsl:choose>
<xsl:when test=".[artiste='Alain Souchon']">
<tr bgcolor="#00FF00">
<td><xsl:value-of select="titre"/></td>
<td><xsl:value-of select="artiste"/></td>
</tr>
</xsl:when>
<xsl:otherwise>
<tr>
<td><xsl:value-of select="titre"/></td>
<td><xsl:value-of select="artiste"/></td>
</tr>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet> |
On enregistre le fichier sous le nom xsl_choose et l'extension .xsl.
Voir le fichier xsl (IE 5 +).
On revient au fichier XML et on y ajoute la balise pour y associer le
fichier XSL.
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="xsl_choose.xsl"?>
<compilation>
<mp3>
<titre>Foule sentimentale</titre>
<artiste>Alain Souchon</artiste>
</mp3>
etc... |
Et voilà notre fichier avec uniquement les titres d'Alain Souchon.

Que vous pouvez aussi apprécier en direct. Voir le fichier
xml + xsl (IE 5 et +).
|