0

I am attempting to loop through a table by column and transform it to individual divs. Id like to have tr[1]/td[1], tr[2]/td[1], tr[3]/td[1] ... all be part of one div. Then I'd like to move on to tr[1]/td[2], tr[2]/td[2], tr[3]/td[2].

I am able to transform my table when I use simpler code, but I'm having issues jumping down each tr row. Below is my XSLT. Any help would be great. I'm pretty new to XSLT, but not programming. The divs/styling is simply to make the output easier to see.

XML:

  <table class="table-with-caption">
  <thead>
    <tr>
      <th>Column Title 1</th>
      <th>Column Title 2</th>
      <th>Column Title 3</th>
      <th>Column Title 4</th>
    </tr>
  </thead>
  <tfoot>
    <tr>
      <td class="alert-info table-scroll-option" colspan="4">column</td>
    </tr>
  </tfoot>
  <tbody>
    <tr>
      <td>TR1, TD1</td>
      <td><span>TR1, TD2</span></td>
      <td><span>TR1, TD3</span></td>
      <td><span>TR1, TD4</span></td>
    </tr>
  <tr>
    <td><span>TR2, TD1</span></td>
    <td>Atque ab corporis modi deserunt adipisci, expedita temporibus.</td>
    <td>Vitae, sint ullam velit accusantium, dolorum ad dolorem.</td>
    <td>Iusto expedita minus praesentium vero voluptatum aut laboriosam.</td>
  </tr>
  <tr>
    <td><span>TR3, TD1</span>.</td>
    <td>Consequuntur corporis quo libero fuga ducimus provident minus.</td>
    <td>Natus reiciendis molestias earum, dolorum similique amet quasi.</td>
    <td>Consequuntur sunt minus laudantium, magnam labore, debitis quia!</td>
  </tr>
  </tbody>
</table>

XSL:

    <xsl:for-each select="tbody/tr[position()&lt;=$count-rows]">
      <xsl:variable select="1" name="counter" />
      <div style="border:thin solid red; padding 5px; margin:5px;">
        <xsl:for-each select="tr[$counter]/td[position() le $count-cols]">
          <div style="border:thin solid blue; padding 5px; margin:5px;">
            <xsl:apply-templates select="node()[name() != 'caption']|attribute()[name() != 'class' and name() != 'tfoot']" />
          </div>
          <xsl:with-param name="counter" select="$counter + 1"/> 
        </xsl:for-each>

      </div>
    </xsl:for-each>
  </div>
</xsl:when>

Expected Output:

<div style="border:thin solid red; padding 5px; margin:5px;">
   <div style="border:thin solid blue; padding 5px; margin:5px;">TR1, TD1</div>
   <div style="border:thin solid blue; padding 5px; margin:5px;"><span>TR2, TD1</span></div>
   <div style="border:thin solid blue; padding 5px; margin:5px;"><span>TR3, TD1</span></div>
</div>
NaCo
  • 15
  • 5

1 Answers1

0

Using the resources @michael.hor257k submitted, I was able to figure out my code. Here is what I came up with:

<xsl:when test="tfoot/tr/td = 'column'">
  <div class="grid ">
    <xsl:variable name="row" select="tbody/tr"/>
    <xsl:variable name="col" select="tbody/tr[1]/td"/>

    <xsl:for-each select="$col">    
      <xsl:variable name="i" select="position()" />
            <div class="grid_item" style="border:thin solid red; padding 5px; margin:5px;"> 
                <xsl:for-each select="$row">
                    <div style="border:thin solid blue; padding 5px; margin:5px;">
                        <xsl:value-of select="td[$i]"/><br/><br/>
                    </div>
                </xsl:for-each>
            </div>
            <xsl:variable name="i" select="$i+1" />
      </xsl:for-each>
  </div>
</xsl:when>

That produced the following output that converts the table to divs: Div Output

NaCo
  • 15
  • 5
  • This is not helpful to anyone because it lacks context. – michael.hor257k Sep 23 '16 at 20:44
  • How does this lack context? Your response lacks context. If you'd provide more info on what you mean, I be glad to edit this submission. This code xsl uses the code in my XML in my original code to pull the first table cell for each row. Then it goes back to the top table row and pulls the 2nd table cell for each row. It does this until it has grabbed every table cell. Essentially turning a table that is read across rows into a column formatted divs. The temporary styling in the divs was used to make the code output easier to follow. – NaCo Sep 25 '16 at 14:23
  • It lacks context because it starts with 'xsl:when'. To make it useful, post a **complete** (even if minimal) stylesheet. – michael.hor257k Sep 25 '16 at 14:37