Go Back   CodingForums.com > :: Client side development > XML

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 08-07-2012, 06:42 AM   PM User | #1
tyelford
New to the CF scene

 
Join Date: Aug 2012
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
tyelford is an unknown quantity at this point
XSL Sheets with a Random number

Hi there,

I am creating a WebPart page in SharePoint that loads a series of announcements and displays one of them at random each time the page loads. So far I have everything setup except for the Random part. Below is a small code snippet where I am having the issues

Code:
<xsl:template name="dvt_1.body">
   <xsl:param name="Rows"/>
   <xsl:for-each select="$Rows[1]" >
       <xsl:call-template name="dvt_1.rowview"/> 
   </xsl:for-each>
</xsl:template>
The above works perfect by loading the first announcement item in the list on displaying it on the WebPart page. If I change this part:
Code:
<xsl:for-each select="$Rows[1]" >
to
Code:
<xsl:for-each select="$Rows[2]" >
as you would expect it displays the second announcement in the list.

What I have tried is to do this
Code:
<xsl:for-each select="$Rows[ddwrt:Random(1, count($Rows))]" >
which by the description of what Random does it should select a number between 1 and the size of Rows (In this case the number of Rows). However what I get is that it just displays all the rows instead of one.

Does anyone know why it is selecting all Rows and not just one or if there is a different tag to be used instead of the for-each one.

Below is the entire style sheet code incase it helps at all.

Code:
<xsl:stylesheet xmlns:x="http://www.w3.org/2001/XMLSchema" xmlns:d="http://schemas.microsoft.com/sharepoint/dsp" version="1.0" exclude-result-prefixes="xsl msxsl ddwrt" xmlns:ddwrt="http://schemas.microsoft.com/WebParts/v2/DataView/runtime" xmlns:asp="http://schemas.microsoft.com/ASPNET/20" xmlns:__designer="http://schemas.microsoft.com/WebParts/v2/DataView/designer" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:SharePoint="Microsoft.SharePoint.WebControls" xmlns:ddwrt2="urn:frontpage:internal">
	<xsl:output method="html" indent="no"/>
	<xsl:decimal-format NaN=""/>
						<xsl:param name="dvt_apos">'</xsl:param>
						<xsl:variable name="dvt_1_automode">0</xsl:variable>
						<xsl:template match="/">
							<xsl:call-template name="dvt_1"/>
	</xsl:template>
						<xsl:template name="dvt_1">
							<xsl:variable name="dvt_StyleName">Table</xsl:variable>
							<xsl:variable name="Rows" select="/dsQueryResponse/Rows/Row"/>
		<table border="0" width="100%" cellpadding="2" cellspacing="0">
			<tr valign="top">
				<xsl:if test="$dvt_1_automode = '1'" ddwrt:cf_ignore="1">
					<th class="ms-vh" width="1%" nowrap="nowrap"></th>
				</xsl:if>
				<!-- <th class="ms-vh" nowrap="nowrap">Title</th>
				<th class="ms-vh" nowrap="nowrap">Body</th> -->
				<th class="ms-vh" nowrap="nowrap">Current Notice Board</th>
			</tr>
			<xsl:call-template name="dvt_1.body">
				<xsl:with-param name="Rows" select="$Rows"/>
			</xsl:call-template>
		</table>
	</xsl:template>
	
						<xsl:template name="dvt_1.body">
							<xsl:param name="Rows"/>
							<xsl:for-each select="$Rows[1]" >
								<xsl:call-template name="dvt_1.rowview"/> 
								</xsl:for-each>
						</xsl:template>
			
						<xsl:template name="dvt_1.rowview">
		<tr>
		
			<xsl:if test="position() mod 2 = 1">
				<xsl:attribute name="class">ms-alternating</xsl:attribute>
			</xsl:if>
			
			<xsl:if test="$dvt_1_automode = '1'" ddwrt:cf_ignore="1">
				<td class="ms-vb" width="1%" nowrap="nowrap">
					<span ddwrt:amkeyfield="ID" ddwrt:amkeyvalue="ddwrt:EscapeDelims(string(@ID))" ddwrt:ammode="view"></span>
				</td>
			</xsl:if>
			<!--<td class="ms-vb">
				<xsl:value-of select="@Title"/>
			</td> -->
			<td class="ms-vb">
			
			<p><xsl:value-of select="@Title" />
			<xsl:value-of select="@Body" disable-output-escaping="yes"/> </p>
			</td>
			<!--<td class="ms-vb">
				<xsl:value-of select="ddwrt:FormatDate(string(@Modified), 1033, 5)"/>
			</td>-->
		</tr>
	</xsl:template>
</xsl:stylesheet>

Sorry for the messy code, SharePoint Designer is not the best tool to work with sometimes.

Thanks for you help
Tyson

Last edited by tyelford; 08-07-2012 at 06:50 AM..
tyelford is offline   Reply With Quote
Reply

Bookmarks

Tags
random, sharepoint, xml, xsl, xslt

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 04:06 AM.


Advertisement
Log in to turn off these ads.