Applying XSLT to XML Using ASP.NET - XSLT with ASP.NET
(Page 3 of 5 )
The above example works well with IE6 or other latest browsers (which support XSLT), but what about the situation of old browsers? Our sample example would never work on those browsers. To overcome this problem, we shift XML and XSLT to the server side programming that supports these technologies. One of them is ASP.NET. I further extend the previous sample example with respect to ASP.NET using a simple web form with few lines of ASP.NET code.
Open your Visual Studio.NET and create a new ASP.NET project. Add the above two files (sample1.xml and sample1.xslt) to the project. Drag an XML control from toolbox on to the webform. Press F7 (code window) and copy the following two statements at the top:
Imports System.Xml
Imports System.Xml.Xsl
Copy the following code in the page load event of that webform.
Dim docXML As New XmlDocument
docXML.Load(Server.MapPath("sample1.xml"))
Dim docXSL As New XslTransform
docXSL.Load(Server.MapPath("sample1.xslt"))
Xml1.Document = docXML
Xml1.Transform = docXSL
I think the above code is self-explanatory. Basically, we are creating two objects ‘docXML’ (to hold an XML document) and ‘docXSL’ (to hold an XSL document). Using the ‘Load’ method, we are loading the two files in to the respective objects. We assign both of them to the XML server control (xml1) provided in ASP.NET. The transformation gets implemented automatically at run-time without any extra hurdle!!
Press F5 to execute the web application and you should be able to see the same output to which you have seen earlier. By now, we designed a web application using XML and XSLT, which is completely browser independent.
But, always designing static XML documents may not be helpful in all the situations. What if I want to have a dynamically generated XML to be transformed through XSLT? The next section addresses this issue in a very pleasant manner.
Next: Query a Database and Transform through XSLT using ASP.NET >>
More XML Articles
More By Jagadish Chaterjee