Your source for ASP-related frequently asked questions and answers! : ASP FAQS : XML

Question: How can I display XSL-formatted XML data in an ASP.NET Web page?

Answer: Introduction
In a previous FAQ, we examined a technology called XSL, or eXtensible Stylesheet Language. XSL allows for XML data to be transformed from its raw form into some other format. A typical application of XSL is to transform XML data into HTML content, such as an HTML <table>. (An XSLT file is a file that contains XSL syntax describing how an XML file should be formatted.)

Oftentimes you want to use XSL to display XML data in a Web page. With certain browsers, this transformation can be applied on the Web browser itself, by specifying the XSL document in the XML file and having the user visit the XML file directly through their browser. More often than not, though, you want to transform the XML data to HTML using an XSLT file on the server-side, and then send this HTML to the requesting browser.

This task can be accomplished in any server-side Web programming language, such as PHP, ASP, ASP.NET, JSP, and so on. This FAQ will look at how to use the ASP.NET XML Web control to display XSL-formatted XML data in an ASP.NET Web page - it's insanely easy. Also, at the end of this FAQ you can find links to information on how to accomplish the same through a classic ASP page.

The XML Web Control
The ASP.NET XML Web control is a Web control designed to display XML data in an ASP.NET Web page. To use the XML Web control in an ASP.NET Web page, simply use the following syntax:

<asp:xml runat="server" id="ID" />

In order to display XML data, you must specify where the Web control should get the XML data from. You can set one of the three following properties to specify where the Web control should retrieve the XML data:

* Document
* DocuumentSournce
* DocumentContent

The Document property, if set, must be set to an instance of the XmlDocument class. Therefore, this is a property that you would set programmatically. For instance, say that you are retrieving XML data from an RSS feed. As discussed in Consuming an RSS Feed with ASP.NET, an RSS feed is XML data retrieved from a remote URL, and can be accessed with just a few lines of source code in an ASP.NET file. This XML stream can be read in through an XmlDocument object, which could then be assigned to the XML Web control's Document property to be displayed in the Web control.

The DocumentSource and DocumentContent properties are string properties. DocumentSource specifies a physical file specifying where the XML data resides. If you do not have the XML content in a file, but within a string variable, you can simply assign the string containing the XML content to the DocumentContent property.

In addition to specifying the XML data for the XML Web control, we also want to specify the XSL to transform the XML into HTML. The XSL can be specified in a similar fashion as the XML data was, through one of three properties:

* Transform
* TransformSource
* TransformArgumentList

As with the Document property, the Transform property expects an instance of the XslTransform class. TransformSource, like DocumentSource, expects a file name that contains the XSL content. The TransformArgumentList property expects an instance of the XsltArgumentList class. The XsltArgumentList class can be used to specify a number of XSLT transformations.

An Example
A simple example of displaying XML data using an XSLT file in an ASP.NET Web page can be seen at this live demo. The live demo has contains an XML Web control with its DocumentSource and TransformSource property set to specific file names. Specifically, the XML data comes from this file, while the XSL comes from this file.

Note that for this example to work, we simply used the following Web control syntax:

<asp:xml runat="server" id="xmlDisp"

          TransformSource="dispBooks.xsl" />

No code needed! It's that simple.

For More Information...
For more information on the ASP.NET XML Web control check out the technical docs. For information on displaying XML data using XSL in a classic ASP page, be sure to read XML and XSL with ASP, Creating an XML and XSL Class, and Using XSL Stylesheets to Translate XML into HTML.

Happy Programming!

FAQ posted by Scott Mitchell at 4/7/2003 6:17:13 PM to the XML category. This FAQ has been viewed 87,519 times.

Copyright 2014 QuinStreet Inc. All Rights Reserved.
Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers