Create ASP.NET pages from XML data
A lot of ASP applications produce their output based on the contents of one or more XML files. XSLT documents transform the browser-neutral contents of those XML files into browser-specific HTML. The transformation is always accomplished through VBScript or JScript code, and the results are appended to the output stream.
In ASP.NET, the implementation of the same technique is made significantly easier and more flexible by the
The
In .NET, the XslTransform class performs the XSLT transformation. The class takes the source document and the style sheet and returns either XML or HTML output. The following code snippet shows the few lines of code you need to use the class:
XslTransform xslt = new XslTransform();
xslt.Load(stylesheet);
xslt.Transform(source, output);
The control operates as a sort of declarative counterpart for the XslTransform class and makes use of the class internally. You use the
<xml id="theXml">
<asp:xml runat="server">
... xml data ...
</asp.xml>
</xml>
You can specify a source document in various ways and not just as inline code. You can use an external file, a string, and even an instance of the .NET XML document object model.
A style sheet can be specified through a file or using a preconfigured XslTransform object. The output of the transformation, if any, is the Web page output stream. Table A shows the properties of the control.
Table A: Control Properties
|
All the settings for the document and the transformation are mutually exclusive, and the last setting wins. For example, if you set both Document and DocumentSource, no exception is thrown, but the first assignment is overridden.
Using the controlListing A demonstrates how to set up an XML-to-HTML transformation using a style sheet document.
Listing A
<asp:xml runat="server" TransformSource="EmpInfo.xsl">
<MyDataSet>
<NorthwindEmployees>
<Employee>
<employeeid>1</employeeid>
<firstname>Nancy</firstname>
<lastname>Davolio</lastname>
<title>Sales Representative</title>
<notes>...</notes>
</Employee>
</NorthwindEmployees>
</MyDataSet>
</asp:xml>
The
You could also describe the whole page with XML and employ a style sheet to translate the document to HTML. However, this is not always the best solution to gain flexibility and results in a poor extensibility model. But the
<asp:xml runat="server" id="theXml"
DocumentSource="Employees.xml"
TransformSource="EmpInfo.xsl" />
If you need to pass in an argument, just create and populate an instance of the XsltArgumentList class and pass it to the control using the TransformArgumentList property:
XsltArgumentList args = new XsltArgumentList();
DateTime today = DateTime.Now;
args.AddParam("orderdate", "", d.ToString());
theXml.TransformArgumentList = args;
The ASP.NET page in Listing B contains an XML data island created using the
The following example demonstrates multibrowser programming. The XML control is bound to a particular file but no transformation style sheet is statically declared:
<asp:xml runat="server" id="theXml"
DocumentSource="Employees.xml" />
At runtime, the host page determines the calling browser and applies the transformation that best fits:
theXml.TransformSource = "standard.xsl";
if (Request.Browser.Type ToUpper() == "IE5")
theXml.TransformSource = "ie.xsl";
The Request.Browser property detects the type and the capabilities of the underlying browser.
Conclusion
In ASP.NET, you can use the