循环通过表

时间:2016-09-21 21:47:19

标签: xslt xpath

我试图逐列循环并将其转换为单个div。我喜欢tr [1] / td [1],tr [2] / td [1],tr [3] / td [1] ......都是一个div的一部分。然后我想转到tr [1] / td [2],tr [2] / td [2],tr [3] / td [2]。

当我使用更简单的代码时,我能够转换我的表,但是我遇到了每个tr行跳过的问题。下面是我的XSLT。任何帮助都会很棒。我是XSLT的新手,但不是编程。 div /样式只是为了让输出更容易看到。

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>

预期输出:

<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>

1 个答案:

答案 0 :(得分:0)

使用提交的资源@ michael.hor257k,我能够找出我的代码。以下是我提出的建议:

<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>

这产生了以下输出,将表转换为div:Div Output