homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / XML Development
Forum Library, Charter, Moderators: httpwebwitch

XML Development Forum

Limiting number of columns per row in XSLT
Dynamically create a table with a max of 2 or 3 columns per row.

5+ Year Member

Msg#: 3923208 posted 10:50 pm on May 30, 2009 (gmt 0)


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.



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

Msg#: 3923208 posted 12:24 am on May 31, 2009 (gmt 0)

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

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / XML Development
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved