Microsoft XML Core Services (MSXML) 5.0 for Microsoft Office - XSLT Developer's Guide

Sample XSLT File for Sorting

This is the complete final file for the topics in the Sorting on Calculated Variables section. Other topics in the Sorting with XSLT section use different versions of the prodsortx.xsl file.

XSLT File (prodsortcvar.xsl)

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
   xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
   xmlns:msxsl="urn:schemas-microsoft-com:xslt">

<xsl:template match="/">
   <HTML>
      <HEAD>
         <TITLE>Wooden Rings and More!</TITLE>
         <STYLE type="text/css">
            th {background-color: silver;
               font-family: Tahoma,Verdana,Arial,sans-serif}
            td {background-color: white;
               font-family: Tahoma,Verdana,Arial,sans-serif}
         </STYLE>
      </HEAD>
      <BODY>
         <xsl:apply-templates/>
      </BODY>
   </HTML>
</xsl:template>

<xsl:template match="products">
   <!-- Create a variable to hold the generated result tree fragment. -->
   <xsl:variable name="prods_with_usd">
      <xsl:apply-templates select="product" mode="calc_usd" />
   </xsl:variable>
   <TABLE width="75%">
      <tr>
         <th>Category</th>
         <th>Prod ID</th>
         <th>Name/Version</th>
         <th>Description</th>
         <th>Price (Currency)</th>
         <th>Price (USD)</th>
      </tr>
      <!-- Fill remainder of table with source tree "clone". -->      
      <xsl:apply-templates select="msxsl:node-set($prods_with_usd)/product">
         <xsl:sort select="usd_equiv" data-type="number" />
      </xsl:apply-templates>
   </TABLE>
</xsl:template>

<xsl:template match="product" mode="calc_usd">
   <xsl:copy>
      <xsl:copy-of select="@*" />
      <xsl:copy-of select="*" />
      <!-- Add a calculated <usd_equiv> child to each <product>. -->      
      <xsl:element name="usd_equiv">
         <xsl:choose>
            <!-- Note: Exchange rates current as of Oct. 2000. -->
            <xsl:when test="price/@curr='USD'">
               <xsl:value-of select="format-number(price, '#,##0.00')"/>
            </xsl:when>
            <xsl:when test="price/@curr='GBP'">
               <xsl:value-of select="format-number(price * 1.47275, '#,##0.00')"/>
            </xsl:when>
            <xsl:when test="price/@curr='EU'">
               <xsl:value-of select="format-number(price * 0.864379, '#,##0.00')"/>
            </xsl:when>
            <xsl:otherwise>Unknown Currency</xsl:otherwise>
         </xsl:choose>
      </xsl:element>
   </xsl:copy>
</xsl:template>

<xsl:template match="product">
   <tr>
      <td valign="top">
         <xsl:value-of select="categ"/>
      </td>
      <td valign="top">
         <xsl:value-of select="@prodID"/>
      </td>
      <td valign="top">
         <xsl:value-of select="concat(name, '/', version)"/>
      </td>
      <td valign="top">
         <xsl:value-of select="descr"/>
      </td>
      <td valign="top" align="center">
         <xsl:value-of select="concat(price, ' (', price/@curr, ')')"/>
      </td>
      <td valign="top" align="right">
         <xsl:value-of select="usd_equiv"/>
      </td>
   </tr>
</xsl:template>

</xsl:stylesheet>