Using the XslTransform Class

Using the XslTransform Class

Instead of using the ASP.NET Xml control to transform an XML document with an XSL stylesheet, you can use the XslTransform class. The advantage of using the XslTransform class is that it enables you to work with XSL transforms without necessarily displaying the results. For example, you can use the XslTransform class to transform an XML document and save it to the file system.


For more information on using the file system from within an ASP.NET page, see Chapter 25, "Working with the File System."

The page in Listing 13.17 uses the XslTransform class to apply the TransformProducts.xsl stylesheet to the Products table in the Northwind database. The results of the transformation are written directly to the Response object's output stream.

Listing 13.17 Xsl.aspx
 <%@ Import Namespace="System.Data" %> <%@ Import Namespace="System.Data.SqlClient" %> <%@ Import Namespace="System.Xml" %> <%@ Import Namespace="System.Xml.Xsl" %> <Script Runat="Server"> Sub Page_Load   Dim conNorthwind As SqlConnection   Dim dadProducts As SqlDataAdapter   Dim dstProducts As DataSet   Dim objDataDocument As XmlDataDocument   Dim objXsl As XslTransform   conNorthwind = New SqlConnection( "Server=localhost;UID=sa;PWD=secret;Database=Northwind" )   dadProducts = New SqlDataAdapter( "Select * From Products", conNorthwind )   dstProducts = New DataSet()   dadProducts.Fill( dstProducts, "Products" )   objDataDocument = New XmlDataDocument( dstProducts )   objXsl = New XslTransform   objXsl.Load( MapPath( "TransformProducts.xsl" ) )   objXsl.Transform( objDataDocument, Nothing, Response.OutputStream ) End Sub </Script> 

The C# version of this code can be found on the CD-ROM.

In Listing 13.17, a DataSet is populated with the contents of the Products table from the Northwind database. Next, an instance of the XmlDataDocument class is created from the DataSet . The XmlDataDocument is used with the XslTransform class.

Next, an instance of the XslTransform class is created. The XslTransform 's Load method retrieves an XSL stylesheet from the file system, and the Transform method is called. The Transform method converts the contents of the XmlDataDocument using the stylesheet and outputs the results to the Response object's output stream.

ASP.NET Unleashed
ASP.NET 4 Unleashed
ISBN: 0672331128
EAN: 2147483647
Year: 2003
Pages: 263

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: