[XSLT] searchresults/exchanging background color

  • Thread starter Peter van Schie
  • Start date
P

Peter van Schie

Hi all,

I'm writing a searchinterface on a mysql database in PHP. I retrieve
the searchoutput and write it to an XML file, which is then being
displayed in the webbrowser. I'm using XSLT to process the XML file and
to output HTML.

So far so good, but now I want to switch the background color for each
resultrow.
So resultrow 1 should have say background color white, resultrow 2
grey, resultrow 3 white again and so on.

The relevant part of my XSLT looks like this:

<table class="searchresults">
<xsl:for-each select="result/document">
<tr><td colspan="3"><strong><a href="viewfulldoc.php"><xsl:value-of
select="dossiernaam"/></a></strong></td></tr>
<tr><td colspan="3" class="preview"><xsl:value-of
select="fulltext"/></td></tr>
<tr>
<td><xsl:value-of select="documenttype"/></td>
<td><xsl:value-of select="organisatie"/></td>
<td><xsl:value-of select="invoerdatum"/></td>
</tr>
</xsl:for-each>
</table>

I'd somehow like to keep track of a rowcounter to know if it's an odd
or even row, but how would I do something like that in XSLT.
I'm obviously new to XSLT, so please bear with me. :)
Thanks in advance.

Kind regards,
Peter.
 
T

Tjerk Wolterink

Peter said:
Hi all,

I'm writing a searchinterface on a mysql database in PHP. I retrieve
the searchoutput and write it to an XML file, which is then being
displayed in the webbrowser. I'm using XSLT to process the XML file and
to output HTML.

So far so good, but now I want to switch the background color for each
resultrow.
So resultrow 1 should have say background color white, resultrow 2
grey, resultrow 3 white again and so on.

The relevant part of my XSLT looks like this:

<table class="searchresults">
<xsl:for-each select="result/document">
<tr><td colspan="3"><strong><a href="viewfulldoc.php"><xsl:value-of
select="dossiernaam"/></a></strong></td></tr>
<tr><td colspan="3" class="preview"><xsl:value-of
select="fulltext"/></td></tr>
<tr>
<td><xsl:value-of select="documenttype"/></td>
<td><xsl:value-of select="organisatie"/></td>
<td><xsl:value-of select="invoerdatum"/></td>
</tr>
</xsl:for-each>
</table>

I'd somehow like to keep track of a rowcounter to know if it's an odd
or even row, but how would I do something like that in XSLT.
I'm obviously new to XSLT, so please bear with me. :)
Thanks in advance.

Kind regards,
Peter.

It is easy,
just use the module operator,

1 mod 2 = 1
2 mod 2 = 0
3 mod 2 = 1
4 mod 2 = 0

so in your case:

<xsl:if test="(position() mod 2)=0">
- put the style here.
</xsl:if>


Hope it helps.
 
P

Peter van Schie

Thanks a lot Tjerk, I got it working using the modulo operator.

Kind regards,
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,001
Messages
2,570,254
Members
46,849
Latest member
Fira

Latest Threads

Top