Altova Mailing List Archives>Archive Index >microsoft.public.xml Archive Home >Recent entries >Thread Prev - Generating 2x2 table [Thread Next] Re: Generating 2x2 tableTo: NULL Date: 1/3/2006 12:07:00 AM "cardinallijr" <cardinallijr@g...> wrote in message news:1136176088.856961.198100@g......
> Hi all, sorry about the simple question but I'm new to XSL. I have a
> XML with 4 elements,
: :
> What I want to do is to generate a HTML table with 2 columns(2
> products) in each row.
Try the following stylesheet,
- - - TwoByTwo.xsl
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
<xsl:template match="/">
<html>
<head>
<title>Demo 2x2 Table</title>
</head>
<body bgcolor='lightgray'>
<table>
<xsl:apply-templates select="/root/products/product[position() mod 2 = 1]" />
</table>
</body>
</html>
</xsl:template>
<xsl:template match="/root/products/product[position() mod 2 = 1]">
<tr>
<td>Product <xsl:value-of select="id" /></td>
<td>Product <xsl:value-of select="following-sibling::product/id"/></td>
</tr>
</xsl:template>
</xsl:stylesheet>
--
Takeaway from this is that you match on the first (leftmost) product in each row by
looking for the product in a position whose remainder is 1 after dividing by the
desired number of columns (in this case, 2).
This template is responsible for all products grouped into that row. You can use
XPath's following-sibling:: axis to reach successive products' content to pull it into
this template and make that content part of the cells you render in the same row.
Derek Harmon
| ||||||
| Company | Legal | Press | Partners | Careers | Sitemap | Contact Us | Altova Blog | Mobile | Full Site | |||
|
