Home > OS >  how to creat an xslt for this xml & the output is table with culomn{numInscription,nom,prenom,nomMod
how to creat an xslt for this xml & the output is table with culomn{numInscription,nom,prenom,nomMod

Time:12-22

here i need for every student his modules groping by id_promo but i d'ont know the syntax of for each with condition and loop in loop

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    exclude-result-prefixes="xs"
    version="2.0">
    <xsl:template match="/">
        <html>
            <head>
                
            </head>
            <body>
                <h1>Liste des Etudiants et les module de promo</h1> 
                <ul>
                    <table width="80%" border="1px" cellpadding="10px" cellspacing="0px">
                        <thead>
                            <th>num_et</th><th>nom_et</th><th>prenom_et</th><th>nom_mod</th><th>id_mod</th>
                            
                            
                        </thead>
                        <tbody>
                     
                            
                            <xsl:for-each select="promotion/etudiants/etudiant">
                                
                      
                                <tr>
                                    
                                    <td><xsl:value-of select="@num_et"/></td> 
                                    <td><xsl:value-of select="@nom_et"/></td> 
                                    <td><xsl:value-of select="@prenom_et"/></td>
                            
                                 </tr>     
                                  
                                
                            </xsl:for-each>
                            
                            
                        </tbody>
                    </table>
                </ul>
            </body>
        </html>   
    </xsl:template>
</xsl:stylesheet>

<?xml version="1.0" encoding="iso-8859-1"?> 
<promotion option="MGL" niveau="2" > 
<etudiants>   
  <etudiant numInscription="3666" nom="X" prenom="Y" id_promo="1"/> 
   .....
   ......
</etudiants> 
<modules> 
 <module idModule="E200" nomModule="Web 2.0" id_promo="1"/> 
 <module idModule="E222" nomModule="Web 3.0" id_promo="1"/> 
  ...... 
  ...... 
</modules> 
</promotion> 
here i need for every student his modules groping by id_promo id'ont know the syntaxe with the id_promo ineed to collect the tow table in one table


Name prename module
X Y Web 2.0
Web 3.0

CodePudding user response:

Try something like:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:key name="module" match="module" use="@id_promo" />

<xsl:template match="/promotion">
    <html>
        <head/>
        <body>
            <h1>Liste des Etudiants et les module de promo</h1> 
            <table border="1">
                <thead>
                    <th>num_et</th>
                    <th>nom_et</th>
                    <th>prenom_et</th>
                    <th>nom_mod</th>
                    <th>id_mod</th>
                </thead>
                <tbody>
                    <xsl:for-each select="etudiants/etudiant">
                        <xsl:variable name="etudiant" select="." />
                        <xsl:for-each select="key('module', @id_promo)">
                            <tr>
                                <xsl:choose>
                                    <xsl:when test="position() =1">
                                        <td>
                                            <xsl:value-of select="$etudiant/@numInscription"/>
                                        </td> 
                                        <td>
                                            <xsl:value-of select="$etudiant/@nom"/>
                                        </td> 
                                        <td>
                                            <xsl:value-of select="$etudiant/@prenom"/>
                                        </td>
                                    </xsl:when>
                                    <xsl:otherwise>
                                        <td/><td/><td/>
                                    </xsl:otherwise>
                                </xsl:choose>
                                <td>
                                    <xsl:value-of select="@nomModule"/>
                                </td> 
                                <td>
                                    <xsl:value-of select="@idModule"/>
                                </td>
                            </tr>     
                        </xsl:for-each>
                    </xsl:for-each>
                </tbody>
            </table>
        </body>
    </html>   
</xsl:template>

</xsl:stylesheet>

When this is applied to the following example input:

XML

<promotion option="MGL" niveau="2">
  <etudiants>
    <etudiant numInscription="3666" nom="X" prenom="Y" id_promo="1" />
    <etudiant numInscription="3667" nom="X" prenom="O" id_promo="2" />
  </etudiants>
  <modules>
    <module idModule="E200" nomModule="Web 2.0" id_promo="1" />
    <module idModule="E222" nomModule="Web 3.0" id_promo="1" />
    <module idModule="E223" nomModule="Web 3.1" id_promo="2" />
  </modules>
</promotion>

the result will be:

HTML

<html>
   <head>
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
   </head>
   <body>
      <h1>Liste des Etudiants et les module de promo</h1>
      <table border="1">
         <thead>
            <th>num_et</th>
            <th>nom_et</th>
            <th>prenom_et</th>
            <th>nom_mod</th>
            <th>id_mod</th>
         </thead>
         <tbody>
            <tr>
               <td>3666</td>
               <td>X</td>
               <td>Y</td>
               <td>Web 2.0</td>
               <td>E200</td>
            </tr>
            <tr>
               <td></td>
               <td></td>
               <td></td>
               <td>Web 3.0</td>
               <td>E222</td>
            </tr>
            <tr>
               <td>3667</td>
               <td>X</td>
               <td>O</td>
               <td>Web 3.1</td>
               <td>E223</td>
            </tr>
         </tbody>
      </table>
   </body>
</html>

Rendered

enter image description here

  • Related