代码之家  ›  专栏  ›  技术社区  ›  Dav.id

使用XSLT显示表格记录,即记录显示x列,然后显示新行

  •  0
  • Dav.id  · 技术社区  · 16 年前

    我有以下XML:

    <?xml version="1.0" encoding="utf-8" ?>
    <data>
     <record>
      <id>1</id>
      <name>David</name>
      <age>40</age> 
     </record>
     <record>
      <id>2</id>
      <name>Tully</name>
      <age>38</age>
     </record>
     <record>
      <id>3</id> 
      <name>Solai</name>
      <age>32</age>
     </record>
     <record>
      <id>4</id> 
      <name>Michael</name>
      <age>49</age>
     </record>
     <record>
      <id>5</id> 
      <name>Tony</name>
      <age>19</age>
     </record>
     <record>
      <id>6</id> 
      <name>Ray</name>
      <age>26</age>
     </record>
     <record>
      <id>7</id> 
      <name>Leeha</name>
      <age>13</age>
     </record>
    
    </data>
    

    record 1   record2   record3   record4
    record 5   record6   record7   record8
    

    等等

    我现在有下面的XSL,至少可以说是不可靠的!

    <xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    
    <xsl:template match="/">
    
     <div>
     <table style="width: 200px" border="1">
    
      <tr>
      <xsl:for-each select="data/record">
    
       <xsl:if test="position() mod 4 = 0">
       <tr></tr>
       </xsl:if> 
    
       <td>
        <xsl:value-of select="name"></xsl:value-of>
        <br />
        <xsl:value-of select="age"></xsl:value-of>
       </td>
    
      </xsl:for-each>
      </tr>
    
     </table>
     </div>
    
    </xsl:template>
    
    </xsl:stylesheet>
    

    所以我的问题是,我在这条路上走对了吗。。还是有更简单、更可靠的方法来实现这一点?

    4 回复  |  直到 16 年前
        1
  •  0
  •   Erlock    16 年前

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    
       <xsl:output method="html"/> 
    
       <xsl:variable name="M" select="5"/> 
       <xsl:variable name="C" select="2"/> 
    
       <xsl:template match="/"> 
          <html> 
            <body> 
               <table border="1"> 
                 <xsl:for-each select="/data/record[position() mod ($M * $C) = 1]"> 
                    <tr>
                      <xsl:for-each select=".|following-sibling::record[position() mod $M = 0 and position() &lt; $M * $C]">
                        <td>
                          <xsl:for-each select=".|following-sibling::record[position() &lt; $M]">
                            <xsl:value-of select="name"/><br/>
                            <xsl:value-of select="age"/>
                          </xsl:for-each>
                        </td>
                      </xsl:for-each>
                      <xsl:if test="position() = last()">
                        <xsl:call-template name="empty-cells">
                          <xsl:with-param name="nb" select="$C - ceiling(count(.|following-sibling::record) div $M)"/>
                        </xsl:call-template>
                      </xsl:if>
                    </tr>
                 </xsl:for-each> 
               </table> 
            </body> 
          </html> 
       </xsl:template> 
    
       <xsl:template name="empty-cells">
        <xsl:param name="nb"/>
        <xsl:if test="$nb &gt;= 1">
            <td/>
            <xsl:call-template name="empty-cells">
              <xsl:with-param name="nb" select="$nb - 1"/>
            </xsl:call-template>
        </xsl:if>       
       </xsl:template>
    
    
    </xsl:stylesheet>
    

        2
  •  1
  •   Community Mohan Dere    9 年前

    看看这个: How can I break a table row in xsl after a specified count?

    或者也包括:

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    
      <xsl:template match="/">
    
        <div>
          <table style="width: 200px" border="1">
    
            <tr>
              <xsl:for-each select="data/record">
                <td>
                  <xsl:value-of select="name" /><br />
                  <xsl:value-of select="age" />
                </td>
                <xsl:if test="position() mod 4 = 0 and position() != last()">
                  <xsl:text disable-output-escaping="yes">
                     &lt;/tr&gt;&lt;tr&gt;
                  </xsl:text>
                </xsl:if>
              </xsl:for-each>
            </tr>
    
          </table>
        </div>
    
      </xsl:template>
    
    </xsl:stylesheet>
    
        3
  •  1
  •   Mike    16 年前
      <xsl:template match="/data">
        <div>
           <table style="width: 200px" border="1">
            <xsl:apply-templates select="record[position() mod 4 = 1]"/>
          </table>
        </div>
      </xsl:template>
      <xsl:template match="record">
        <row>
          <xsl:apply-templates select=". | following-sibling::record[position() &lt; 4]" mode="mode" />
        </row>
      </xsl:template>
      <xsl:template match="record" mode="mode">
        <cell>
          <xsl:value-of select="id"/>
        </cell>
        <xsl:if test="position() = last() and position() &lt; 3">
          <xsl:call-template name="complete-row">
            <xsl:with-param name="count" select="position()"/>
          </xsl:call-template>
        </xsl:if>
      </xsl:template>
      <xsl:template name="complete-row">
         <xsl:param name="count"/>
         <td>&#160;</td>
         <xsl:if test="($count + 1) &lt; 3">
            <xsl:call-template name="complete-row">
               <xsl:with-param name="count" select="$count + 1"/>
            </xsl:call-template>
         </xsl:if>
      </xsl:template>
    
        4
  •  0
  •   peter.murray.rust    16 年前

    我认为你大致上是对的,但你的答案是空的 tr tr 它不会做任何有用的事情,可能会弄脏浏览器。如果你发布了你期望的HTML,那么它将更清楚如何实现它。