ASP.NET
  Home arrow ASP.NET arrow Page 6 - Advanced XSL Transformations With ASP.NET
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Mobile Linux 
App Generation ROI 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
ASP.NET

Advanced XSL Transformations With ASP.NET
By: Harish Kamath
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 8
    2005-02-07

    Table of Contents:
  • Advanced XSL Transformations With ASP.NET
  • Passing the buck
  • Getting Feedback
  • Code in Your Style Sheets
  • The Magic of Compounding
  • The Magic of Compounding Demystified
  • Conclusion

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Advanced XSL Transformations With ASP.NET - The Magic of Compounding Demystified


    (Page 6 of 7 )

    First, we have our "interest_calculator.aspx" ASP.NET script that displays a neat little form (as shown below) when first loaded in the browser. This Web form allows the user to enter the principal amount of the deposit, the interest rate offered and the number of years for which the amount will be deposited.

    Advanced XSL Transformations in ASP.NET

    As the user enters the required values in the form, the different ASP.NET Field Validator controls check if the values entered in the text fields are valid. If not, appropriate error messages are displayed. Here is a sample output if a user enters incorrect values:

    Advanced XSL Transformations in ASP.NET

    You can learn more about how to use these ASP.NET Field Validator server controls here: http://www.aspfree.com/c/a/ASP.NET/Input-Validation-With-ASP.NET-1/

    When the user hits the "Calculate" button, the "OnClick" event associated with the button is fired and the ASP.NET script invokes our custom Submit_Form() function.

    This Submit_Form() function -- once again -- checks if all the values entered in the form are valid. This is a quick server-side check made by checking the state of the "IsValid" flag. If there are no errors, it makes the "pnlInputForm" Panel control (displaying the Web form) disappear and the "pnlOutput" Panel control (enclosing a label control) visible.

    Next, we proceed to carry out the first transformation by loading the "compounding_terms.xml" file and the "interest_calculator.xsl" file in XPathDocument() and XslTransform() objects respectively. Note that we have created a MemoryStream() object to store the XML output of this transformation.

    Since the first style sheet require three input parameters -- corresponding to the values entered on the Web form -- we proceed to create an instance of the XsltArgumentList() object and add the required parameters (and values from the Text server controls) using the AddParam() method.

    Next, we invoke the Transform() method to complete the first transformation. Here we use the custom calcCompundedAmount() function enclosed within the <msxsl:script> element (in the "interest_calculator.xsl" style sheet) to calculate the final value for each compounding term.

    Here is a quick look at this function:

    <%

    // snip

    <msxsl:script language="C#" implements-prefix="calculator">

    <![CDATA[

     public double calcCompundedAmount(double dblPrinicipal, double dblInterestRate, double intYears, double intCompoundingTerm){

     double dblCompoundedAmount = 0;

     dblCompoundedAmount = dblPrinicipal * (Math.Pow((1 + (dblInterestRate/(100
    * intCompoundingTerm))), intYears * intCompoundingTerm));

    return Math.Round(dblCompoundedAmount, 2);

    }
    ]]>
    </msxsl:script>

    // snip

    %>

    The above formula (to calculate the compound interest) looks daunting, but it does the required job -- take our word for it!

    The following XML is generated as the output of the first transformation. Note that this output is never visible to the end user.

    <?xml version="1.0" encoding="utf-8"?>
    <output>
    <principal>1000</principal>
    <interest>10</interest>
    <years>3</years>
    <compoundedvalue term="1">1331</compoundedvalue> <compoundedvalue term="2">1340.1</compoundedvalue> <compoundedvalue term="4">1344.89</compoundedvalue>
    <compoundedvalue term="6">1346.53</compoundedvalue>
    <compoundedvalue term="12">1348.18</compoundedvalue>
    </output>

    Instead, we proceed to instantiate a new XPathDocument() object using the MemoryStream() object and transform this XML data using the "interest_calculator_output.xsl" style sheet.

    The end result is a neat display of data in a user-friendly manner, as shown below.

    Advanced XSL Transformations in ASP.NET

    Just a little warning before we close: it is required to reset the position of MemoryStream() to 0 before we pass it as an input of the second transformation; otherwise the .NET compile returns an error message stating the "root element cannot be found" and the script stops execution.

    Now that we have demonstrated the "magic of compounding" using some powerful ASP.NET scripting and nifty XSL Transformations, it's time to tell your neighborhood bank manager that you too want to enjoy this magic with your money!

    More ASP.NET Articles
    More By Harish Kamath


     

    ASP.NET ARTICLES

    - Disadvantages of the ASP.NET MVC Framework
    - Advantages of the ASP.NET MVC Approach
    - ASP.NET Web Forms Weaknesses
    - ASP.NET Web Forms Meets ASP.NET MVC
    - Source Code for Saving and Retrieving Data w...
    - Using GridView to Save and Retrieve Data wit...
    - Handling Dynamic Images in ASP.NET 3.5 AJAX ...
    - Retrieving Data with AJAX and the GridView C...
    - Playing with Images in ASP.NET 3.5 AJAX Appl...
    - Saving and Retrieving Data with AJAX
    - Enhancing PHP Via the ASP.NET AJAX Framework...
    - Enhancing PHP Programming with the ASP.NET A...
    - Classes and ASP.NET AJAX
    - Using ASP.NET AJAX
    - Building a Simple Storefront with LINQ





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
    Stay green...Green IT