我希望在SharePoint网站的列表中每行显示2条记录。
我正在使用XSLT。这就是我想要显示记录的方式:
<tr>
<td>Record1</td>
<td>Record2</td>
</tr>
<tr>
<td>Record3</td>
<td>Record4</td>
</tr>
我已经写了以下代码来完成这项工作,但在写的地方出现了错误。基本上,XSLT不允许我在不关闭的情况下进行编写。但我希望这样,否则我的逻辑就不会起作用。那么,有办法实现这一点吗?
代码
<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循环。
EDIT-完整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中的列表的链接。该列表有两列:
标题
URL
所以我想每行显示2个标题,并将它们的URL作为超链接。
您必须仅在奇数记录上循环,然后使用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>