René Nyffenegger's collection of things on the web | |
René Nyffenegger on Oracle - Most wanted - Feedback
- Follow @renenyffenegger
|
XSL: Using when .. otherswise to color every other row differently | ||
The XML File:
<?xml version="1.0"?> <items> <item> <a>one</a> <b>two</b> <c>three</c> </item> <item> <a>eins</a> <b>zwei</b> <c>drei</c> </item> <item> <a>apple</a> <b>pear</b> <c>banana</c> </item> <item> <a>anna</a> <b>bob</b> <c>claude</c> </item> <item> <a>large</a> <b>small</b> <c>medium</c> </item> <item> <a>here</a> <b>there</b> <c>everywhere</c> </item> </items>
The XSL File:
<?xml version="1.0"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:db="http://ananas.org/2002/docbook/subset"> <xsl:template match="/"> <html><head><title>alternating rows</title></head><body> <xsl:apply-templates/> </body></html> </xsl:template> <xsl:template match="items"> <table> <xsl:apply-templates/> </table> </xsl:template> <xsl:template match="item"> <xsl:choose> <xsl:when test="position() mod 2 = 0"> <xsl:text disable-output-escaping="yes"> <tr style='background-color="#cceeee"'> </xsl:text> </xsl:when> <xsl:otherwise> <xsl:text disable-output-escaping="yes"> <tr style='background-color="#eeffcc"'> </xsl:text> </xsl:otherwise> </xsl:choose> <td><xsl:value-of select="a"/></td> <td><xsl:value-of select="b"/></td> <td><xsl:value-of select="c"/></td> <xsl:text disable-output-escaping="yes"> </tr> </xsl:text> </xsl:template> </xsl:stylesheet>
The Output (whitespaces manually modified):
<html> <head> <META http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>alternating rows</title></head> <body> <table> <tr style='background-color="#eeffcc'> <td>one</td><td>two</td><td>three</td> </tr> <tr style='background-color="#ffffee'> <td>eins</td><td>zwei</td><td>drei</td> </tr> <tr style='background-color="#eeffcc'> <td>apple</td><td>pear</td><td>banana</td> </tr> <tr style='background-color="#ffffee'> <td>anna</td><td>bob</td><td>claude</td> </tr> <tr style='background-color="#eeffcc'> <td>large</td><td>small</td><td>medium</td> </tr> <tr style='background-color="#ffffee'> <td>here</td><td>there</td><td>everywhere</td> </tr> </table> </body> </html> |