Bonjour à tous,
J'essaie par un XSL de récupérer une variable pour afficher certains mots dans la couleur correspondante (pas compris? je comprends. Une exemple vaudrait mieux)
exemple, extrait de mon XML:
Je m'en suis sortie en faisant ça:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 <?xml version="1.0" encoding="utf-8"?> <?xml-stylesheet type="text/xsl" href="foret.xsl"?> <root xmlns:xi="http://www.w3.org/2001/XInclude"> <foret hemisphere="nord"> <fleur> <bleu hexa="#00F"> <nom>myosotis</nom> <nom>pensee</nom> </bleu> <vert hexa="#0F0"> <nom>belladone</nom> <nom>bryone</nom> </vert> <rouge hexa="#F00"> <nom>coquelicot</nom> <nom>pavot</nom> </rouge> <jaune hexa="#FF0"> <nom>pissenlit</nom> <nom>moutarde des champs</nom> </jaune> </fleur> </foret> </root>Très peu commode, vous en conviendrez
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37 <?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <html> <title>Les Fôrets</title> <body> <xsl:apply-templates/> </body> </html> </xsl:template> <xsl:template match="root/foret"> Les fleurs sont de différentes couleurs: <ul> <xsl:for-each select="//fleur/*"> <xsl:sort select="name(.)"/> <xsl:if test="./@hexa='#00F'"> <li><span style="color:#00F;"><xsl:value-of select="name(.)"/></span></li> </xsl:if> <xsl:if test="./@hexa='#0F0'"> <li><span style="color:#0F0;"><xsl:value-of select="name(.)"/></span></li> </xsl:if> <xsl:if test="./@hexa='#F00'"> <li><span style="color:#F00;"><xsl:value-of select="name(.)"/></span></li> </xsl:if> <xsl:if test="./@hexa='#FF0'"> <li><span style="color:#FF0;"><xsl:value-of select="name(.)"/></span></li> </xsl:if> </xsl:for-each> </ul> </xsl:template> </xsl:stylesheet>
Alors j'ai essayé ça:
Evidemment, je dois me planter quelque part.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <html> <title>Les Fôrets</title> <body> <xsl:apply-templates/> </body> </html> </xsl:template> <!--déclarations de variables--> <xsl:variable name="col_hexad" select="//fleur/*/@hexa"/> </xsl:variable> <xsl:template match="root/foret"> <ul> <xsl:for-each select="//fleur/*"> <xsl:sort select="name(.)"/> <li><span style="color:'$col_hexad';"><xsl:value-of select="name(.)"/></span></li> </xsl:for-each> </ul> </xsl:template> </xsl:stylesheet>
Merci pour vos lumières,
V.
Partager