Mike
Mike

Reputation: 73

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.

Upvotes: 0

Views: 302

Answers (1)

Martin Honnen
Martin Honnen

Reputation: 167716

I haven't understood which table structure you want but inside of a <xsl:for-each select = "/keyboard/brand"> you can use e.g. <th rowspan="{count(model)}"> to get e.g. <th rowspan="3"> for your sample, that is a cell that spans three rows where you then might want to populate them with an inner <xsl:for-each select="model"><tr><td>...</td>...</tr></xsl:for-each>.

Upvotes: 0

Related Questions