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.

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:

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.

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!
Next: Conclusion >>
More ASP.NET Articles
More By Harish Kamath