21.3. Transforming XML to HTML

Problem

The content for your application is in XML format, and you need to transform it to HTML for display in a browser.

Solution

Use an ASP.NET XML control and set its DocumentSource property to the XML document you need to transform and the TransformSource property to the XSLT document that specifies the transformation to be performed.

In the .aspx file, place an asp:Xml control where you want the HTML from the transformation to be placed in the page.

In the code-behind class for the page, use the .NET language of your choice to:

  1. Set the DocumentSource property of the XML control to the relative path to the XML document to convert.

  2. Set the TransformSource property to the relative path to the XSLT document.

Examples 21-5, 21-6 through 21-7 show the .aspx file and VB and C# code-behind files for an application that demonstrates this solution. The XML used as the source is shown in Example 21-8, and the XSLT used to transform the XML is shown in Example 21-9. The output transformed to HTML is shown in Figure 21-1.

Discussion

XML is becoming the predominant format for storing content. XML provides a platform-independent format that can be converted to many other formats, including HTML. By storing the content for your web application in XML, the same content can be transformed to the HTML needed for display in a standard browser or the HTML needed for a PDA.

In our example to illustrate this solution, an XML document containing book information (Example 21-8

Get ASP.NET 2.0 Cookbook, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.