XML Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming Languages - MoreXML Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old December 4th, 2003, 10:40 AM
cdodger cdodger is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2003
Posts: 2 cdodger User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
formatting a table with xslt

hi,

i'm trying to create a table of thumbnailed images which link to the full-size copies.

i've got the following xml data:

<images>
<image id="sky1">
<title>clouds</title>
</image>
<image id="beach">
<title>sandy beach</title>
</image>
<image id="stars">
<title>the night sky</title>
</image>
<image id="sea">
<title>deep blue</title>
</image>
.
.
.
</images>

i'm trying to put it in a table with 5 columns per row by using the following xslt:

<xsl:template match="images">
<table>
There are <xsl:value-of select="count(image)"/> images below.
<xsl:for-each select="image">
<xsl:choose>
<xsl:when test="count(preceding::image) mod 5 = 0">
<tr><td><a href="images/{@id}.jpg"><img src="images/tn_{@id}.jpg"/></a></td></tr>
</xsl:when>
<xsl:otherwise>
<td><a href="images/{@id}.jpg"><img src="images/tn_{@id}.jpg"/></a></td>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</table>
</xsl:template>

this doesn't work. the html formatting comes out wrong, so only one line has the <tr> tags. what i want to do is put 5 image elements within <tr></tr> tags. i can't work out how to do this.

today i thought of perhaps having a variable which could be assigned the string resulting from applying a template (to do the <td></td> tags), and if the test for mod 5 works, put that variable between <tr></tr> tags. i don't think this will work because i read variables can only be written to when they are declared.

has anyone any ideas please? i don't want to add another tag to group every 5 elements.

thanks in advance,
chris.

Reply With Quote
  #2  
Old December 4th, 2003, 03:22 PM
teedee teedee is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2003
Posts: 42 teedee User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 5
Only one way to do this kind of thing, use a template that calls itself:

Code:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
	<xsl:param name="intImagesPerTableRow">5</xsl:param>
	<xsl:template match="/">
		<html>
			<head/>
			<body>
				<table>
					<xsl:call-template name="imagesRow">
						<xsl:with-param name="intStartPos">1</xsl:with-param>
					</xsl:call-template>
				</table>
			</body>
		</html>
	</xsl:template>
	<xsl:template name="imagesRow">
		<xsl:param name="intStartPos"/>
		<xsl:variable name="intEndPos" select="$intStartPos + $intImagesPerTableRow"/>
		<tr>
		<xsl:for-each select="/images/image[(position() >= $intStartPos) and (position() < $intEndPos) ]">
			<td>
				<img src="{@id}.jpg" alt="{title}"/>
			</td>
		</xsl:for-each>
		</tr>
		<!--
		Are there images after intEndPos?
		-->
		<xsl:if test="/images/image[position() > =$intEndPos]">
			<!--
			Call this template again starting at intEndPos
			-->
			<xsl:call-template name="imagesRow">
				<xsl:with-param name="intStartPos" select="$intEndPos"/>
			</xsl:call-template>
		</xsl:if>
	</xsl:template>
</xsl:stylesheet>

Last edited by teedee : December 4th, 2003 at 06:40 PM.

Reply With Quote
  #3  
Old December 8th, 2003, 10:45 AM
cdodger cdodger is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2003
Posts: 2 cdodger User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
thanks a lot teedee, i've got it working now. i'm going to try to use a similar approach to display 20 images per page (where each page is called page1.html, page2.html, etc).

chris

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreXML Programming > formatting a table with xslt


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
Stay green...Green IT