Bonjour,
Je suis en train de faire mes premiers pas en XML/XSLT, en fouinant des tutos et à coups de tests j'ai réussi à faire ce que je voulais mais je doute que mon code soit une belle beauté mais plutôt Cunégonde.
Comme exercice, j'ai pris un cas que je connais bien (étant amateur de comics).
Pour situer vite fais l'exercice, les comics sortent généralement mensuellement en kiosque puis arrivé au bout de x mois une compilation regroupant plusieurs numéros kiosques sort en librairie.
J'ai donc essayé de schématisé cela en XML/XSLT.
Dans un premier temps, un tableau affiche tous les numéros mensuels.
Puis un second tableau affiche les compilations faisant un lien avec les numéros mensuels qu'elles contiennent pour récupérer leurs infos.
Auriez-vous des recommandations, tips, corrections, améliorations à faire sur le code XSLT ? Je suis preneur de tout amélioration/correction, merci bcp !
Fichier XML : test.xml
Fichier XSLT : xslt.xsl
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
38
39
40
41
42 <?xml version="1.0" encoding="utf-8"?> <?xml-stylesheet href="xslt.xsl" type="text/xsl"?> <comics> <kiosque> <book id="Batman.001"> <nom>Bat001</nom> <episode>001</episode> <groupe>Batman</groupe> </book> <book id="Batman.002"> <nom>Bat002</nom> <episode>002</episode> <groupe>Batman</groupe> </book> <book id="Batman.003"> <nom>Bat003</nom> <episode>003</episode> <groupe>Batman</groupe> </book> <book id="Nightwing.001"> <nom>Nightwing001</nom> <episode>001</episode> <groupe>Batman</groupe> </book> </kiosque> <librairie> <book id="Batman.001"> <nom>La cour des Hiboux</nom> <volume>01</volume> <groupe>Batman</groupe> <contents> <book>Batman.001</book> <book>Nightwing.001</book> <book>Batman.002</book> <book>Batman.003</book> </contents> </book> </librairie> </comics>
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62 <?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="html" encoding="utf-8" doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN" doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"/> <xsl:template match="/"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title>Mise en forme avec XSLT</title> </head> <body> <table width="1000" border="1" cellspacing="0" cellpadding="0"> <tr> <th scope="col">ID</th> <th scope="col">Nom</th> <th scope="col">Episode</th> <th scope="col">Groupe</th> </tr> <xsl:for-each select="comics/kiosque/book"> <tr> <td><xsl:value-of select="@id"/></td> <td><xsl:value-of select="nom"/></td> <td><xsl:value-of select="episode"/></td> <td><xsl:value-of select="groupe"/></td> </tr> </xsl:for-each> </table> <table width="1000" border="1" cellspacing="0" cellpadding="0"> <tr> <th scope="col">ID</th> <th scope="col">Nom</th> <th scope="col">Episode</th> <th scope="col">Groupe</th> <th scope="col">Nom épisode</th> <th scope="col">Numéro épisode</th> </tr> <xsl:for-each select="comics/librairie/book/contents/book"> <tr> <td><xsl:value-of select="."/></td> <td><xsl:value-of select="../../nom"/></td> <td><xsl:value-of select="../../volume"/></td> <td><xsl:value-of select="../../groupe"/></td> <xsl:variable name="ID"><xsl:value-of select="."/></xsl:variable> <xsl:for-each select="../../../../kiosque/book[@id = $ID]"> <td><xsl:value-of select="nom"/></td> <td><xsl:value-of select="episode"/></td> </xsl:for-each> </tr> </xsl:for-each> </table> </body> </html> </xsl:template> </xsl:stylesheet>
Partager