Skip to main content

Posts

Showing posts from October, 2007

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 sepa

XSL Calculations and Formatting

While using XSL I came across the need to perform a calculation by converting metres to some other units, and then formatting the output. number() takes the value of the selected node and treats it as an actual number.  number() div 1000 is my actual calculation to convert metres to kilometres. Notice that I am using the keyword div and not the / operator as you would with other languages such as Javascript:     <xsl:value-of select="number() div 1000"/> km Being all nice and outputting my desired output, this is flawed. When it comes to outputting in other units such as miles and yards, it shows a large nasty decimal number.  It's far too accurate and long that it's not really user friendly.  A simple presentation would be to limit the output to 2 decimal places.  The answer to this is to use the format-number() function. This was all achieved in this one-liner:     <xsl:value-of select="format-number(number() div 1000,'#0.00')"/> km