2

I want to display 2 records per row from a list in my SharePoint site.

I am using XSLT. This is how I want to display records:

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

I have written the following code to do this job but getting error on line where is written. Basically XSLT is not allowing me to write without closing . But I want it that way otherwise my logic won't work. So is there a way to achieve this?

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>  

EDIT
This is how my complete SharePoint code looks like.

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

So basically the code where it says <xsl:apply-templates />, this is where complete block of code starting from <xsl:template match="Row" name="items"> is repeated and filled with rows so there is no for loop here.

EDIT - Complete XSL Code

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

I am trying to display links which are coming from a List in SharePoint. The list has two columns:

Title
URL

So I want to display 2 titles per row with their URL as hyper link.

4

1 回答 1

2

You have to loop on the odd records only and then combine a record with the next one using the following-sibling axis:

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

If the number of records is odd and you don't want an empty td you can add a test like this:

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

Avoiding a for-each the same result can be obtained using a template that matches only the odd records:

<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>
于 2013-08-02T13:31:20.597 回答