Sample XSLT File for DOM Context

MSXML 5.0 SDK

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

Sample XSLT File for DOM Context

This file is part of the Invoice sample.

XSLT File (invoice.xsl)

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
            xmlns:msxsl="urn:schemas-microsoft-com:xslt"
        xmlns:user="mynamespace"
        version="1.0">
  <xsl:template match="/">
    <HTML>
      <STYLE>
        TD {font-size:9pt}
      </STYLE>
      <BODY STYLE="font:9pt Verdana">
        <H3>Invoices</H3>
        <TABLE BORDER="1">
          <TR>
            <TD><B>Qty</B></TD>
            <TD><B>Description</B></TD>
            <TD><B>Price</B></TD>
            <TD><B>Discount</B></TD>
            <TD><B>Total</B></TD>
          </TR>
          <xsl:for-each select="invoices/invoice">
                  <xsl:variable name="customer-id" select="./customer/@ref"/>
            <TR>
              <TD COLSPAN="5" STYLE="border:none; background-color:#DDDDDD">
                Invoice #<xsl:value-of select="@id"/>,
                for customer: <xsl:value-of select="/invoices/customers/customer[@id=$customer-id]"/>
              </TD>
            </TR>
            <xsl:for-each select="items/item">
                     <TR>
                <TD>
                  <xsl:value-of select="qty"/>
                </TD>
                <TD>
                  <xsl:value-of select="description"/>
                </TD>
                <TD>
                  $<xsl:value-of select="format-number(price,'#,##0.00')"/>
                </TD>
                <TD> <!-- 10% volume discount -->
                  <xsl:if test="qty[. &gt;= 10]">$<xsl:value-of select="format-number(price * .10,'##0.00')"/></xsl:if>
                </TD>
                <TD STYLE="text-align:right"> <!-- line total -->
                           <xsl:choose>
                           <xsl:when test="qty[. &gt;= 10]">
                              $<xsl:value-of select="format-number((price * .90) * qty,'#,##0.00')"/>
                           </xsl:when>
                           <xsl:otherwise>
                              $<xsl:value-of select="format-number(price * qty,'#,##0.00')"/>
                           </xsl:otherwise>
                           </xsl:choose>
                </TD>
              </TR>
            </xsl:for-each>
            <TR>
              <TD COLSPAN="4"></TD>
              <TD STYLE="text-align:right; border:none; border-top:1px solid black">$<xsl:value-of select="format-number(user:invoiceTotal(.),'#,##0.00')"/>
              </TD>
            </TR>
            <TR/>
          </xsl:for-each>
        </TABLE>
      </BODY>
    </HTML>
  </xsl:template>
  
    <msxsl:script implements-prefix="user"><![CDATA[
    function invoiceTotal(e)
    {
     var invoice = e.nextNode();
     var items = invoice.selectNodes("items/item");
      var sum = 0;
      for (var item = items.nextNode(); item; item = items.nextNode())
      {
        var price = item.selectSingleNode("price").nodeTypedValue;
        var qty = item.selectSingleNode("qty").nodeTypedValue;
        if (qty >= 10)
          price = 0.9*price;
        sum += price * qty;
      }
      return sum;
    }
  ]]></msxsl:script>
</xsl:stylesheet>