XSL Transformations using ASP.NET - Code dissection
(Page 5 of 7 )
<%@ Page Language="C#" %>
<%@ import Namespace="System.Xml.Xsl" %>
<%@ import Namespace="System.Xml.XPath" %>
<%@ import Namespace="System.IO" %>
<%
// snip
%>
For starters, I have imported the "System.Xml.Xsl" and "System.Xml.XPath" assemblies in order to create instances of the required classes later in the script. Next, the "System.IO" assembly has to be imported in order to create an instance of the StringWriter() object, the purpose of which will be clear as I proceed with the explanations.
<%
// snip
<SCRIPT runat="server">
void Page_Load() {
// path to the XML and XSLT files
string strXMLFile = Server.MapPath("articles.xml");
string strXSLTFile = Server.MapPath("articles.xsl");
// create an instance of XPathDocument object
// to load the XML file
XPathDocument objXmlFile = new XPathDocument(strXMLFile);
// create instance of XstTransform object
XslTransform objXSLTransform = new XslTransform();
// snip
}
</SCRIPT>
// snip
%>
Next, I have the customary Page_Load() function - the function begins with the definitions of two variables i.e. strXMLFile and strXSLTFile, to store path to the XML and XSLT code files respectively. Next, I have created instances of the XPathDocument() and XslTransform() classes for use in my script.
Note that I could have opted for the more-familiar XmlDocument() object instead of the XPathDocument() object. However, if you are familiar with the Document Object Model (a.k.a. the DOM) and its .NET implementation i.e. the aforementioned XmlDocument() object, you will know that this is memory intensive, since the object is required to keep the entire tree structure of the XML data in memory at all times. In contrast, the XPathDocument() is a much more lightweight object that uses the relatively simpler and easy-to-understand XPath syntax to access the required nodes directly without needing to keep any information in memory.
<%
// snip
<SCRIPT runat="server">
void Page_Load() {
// snip
// load the XSLT style sheet
objXSLTransform.Load(strXSLTFile);
// create an instance of StringWriter object
// to store result of transform
StringWriter objSWriter = new StringWriter();
// transform the XML file using the XSLT file
// store the output in a StringWriter object
objXSLTransform.Transform(objXmlFile, null, objSWriter, null);
// display the output in the browser
output.Text = objSWriter.ToString();
// clean up memory
objXmlFile = null;
objXSLTransform = null;
}
</SCRIPT>
// snip
%>
Next, I invoke the Load() method of the XslTransform() object to load the XSLT style sheet file - the location to the file stored in the variable "strXSLTFile" is passed as an input parameter. This is followed by the creation of an instance of the StringWriter() object that is passed as an output parameter to the Transform() method of the XslTransform() object.
As you might have guessed, the Transform() method does all the hard work of applying the XSLT rules (present in the style sheet file) to the XML data (loaded in the local instance XPathDocument() object). This method in turn returns the transformed HTML output as a StringWriter() object.
Finally, I have used the ToString() method of the StringWriter() object to assign the transformed output to the "Text" property of an ASP.Net "Label" server control for display in the browser.
Next: Publish or you will perish >>
More XML Articles
More By Harish Kamath