[xsl] laying out list of nodes in 2 table columns

Subject: [xsl] laying out list of nodes in 2 table columns
From: "Wong Chin Shin" <publicbbs@xxxxxxxxxxxxxxxx>
Date: Thu, 18 Nov 2004 02:24:41 +0800
Hi,

I've got an XML file that looks like the following:

<Services>
	<ServiceItem>
		<ServiceName>Item 1</ServiceName>
	</ServiceItem>
	<ServiceItem>
		<ServiceName>Item 2</ServiceName>
	</ServiceItem>
	<ServiceItem>
		<ServiceName>Item 3</ServiceName>
	</ServiceItem>
	<ServiceItem>
		<ServiceName>Item 4</ServiceName>
	</ServiceItem>
</Services>

Basically I want to lay them out into a table of 2 columns which looks like
this:

<table>
	<tr><td>Item 1</td><td>Item 2</td></tr>
	<tr><td>Item 3</td><td>Item 4</td></tr>
</table>

So far, I've managed to come up with the XSL rules for the left column, but
I just can't seem to find the right node for the right column:

<xsl:template match="Services">
	<table border="1" cellpadding="0" cellspacing="0" width="90%"
align="center">
		<tbody>
			<xsl:for-each select="ServiceItem[(position()-1) mod
2 = 0]">
				<xsl:apply-templates select="." />
			</xsl:for-each>
		</tbody>
	</table>
</xsl:template>
<xsl:template match="ServiceItem[(position()-1) mod 2 = 0]">
	<tr>
		<td width="50%" valign="top">
			<p><xsl:value-of select="ServiceName" /></p>
		</td>
		<td>
			<xsl:for-each
select="parent/following-sibling::node()[position() &lt; 2]">
				<xsl:value-of select="ServiceName" />
			</xsl:for-each>
		</td>
	</tr>
</xsl:template>

I'm now still trying to find the right rule for the 2nd column... Can
anybody help? I'm basically a newbie, and trying to do things right... I
guess the ugly workaround is to force 2 blocks on data into one XML node n
then do a for-each but that would really defeat the purpose...

Thanks!
Wong

Current Thread