Modérateur PHPfrance |
2575 Messages
09 oct. 2006, 09:54
Hé bien, dans le fichier XSL, tu crée un champ xsl
img avec un attribut
src qui contient la valeur du champ XML
<source>
La syntaxe est :
- <xsl:element name="img">
- <xsl:attribute name="src">
- <xsl:value-of select="source" />
</xsl:attribute>
</xsl:element>
Exemple : client.xml
Code : Tout sélectionner
<?xml version="1.0" encoding ="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="client.xsl"?>
<test>
<client>
<code>1</code>
<nom>client 1</nom>
<prenom>client 1</prenom>
<email>[email protected]</email>
<adresse>domicile de client1</adresse>
<ville>le mans</ville>
<code_postal>72000</code_postal>
<telephone></telephone>
<code_ville>1</code_ville>
<photo>client1.gif</photo>
</client>
<client>
<code>2</code>
<nom>client 2</nom>
<prenom>client 2</prenom>
<email>[email protected]</email>
<adresse>domicile de client2</adresse>
<ville>le mans</ville>
<code_postal>72000</code_postal>
<telephone></telephone>
<code_ville>1</code_ville>
<photo>client2.gif</photo>
</client>
<test>
Le client.xsl:
On peut voir les clients enregistrés dans
client.xml sous forme d'un tableau HTML
Code : Tout sélectionner
<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet xmlns:xsl= "http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="test"> <!-- Analyse du fichier à partir du nœud 'test' -->
<html>
<head><title>test/client</title></head>
<body>
<h3>test/client</h3> <!-- Affichage du titre -->
<table border="1"> <!-- Déclaration d'un tableau -->
<tr style='background:GREY; color:WHITE'> <!-- Ligne d'entête du tableau -->
<th>code</th>
<th>nom</th>
<th>prenom</th>
<th>email</th>
<th>adresse</th>
<th>ville</th>
<th>code_postal</th>
<th>telephone</th>
<th>code_ville</th>
<th>photo</th>
</tr>
<xsl:for-each select="client"> <!-- Traitement effectué pour chaque nœud 'client' rencontré -->
<tr> <!-- Une ligne par client -->
<td> <xsl:value-of select="code"/> </td>
<!-- Affichage du contenu du noeud 'code' -->
<td> <xsl:value-of select="nom"/> </td>
<td> <xsl:value-of select="prenom"/> </td>
<td> <xsl:value-of select="email"/> </td>
<td> <xsl:value-of select="adresse"/> </td>
<td> <xsl:value-of select="ville"/> </td>
<td> <xsl:value-of select="code_postal"/> </td>
<td> <xsl:value-of select="telephone"/> </td>
<td> <xsl:value-of select="code_ville"/> </td>
<td>
<xsl:element name="img">
<xsl:attribute name="src">
<xsl:value-of select="photo" />
</xsl:attribute>
<xsl:attribute name="alt">
La photo de <xsl:value-of select="prenom" />
</xsl:attribute>
</xsl:element>
</td>
<!-- Affichage du contenu du noeud 'photo' dans une balise HTML <img src=... alt=.... /> -->
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Exemple 2 : Liste déroulante :
A partir de la même source de données
client.xml, on peut voir les clients dans une liste déroulante :
liste_client.xsl
Code : Tout sélectionner
<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet xmlns:xsl= "http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="test"> <!-- Analyse du fichier à partir du nœud 'test' -->
<html>
<head><title>test/client</title></head>
<body>
<h3>Liste des clients</h3> <!-- Affichage du titre -->
<xsl:element name="select"><xsl:attribute name="name">client</xsl:attribute>
<!-- Début d'une balise <Select name=..> -->
<xsl:for-each select="client"> <!-- Traitement effectué pour chaque nœud 'client' rencontré -->
<xsl:element name="option">
<xsl:attribute name="value"><xsl:value-of select="code" /></xsl:attribute>
<xsl:value-of select="nom" />
</xsl:element>
<!-- Affichage du contenu des noeuds 'prenom', 'nom' et 'code' dans une balise <option value=..>...</option> -->
</xsl:for-each>
</xsl:element>
<!-- Fin de la balise </Select> -->
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Pour plus d'infos sur xsl/xml voir :
http://gilles.chagnon.free.fr/cours/xml/recapxsl.html
--------
//////----
//---
//----
//////
-------
//---
//----
//---
//----
//---
//
------
//////----
//////-----
//////
-----
||--------
||--
||---
||
Prendre le recul n'est pas une perte de temps.
ps:
Affrontez moi dans l'arène