【问题标题】:Loop Through Table [duplicate]循环表[重复]
【发布时间】:2016-09-21 21:47:19
【问题描述】:

我正在尝试逐列遍历表格并将其转换为单个 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>

【问题讨论】:

标签: xslt xpath


【解决方案1】:

使用@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

【讨论】:

  • 这对任何人都没有帮助,因为它缺乏上下文。
  • 这怎么缺少上下文?你的回答缺乏背景。如果您想提供更多关于您的意思的信息,我很高兴编辑此提交。此代码 xsl 在我的原始代码中使用我的 XML 中的代码来拉取每一行的第一个表格单元格。然后它返回顶部表格行并为每一行拉出第二个表格单元格。它这样做直到它抓住了每个表格单元格。本质上是将跨行读取的表格转换为列格式的 div。 div 中的临时样式用于使代码输出更易于遵循。
  • 它缺少上下文,因为它以 'xsl:when' 开头。为了使它有用,请发布一个完整的(即使是最小的)样式表。
猜你喜欢
  • 2018-11-08
  • 2018-10-29
  • 2019-03-11
  • 2015-06-12
  • 1970-01-01
  • 2017-11-24
  • 1970-01-01
  • 2014-03-23
  • 1970-01-01
相关资源
最近更新 更多