|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
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. |
|
#2
|
|||
|
|||
|
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. |
|
#3
|
|||
|
|||
|
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 |
![]() |
| Viewing: Dev Shed Forums > Programming Languages - More > XML Programming > formatting a table with xslt |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|