Finished Example for Named Templates

MSXML 5.0 SDK

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

Finished Example for Named Templates

This finished example is based on the Initial Example for Named Templates. The other topics in this section have added named templates to this initial example.

Example

The formatted output at the end of this topic shows the completed company-wide results, as well as some of the regional results.

XML File (region_qtr.xml)

<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="region_qtr_param.xsl" ?>
<sales quarter="2001-01-01">
    <region>
        <name>Northeast</name>
        <manager>Kim Abercrombie</manager>
        <units>9881</units>
        <sales_amt curr="">150680.89</sales_amt>
    </region>
    <region>
        <name>Southeast</name>
        <manager>Jeff D. Henshaw</manager>
        <units>4329</units>
        <sales_amt curr="">67015.48</sales_amt>
    </region>
    <region>
        <name>Southwest</name>
        <manager>Sunil Koduri</manager>
        <units>3543</units>
        <sales_amt curr="">57029.25</sales_amt>
    </region>
    <region>
        <name>Northwest</name>
        <manager>Brad Sutton</manager>
        <units>7569</units>
        <sales_amt curr="">127423.90</sales_amt>
    </region>
</sales> 

XSLT File (region_qtr_param.xsl)

This file, region_qtr_param.xsl, is an expanded version of region_qtr.xsl. This is the result of the above topics.

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

<!-- Template rule for document root: -->
<xsl:template match="/">
   <HTML>
      <HEAD>
         <TITLE>Quarterly Sales (Quarter Beginning
            <xsl:value-of select="sales/@quarter"/>)
         </TITLE>
         <STYLE type="text/css">
            h1 {background-color: teal;
               font-family: Tahoma,Verdana,Arial,sans-serif;
               font-size: 24pt}
            h2 {background-color: yellow;
               font-family: Tahoma,Verdana,Arial,sans-serif;
               font-size: 18pt}
            h3 {background-color: white;
               font-family: Tahoma,Verdana,Arial,sans-serif;
               font-size: 14pt;
               font-weight: bold}
            th {background-color: silver;
               font-family: Tahoma,Verdana,Arial,sans-serif}
            td {background-color: white;
               font-family: Tahoma,Verdana,Arial,sans-serif}
            .page {width: 75%}
         </STYLE>
      </HEAD>
      <BODY>
         <DIV class="page">
            <xsl:apply-templates/>
         </DIV>
      </BODY>
   </HTML>
</xsl:template>

<!-- Named template for constructing sales results headings and tables: -->
<xsl:template name="common_table">
   <xsl:param name="table_hdg" select="'Default Heading'"/>
   <xsl:param name="units_param" select="'Default Units'"/>
   <xsl:param name="amt_param" select="'Default Amount'"/>
   <xsl:if test="$table_hdg='Company-Wide Results'">
      <p />
      <hr />
   </xsl:if>
   <h3><xsl:value-of select="$table_hdg"/></h3>
   <table width="100%">
      <tr>
         <th width="33%"># Units</th>
         <th width="33%">Qtr Sales Amount</th>
         <th>Amt/Unit</th>
      </tr>
      <tr>
         <td width="33%" align="right">
            <xsl:value-of select="format-number($units_param, '#,##0')"/>
         </td>
         <td width="33%" align="right">
            <xsl:value-of select="format-number($amt_param, '#,##0.00')"/>
         </td>
         <td align="right">
            <xsl:value-of select="format-number($amt_param div $units_param, '#,##0.000')"/>
         </td>
      </tr>
   </table>
</xsl:template>

<!-- Template rule for root <sales> element: -->
<xsl:template match="sales">
   <h1>Quarterly Sales by Region</h1>
   <h2>Quarter Beginning <xsl:value-of select="@quarter"/></h2>
   <xsl:apply-templates/>
   <xsl:call-template name="common_table">
      <xsl:with-param name="table_hdg" select="'Company-Wide Results'"/>
      <xsl:with-param name="units_param" select="sum(region/units)"/>
      <xsl:with-param name="amt_param" select="sum(region/sales_amt)"/>
   </xsl:call-template>
</xsl:template>

<!-- Template rule for each <region> element: -->
<xsl:template match="region">
   <xsl:call-template name="common_table">
      <xsl:with-param name="table_hdg">
         <xsl:value-of select="name"/> Region
         (Manager: <xsl:value-of select="manager"/>)
      </xsl:with-param>
      <xsl:with-param name="units_param" select="units"/>
      <xsl:with-param name="amt_param" select="sales_amt"/>
   </xsl:call-template>
</xsl:template>

</xsl:stylesheet>

Formatted Output

The following is a portion of the result.