Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 9th, 2005, 11:29 AM
Registered User
 
Join Date: Jun 2005
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Generating a 3 Column Table

Hello

I'd like to generate a three column HTML table using XSLT.

For example if I have this XML:-
Code:
--------
<product>
   <name>Product 1</name>
</product>
<product>
   <name>Product 2</name>
</product>
<product>
   <name>Product 3</name>
</product>
<product>
   <name>Product 4</name>
</product>
<pro.... etc, etc, etc, etc
-----------
And I want to produce a three column table using something like this:-
----
Code:
<table>
   <tr>
      <td>Product 1</td>
      <td>Product 2</td>
      <td>Product 3</td>
   </tr>
   <tr>
      <td>Product 4</td>
      <td>etc, etc, etc
----

How would I go about this?

I've looked through the W3C XSLT & XPath specs and I can't see a way, but I'm sure there is one.

Presumably I need to do something like go through the product nodes, but go through three at once, then write the <td>s.

Kind of like this in pseudo code
Code:
for (i = 0; i < total_number_of_products; i = (i + 3)) {
   echo '<tr>';
   for (j = 0; j < 3; j++) {
      echo '<td>' + product_name + '</td>';
   }
   echo '</tr>';
}
--- or ---
Code:
<table>

<xsl:for-each select="product"> 
   <tr>
       <xsl:for-each (of the products just selected)
           <td><xsl:value-of select="name" /></td>
       </xsl:for-each>
   </tr>
</xsl:for-each>
</table>
----
Hope this makes sense?

Any help would be much appreciated.

Thanks

Gareth
 
Old June 10th, 2005, 05:37 AM
Authorized User
 
Join Date: Mar 2005
Posts: 33
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Step through every product and add "<td>", "</td>" around each match. To produce a new row just count the records and use the modulo operator inserting "</tr><tr>" every time its equal to zero.

 
Old June 10th, 2005, 05:43 AM
Authorized User
 
Join Date: Mar 2005
Posts: 33
Thanks: 0
Thanked 0 Times in 0 Posts
Default

<table>
<tr>
<xsl:for-each select="product">
 <td><xsl:value-of select="name" /></td>
 <xsl:if test="count(preceding-sibeling::node()) mod 3 = 0">
  </tr><tr>
 </xsl:if>
</xsl:for-each>
</tr>
</table>

 
Old June 10th, 2005, 07:53 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

That stylesheet (from arcuza) isn't even well-formed XML!

Take a look at

http://www.dpawson.co.uk/xsl/sect2/N7450.html#d9655e13



Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old June 14th, 2005, 01:22 PM
Registered User
 
Join Date: Jun 2005
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks very much, that helped loads

Gareth

Quote:
quote:Originally posted by mhkay
 That stylesheet (from arcuza) isn't even well-formed XML!

Take a look at

http://www.dpawson.co.uk/xsl/sect2/N7450.html#d9655e13



Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference





Similar Threads
Thread Thread Starter Forum Replies Last Post
Help on generating HTML table please!! minhnghia XSLT 5 September 2nd, 2007 12:35 PM
Generating Pivot Table from MS Access Database. rupen Excel VBA 0 April 5th, 2006 05:30 AM
getting identity column from the table g_vamsi_krish SQL Server 2000 1 March 15th, 2006 05:05 PM
generating a table w. xslt, newbie problem themuffinman_swe XSLT 5 November 26th, 2005 01:05 PM
how to make column of table 1 = to column of table gilgalbiblewheel Classic ASP Databases 4 October 11th, 2004 11:57 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.