XML technologies / XSLT / XSLT Cookbook / Transform XML into HTML / Generate data-driven stylesheets

(Excerpt from “XSLT Kochbuch” by Sal Mangano)

Problem

You want HTML whose style depends on its data.

Solution

XSLT 1.0

Attribute sets encapsulate data-driven styling complexity. Consider this XML investment portfolio:

<portfolio>
  <investment>
    <symbol>IBM</symbol>
    <current>72.70</current>
    <paid>65.00</paid>
    <qty>1000</qty>
  </investment>
  <investment>
    <symbol>JMAR</symbol>
    <current>1.90</current>
    <paid>5.10</paid>
    <qty>5000</qty>
  </investment>
  <investment>
    <symbol>DELL</symbol>
    <current>24.50</current>
    <paid>18.00</paid>
    <qty>100000</qty>
  </investment>
  <investment>
    <symbol>P</symbol>
    <current>57.33</current>
    <paid>63</paid>
    <qty>100</qty>
  </investment>
</portfolio>

Display it as a table with gains in black and losses in red:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="html"/>
  <xsl:attribute-set name="gain-loss-font">
    <xsl:attribute name="color">
      <xsl:choose>
        <xsl:when test="(current - paid) * qty &gt;= 0">black</xsl:when>
        <xsl:otherwise>red</xsl:otherwise>
      </xsl:choose>
    </xsl:attribute>
  </xsl:attribute-set>
  <xsl:template match="portfolio">
    <html>
      <head>
        <title>My Portfolio</title>
      </head>
      <body bgcolor="#FFFFFF" text="#000000">
        <h1>Portfolio</h1>
        <table border="1" cellpadding="2">
          <tbody>
            <tr>
              <th>Symbol</th>
              <th>Current</th>
              <th>Paid</th>
              <th>Qty</th>
              <th>Gain/Loss</th>
            </tr>
            <xsl:apply-templates/>
          </tbody>
        </table>
      </body>
    </html>
  </xsl:template>
  <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:use-attribute-sets="gain-loss-font">
          <xsl:value-of select="format-number((current - paid) * qty, '#,##0.00')"/>
        </font>
      </td>
    </tr>
  </xsl:template>
</xsl:stylesheet>

For a clearer version without older-browser constraints, replace element font with attribute style from HTML 4.0:

<xsl:attribute-set name="gain-loss-color">
  <xsl:attribute name="style">color:<xsl:text/>
    <xsl:choose>
      <xsl:when test="(current - paid) * qty &gt;= 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>

XSLT 2.0

Version 2.0 mainly improves concision with XPath expressions compared with xsl:choose. Here is a rewritten excerpt:

<xsl:attribute-set name="gain-loss-font">
  <xsl:attribute name="color" select="if ((current - paid) * qty ge 0) then 'black' else 'red'"/>
</xsl:attribute-set>

Discussion

As often in XSLT, many approaches work. You might put styling logic directly inside 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 &gt;= 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>

Inline color selection may clarify the immediate behavior but complicates table generation. A complex example might compute style attributes for many elements. Mixing structural and styling concerns makes both harder to understand and modify.

Hard-coded element references in attribute sets may reduce reuse. Move attribute-determination logic outside the set with templates and modes. Consider investments with different profit calculations:

<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>
    <appraisal>250000</appraisal>
  </property>
  <property>
    <address>13 Skunks Misery Dr. Stinksville NJ</address>
    <paid>200000</paid>
    <appraisal>50000</appraisal>
  </property>
</portfolio>

To avoid two sets with the same purpose, move their logic into templates:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="html"/>
  <xsl:attribute-set name="gain-loss-font">
    <xsl:attribute name="color">
      <xsl:apply-templates select="." mode="gain-loss-font-color"/>
    </xsl:attribute>
  </xsl:attribute-set>
  <xsl:template match="stock" mode="gain-loss-font-color">
    <xsl:choose>
      <xsl:when test="(current - paid) * qty &gt;= 0">black</xsl:when>
      <xsl:otherwise>red</xsl:otherwise>
    </xsl:choose>
  </xsl:template>
  <xsl:template match="property" mode="gain-loss-font-color">
    <xsl:choose>
      <xsl:when test="appraisal - paid  &gt;= 0">black</xsl:when>
      <xsl:otherwise>red</xsl:otherwise>
    </xsl:choose>
  </xsl:template>
  ...
</xsl:stylesheet>

You may prefer keeping colors, fonts and other styling outside XSLT. A corporate-identity specialist, rather than you, may decide how gains and losses look. Classify elements and delegate styling to a separate stylesheet:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="html"/>
  <xsl:attribute-set name="gain-loss">
    <xsl:attribute name="class">
      <xsl:apply-templates select="." mode="gain-loss"/>
    </xsl:attribute>
  </xsl:attribute-set>
  <xsl:template match="stock" mode="gain-loss">
    <xsl:choose>
      <xsl:when test="(current - paid) * qty &gt;= 0">gain</xsl:when>
      <xsl:otherwise>loss</xsl:otherwise>
    </xsl:choose>
  </xsl:template>
  <xsl:template match="property" mode="gain-loss">
    <xsl:choose>
      <xsl:when test="appraisal - paid  &gt;= 0">gain</xsl:when>
      <xsl:otherwise>loss</xsl:otherwise>
    </xsl:choose>
  </xsl:template>
  <xsl:template match="portfolio">
    <html>
      <head>
        <title>My Portfolio</title>
        <link rel="stylesheet" type="text/css" href="portfolio.css"/>
      </head>
      ...
  </xsl:template>
  <xsl:template match="stock">
    <tr>
      <td><xsl:value-of select="symbol"/></td>
      <td align="right"><xsl:value-of select="current"/></td>
      <td align="right"><xsl:value-of select="paid"/></td>
      <td align="right"><xsl:value-of select="qty"/></td>
      <td align="right" xsl:use-attribute-sets="gain-loss">
        <xsl:value-of select="format-number((current - paid) * qty, '#,##0.00')"/>
      </td>
    </tr>
  </xsl:template>
  <xsl:template match="property">
    <tr>
      <td><xsl:value-of select="address"/></td>
      <td align="right"><xsl:value-of select="paid"/></td>
      <td align="right"><xsl:value-of select="appraisal"/></td>
      <td align="right" xsl:use-attribute-sets="gain-loss">
        <xsl:value-of select="format-number(appraisal - paid, '#,##0.00')"/>
      </td>
    </tr>
  </xsl:template>
</xsl:stylesheet>

The style specialist then controls <td class="gain"> and <td class="loss"> through portfolio.css, as below.

Example: portfolio.css

td.gain
{
     color:black;
}

td.loss
{
     color:red;
     font-weight:700;
}

  

 

 

 

back to top