XSLT creating a table with varying amount of columns

不问归期 提交于 2019-12-06 16:46:04

Easy when you step back a bit. Split the problem into smaller parts.

<xsl:param name="numColumns" select="3" />

<xsl:template match="channel">
  <table>
    <!-- all items that start a column have position() mod x = 1 -->
    <xsl:apply-templates 
       select="item[position() mod $numColumns = 1]" 
       mode="tr"
    />
  </table>
</xsl:template>

<xsl:template match="item" mode="tr">
  <tr>
    <!-- all items make a column: this one (.) and the following x - 1 -->
    <xsl:apply-templates 
      select=".|following-sibling::item[position() &lt; $numColumns]"
      mode="td"
    />
  </tr>
</xsl:template>

<xsl:template match="item" mode="td">
  <td>
    <!-- calculate optional colspan for the last td -->
    <xsl:if test="position() = last() and position() &lt; $numColumns">
      <xsl:attribute name="colspan">
        <xsl:value-of select="$numColumns - position() + 1" />
      </xsl:attribute>
    </xsl:if>
    <xsl:value-of select="title"/>
  </td>
</xsl:template>
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!