Welcome to WebmasterWorld Guest from 54.211.101.8

Forum Moderators: httpwebwitch

Limiting number of columns per row in XSLT

Dynamically create a table with a max of 2 or 3 columns per row.

   
10:50 pm on May 30, 2009 (gmt 0)

5+ Year Member



Hello,

I've been playing around with XSLT, trying to create tables in which each column contains a couple lines of text and a hyperlinked image. What I can't figure out is how to specify not to exceed 2 - or sometimes 3 - columns per row.

Here's my XSL file:


<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<table style="border: 1px solid #ff0000;" align="center">
<tr>
<xsl:for-each select="archive/edition">
<td align="center">
<span style="font:12px Verdana, sans-serif;">
<xsl:value-of select="edname"/>
</span>
<br />
<xsl:element name="A">
<xsl:attribute name="HREF">
<xsl:value-of select="path"/>
</xsl:attribute>
<xsl:element name="IMG">
<xsl:attribute name="SRC">
<xsl:value-of select="image"/>
</xsl:attribute>
<xsl:attribute name="BORDER">0</xsl:attribute>
<xsl:attribute name="ALT">
<xsl:value-of select="edname"/>
</xsl:attribute>
</xsl:element>
</xsl:element>
<br />
<span style="font:12px Verdana, sans-serif;">
<xsl:value-of select="date"/>
</span>
</td>
</xsl:for-each>
</tr>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

And here is the XML file:


<?xml version="1.0" encoding="windows-1250"?>
<archive>
<edition>
<edname>Demo Section</edname>
<image>someimage_20090528.jpg</image>
<date>June 5th, 2010</date>
<path>../20090528/</path>
</edition>
<edition>
<edname>Demo Section Part 2: The Return</edname>
<image>someimage_20090521.jpg</image>
<date>July 28th, 2015</date>
<path>../20090528/</path>
</edition>
</archive>

The problem as it stands is that it will just continually make new columns each time there is a new edition node. I need to find out how to set a maximum of 2 columns.

12:24 am on May 31, 2009 (gmt 0)

WebmasterWorld Administrator httpwebwitch is a WebmasterWorld Top Contributor of All Time 10+ Year Member



you could loop through the edition nodes using a foreach [w3schools.com] loop, selecting the first n items using position() [xsltfunctions.com] in your XPATH.

fn:position()
Returns the index position of the node that is currently being processed
Example: //book[position()<=3]
Result: Selects the first three book elements

 

Featured Threads

Hot Threads This Week

Hot Threads This Month