如何在XSLT中每行显示2条记录

时间:2013-08-02 13:17:25

标签: sharepoint xslt xslt-1.0

我想在SharePoint网站的列表中显示每行2条记录。

我正在使用XSLT。这就是我想要显示记录的方式:

<tr>
    <td>Record1</td>
    <td>Record2</td>
</tr>
<tr>
    <td>Record3</td>
    <td>Record4</td>
</tr>

我已经编写了以下代码来完成这项工作,但是在线写错了。基本上XSLT不允许我在没有关闭的情况下编写。但我希望这样,否则我的逻辑将无法运作。有没有办法实现这个目标?

CODE

<xsl:choose>
    <xsl:when test="position() mod 2 = 1">
        <tr> (this is my error line)
    </xsl:when>
</xsl:choose>

<td>rest of my code will come here</td>

<xsl:choose>
    <xsl:when test="position() mod 2 = 0">
        </tr>
    </xsl:when>
</xsl:choose>  

修改
这就是我的完整SharePoint代码的样子。

<xsl:template match="/" xmlns:ddwrt="http://schemas.microsoft.com/WebParts/v2/DataView/runtime" ddwrt:ghost="" xmlns:ddwrt2="urn:frontpage:internal">
<table width="327" border="0" cellspacing="0" cellpadding="0">
    <tr>
        <td>Links</td>
    </tr>
    <tr>
        <td>
            <table width="327" border="0" cellspacing="0" cellpadding="0">
                <xsl:apply-templates />
            </table>
        </td>
    </tr>
</table>
</xsl:template>

<xsl:template match="Row" name="items">
<xsl:choose>
    <xsl:when test="position() mod 2 = 1">
        <tr> (this is my error line)
    </xsl:when>
</xsl:choose>

<td>rest of my code will come here</td>

<xsl:choose>
    <xsl:when test="position() mod 2 = 0">
        </tr>
    </xsl:when>
</xsl:choose>  
</xsl:template>

所以基本上它代表<xsl:apply-templates />的代码,这是从<xsl:template match="Row" name="items">开始的完整代码块被重复并填充行,所以这里没有for循环。

编辑 - 完成XSL代码

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" exclude-result-prefixes="xsl" xmlns:ddwrt2="urn:frontpage:internal">
<xsl:output method="html" indent="yes"/>
<xsl:param name="ViewAll" />

<!-- This template is the "wrapper" or "container" for the custom view. -->
<xsl:template match="/" xmlns:ddwrt="http://schemas.microsoft.com/WebParts/v2/DataView/runtime" ddwrt:ghost="" xmlns:ddwrt2="urn:frontpage:internal">

<!-- This is the actual wrapper element that will be emitted -->
<table width="327" border="0" cellspacing="0" cellpadding="0">
    <tr>
        <td colspan="3">
            <table width="327" border="0" cellspacing="0" cellpadding="0">
                <tr>
                <td width="10" height="30" class="webpart_img1"></td>
                <td width="307" height="30" class="webpart_img2">Links</td>
                    <td width="10" height="30" class="webpart_img3"></td>
            </tr>
            </table>
        </td>
    </tr>
    <tr>
        <td height="1" colspan="3"></td>
    </tr>
    <tr>
        <td width="10" height="105" class="quicklink_img1"></td>
        <td width="307" height="105" class="quicklink_img1">
            <table width="307" border="0" cellspacing="0" cellpadding="0">
                <xsl:apply-templates />
            </table>
        </td>
        <td width="10" height="105" class="quicklink_img1"></td>
    </tr>
    <tr>
        <td width="10" height="6" class="quicklink_img2"></td>
        <td width="307" height="6" class="quicklink_img1"></td>
        <td width="10" height="6" class="quicklink_img3"></td>
    </tr>
</table>
<!-- end wrapper -->
</xsl:template>

<!-- This template is for the repeating content -->
<xsl:template match="Row" name="repeat">
    <tr>
        <td width="16" height="21"><img src="images/mybullet.png"/></td>
        <td width="142" height="21"><a href="URL" class="quicklink">Title of URL</a></td>
        <td width="11"></td>
        <td width="16" height="21"><img src="images/mybullet.png"/></td>
        <td width="142" height="21"><a href="URL" class="quicklink">Title of URL</a></td>
    </tr>
</xsl:template>
</xsl:stylesheet>

我正在尝试显示来自SharePoint中列表的链接。该列表有两列:

标题
网址

所以我希望每行显示2个标题,并将其URL作为超链接。

1 个答案:

答案 0 :(得分:2)

您必须仅对奇数记录进行循环,然后使用following-sibling轴将记录与下一个记录合并:

    <xsl:for-each select="*[position() mod 2 = 1]">
      <tr>
        <td>
          <xsl:value-of select="."/>
        </td>
        <td>
          <xsl:value-of select="following-sibling::*[1]"/>
        </td>
      </tr>        
    </xsl:for-each>

如果记录数是奇数而您不想要空td,则可以添加如下测试:

    <xsl:for-each select="*[position() mod 2 = 1]">
      <tr>
        <td>
          <xsl:value-of select="."/>
        </td>
        <xsl:if test="following-sibling::*">
          <td>
            <xsl:value-of select="following-sibling::*[1]"/>
          </td>
        </xsl:if>
      </tr>        
    </xsl:for-each>

使用仅匹配奇数记录的模板可以获得for-each相同的结果:

<xsl:template match="Row[position() mod 2 = 1]">
  <tr>
    <td>
    <td width="16" height="21"><img src="images/mybullet.png"/></td>
    <td width="142" height="21"><a href="{@URL}" class="quicklink"><xsl:value-of select="@Title"/></a></td>
    <xsl:if test="following-sibling::*">
      <td width="11"></td>
      <td width="16" height="21"><img src="images/mybullet.png"/></td>
      <td width="142" height="21"><a href="{following-sibling::*[1]/@URL}" class="quicklink"><xsl:value-of select="following-sibling::*[1]/@Title"/></a></td>
    </xsl:if>
  </tr>
</xsl:template>