Question about xslt:for-each

Discussion in 'XML' started by gene.ellis@gmail.com, Jul 27, 2005.

  1. Guest

    Hello everyone. I have a pretty straight forward question:

    I have some data stored in an XMl document in the format of:

    <comment_info>
    <comments>These are the comments</comments>
    <comment_heading>This is comment heading</comment_heading>
    </comment_info>

    <comment_info>
    <comments>These are the more comments</comments>
    <comment_heading>This is another comment heading</comment_heading>
    </comment_info>

    and I am using an xslt:for-each command to create an .html page. I am
    putting each comment_info section into it's own table on the .html
    page. My question is how would I alternate table background colors of
    these tables? For example I would want the 1,3,5... tables to have a
    certain background color and the 2,4,6... tables to have another
    background color. Is there some sort of function I can use to determine
    if the comment_info block being returned is even or odd? I was even
    thinking of placing the background color into the xml document itself,
    but I would still need to know which background color I should insert
    (again, is the comment_info block even or odd). I welcome any ideas.
    Thank you.
     
    , Jul 27, 2005
    #1
    1. Advertising

  2. Sorrow Guest

    :

    //snip
    > My question is how would I alternate table background colors of
    > these tables? For example I would want the 1,3,5... tables to have a
    > certain background color and the 2,4,6... tables to have another
    > background color. Is there some sort of function I can use to determine
    > if the comment_info block being returned is even or odd?

    //snip

    Use XPath function position() and operator "mod" to check division
    remainder:

    <xsl:for-each select="select-statement">
    <xsl:choose>
    <xsl:when test="position() mod 2 = 0">
    <!--do something here-->
    </xsl:when>
    <xsl:eek:therwise>
    <!-- do something different here-->
    </xsl:eek:therwise>
    </xsl:choose>
    </xsl:for-each>

    This solution actually gives you the possibility to use any nmber of
    colours.
    To sort nodes use <xsl:sort>.

    Hope this was helpful.

    Best regards,
    Sorrow.

    PS. This is my first post to this group, so a great welcome to you all. I
    hope I'll learn much here :)
     
    Sorrow, Jul 27, 2005
    #2
    1. Advertising

  3. Guest

    AWESOME. Worked like a charm. Thanks!
     
    , Jul 29, 2005
    #3
    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. John Blair
    Replies:
    1
    Views:
    427
    Eliyahu Goldin
    Aug 3, 2005
  2. Phil
    Replies:
    3
    Views:
    584
    Janwillem Borleffs
    May 27, 2004
  3. Tjerk Wolterink

    xsl:for-each for each 3 elements problem

    Tjerk Wolterink, Nov 3, 2004, in forum: XML
    Replies:
    3
    Views:
    446
    Tjerk Wolterink
    Nov 3, 2004
  4. Pat Maddox
    Replies:
    6
    Views:
    163
    Marcin Mielżyński
    Jan 20, 2006
  5. Igor Nn
    Replies:
    7
    Views:
    456
    Johnny Morrice
    May 28, 2011
Loading...

Share This Page