XSL Transformations using ASP.NET - Dynamic transformation
(Page 3 of 7 )
The first example is about as simple as it gets when it comes to using ASP.NET for XSL Transformations. Next, I will attempt to make things dynamic - take a look at the next two examples that re-use the ASP.NET "Xml" server control; each introducing its own new, little twist.
<%@ Page Language="C#" %>
<SCRIPT runat="server">
void Page_Load() {
output.DocumentSource = "articles.xml";
output.TransformSource = "articles.xsl";
}
</SCRIPT>
<HTML>
<HEAD>
<TITLE>ASPFree.com</TITLE>
<BASEFONT face="Arial" size="2">
</HEAD>
<BODY>
<asp:Xml id="output" runat="server" />
</BODY>
</HTML>
If you have been using ASP.NET server controls for some time, you will already know that it is possible to ssign values to the attributes of a server control by invoking them as properties of the same .NET object at run time.
Confused? Let me review quickly.
Behind the scenes in the .NET environment, I can safely state that "everything is an object." Consider the ASP.NET "Xml" server control used in all my examples so far. Under the hood, this control is an instance of the "System.Web.UI.WebControls.Xml" class, equipped with its own set of properties and methods. This means that you can specify values for the attributes of the server control by simply assigning those values to the corresponding properties of the underlying class.
This is exactly what I have done in the second example - I have assigned values to the "DocumentSource" and "TransformSource" properties of the local instance of the "System.Web.UI.WebControls.Xml" class at run time. This modification allows me to drop the definitions of the "DocumentSource" and "TransformSource" attributes from within the <asp:Xml> element and thereby make the process of XSL Transformation more dynamic.
The next example demonstrates another manner in which this <asp:Xml> element can be used:
<%@ Page Language="C#" %>
<HTML>
<HEAD>
<TITLE>ASPFree.com</TITLE>
<BASEFONT face="Arial" size="2">
</HEAD>
<BODY>
<asp:Xml id="output" runat="server" TransformSource="articles.xsl" >
<article id="110">
<title>Implementing XSL Transformations with ASP.NET</title>
<author>Harish R. Kamath</author>
<category>ASP.NET</category>
<abstract>Leverage on the built-in functionality available in ASP.NET to transform your XML documents using XSL Transformations!</abstract>
</article>
</asp:Xml>
</BODY>
</HTML>
While the output (for the third example) remains the same as that for the first two, the noteworthy difference is the manner in which the XML content has been provided to the "Xml" server control. Instead of the customary use of the "DocumentSource" attribute (or property), you will notice that I have chosen to enclose the XML text within the <asp:Xml> element.
This ability to enclose the raw XML between start and end elements of the <asp:Xml> element allows me to change the source of XML data with great ease. This technique is useful if I do not have the XML data in a flat file, but I have to generate it dynamically - say, from a database, or by combining multiple XML documents into a single file at run time. In case of the former, I can easily connect to a database, build the entire XML structure in memory and spit it out between the <asp:Xml> elements. Then, the .NET compiler takes over - it loads the associated XSLT style sheet, applies it to the raw XML data between the <asp:Xml> tags, thereby generating the desired HTML output.
And there’s more - you can learn about the ASP.NET "Xml" server control at the following URL: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemWebUIWebControlsXmlClassTopic.asp
Next: Jumping higher >>
More XML Articles
More By Harish Kamath