XML/XSL Sequence Numbers

H

HugeBob

Hi All,

I've got a successful XSL file working that generates a table. I want
to be able to perform alternate background colors for the table's rows.
How can I count the rows generated by the table and then determine if
that number is even/odd?

Thanks.
 
M

Martin Honnen

HugeBob wrote:

I've got a successful XSL file working that generates a table. I want
to be able to perform alternate background colors for the table's rows.
How can I count the rows generated by the table and then determine if
that number is even/odd?

Usually you have some elements in the XML input that correspond to the
table rows, e.g. some item elements. When you process these with either
<xsd:for-each select="item">
or
<xsl:apply-templates select="item"/>
to create the rows you can simply use the position function to check
whether it is an even or odd position
position() mod 2 = 0
Use that test to generate an attribute for the background color or for
the corresponding style class.
 
J

Joe Kesselman

Rather than maintaining your own counter, it's usually better to use
XSLT's built-in position(). (In this case, position modulo 2 to get
odd/even tests.) Related FAQs:

Position: http://www.dpawson.co.uk/xsl/sect2/N6099.html

If you want the length of a nodeset, that's last().

(The Numbering section of the FAQ may also be worth reviewing.)
 
H

HugeBob

Martin,

Here's what I'm trying:

<TR
<xsl:if test="number(position() mod 2) &gt; 0">

I'm not having any luck though.
 
J

Johannes Koch

HugeBob said:
Here's what I'm trying:

<TR
<xsl:if test="number(position() mod 2) &gt; 0">
<xsl:text>class="alternateBg"</xsl:text>
</xsl:if>

Here you are creating a text node. Use xsl:attribute.
 
P

Peter Flynn

HugeBob said:
Hi All,

I've got a successful XSL file working that generates a table. I want
to be able to perform alternate background colors for the table's rows.
How can I count the rows generated by the table and then determine if
that number is even/odd?

This is covered in the Dave Pawson's XSL FAQ.

///Peter
 

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,002
Messages
2,570,261
Members
46,859
Latest member
VallieMcKe

Latest Threads

Top