Recipe 21.4. 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) is transformed into an HTML table using an XSLT document (Example 21-9). The transformation is performed by an Xml server control.

Figure 21-1. Transforming XML to HTML output


When you use an Xml control to do the work, you need to set only two of its properties to convert XML to HTML. The DocumentSource property needs to be set to the relative path to the XML document to convert, and the TRansformSource property needs to be set to the relative path to the XSLT document, as we have done in our example:

 

xmlTransform.DocumentSource = "xml/books.xml" xmlTransform.TransformSource = "xml/books.xslt"

xmlTransform.DocumentSource = "xml//books.xml"; xmlTransform.TransformSource = "xml//books.xslt";

Most controls that use files require a fully qualified name of the file on the web server. However, the Xml control requires a relative path from the root folder of the web site to the XML and XSLT files. Setting the properties to fully qualified paths will result in an exception being thrown.


The majority of the work to perform an XSL transformation is in the creation of the XSLT. An XSLT document is a specially formatted XML document and, as such, requires a declaration defining the version of XML and the encoding of the document.

In addition, elements indicating that the document is an XSL stylesheet and defining the output method are required:

 <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/> … </xsl:stylesheet> 

In XSLT, templates are used to replace specific content in the XML document with the template defined in an xsl:template element. In our example, two templates are used. The first template defines the base structure of the HTML table that will be used to display the XML content when it is converted to HTML, as shown here:

 <!-- output main table with header row --> <xsl:template match="Root"> <table width="80%" border="1" cellspacing="0" cellpadding="4" align="center"> <tr  bgcolor="#000080"> <td width="50%" align="center" >Title</td> <td width="25%" align="center" >ISBN</td> <td width="25%" align="center" >Publisher</td> </tr> <xsl:apply-templates select="Book" /> </table> </xsl:template> 

This template instructs the conversion to apply additional templates using the Book element of the XML document as a source for the data:

 <xsl:apply-templates select="Book"/> 

The xsl:apply-templates element is roughly equivalent to a for loop that would iterate through each Book element in the XML document applying our second template:

 <!-- output a row in the table for each Book node in the XML document --> <xsl:template match="Book" > <tr  bgcolor="#FFFFE0"> <td width="50%"> <xsl:value-of select="Title"/> </td> <td width="25%" align="center"> <xsl:value-of select="ISBN" /> </td> <td width="25%" align="center"> <xsl:value-of select="Publisher" /> </td> </tr> </xsl:template> 

This template generates a row for the base HTML table for each Book element in the XML document. Each cell in the table contains an xsl:value-of element that instructs the transformation to insert the value of the element indicated by the select attribute in the cell.

XSLT is powerful and can be used to perform complex transformations, including transformations that dynamically vary according to passed parameters, and is not limited to converting XML to HTML. To learn the techniques of XSLT, these books are recommended: XSLT and the XSLT Cookbook, both from O'Reilly.

See Also

XSLT, by Doug Tidwell (O'Reilly); XSLT Cookbook, by Sal Mangano (O'Reilly)

Example 21-5. Transforming XML to HTML (.aspx)

 <%@ Page Language="VB" MasterPageFile="~/ASPNetCookbookVB.master" AutoEventWireup="false" CodeFile="CH21TransformingXMLToHTMLVB.aspx.vb" Inherits="ASPNetCookbook.VBExamples.CH21TransformingXMLToHTMLVB" Title="Transforming XML To HTML" %> <asp:Content  runat="server" ContentPlaceHolder> <div align="center" > Transform XML To HTML (VB) </div> <div align="center"> <br /> <asp:Xml  Runat="server" /> </div> </asp:Content> 

Example 21-6. Transforming XML to HTML code-behind (.vb)

 Option Explicit On  Option Strict On Namespace ASPNetCookbook.VBExamples ''' <summary> ''' This class provides the code-behind for ''' CH21TransformingXMLToHTMLVB.aspx ''' </summary> Partial Class CH21TransformingXMLToHTMLVB Inherits System.Web.UI.Page '''*********************************************************************** ''' <summary> ''' This routine provides the event handler for the page load event. It ''' is responsible for initializing the controls on the page. ''' </summary> ''' ''' <param name="sender">Set to the sender of the event</param> ''' <param name="e">Set to the event arguments</param> Private Sub Page_Load(ByVal sender As Object, _   ByVal e As System.EventArgs) Handles Me.Load 'set the names of the XML and XSLT documents used in the 'transformation xmlTransform.DocumentSource = "xml/books.xml" xmlTransform.TransformSource = "xml/books.xslt" End Sub 'Page_Load  End Class 'CH21TransformingXMLToHTMLVB  End Namespace 

Example 21-7. Transforming XML to HTML code-behind (.cs)

 using System; namespace ASPNetCookbook.CSExamples { /// <summary> /// This class provides the code-behind for  /// CH21TransformingXMLToHTMLCS.aspx  /// </summary> public partial class CH21TransformingXMLToHTMLCS : System.Web.UI.Page { ///*********************************************************************** /// <summary> /// This routine provides the event handler for the page load event. /// It is responsible for initializing the controls on the page. /// </summary> /// /// <param name="sender">Set to the sender of the event</param> /// <param name="e">Set to the event arguments</param> protected void Page_Load(object sender, EventArgs e) { // set the names of the XML and XSLT documents used in the // transformation xmlTransform.DocumentSource = "xml//books.xml"; xmlTransform.TransformSource = "xml//books.xslt"; } // Page_Load  } // CH21TransformingXMLToHTMLCS  } 

Example 21-8. XML source used for transformation

 <Root> <Book> <BookID>1</BookID> <Title>Access Cookbook</Title> <ISBN>0-596-00084-7</ISBN> <Publisher>O'Reilly</Publisher> </Book> <Book> <BookID>2</BookID> <Title>ASP.NET Cookbook</Title> <ISBN>0-596-00378-1</ISBN> <Publisher>O'Reilly</Publisher> </Book> <Book> <BookID>3</BookID> <Title>Perl Cookbook</Title> <ISBN>1-565-92243-3</ISBN> <Publisher>O'Reilly</Publisher> </Book> <Book> <BookID>4</BookID> <Title>Java Cookbook</Title> <ISBN>0-596-00170-3</ISBN> <Publisher>O'Reilly</Publisher> </Book> <Book> <BookID>5</BookID> <Title>JavaScript Application Cookbook</Title> <ISBN>1-565-92577-7</ISBN> <Publisher>O'Reilly</Publisher> </Book> <Book> <BookID>6</BookID> <Title>VB .Net Language in a Nutshell</Title> <ISBN>0-596-00092-8</ISBN> <Publisher>O'Reilly</Publisher> </Book> <Book> <BookID>7</BookID> <Title>Programming Visual Basic .Net</Title> <ISBN>0-596-00093-6</ISBN> <Publisher>O'Reilly</Publisher> </Book> <Book> <BookID>8</BookID> <Title>Programming C#</Title> <ISBN>0-596-00117-7</ISBN> <Publisher>O'Reilly</Publisher> </Book> <Book> <BookID>9</BookID> <Title>.Net Framework Essentials</Title> <ISBN>0-596-00165-7</ISBN> <Publisher>O'Reilly</Publisher> </Book> <Book> <BookID>10</BookID> <Title>COM and .Net Component Services</Title> <ISBN>0-596-00103-7</ISBN> <Publisher>O'Reilly</Publisher> </Book> </Root> 

Example 21-9. XSLT used to transform HTML

 <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/> <!-- output main table with header row --> <xsl:template match="Root"> <table width="80%" border="1" cellspacing="0" cellpadding="4" align="center"> <tr  bgcolor="#000080"> <td width="50%" align="center" >Title</td> <td width="25%" align="center" >ISBN</td> <td width="25%" align="center" >Publisher</td> </tr> <xsl:apply-templates select="Book" /> </table> </xsl:template> <!-- output a row in the table for each Book node in the XML document --> <xsl:template match="Book" > <tr  bgcolor="#FFFFE0"> <td width="50%"><xsl:value-of select="Title"/></td> <td width="25%" align="center"><xsl:value-of select="ISBN" /></td> <td width="25%" align="center"><xsl:value-of select="Publisher" /></td> </tr> </xsl:template> </xsl:stylesheet> 



ASP. NET Cookbook
ASP.Net 2.0 Cookbook (Cookbooks (OReilly))
ISBN: 0596100647
EAN: 2147483647
Year: 2003
Pages: 202

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net