Welcome to WebmasterWorld Guest from

Forum Moderators: httpwebwitch

Message Too Old, No Replies

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


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="/">
<table style="border: 1px solid #ff0000;" align="center">
<xsl:for-each select="archive/edition">
<td align="center">
<span style="font:12px Verdana, sans-serif;">
<xsl:value-of select="edname"/>
<br />
<xsl:element name="A">
<xsl:attribute name="HREF">
<xsl:value-of select="path"/>
<xsl:element name="IMG">
<xsl:attribute name="SRC">
<xsl:value-of select="image"/>
<xsl:attribute name="BORDER">0</xsl:attribute>
<xsl:attribute name="ALT">
<xsl:value-of select="edname"/>
<br />
<span style="font:12px Verdana, sans-serif;">
<xsl:value-of select="date"/>

And here is the XML file:

<?xml version="1.0" encoding="windows-1250"?>
<edname>Demo Section</edname>
<date>June 5th, 2010</date>
<edname>Demo Section Part 2: The Return</edname>
<date>July 28th, 2015</date>

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.

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