IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

XSL/XSLT/XPATH XML Discussion :

[xml-xsl] Probleme avec position()


Sujet :

XSL/XSLT/XPATH XML

  1. #1
    Nouveau membre du Club
    Inscrit en
    Septembre 2004
    Messages
    45
    Détails du profil
    Informations forums :
    Inscription : Septembre 2004
    Messages : 45
    Points : 28
    Points
    28
    Par défaut [xml-xsl] Probleme avec position()
    Bonjour à tous,

    Je fais un tri sur un fichier xml avec la balise name par ordre descendant et par lettre. Par exemple j'extrais seulement les données commencant par A.

    Mon probleme est que j'aimerais paginer les resultats par 10 ou 15.
    Pour cela j'utilise la fonction position(), et je n'arrive pas a l'utiliser seulement sur les données concernées.

    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
     
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="html" version="1.0" encoding="UTF-8" indent="yes"/>
     
    <xsl:param name="lettreMin"/>
    <xsl:param name="lettreMaj"/>
    <xsl:param name="max"/>
    <xsl:param name="min"/>
     
    <xsl:template match="/">
     
    <html>
     
    <body>
    <div align="center">
    <table border="1" cellspacing="0" cellpadding="3">
       <tr bgcolor="#FFFF00">
           <td>Nom</td>
           <td>Description</td>
           <td>Position</td>
       </tr>
     
       <xsl:for-each select="tools/tool">
       <xsl:sort select="name"/>
           <xsl:variable name="intro" select="name[starts-with(.,$lettreMin) or starts-with(.,$lettreMaj)]"/>
     
           <xsl:if test="$intro[position() &lt;= $max  and position() &gt;= $min]">
               <tr>
                   <td><xsl:value-of select="name"/></td>
                   <td><xsl:value-of select="description"/></td>
                   <td><xsl:value-of select="position()"/></td>
               </tr>
           </xsl:if>
     
       </xsl:for-each>
    </table>
    </div>
     
    </body>
     
    </html>
     
    </xsl:template>
     
    </xsl:stylesheet>
    J'ai jete un coup d'oeil au post de grandfather, mais je n'y arrive toujours pas.

    Merci d'avance pour votre aide.

  2. #2
    Nouveau membre du Club
    Inscrit en
    Septembre 2004
    Messages
    45
    Détails du profil
    Informations forums :
    Inscription : Septembre 2004
    Messages : 45
    Points : 28
    Points
    28
    Par défaut
    J'ai trouve une solution, mais mes connaissances en xsl etant tres faible j'ai l'impression d'avoir fais du bidouillage.
    Peut etre que quelqu'un pourrait me dire comment optimiser ce code, merci d'avance.

    fichier 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
    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
     
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="html" version="1.0" encoding="UTF-8" indent="yes"/>
     
    <xsl:param name="lettreMin"/>
    <xsl:param name="lettreMaj"/>
    <xsl:param name="max"/>
    <xsl:param name="min"/>
    <xsl:param name="nbPage"/>
    <xsl:param name="decalage"/><!-- Nombre de ligne par page -->
     
    <xsl:template match="/">
     
    <html>
     
    <body>
     
    <div align="center">
     
        <a href="Presentation.php?lettre=a">A</a> /
        <a href="Presentation.php?lettre=b">B</a> /
        <a href="Presentation.php?lettre=c">C</a> /
        <a href="Presentation.php?lettre=d">D</a> /
        <a href="Presentation.php?lettre=e">E</a> /
        <a href="Presentation.php?lettre=f">F</a> /
        <a href="Presentation.php?lettre=g">G</a> /
        <a href="Presentation.php?lettre=h">H</a> /
        <a href="Presentation.php?lettre=i">I</a> /
        <a href="Presentation.php?lettre=j">J</a> /
        <a href="Presentation.php?lettre=k">K</a> /
        <a href="Presentation.php?lettre=l">L</a> /
        <a href="Presentation.php?lettre=m">M</a> /
        <a href="Presentation.php?lettre=n">N</a> /
        <a href="Presentation.php?lettre=o">O</a> /
        <a href="Presentation.php?lettre=p">P</a> /
        <a href="Presentation.php?lettre=q">Q</a> /
        <a href="Presentation.php?lettre=r">R</a> /
        <a href="Presentation.php?lettre=s">S</a> /
        <a href="Presentation.php?lettre=t">T</a> /
        <a href="Presentation.php?lettre=u">U</a> /
        <a href="Presentation.php?lettre=v">V</a> /
        <a href="Presentation.php?lettre=w">W</a> /
        <a href="Presentation.php?lettre=x">X</a> /
        <a href="Presentation.php?lettre=y">Y</a> /
        <a href="Presentation.php?lettre=z">Z</a>
     
    </div>
     
    <br/><br/>
     
    <div align="center">
    <table border="1" width="80%" cellspacing="0" cellpadding="3">
       <tr bgcolor="#FFFF00">
           <td width="25%">Nom</td>
           <td>Description</td>
           <td>Position</td>
       </tr>
     
       <xsl:variable name="tri" select="tools/tool/@name[starts-with(.,$lettreMin) or starts-with(.,$lettreMaj)]"/>
     
       <xsl:variable name="compteur">
           <xsl:value-of select="count(tools/tool/@name[starts-with(.,$lettreMin) or starts-with(.,$lettreMaj)])"/>
       </xsl:variable>
     
       <xsl:for-each select="$tri[position() &lt;= $max  and position() &gt;= $min]">
            <xsl:sort select="." order="ascending"/>
            <tr>
                <td width="25%"><xsl:value-of select="."/></td>
                <td><xsl:value-of select=".."/></td>
                <td><xsl:value-of select="position()"/></td>
            </tr>
       </xsl:for-each>
       <tr>
           <td colspan="2" align="center">
               <xsl:if test="$nbPage &gt; 1">
                   <a href="Presentation.php?lettre={$lettreMin}&amp;min={number($min)-number($decalage)}&amp;max={number($max)-number($decalage)}&amp;nbPage={number($nbPage)-1}">&lt;&lt;</a>
               </xsl:if>
               <xsl:if test="$compteur &gt; (number($decalage)*number($nbPage))">
                   <a href="Presentation.php?lettre={$lettreMin}&amp;min={number($max)+1}&amp;max={number($max)+number($decalage)}&amp;nbPage={number($nbPage)+1}">&gt;&gt;</a>
               </xsl:if>
           </td>
       </tr>
     
    </table>
    </div>
     
    </body>
     
    </html>
     
    </xsl:template>
     
     
    </xsl:stylesheet>
    Fichier 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
     
    <?xml version="1.0" encoding="UTF-8"?>
    <?xml-stylesheet type="text/xsl" href="ToolsFormat.xsl"?>
    <tools>
       <tool name="aa">
          <description>essai 1</description>
       </tool>
       <tool name="ab">
          <description>essai 2</description>
       </tool>
       <tool name="ba">
          <description>essai 3</description>
       </tool>
       <tool name="bb">
          <description>essai 4</description>
       </tool>
       <tool name="ca">
          <description>essai 5</description>
       </tool>
       ...
    </tools>

  3. #3
    Expert éminent
    Avatar de GrandFather
    Inscrit en
    Mai 2004
    Messages
    4 587
    Détails du profil
    Informations personnelles :
    Âge : 54

    Informations forums :
    Inscription : Mai 2004
    Messages : 4 587
    Points : 7 103
    Points
    7 103
    Par défaut
    Bonjour,

    Quel est l'intérêt de trier les balises "tool", puisqu'elles le sont déjà dans le XML ?

    A part cela, il me semble plus correct de procéder ainsi :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
       <xsl:variable name="tri" select="tools/tool[starts-with(@name,$lettreMin) or starts-with(@name,$lettreMaj)]"/> 
     
       <xsl:variable name="compteur"> 
           <xsl:value-of select="count($tri)"/> 
       </xsl:variable> 
     
       <xsl:for-each select="$tri[position() &lt;= $max  and position() &gt;= $min]"> 
            <xsl:sort select="@name" order="ascending"/> 
            <tr> 
                <td width="25%"><xsl:value-of select="@name"/></td> 
                <td><xsl:value-of select="description"/></td> 
                <td><xsl:value-of select="position()"/></td> 
            </tr> 
       </xsl:for-each>
    FAQ XML
    ------------
    « Le moyen le plus sûr de cacher aux autres les limites de son savoir est de ne jamais les dépasser »
    Giacomo Leopardi

  4. #4
    Nouveau membre du Club
    Inscrit en
    Septembre 2004
    Messages
    45
    Détails du profil
    Informations forums :
    Inscription : Septembre 2004
    Messages : 45
    Points : 28
    Points
    28
    Par défaut
    Avant tout merci pour ta reponse GrandFather.

    Les tools ne seront pas triés dans le fichier xml, c'est vrai que j'aurais pu donner un meilleur exemple.

    Encore merci, je vais essayer de ce pas ton code qui est bien mieux que le mien.

  5. #5
    Nouveau membre du Club
    Inscrit en
    Septembre 2004
    Messages
    45
    Détails du profil
    Informations forums :
    Inscription : Septembre 2004
    Messages : 45
    Points : 28
    Points
    28
    Par défaut
    GrandFather,

    J'ai melangé mes tools dans mon fichier xml et de la est sortie un probleme.

    En fait le tri se fait sur les lignes selectionnées, par exemple les 5 premières et non sur toutes les lignes commencant par la meme lettre donc ceci:
    ab,ac,av,ad,ak,al,aa
    sortira de cette maniere :
    ab,ac,ad,ak,av sur la première page
    aa,al sur la deuxième.

    J'ai donc rajouté une condition dans le foreach:
    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
     
    <xsl:variable name="tri" select="tools/tool[starts-with(name,$lettreMin) or starts-with(name,$lettreMaj)]"/>
     
    <xsl:variable name="compteur">
           <xsl:value-of select="count($tri)"/>
    </xsl:variable>
     
    <xsl:for-each select="$tri">
            <xsl:sort select="name" order="ascending"/>
            <xsl:if test="position() &lt;= $max  and position() &gt;= $min">
                 <tr>
                     <td width="25%"><xsl:value-of select="name"/></td>
                     <td><xsl:value-of select="description"/></td>
                     <td><xsl:value-of select="position()"/></td>
                 </tr>
            </xsl:if>
    </xsl:for-each>
    Y aurait-il une manière plus conventionnelle?

    Merci pour ton aide.

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Réponses: 2
    Dernier message: 26/07/2007, 09h27
  2. XML/XSL probleme URL
    Par moog14 dans le forum XML/XSL et SOAP
    Réponses: 2
    Dernier message: 19/07/2007, 16h26
  3. [XSLT] XML+XSL=HTML avec charset UTF-16
    Par Steph4fun dans le forum XSL/XSLT/XPATH
    Réponses: 4
    Dernier message: 02/10/2006, 07h54
  4. [XML/XSL]problème avec for-each
    Par mavina dans le forum XSL/XSLT/XPATH
    Réponses: 2
    Dernier message: 29/06/2006, 15h28
  5. [XML][XSL]probleme html dans balise xml
    Par chateau64 dans le forum Balisage (X)HTML et validation W3C
    Réponses: 6
    Dernier message: 02/12/2005, 11h42

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo