Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New Coder
    Join Date
    Dec 2002
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    XSLT: Determing Odd or Even node count

    Hi there....
    Can anyone point me in the right direction here...

    I am trying to write an xsl:if statement that checks if the node count is odd or even. If it is odd (say 1 or 3) it writes:

    <tr><td>STUFF</td>

    If it is even (say 2 or 4) it writes:

    <td>STUFF</td></tr>

    The result is a table with two <td>'s per row.

    If anyone is familiar with the Mod function in VB, that's what I want to accomplish.

    Thanks for any help!!
    Last edited by Joseph McGarvey; 12-04-2002 at 09:26 PM.

  • #2
    jkd
    jkd is offline
    Senior Coder jkd's Avatar
    Join Date
    May 2002
    Location
    metro DC
    Posts
    3,163
    Thanks
    1
    Thanked 18 Times in 18 Posts
    <xsl:choose>
    <xsl:when test="position() mod 2 = 1">
    <tr><td>STUFF</td>
    </xsl:when>
    <xsl:otherwise>
    <td>STUFF</td></tr>
    </xsl:otherwise>
    </xsl:choose>

    Note however, this is invalid. Each template inside the <xsl:when> and <xsl:otherwise> must be well-formed XML markup. In the first instance, you have an unclosed <tr>, while in the second you never had an open <tr>.

  • #3
    New Coder
    Join Date
    Dec 2002
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Thanks!

    Thanks very much!!

    I actually thought of the well-formed issue already and am writing:

    <xsl:text disable-output-escaping="yes">&lt;tr&gt;</xsl:text>

    Works great!!

  • #4
    New to the CF scene
    Join Date
    Jan 2006
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Another Option

    <xsl:variable name="colour" />
    <xsl:choose>
    <xsl:when test="position() mod 2 ">
    <xsl:value-of select="color1"/>
    </xsl:when>
    <xsl:otherwise>
    <xsl:value-of select="color1"/> </xsl:otherwise>
    </xsl:choose>

    <tr class="{$colour}">
    :
    :
    Last edited by Alex Vincent; 01-18-2006 at 08:18 AM.

  • #5
    New to the CF scene
    Join Date
    Feb 2006
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts
    This will work better ;;):

    <xsl:variable name="colour">
    <xsl:choose>
    <xsl:when test="position() mod 2 ">
    <xsl:value-of select="color1"/>
    </xsl:when>
    <xsl:otherwise>
    <xsl:value-of select="color2"/>
    </xsl:otherwise>
    </xsl:choose>
    </xsl:variable>

    <tr class="{$colour}">
    :

  • #6
    Regular Coder
    Join Date
    Nov 2002
    Posts
    672
    Thanks
    1
    Thanked 1 Time in 1 Post
    I've been trying to do this for awhile, but didn't think it could be done since XSL doesn't support bitwise operators. Getting the position of parent <for-each>'s would be more difficult, but could probably be done with more when / otherwise.

  • #7
    Regular Coder
    Join Date
    Aug 2005
    Posts
    282
    Thanks
    0
    Thanked 0 Times in 0 Posts
    one thing to note is that a mixed childNodes set could throw this type of simple-calculation off, eg:
    Code:
    <data>
     <record/>
     <record/>
     <!-- note -->
     <record/>
    </data>
    the comment tag will interupt the position() for following records.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •