Home > Articles > Open Source > Ajax & JavaScript

  • Print
  • + Share This
Like this article? We recommend

Like this article? We recommend


The XSLT recommendation provides a means for extending the functionality of style sheets using extension elements and functions. Extension functions behave like built-in XPath functions and can take arguments, and extension elements provide a means for accessing both custom attributes and the context of the request. Both extension elements and functions can be defined using JavaScript. The Xalan-Java 2 XSLT transformation engine defines them, as seen in the final style sheet shown in Listing 11.

Listing 11—The Final Style Sheet

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

 <lxslt:component prefix="totalSys" elements="resetTotal customerInfo" 
                   functions="totalCost getExtPrice">
  <lxslt:script lang="javascript">

   var orderTotal = 0;

   function customerInfo (xslContext, elem) {
     thisOrder = xslContext.getContextNode();
     thisCustomer = thisOrder.getAttribute("custid");
     custInfo = getCustomerInfo(thisCustomer);
     return custInfo;

   function getCustomerInfo(custID) {
     if (custID == "969982") {
        return "Darling Clementine, Inc., 228 Range Road, Oklahoma City, OK 73101";
     } else {
        return "Into the Wind, LLC, 849 Research Way, Suite 399, Miami Beach, FL 33109";

   function resetTotal(xslContext, elem) {
     resetTo = elem.getAttribute("resetTo");
     orderTotal = resetTo;
     return null;

   function getExtPrice (qty, unitPrice) {
    extPrice = parseFloat(qty*unitPrice);
    orderTotal = parseFloat(orderTotal + extPrice);
    return extPrice;

   function totalCost() {
     return orderTotal;


 <xsl:template match="/">

   <head><title>Order Summaries</title></head>

     <h1>Order Summaries</h1>




 <xsl:template match="order">
   <p><b>Customer Information: </b><totalSys:customerInfo/>
   <br /><b>Order number: </b><xsl:value-of select="@orderid"/></p>

   <table width="50%" border="1">
       <th>Product Number</th>
       <th>Unit Price</th>
       <th>Extended Price</th>
     <totalSys:resetTotal resetTo="0"/>
     <xsl:apply-templates select="product"/>
       <td colspan="3" align="right">Order Total: </td>
       <td><xsl:value-of select="totalSys:totalCost()"/></td>
   <br /><hr /><br />


 <xsl:template match="product">
    <td><xsl:value-of select="@itemid"/></td>
    <td><xsl:value-of select="quantity"/></td>
    <td><xsl:value-of select="price"/></td>
    <td><xsl:value-of select="totalSys:getExtPrice(number(quantity), number(price))"/></td>
  • + Share This
  • 🔖 Save To Your Account