Microsoft KB Archive/320847

= HOW TO: Pipeline XSLT Transformations in .NET Applications =

Article ID: 320847

Article Last Modified on 9/4/2003

-

APPLIES TO


 * Microsoft .NET Framework 1.0
 * Microsoft .NET Framework 1.1

-



This article was previously published under Q320847



IN THIS TASK

 * Summary - An Overview of Pipelining XSLT Transformations
 * Create and Setup the Sample ASP.NET Application
 * Test the Sample ASP.NET Application



SUMMARY
This article demonstrates how to pipeline XSL Transformation (XSLT) transformations in .NET applications. The term &quot;Pipelining&quot; in the context of XSLT transformations, is the process by which the data in a source Extensible Markup Language (XML) document is incrementally transformed by a sequence of two or more XSLT stylesheets to generate the final required output. During the process, the output that is generated by each intermediate phase in the transformation pipeline is used as the input to the following phase all the way through to the final transformation phase that generates the output that you want.

The .NET Framework classes in the System.Xml.Xsl, System.Xml.XPath, and the System.IO namespaces can be used to programmatically pipeline XSLT transformations in .NET applications. The following sections of this article provide instructions to help you implement and examine the process of pipelining XSLT transformations in an ASP.NET application. The illustrated concept can also be applied and used in other types of .NET applications that have to pipeline XSLT transformations.

back to the top

Create and Setup the Sample ASP.NET Application
 Use Visual Studio .NET to create a new Microsoft Visual Basic .NET ASP.NET Web Application project named &quot;XSLTPipelineDemo&quot; (without quotation marks).  Use the following code to create and add an XML document called &quot;Employees.xml&quot; (without quotation marks) to the project:  1 Nancy Davolio 2 Andrew Fuller 3 Janet Leverling   Use the following code to create and add an XSLT style sheet called &quot;sortEmployees.xslt&quot; (without quotation marks) to the project. This stylesheet contains XSLT code to sort the data in the Employees.xml XML document (in descending order on the family name [employee, name, and family] element). The output that is generated when you apply this stylesheet is a sorted version of the source XML data: <?xml version='1.0'?>

<xsl:stylesheet xmlns:xsl=&quot;http://www.w3.org/1999/XSL/Transform&quot; version=&quot;1.0&quot;>

<xsl:param name=&quot;sortColumn&quot; select=&quot;'family'&quot;/> <xsl:param name=&quot;sortOrder&quot; select=&quot;'descending'&quot;/>

<xsl:template match=&quot;employees&quot;>

<xsl:apply-templates select=&quot;employee/name&quot;> <xsl:sort select=&quot;./*[local-name = $sortColumn]&quot; order=&quot;{$sortOrder}&quot;/> </xsl:apply-templates>

</xsl:template>

<xsl:template match=&quot;employee/name&quot;> <xsl:copy-of select=&quot;parent::node/*&quot;/> </xsl:template>

</xsl:stylesheet> </li>  Use the following code to create and add an XSLT style sheet called &quot;EmployeesHTMLTable.xslt&quot; (without quotation marks) to the project. This stylesheet contains XSLT code to transform the source Employee XML data (that is contained in the Employees.xml XML document or in the sorted XML that is generated when you apply the sortEmployees.xsl XSLT style sheet to the Employees.xml XML document) to a Hypertext Markup Language (HTML) table: <?xml version='1.0'?>

<xsl:stylesheet xmlns:xsl=&quot;http://www.w3.org/1999/XSL/Transform&quot; version=&quot;1.0&quot;>

<xsl:template match=&quot;employees&quot;> <HTML> <BODY> </BODY> </HTML> </xsl:template>

</xsl:stylesheet>

</li> You can apply both of the XSLT stylesheets independently of one another to the Employees.xml file. Applying sortEmployees.xsl sorts the data in the Employees.xml file, while applying employeesHTMLTable.xsl generates an HTML table representation of the source XML. To obtain an HTML table representation of the data sorted in descending order by family name, pipeline these two XSLT transformations (first apply sortEmployees.xsl to the Employees.xml file, and then apply employeesHTMLTable.xsl to the output generated by the first transformation).</li>  Copy and paste the following code in the Page_Load event procedure of WebForm1.aspx to implement the previously described pipelining: 'Load the source XML document (Employees.xml) into an XPathDocument object.

Dim xmlDoc As New System.Xml.XPath.XPathDocument(Server.MapPath(&quot;employees.xml&quot;))

'Load the first stylesheet (sortEmployees.xslt) that sorts the data in employees.xml 'into an XslTransform object.

Dim xslt As New System.Xml.Xsl.XslTransform xslt.Load(Server.MapPath(&quot;sortEmployees.xslt&quot;))

'Execute the first transformation and write the interim output (the sorted XML) 'to a MemoryStream object. Using an in memory stream (as opposed to a FileStream object) 'to store the interim output generated by the first transformation is a better option from 'a performance standpoint as it avoids unnecessary disk I/O.

Dim memStream As New System.IO.MemoryStream xslt.Transform(xmlDoc, Nothing, memStream)

'Reset the MemoryStream internal pointer to point to the 'beginning of the stream. This is required for the following line of 'code to be able to read the data from the memory stream.

memStream.Position = 0

'Load the interim sorted XML output in an XPathDocument object. 'Notice that a second XPathDocument object is created to do this. 'The reason for this is that the XPathDocument class does not implement a Load 'method (like the System.Xml.XmlDocument class) that can be used to re-load new XML data into an existing 'object instance. While a single XmlDocument object could have been used to execute the two pipelined 'transformations in this sample, it has not been chosen because the XPathDocument is highly optimized for XPath query 'execution and XSLT transformations. It is the recommended interface to use when programatically executing 'XSLT transformations in .NET applications.

Dim interimDoc As New System.Xml.XPath.XPathDocument(memStream)

'Load the second stylesheet(employeesHTMLTable.xslt)that transforms the sorted data to an HTML table 'into the XslTransform object.

xslt.Load(Server.MapPath(&quot;employeesHTMLTable.xslt&quot;))

'Execute the second transformation and stream the output directly to the 'ASP.Net Response object's OutputStream to display the results in the client browser.

xslt.Transform(interimDoc, Nothing, Response.OutputStream) </li> Save and build the solution.</li></ol>

back to the top

Test the Sample ASP.NET Application

 * 1) Execute the XSLTPiplelineDemo ASP.NET application to start and display WebForm1.aspx in the browser.
 * 2) The code in the Page_Load event procedure of WebForm1.aspx runs the XSLT transformations and generates the output in the form of an HTML table that displays a sorted (in descending order based on the family name) listing of the employees.
 * 3) Close the browser window to quit the ASP.NET application.

back to the top

Keywords: kbhowtomaster kbbcl KB320847

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.