RE: [xsl] Newbie, question about looping

Subject: RE: [xsl] Newbie, question about looping
From: "Passin, Tom" <tpassin@xxxxxxxxxxxx>
Date: Wed, 23 Oct 2002 12:53:56 -0400
[Erick Todd]

> Here is my problem.  I have an xml document of items.  And I 
> need to display these items in a table 3 across.
> 
> So every 3 I try to put in a </tr><tr> but that is invalid.  
> Has anyone had to try and do this??
> 

Xslt is mostly about selecting a set of nodes and then doing something
with them.  So you should try to find solutions that select the right
set of nodes, rather than think about looping through.  It is usually
more productive. 

You also have to think in terms of creating complete elements at a time,
not parts.

In this case, you want to select every third item, because those
elements will represent the start of each row.  For each item in that
set of nodes, you want to output it and its next two sibling items,
wrapped in <tr>..</tr> element.  Right?

Here is a simple way to do that, assuming that you have a series of
"item" elements like this:

<root>
   <item>1</item>
   <item>2</item>
   ...
</root>

=================================================
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
<xsl:output encoding='iso-8859-1'/>

<xsl:template match="/root">
<results>
	<xsl:apply-templates select='item[position() mod 3 =0]'/>
</results>
</xsl:template>

<xsl:template match='item'>
<tr>
	<td><xsl:value-of select='.'/></td>
	<td><xsl:value-of select='following-sibling::item[1]'/></td>
	<td><xsl:value-of select='following-sibling::item[2]'/></td>
</tr>
</xsl:template>

</xsl:stylesheet>
====================================================================

Cheers,

Tom P

 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread