...

View Full Version : XSLT: Determing Odd or Even node count



Joseph McGarvey
12-04-2002, 09:23 PM
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!!

jkd
12-04-2002, 09:40 PM
<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>.

Joseph McGarvey
12-04-2002, 09:46 PM
Thanks very much!! :cool:

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!!

zbhunnoo
01-17-2006, 04:37 PM
<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}">
:
:

zipit74
02-01-2006, 12:56 PM
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}">
:

Tails
02-05-2006, 09:52 PM
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.

KC-Luck
02-06-2006, 03:54 PM
one thing to note is that a mixed childNodes set could throw this type of simple-calculation off, eg:


<data>
<record/>
<record/>
<!-- note -->
<record/>
</data>
the comment tag will interupt the position() for following records.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum