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

Step 1: Cloning the Source Tree

The following XSLT file contains template rules to process the prodsort.xml file. The first template rule handles the root <products> element. The first step in that template rule creates a variable, prods_with_usd. This variable contains a result tree fragment. This fragment is generated by the second template rule, which contains a copy of each <product> element, its attributes, and its children. Note the use of the mode attribute, which causes this second template rule to be fired only when the <apply-templates> element has a matching mode.

After making the changes shown below, the prods_with_usd variable simply contains a root node, the seven <product> elements, and their children. If we stopped here, there'd be no difference in the data available to be sorted.

XML File (prodsort.xml)

Use prodsort.xml, in Sample XML Data File for XSLT Sorting. Change the href attribute to reference prodsortcvar1.xsl.

XSLT File (prodsortcvar1.xsl)

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

<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>
    </TABLE>
</xsl:template>

<xsl:template match="product" mode="calc_usd">
    <xsl:copy>
        <xsl:copy-of select="@*" />
        <xsl:copy-of select="*" />
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>