[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

[xsl] Another Alternate table-row background color question - Using filters


Subject: [xsl] Another Alternate table-row background color question - Using filters
From: GNallan@xxxxxxxxxxx
Date: Mon, 30 May 2005 20:06:23 +0530

Sorry if this question has been addressed before. I'm trying to create a
XSL, that will transform a XML into a HTML document (report); the
stylesheet will extract only some information (filter) from the XML and
create a table. I want the rows in this table to have alternate background
color. Because of the filter condition in the XSL, I cannot use the
position() to check for odd/even rows. I need to (essentially) check the
row number inserted into the HTML <TABLE>.

Here is my xml..

<Codes>
    <Code>
        <Name>10</Name>
        <Description>Decription 10</Description>
    </Code>
    <Code>
        <Name>20</Name>
        <Description> Description 20</Description>
    </Code>
    <Code>
        <Name>30</Name>
        <Description>description 30</Description>
    </Code>
    <Code>
        <Name>40</Name>
        <Description> Description 40</Description>
    </Code>
    <Code>
        <Name>50</Name>
        <Description> Description 50 </Description>
    </Code>
    <Code>
        <Name>60</Name>
        <Description> Description 60 </Description>
    </Code>
    <Code>
        <Name>70</Name>
        <Description> Description 70 </Description>
    </Code>
</Codes>

And here is my stylesheet (it extracts only codes 30, 50 & 60). The output
should contain the first and third rows in one bg color and the second row
in a different color. I tried the position() in this XSL, but it does not
work...

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
    <xsl:output method="html"/>

    <xsl:template match="/">
        <html>
            <head>
                <title>Code Report</title>
                <style>
                    .row0 {background-color:#CCCCCC;}
                    .row1 {background-color:#FFFFFF;}
                </style>
            </head>
            <body>
                <p>
                    <font face="Arial" size="2">
                            <b>Code Decription Report</b>
                    </font>
                </p>
                <table border="1" width="90%" cellspacing="0">
                    <tr>
                        <td width="50%" align="center" bgcolor="#808080">
                            <font face="Arial" size="2" color="#FFFFFF">
                                    <b>Code Name</b>
                            </font>
                        </td>
                        <td width="50%" align="center" bgcolor="#808080">
                            <font face="Arial" size="2" color="#FFFFFF">
                                    <b>Code Description</b>
                            </font>
                        </td>
                    </tr>
                    <xsl:for-each select="Codes/Code">
                        <xsl:choose>
                            <xsl:when test="Name[. = '30']">
                                <tr class="{name(.)} row{position() mod
2}">
                                    <xsl:apply-templates select="."/>
                                </tr>
                            </xsl:when>
                            <xsl:when test="Name[. = '50']">
                                <tr class="{name(.)} row{position() mod
2}">
                                    <xsl:apply-templates select="."/>
                                </tr>
                            </xsl:when>
                            <xsl:when test="Name[. = '60']">
                                <tr class="{name(.)} row{position() mod
2}">
                                    <xsl:apply-templates select="."/>
                                </tr>
                            </xsl:when>
                        </xsl:choose>
                    </xsl:for-each>
                </table>
            </body>
        </html>
    </xsl:template>
    <xsl:template match="Code">
        <td width="50%" align="center">
            <xsl:value-of select="Name"/>
        </td>
        <td width="50%" align="left">&#160;&#160;
            <font face="Arial" size="2">
                <xsl:choose>
                    <xsl:when test="Description[string-length(.) > 0]">
                        <xsl:value-of select="Description"/> -
[<xsl:value-of select="Name"/>]
                    </xsl:when>
                    <xsl:otherwise>
                        <xsl:value-of select="Description"/>
                    </xsl:otherwise>
                </xsl:choose>
            </font>
        </td>
    </xsl:template>
</xsl:stylesheet>

Any help in this regard is appreciated. Thank you for your time.

Regards,
Gowtham


Current Thread
Keywords