XSLT创建具有不同列数的表 [英] XSLT creating a table with varying amount of columns

查看:67
本文介绍了XSLT创建具有不同列数的表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个RSS提要,我需要在表格中显示(它的衣服来自网上商店系统).RSS中的图像的宽度和高度各不相同,我想制作一张表格以显示所有图像.首先,我很乐意仅显示3列中的所有项目,但是在接下来的过程中,我需要能够通过参数指定表中的列数.我遇到一个显示tr标签的问题,并弄正确了,到目前为止,这是我的代码:

I have a RSS feed i need to display in a table (its clothes from a webshop system). The images in the RSS vary in width and height and I want to make a table that shows them all. First off i would be happy just to show all of the items in 3 columns, but further down the road i need to be able to specify through a parameter the amount of columns in my table. I run into a problem showing the tr tag, and making it right, this is my Code so far:

 <xsl:template match="item">
    <xsl:choose>      
      <xsl:when test="position() mod 3 = 0 or position()=1">
        <tr>
          <td>
            <xsl:value-of select="title"/>
          </td>
        </tr>
        </xsl:when>
      <xsl:otherwise>
        <td>
          <xsl:value-of select="title"/>
        </td>
      </xsl:otherwise>
    </xsl:choose>    
  </xsl:template> 

在RSS中,所有项目"标签在xml中都处于同一级别,因此到目前为止,我只需要显示标题即可.Thr的问题似乎是我需要指定tr元素的开始标签以及结束标签,并且无法将所有3个元素都放入其中,有人知道怎么做吗?

In RSS all "item" tags are on the same level in the xml, and thus far i need only the title shows. Thr problem seems to be that i need to specify the start tag as well as the end tag of the tr element, and cant get all 3 elements into it, anyone know how to do this?

推荐答案

退后一步很容易.将问题分成更小的部分.

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>

这篇关于XSLT创建具有不同列数的表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆