Skip to main content

Alternative row colours in XSL

In the same project as before, my search results are being presented in a table.  The issue here, is that I want to have a different background colour for an alternate for the purpose of easy readability.

Luckily, XSL saves the day with the position() function.  This is really handy for obtaining your current index when in a loop.  Just as with using any other server side language you want to mod 2 the position to identify the alternate row:

<xsl:if test="position() mod 2 ='1'">
    <xsl:attribute name="class">rowA</xsl:attribute>
</xsl:if>


For a quick reminder of the modulus operation (mod): when applying the arithmetic on an even number it, correctly returns 0.  When applying to an uneven number, such as 5, it will return the value 1.  1 denotes that there is a remainder.

The purpose of xsl:attribute is to apply XML/XSL to an attribute of a HTML tag.  In this case I want to apply a CSS style to the table row (tr), to keep separation of design and content in the resulting HTML.





Blogged with Flock

Comments

Popular posts from this blog

Open Source alternatives to Adobe Creative Suite

With the take over of Macromedia by Adobe, they have increased their arsenal of design packages, as well as the price. No-one can do pretty much without the popular package of Photoshop for their images, and Dreamweaver for websites, especially aspiring design students who do not have much money. Nevertheless, open source software has come to save the day. What is open source software? In a nutshell it is free software with its source-code freely available to those who would like to expand or improve on the software. To expand further, anyone who’s tinkered with the source code such as optimisation or additional functionality can resubmit it back to the holders and let others enjoy the fruits of your labour. I have trawled through the World Wide Web to find out how everyone can benefit from Open Source to build up their own studio and compete against Adobe Creative Suite. All this for free, apart from the cost of the Internet. I am planning to have a facility where you can purchase...

Download free software and help the poor

Yes, you heard me. To celebrate this festive season, SoftMaker has made their excellent alternative to Microsoft Office freely available. On top of this they will donate to charity with every download. With such a commendable act, why not contribute towards a good cause and try out an Office alternative. So head over to Load and Help ( http://www.loadandhelp.de/home-en.html ).

21 Years of the Web

Who would have thought that an independent contractor in the 1980s would have created the foundations of a revolutionary technology, which changes the way we live? It all began in the European Organisation for Nuclear Research (CERN), in Switzerland, when Tim Berners-Lee proposed and built a database of people and software models that can be shared over the Internet using the HyperText. By the end of 1990s, he created the set of elements that build a web page and its access, HTTP and HTML. In fact he also created the first browser - WorldWideWeb. The rest, you can say, is history... with a large commercial undertaking and the growth of servers across academia then the world Now, there exists well over 80 million websites and a growing user base - hundreds of millions. So it's happy 21st birthday to the Web. Celebrate good times.