[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

RE: Another Alternate table-row background color quest

Subject: RE: Another Alternate table-row background color question - Using filters
From: Maria Amuchastegui <mamuchastegui@xxxxxxxxxxx>
Date: Mon, 30 May 2005 10:56:31 -0400
table row background
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@xxxxxxxxxxx [mailto:GNallan@xxxxxxxxxxx] 
Sent: Monday, May 30, 2005 10:36 AM
To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Subject:  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

Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.