homepage Welcome to WebmasterWorld Guest from 54.163.139.36
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.
hadiz

5+ Year Member



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

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.

 

httpwebwitch

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.

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

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