简体   繁体   中英

XSL create html header with rowspan = number of iterations in for-each loop

What I am trying to do is format a table header to span the number of rows entered in a for-each loop. I thought to set up a separate for-each loop to count the number of entries and increment a variable to use later, but I guess that does not work with xsl. This is my first attempt at using it. Here is how I tried to set it up:

<xsl:variable name= "increment" select="0" />
<xsl:for-each select = "/keyboard/brand">
       <xsl:for-each select = "/keyboard/brand/model">
              <xsl:variable name="increment" select="$increment + 1" />
       </xsl:for-each>
       <tr>
              <th rowspan = $increment><xsl:value-of select = "brand"/></td>
              <xsl:for-each select = "/keyboard/brand/model">
                 <td><xsl:value-of select = "keytype"/></td>     
                 <td><xsl:value-of select = "backlit"/></td>
       </tr>
       <tr>
              </xsl:for-each>
           <xsl:variable name= "increment" select="0" />
        </xsl:for-each>

Here is a portion of the xml I am working with:

    <keyboard>
    <brand>Logitech
      <model>MK270
         <keytype>Membrane</keytype>
         <backlit check = "no">Qwerty</backlit>
         <backlit check = "no">Numpad</backlit>
         <interface>Wireless USB</interface>
         <batterypowered>Yes</batterypowered>
         <programmablekeys location = "none">None</programmablekeys>
         <launchkeys location = "top">Media Controls</launchkeys>
      </model>

      <model>K350
         <keytype>Membrane</keytype>
         <backlit check = "no">Qwerty</backlit>
         <backlit check = "no">Numpad</backlit>
         <interface>Wireless USB</interface>
         <batterypowered>Yes</batterypowered>
         <programmablekeys location = "none">None</programmablekeys>
         <launchkeys location = "side">Zoom Controls</launchkeys>
         <launchkeys location = "top">Media Controls</launchkeys>
      </model>

      <model>G110
         <keytype>Membrane</keytype>
         <backlit check = "yes">Qwerty</backlit>
         <backlit check = "yes">Numpad</backlit>
         <interface>USB 2.0</interface>
         <batterypowered>No</batterypowered>
         <programmablekeys location = "side">12 programmable G-keys</programmablekeys>
         <launchkeys location = "top">Audio/Mic/Media Controls</launchkeys>
      </model>   
   </brand>

What I would like to do is have the Brand span all of the rows that the models take up. I am going to put each model in it's own row.

I have read that I am supposed to use the position() function instead of how I tried to handle it, but I am not sure how I would implement that for this situation, since I would need to determine the rowspan size before position() would finish counting.

I am sure there are other bugs I will have to work out, like I haven't quite worked out how to not add the last tr on the final iteration, but that will come later. So will the rest of the data I need to add.

我还不了解您想要哪种表结构,但是在<xsl:for-each select = "/keyboard/brand">您可以使用例如<th rowspan="{count(model)}">来获得例如<th rowspan="3">对于您的示例, <th rowspan="3">是一个跨越三行的单元格,然后您可能希望在其中用内部<xsl:for-each select="model"><tr><td>...</td>...</tr></xsl:for-each>填充它们<xsl:for-each select="model"><tr><td>...</td>...</tr></xsl:for-each>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM