473,395 Members | 1,891 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Help - Generate N x 2 html table

Hi all, sorry about the simple question but I'm new to XSL. I have a
XML with 5 elements, for example:
<root>
<products>
<product>
<id>1</id>
</product>
<product>
<id>2</id>
</product>
<product>
<id>3</id>
</product>
<product>
<id>4</id>
</product>
<product>
<id>5</id>
</product>
</products>
</root>
What I want to do is to generate a HTML table with N rows(depending of
the number of elements) and 2 columns(2
products) for each row. For example:
<table>
<tr>
<td>Product 1</td>
<td>Product 2</td>
</tr>
<tr>
<td>Product 3</td>
<td>Product 4</td>
</tr>
<tr>
<td>Product 5</td>
</tr>
</table>
I've tried a lot of ways to do it with a XSL but couldn't get this to
work.
Please, Do you have an idea ?
P.S.: Sorry about my english.
--
Alberto Cardinalli Jr.

Jan 2 '06 #1
4 1405
cardinallijr wrote:
What I want to do is to generate a HTML table with N rows(depending of
the number of elements) and 2 columns(2
products) for each row. For example: [...] I've tried a lot of ways to do it with a XSL but couldn't get this to
work.
Please, Do you have an idea ?


Here's one of many ways to do this:

<xsl:template match="root">
<table>
<xsl:apply-templates
select="products/product[position() mod 2 != 0]" />
</table>
</xsl:template>

<xsl:template match="products/product[position() mod 2 != 0]">
<tr>
<td>Product <xsl:value-of select="id" /></td>
<xsl:if test="following-sibling::*">
<td>Product <xsl:value-of select="following-sibling::*/id" /></td>
</xsl:if>
</tr>
</xsl:template>
JW
Jan 2 '06 #2
cardinallijr wrote:
Hi all, sorry about the simple question but I'm new to XSL. I have a
XML with 5 elements, for example:
<root>
<products>
<product>
<id>1</id>
</product>
<product>
<id>2</id>
</product>
<product>
<id>3</id>
</product>
<product>
<id>4</id>
</product>
<product>
<id>5</id>
</product>
</products>
</root>
What I want to do is to generate a HTML table with N rows(depending of
the number of elements) and 2 columns(2
products) for each row.


FAQ. See the XSLT FAQ about arranging data in multiple columns at
http://www.dpawson.co.uk/xsl/sect2/N7450.html#d9550e13

///Peter
--
XML FAQ: http://xml.silmaril.ie/

Jan 2 '06 #3
Thanks JW, it works fine. I have just one more question. I have the
template below and I want to use it together with the previous XSL.

<xsl:template>
<td>
<img src="{product_image_link}" ...></img>
</td>
<td>
<xsl:value-of select="product_name"/><br/>
<xsl:value-of select="product_price"/>
</td>
</xsl:template>

What can I do to have not to repeat the XSL Code for the first node and
again for the following node ? Is there a way to do it ?

Thanks for your help,

--
Alberto

Jan 3 '06 #4
cardinallijr wrote:
What can I do to have not to repeat the XSL Code for the first node
and again for the following node ? Is there a way to do it ?


Not sure what you mean, but when the template you want to apply contains the
contents for a single row, you could do:

<xsl:template match="root">
<table>
<xsl:apply-templates
select="products/product[position() mod 2 != 0]" />
</table>
</xsl:template>

<xsl:template match="products/product[position() mod 2 != 0]">
<tr>
<xsl:call-template name="generate-row" />
</tr>
</xsl:template>

<xsl:template name="generate-row">
<td>Product <xsl:value-of select="id" /></td>
<xsl:if test="following-sibling::*">
<td>Product <xsl:value-of select="following-sibling::*/id" /></td>
</xsl:if>
</xsl:template>
JW
Jan 3 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: no one | last post by:
I have been searching various boards trying to figure out how to generate an image along with other html. I intend to put the image in a table, but for now I am just having problem getting the...
0
by: millw0rm | last post by:
Hi, here is the complete detail what i m trying 2 do. i got around 2000+ images with size/desp/image_no/slno etc TABLE `sheet1` ( `SlNo` int(11) NOT NULL auto_increment, `Image No` double...
6
by: Virginia Kirkendall | last post by:
Hi: I'm new with this & need help creating a XSL table that looks like the following: --------------------------------------------------------- | | | | | |...
2
by: Richard | last post by:
**** Post for FREE via your newsreader at post.usenet.com **** HI, I am working on a project where I need to input data to a (local) HTML page using multiple form elements, such as text,...
0
by: serge calderara | last post by:
Dear all, I have a dataset object which is the source of a datagrid server control. In my application I am using that dataset to generate an XML file. The am using table mapping to defined my...
0
by: ward | last post by:
Greetings. Ok, I admit it, I bit off a bit more than I can chew. I need to complete this "Generate Report" page for my employer and I'm a little over my head. I could use some additional...
1
by: atombee | last post by:
Hi- this is the project that will not end! (sure you've all been there). I had originally purchased a php/css nav bar for the client, but it was buggy as hell, so I decided to do in css, in which I...
0
by: gunimpi | last post by:
http://www.vbforums.com/showthread.php?p=2745431#post2745431 ******************************************************** VB6 OR VBA & Webbrowser DOM Tiny $50 Mini Project Programmer help wanted...
2
by: Dave Ekhaus | last post by:
hi i'm new to javascript. i'm hoping to get some help and find out if what i want to do is possible and - assuming it is, get some tips on how to accomplish the task. ok - assume i have a...
1
by: deepaks85 | last post by:
Dear All, I want to send some data through a form with Multiple attachment in an HTML Format. I have tried it but it is not working for me. I am able to send data without attachment but with the...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.