【问题标题】:如何为此 xml 创建一个 xslt,输出是带有列 {num Inscription,nom,prenom,nom Module} 和条件 etudiant.id_promo=module.id_prom 的表
【发布时间】:2022-01-23 03:15:45
【问题描述】:

在这里,我需要每个学生通过 id_promo 摸索他的模块,但是 我不知道 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> 
在这里,我需要每个学生通过 id_promo 摸索他的模块 id'ont 知道语法 使用 id_promo 需要将拖车表收集到一张表中
Name prename module
X Y Web 2.0
Web 3.0

【问题讨论】:

  • 请就您在尝试完成此操作时遇到的困难提出一个具体问题。否则,看起来您只是在寻找某人为您编写代码。 -- 提示:使用key 来解析交叉引用。
  • 困难在于:在我的文档 xml 中有许多 节点,每个节点学生都有许多 ,将 XML 文件转换为每个标签所需的 HTML 表 name_mod 的许多标签,例如循环内循环,或 for_each 内的 for_eache,但这不起作用
  • 我们需要的那个表:SELECT num_et,nom_et,prenom_et,nom_mod FROM etudiant,modules WHERE etudiant.id_promo=modules.id_promo;
  • 请编辑您的问题并添加您当前的尝试和预期结果(作为 HTML 代码)。
  • 我想现在很清楚了

标签: xml xslt xhtml


【解决方案1】:

尝试类似:

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>

当它应用于以下示例输入时:

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>

结果将是:

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>

渲染

【讨论】:

  • 谢谢兄弟晚安
猜你喜欢
相关资源
最近更新 更多
热门标签