| home / programming / xslt / chap8 / 2 | [previous] [next] |
|
|
If you are not concerned with backward compatibility to older browsers, this example can be made cleaner by using the HTML 4.0 style attribute instead of the font element:
<xsl:attribute-set name="gain-loss-color">
<xsl:attribute name="style">color:<xsl:text/>
<xsl:choose>
<xsl:when test="(current - paid) * qty >= 0">black</xsl:when>
<xsl:otherwise>red</xsl:otherwise>
</xsl:choose>
</xsl:attribute>
</xsl:attribute-set>
...
<xsl:template match="investment">
<tr>
<td><xsl:value-of select="symbol"/></td>
<td><xsl:value-of select="current"/></td>
<td><xsl:value-of select="paid"/></td>
<td><xsl:value-of select="qty"/></td>
<td xsl:use-attribute-sets="gain-loss-color">
<xsl:value-of
select="format-number((current - paid) * qty, '#,##0.00')"/>
</td>
</tr>
</xsl:template>
As is usually the case with XSLT, you can approach this problem in many ways. You might consider embedding the style logic directly into the table-generation logic:
<xsl:template match="investment">
<tr>
<td><xsl:value-of select="symbol"/></td>
<td><xsl:value-of select="current"/></td>
<td><xsl:value-of select="paid"/></td>
<td><xsl:value-of select="qty"/></td>
<td>
<font>
<xsl:attribute name="color">
<xsl:choose>
<xsl:when test="(current - paid) * qty >= 0">black</xsl:when>
<xsl:otherwise>red</xsl:otherwise>
</xsl:choose>
</xsl:attribute>
<xsl:value-of
select="format-number((current - paid) * qty, '#,##0.00')"/>
</font>
</td>
</tr>
</xsl:template>
Although placing the color determination logic inline might help you figure out what is happening, it complicates the table-creation logic. A more complex example might compute style attributes for many elements. Mixing the structural building aspect of the stylesheet with the stylization aspect will make each aspect harder to understand and modify.
Nevertheless, you can argue that the hardcoding element references in attributes sets detract from their reusability. However, you can usually remedy this problem by simply moving the logic of the attribute determination outside of the attribute set by using templates and modes. Consider a portfolio with varied investments whose profitability is calculated in different ways:
<portfolio>
<stock>
<symbol>IBM</symbol>
<current>72.70</current>
<paid>65.00</paid>
<qty>1000</qty>
</stock>
<stock>
<symbol>JMAR</symbol>
<current>1.90</current>
<paid>5.10</paid>
<qty>5000</qty>
</stock>
<stock>
<symbol>DELL</symbol>
<current>24.50</current>
<paid>18.00</paid>
<qty>100000</qty>
</stock>
<stock>
<symbol>P</symbol>
<current>57.33</current>
<paid>63.00</paid>
<qty>100</qty>
</stock>
<property>
<address>123 Main St. Anytown NY</address>
<paid>100000</paid>
<appriasal>250000</appriasal>
</property>
<property>
<address>13 Skunks Misery Dr. Stinksville NJ</address>
<paid>200000</paid>
<appriasal>50000</appriasal>
</property>
</portfolio>
| home / programming / xslt / chap8 / 2 | [previous] [next] |
Created: March 27, 2003
Revised: May 5, 2003
URL: http://webreference.com/programming/xslt/cookbook/chap8/2