Help - Generate N x 2 html table

Discussion in 'XML' started by cardinallijr, Jan 2, 2006.

  1. cardinallijr

    cardinallijr Guest

    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.
     
    cardinallijr, Jan 2, 2006
    #1
    1. Advertising

  2. 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
     
    Janwillem Borleffs, Jan 2, 2006
    #2
    1. Advertising

  3. cardinallijr

    Peter Flynn Guest

    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/
     
    Peter Flynn, Jan 2, 2006
    #3
  4. cardinallijr

    cardinallijr Guest

    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
     
    cardinallijr, Jan 2, 2006
    #4
  5. 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
     
    Janwillem Borleffs, Jan 3, 2006
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Weng Tianxiang
    Replies:
    5
    Views:
    1,318
    Christophe
    Feb 16, 2006
  2. Kamal Ahmed
    Replies:
    0
    Views:
    477
    Kamal Ahmed
    Jun 28, 2006
  3. Replies:
    3
    Views:
    416
    red floyd
    Apr 7, 2006
  4. Replies:
    9
    Views:
    506
    Bruno Desthuilliers
    Dec 28, 2007
  5. Greg

    Generate html table with merged cells

    Greg, Jul 24, 2007, in forum: ASP .Net Web Controls
    Replies:
    1
    Views:
    411
    Steve C. Orr [MCSD, MVP, CSM, ASP Insider]
    Jul 25, 2007
Loading...

Share This Page