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 new <asp:xml> server control. This control functions as a placeholder to inject the contents of an XML document directly into an ASP.NET page. The control can display the source of an XML document verbatim or display the results of an XSLT transformation.

The <asp:xml> control
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.Transform(source, output);  

The <asp:xml> control operates as a sort of declarative counterpart for the XslTransform class and makes use of the class internally. You use the <asp:xml> control when you need to embed XML documents in a Web page. For example, the control comes in extremely handy when you need to create XML data islands that the client will utilize. The snippet below shows how to embed the output of the server control into a client <xml> tag that Internet Explorer will interpret as an XML data island. In this case, the XML source is written as inline code:
<xml id=”theXml”>
<asp:xml runat=”server”>
… xml data …

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


Document  Gets or sets the XML source document using an XmlDocument
DocumentContent Gets or sets the XML source document using a string
DocumentSource Gets or sets the XML source document using a file
Transform Gets or sets the XslTransform class to use for transformations
TransformSource Gets or sets the stylesheet to use for transformations
TransformArgumentList Gets or sets the argument list for transformations

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 control
Listing A demonstrates how to set up an XML-to-HTML transformation using a style sheet document.

The <asp:xml> control can have an ID and can be programmatically accessed. For instance, you can check the browser’s capabilities and decide dynamically what style sheet would work best.

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> control definitely makes implementing that solution considerably easier:
<asp:xml runat=”server” id=”theXml”
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 <asp:xml> control and includes some VBScript code that retrieves and displays the content of the data island.

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.

In ASP.NET, you can use the <asp:xml> server control to accomplish a common task of ASP applications—converting some generic XML document to browser-specific code. The control works as a placeholder for the final XML or HTML text generated by the XML source file and the optional style sheet. The programming interface of the control closely reflects that of the .NET class devoted to XSL transformations, XslTransform.