Xslt - sort en utilisant une variable

Fermé
Utilisateur anonyme - 6 nov. 2005 à 20:56
 Utilisateur anonyme - 6 nov. 2005 à 22:18
boujour,
je veux faire une page en xlst et je veux trier mon fichier xml selon une variable dans une boucle for...

<xsl:template match="/">
<xsl:for-each select="sites/site">
...
...
<xsl:variable name="cote" select="$points div $total"/>

<xsl:sort select="$cote"/> ERREUR

</xsl:for-each>

donc je ne sais pas ou placer <xsl:sort select="$cote"/>

merci de votra aide...

2 réponses

crabs Messages postés 908 Date d'inscription lundi 18 avril 2005 Statut Membre Dernière intervention 3 août 2008 507
6 nov. 2005 à 21:56
Salut,
Cela n'est pas possible directement sauf si tu peux calculer directement ta
valeur de tri sur des arguments ou des éléments du noeud site.

Dans le cas contraire, il faut faire 2 transformations 1 qui fait les calculs, 1 qui
fait le tri. Mais pour les calculs y a mieux que XSLT, faut mieux revoir la
génération du XML

Exemple vis à vis de la première 'option':
Fichier XML
<?xml version="1.0" encoding="iso-8859-1" ?>
<?xml-stylesheet type="text/xsl" href="sort_calc.xsl" ?>
<sites>
 <site nom="1" vu="30" vote="3"/>
 <site nom="2" vu="30" vote="20"/>
 <site nom="3" vu="20" vote="3"/>
 <site nom="4" vu="40" vote="20"/>
</sites>

Fichier XSL:
<?xml version="1.0" encoding="iso-8859-1" ?>
<xsl:stylesheet
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">
<xsl:output encoding="iso-8859-1" method="html"
    doctype-public="-//W3C//DTD HTML 4.01//EN"
    doctype-system="http://www.w3.org/TR/html4/strict.dtd" />

<xsl:template match="/">
<html>
<head><title>XML: tri avec calcul</title></head>
<body>
<table style="width:80%;">
    <tr>
        <th style="width:25%;">site</th>
        <th style="width:25%;">vote</th>
        <th style="width:25%;">vu</th>
        <th style="width:25%;">ratio</th>
    </tr>
    <xsl:for-each select="sites/site">
        <xsl:sort select="@vote div @vu" data-type="number" order="descending"/>
        <tr>
            <td style="text-align:center;"><xsl:value-of select="@nom"/></td>
            <td style="text-align:center;"><xsl:value-of select="@vote"/></td>
            <td style="text-align:center;"><xsl:value-of select="@vu"/></td>
            <td><xsl:value-of select="@vote div @vu"/></td>
        </tr>
    </xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

2
Utilisateur anonyme
6 nov. 2005 à 22:18
merci...

j'aimais mieux mon beau petit calcul:

<xsl:variable name="r1" select="rang/r1"/>
<xsl:variable name="r2" select="rang/r2"/>
<xsl:variable name="r3" select="rang/r3"/>
<xsl:variable name="r4" select="rang/r4"/>
<xsl:variable name="r5" select="rang/r5"/>
<xsl:variable name="total" select="$r1+$r2+$r3+$r4+$r5"/>
<xsl:variable name="points" select="$r1+(2*$r2)+(3*$r3)+(4*$r4)+(5*$r5)"/>
<xsl:variable name="cote" select="$points div $total"/>

mais maintenant ça fonctionne avec...

<xsl:sort select="(rang/r1 + (2*rang/r2) + (3*rang/r3) + (4*rang/r4) + (5*rang/r5)) div (rang/r1 + rang/r2 + rang/r3 + rang/r4 + rang/r5)"/>
0