Help - Generate N x 2 html table

C

cardinallijr

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.
 
J

Janwillem Borleffs

cardinallijr said:
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
 
P

Peter Flynn

cardinallijr said:
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
 
C

cardinallijr

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,
 
J

Janwillem Borleffs

cardinallijr said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,001
Messages
2,570,254
Members
46,850
Latest member
VMRKlaus8

Latest Threads

Top