xsl-list
[Top] [All Lists]

RE: Another Alternate table-row background color question - Using filters

2005-05-30 07:56:31
If you are using a parser that supports the node-set function, you can copy
the rows to a virtual node-set and then use position() to check if the
result is an odd or even row. Here is a working example.
-Maria

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
xmlns:msxsl="urn:schemas-microsoft-com:xslt"
exclude-result-prefixes="msxsl">
        <xsl:variable name="TableRows">
                <xsl:for-each select="//Code[(Name='30') or (Name='50') or
(Name='60')]">
                        <Row>
                                <xsl:copy-of select="Name"/>
                                <xsl:copy-of select="Description"/>
                        </Row>
                </xsl:for-each>
        </xsl:variable>
        <xsl:template match="/">
                <html>
                        <head>
                                <style>
                    .row0 {background-color:#CCCCCC;}
                    .row1 {background-color:#FFFFFF;}
                </style>
                        </head>
                        <body>
                                <table>
                                        <tr>
                                                <th>Name</th>
                                                <th>Description</th>
                                        </tr>
                                        <xsl:for-each
select="msxsl:node-set($TableRows)/Row">
                                                <tr>
                                                        <xsl:attribute
name="class"><xsl:choose><xsl:when test="position() mod 2 =
0">row0</xsl:when><xsl:otherwise>row1</xsl:otherwise></xsl:choose></xsl:attr
ibute>
                                                        <td>
        
<xsl:value-of select="Name"/>
                                                        </td>
                                                        <td>
        
<xsl:value-of select="Description"/>
                                                        </td>
                                                </tr>
                                        </xsl:for-each>
                                </table>
                        </body>
                </html>
        </xsl:template>
</xsl:stylesheet>

 

-----Original Message-----
From: GNallan(_at_)inautix(_dot_)com [mailto:GNallan(_at_)inautix(_dot_)com] 
Sent: Monday, May 30, 2005 10:36 AM
To: xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
Subject: [xsl] Another Alternate table-row background color question - Using
filters

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



--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--

--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--



<Prev in Thread] Current Thread [Next in Thread>
  • RE: Another Alternate table-row background color question - Using filters, Maria Amuchastegui <=