Helpful Information
 
 
Category: Programming Languages More
XSL problem

Hello,

I'm trying to use XSL to display different color in each row of the table. But the method I used below does not work. Any idea?

(In HTML)
<TABLE>
<TR BGCOLOR="#FFFFFF"><TD>1</TD> ... <TR>
<TR BGCOLOR="#AAAAAA"><TD>2</TD> ... </TR>
<TR BGCOLOR="#FFFFFF"><TD>3</TD> ... </TR>
<TR BGCOLOR="#AAAAAA"><TD>4</TD> ...</TR>
</TABLE>

(In XSL)
<TABLE>
<xsl:for-each select="./Atts/Att">
<xsl:variable name="CHOOSE"><xsl:value-of select="position()"/></xsl:variable>
<xsl:for-each select="./AttFormats/AttFormat">

<tr>
<xsl:choose>
<xsl:when test="$CHOOSE mod 2 = 0">
<xsl:attribute name="bgcolor">#FFFFFF</xsl:attribute>
</xsl:when>
<xsl:otherwise>
<xsl:attribute name="bgcolor">#AAAAAA</xsl:attribute>
</xsl:otherwise>
</xsl:choose>
<td><xsl:value-of select=".//somevalue"/></td>
<td>...</td>
</tr>
</xsl:for-each>
</xsl:for-each
</TABLE>

=================================
The variable $CHOOSE seems NOT work with the function 'mod'.

more info:
The XML look like
<Atts>
<Att>
<something></something>
<AttFormats>
<AttFormat>jpg</AttFormat>
<AttFormat>gif</AttFormat>
</AttFormats>
</Att>
<Att>
...
...
</Att>
</Atts>


I need to display each file format (Attformat) in one row inside a table.

I think you can try the following XSL stylesheet with your own XML document...
(you must put "bgcolor" attribute into "td" tag only...)

<!-- ************************************* -->
<?xml version='1.0' encoding='iso-8859-1' ?>

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

<xsl:output method="html" indent="yes" encoding="iso-8859-1" media-type="text/html"/>
<!-- ************************************* -->

<xsl:template match="Atts">
<html>
<head>
</head>
<body>
<xsl:apply-templates/>
</body>
</html>
</xsl:template>
<!-- ************************************* -->
<xsl:template match="Att">
<table>
<xsl:apply-templates select="AttFormats"/>
</table>
</xsl:template>
<!-- ************************************* -->
<xsl:template match="AttFormats">
<xsl:for-each select="AttFormat">
<xsl:variable name="CHOOSE" select="position()"/>
<tr>
<xsl:choose>
<xsl:when test="$CHOOSE mod 2 = 0">
<xsl:element name="td">
<xsl:attribute name="bgcolor">#FFFFFF</xsl:attribute>
<xsl:value-of select="$CHOOSE"/>
</xsl:element>
</xsl:when>
<xsl:otherwise>
<xsl:element name="td">
<xsl:attribute name="bgcolor">#AAAAAA</xsl:attribute>
<xsl:value-of select="$CHOOSE"/>
</xsl:element>
</xsl:otherwise>
</xsl:choose>
</tr>
</xsl:for-each>
</xsl:template>
<!-- ************************************* -->

</xsl:stylesheet>
i hope it will work fine as you want

:)

Regards

Analogx

(don't hesitate to contact me if you need some help...)

We're using xalan (Java) to do XSLT. What we found out is that you can only set a xsl:variable ONCE. So when it's set once, it keeps that value. We work around it by determining the value elsewhere and passing it in through xsl:param

Hello

I don't know Xalan.
i'm using Sablotron with PHP.
Sablotron is a XSLT Processor written in C++ (i think it's greter than Java :-)) ).

For more informations i invite you to see Sablotron homepage
http://www.gingerall.com

Regards

analogx