Bonjour,

Je vous explique mon problème, j'ai une liste d'acheteurs avec leurs commandes et les articles associés. Je souhaite, grâce à XSLT, sortir l'article le plus acheté et uniquement celui la.

J'ai essayé diverses solutions et j'arrive à un résultat qui ne me satisfait pas totalement. Je ressors la liste des articles, avec le nombre d'occurence et je les tri.

voila ce que de quoi je pars :

le XML :

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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
<?xml version="1.0"?> 
<?xml-stylesheet type="text/xsl" href="commandes.xsl"?>
<users> 
  <user>
    <prenom>Alain</prenom>
    <nom>Souchon</nom>
    <email>alain.souchon@gmail.com</email>
    <commandes>
      <commande>
        <date>06/24/2013 09:34:21</date>
        <montant>16.45</montant>
        <articles>
          <article>
            <id>12</id>
            <nom>velo</nom>
            <url>http://www.velo-cycle-vtt.com/images/velo-kx-30.jpg</url>
            <prix>8.30</prix>
          </article>
          <article>
            <id>37</id>
            <nom>bateau</nom>
            <url>http://www.locationbateaunice.com/bateau-a-moteur-nice.jpg</url>
            <prix>8.15</prix>
          </article>
        </articles>
      </commande>
      <commande>
        <date>06/24/2013 09:34:21</date>
        <montant>16.45</montant>
        <articles>
          <article>
            <id>37</id>
            <nom>bateau</nom>
            <url>http://www.locationbateaunice.com/bateau-a-moteur-nice.jpg</url>
            <prix>8.15</prix>
          </article>
        </articles>
      </commande>
      <commande>
        <date>06/24/2013 09:34:21</date>
        <montant>16.45</montant>
        <articles>
          <article>
            <id>37</id>
            <nom>bateau</nom>
            <url>http://www.locationbateaunice.com/bateau-a-moteur-nice.jpg</url>
            <prix>8.15</prix>
          </article>
        </articles>
      </commande>
    </commandes>
  </user>
  <user>
    <prenom>Oscar</prenom>
    <nom>Batare</nom>
    <email>multikill@laposte.net</email>
    <commandes>
      <commande>
        <date>08/02/2013 19:14:21</date>
        <montant>166</montant>
        <articles>
          <article>
            <id>42</id>
            <nom>timbre femen</nom>
            <url>http://www.nationalite-citoyennete-identite.com/wp-content/uploads/2013/07/timbre-marianne-femen3.jpg</url>
            <prix>166</prix>
          </article>
        </articles>
      </commande>
      <commande>
        <date>08/03/2013 11:37:21</date>
        <montant>53</montant>
        <articles>
          <article>
            <id>8</id>
            <nom>timbre toutou</nom>
            <prix>53</prix>
            <url>http://timbres.laposte.fr/bpmapp-upload/download/fstore/timbre%20chiot.jpg</url>
          </article>
        </articles>
      </commande>
      <commande>
        <date>01/01/2014 12:01:21</date>
        <montant>170</montant>
        <articles>
          <article>
            <id>42</id>
            <nom>timbre femen</nom>
            <url>http://www.nationalite-citoyennete-identite.com/wp-content/uploads/2013/07/timbre-marianne-femen3.jpg</url>
            <prix>166</prix>
          </article>
          <article>
            <id>22</id>
            <nom>Pot</nom>
            <url>https://encrypted-tbn3.gstatic.com/images?q=tbn:ANd9GcQz4APiMYah_Kiwy43kczWtbwmPNISEuQlw34EuDId0CUy5AK75Lphno9jP</url>
            <prix>4</prix>
          </article>
        </articles>
      </commande>
    </commandes>
  </user>
</users>
Le XSLT :

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
 
<?xml version='1.0' encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:key name="article-by-id" match="/users/user/commandes/commande/articles/article" use="id" />
  <xsl:template match="/"> 
    <html>
      <head>
        <title>TEST XSL</title>
      </head>
      <body>
 
        <table border="1" cellspacing="0" cellpadding="3">
          <tr bgcolor="#FFFF00"> 
            <td>id Article </td>
            <td>nb fois commandé</td>
          </tr>
          <xsl:for-each select="/users/user/commandes/commande/articles/article[generate-id() = generate-id(key('article-by-id',id)[1])]" >
            <xsl:sort select="count(key('article-by-id',current()/id))" data-type="number" order="descending" />
            <tr>
              <td>
                <xsl:value-of select="id"/>
              </td>
              <td>
                <xsl:value-of select="count(key('article-by-id',current()/id))"/>
              </td>
            </tr> 
          </xsl:for-each>
        </table>
      </body>
    </html>
  </xsl:template> 
</xsl:stylesheet>
Y a t'il possibilité de ne cibler que celui qui a été le plus commandé ?


D'avance merci pour votre aide.

Olivier