Home > Articles > Open Source > Ajax & JavaScript

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

Like this article? We recommend

Summary




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"
        xmlns:lxslt="http://xml.apache.org/xslt"
        xmlns:totalSys="TotalSystem"
        extension-element-prefixes="totalSys"
        version="1.0">

 <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;
   }

  </lxslt:script>
 </lxslt:component>

 <xsl:template match="/">

   <html>
   <head><title>Order Summaries</title></head>
   <body>

     <h1>Order Summaries</h1>

     <xsl:apply-templates/>

   </body>
   </html>

 </xsl:template>

 <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">
     <tr>
       <th>Product Number</th>
       <th>Quantity</th>
       <th>Unit Price</th>
       <th>Extended Price</th>
     </tr>
     <totalSys:resetTotal resetTo="0"/>
     <xsl:apply-templates select="product"/>
     <tr>
       <td colspan="3" align="right">Order Total: </td>
       <td><xsl:value-of select="totalSys:totalCost()"/></td>
     </tr>
   </table>
   <br /><hr /><br />

 </xsl:template>

 <xsl:template match="product">
  <tr>
    <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>
  </tr>
 </xsl:template>
 
</xsl:stylesheet>
  • + Share This
  • 🔖 Save To Your Account