When you think ASP, think...
Recent Articles
All Articles
ASP.NET Articles
Related Web Technologies
User Tips!
Coding Tips

Sample Chapters
JavaScript Tutorials
MSDN Communities Hub
Official Docs
Stump the SQL Guru!
XML Info
Author an Article

The 4 Guys Present: ASPFAQs.com

Jump to a FAQ
Enter FAQ #:
..or see our 10 Most Viewed FAQs.

4GuysFromRolla.com : ASP FAQS : XML


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

[Print this FAQ]

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.

Do you have a FAQ you'd like to suggest? Suggestions? Comments? If so, send it in! Also, if you'd like to be a FAQ Admin (creating/editing FAQs), let me know! If you are looking for other FAQs, be sure to check out the 4Guys FAQ and Commonly Asked Messageboard Questions!

Most Viewed FAQs:

1.) How can I format numbers and date/times using ASP.NET? For example, I want to format a number as a currency. (761643 views)
2.) I am using Access and getting a 80004005 error (or a [Microsoft][ODBC Microsoft Access Driver] The Microsoft Jet database engine cannot open the file '(unknown)' error) when trying to open a connection! How can I fix this problem? (207777 views)
3.) How can I convert a Recordset into an array? Also, how can I convert an array into a Recordset? (202549 views)
4.) How can I quickly sort a VBScript array? (196039 views)
5.) How can I find out if a record already exists in a database? If it doesn't, I want to add it. (156019 views)
6.) How do I display data on a web page using arrays instead of Do...While...MoveNext...???... (152331 views)
7.) When I get a list of all files in a directory via the FileSystemObject, they aren't ordered in any reasonable way. How can I sort the files by name? Or by size? Or by date created? Or... (140381 views)
8.) For session variables to work, must the Web visitor have cookies enabled? (110162 views)
9.) Can I send emails without using CDONTS? (107083 views)
10.) How can I take the result of a SELECT...MULTIPLE or a group of same-named checkboxes and turn it into a query? That is, if the user selects 3 answers, how can I construct a query that looks for all 3? (106308 views)
Last computed at 9/17/2007 3:22:00 AM

ASP.NET [1.x] [2.0] | ASPFAQs.com | Advertise | Feedback | Author an Article