Channels ▼
RSS

Web Development

XSL Transformations

Source Code Accompanies This Article. Download It Now.


Sample Web Application

In our sample application, the user decides on a function to invoke (one of "Factorial," "Fibonacci," "IsPrime" and "nthPrime"), provides the parameter, and clicks on the button corresponding to the desired operation. The basic structure of the code that processes the client input is:

  1. XmlDocumentObject <-XML document containing the XIM code of the requested computation.
  2. Get user input parameter and insert it into XmlDocumentObject.
  3. Insert the processing instruction element to declare the interpreter stylesheet.
  4. Save the modified document in a file.
  5. Redirect the client to the saved XML file.

First, the stored "prepackaged" XIM code for the requested computation (whose only missing part is the input parameter value) is loaded into an XmlDocument object and the parameter supplied by the user is inserted into the code at the correct spot. Then the processing instruction indicating the name of the stylesheet (the XIM interpreter) to be applied to the document is inserted into the code. The resulting code in the object is stored in an XML document, and the client browser is redirected to this XML document. To demonstrate what happens when the client supplies a parameter and makes a choice, this code processes a "factorial" request:


private void FactBtn_Click(object sender, System.EventArgs e)
  {
   XmlDocument xmlDoc=new XmlDocument();
   xmlDoc.Load("c://inetpub/wwwroot/xim/fact5.xml");
   XmlElement var_n=xmlDoc.CreateElement("var_declare","");
   var_n.InnerText=TextBox1.Text;
   var_n.SetAttribute("name","nb"); 
       // Adding name attribute to variable element
       // Appending client input as a variable to XIM code 
       // for factorial computation
   xmlDoc.DocumentElement.FirstChild.AppendChild(var_n);
   XmlProcessingInstruction newPI;
      // Processing instruction to declare stylesheet name
   String PItext = "type='text/xsl' href='interp.xsl'";
      // Assigning the name of the interpreter 
      // stylesheet to processing instruction
   newPI = xmlDoc.CreateProcessingInstruction
                             ("xml-stylesheet", PItext);
      // Add processing instruction node to the document
   xmlDoc.InsertBefore(newPI, xmlDoc.DocumentElement);
   xmlDoc.Save("c://Inetpub/wwwroot/XIM/test.xml");
     // Save the modifications to the XIM code
     // Redirectng the client response 
     // to the just created XIM file
   Response.Redirect("http://194.27.78.64/XIM/test.xml");
  }

See Figure 3 for the result of the interpretation on the client machine of the factorial function, applied to number "20."

[Click image to view at full size]

Figure 3: The result of the computation of 20! executed and displayed on client's browser.


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.
 
Dr. Dobb's TV