HOW TO: Pipeline XSLT Transformations in .NET Applications (320847)
The information in this article applies to:
- Microsoft XML Classes (included with the .NET Framework 1.0)
- Microsoft XML Classes (included with the .NET Framework 1.1)
This article was previously published under Q320847 SUMMARY
This article demonstrates how to pipeline XSL Transformation (XSLT) transformations in .NET applications. The term "Pipelining" 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 "XSLTPipelineDemo" (without quotation marks).
- Use the following code to create and add an XML document called "Employees.xml" (without quotation marks) to the project:
<?xml version='1.0'?>
<employees>
<employee>
<employeeId>1</employeeId>
<name>
<given>Nancy</given>
<family>Davolio</family>
</name>
</employee>
<employee>
<employeeId>2</employeeId>
<name>
<given>Andrew</given>
<family>Fuller</family>
</name>
</employee>
<employee>
<employeeId>3</employeeId>
<name>
<given>Janet</given>
<family>Leverling</family>
</name>
</employee>
</employees>
- Use the following code to create and add an XSLT style sheet called "sortEmployees.xslt" (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="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:param name="sortColumn" select="'family'"/>
<xsl:param name="sortOrder" select="'descending'"/>
<xsl:template match="employees">
<employees>
<xsl:apply-templates select="employee/name">
<xsl:sort select="./*[local-name() = $sortColumn]" order="{$sortOrder}"/>
</xsl:apply-templates>
</employees>
</xsl:template>
<xsl:template match="employee/name">
<employee>
<xsl:copy-of select="parent::node()/*"/>
</employee>
</xsl:template>
</xsl:stylesheet>
- Use the following code to create and add an XSLT style sheet called "EmployeesHTMLTable.xslt" (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="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="employees">
<HTML>
<BODY>
<TABLE BORDER="1">
<TR>
<TH>Employee ID</TH>
<TH>Employee Name</TH>
</TR>
<xsl:for-each select="employee">
<TR>
<TD>
<xsl:value-of select="employeeId"/>
</TD>
<TD>
<xsl:value-of select="name/family"/>, <xsl:value-of select="name/given"/>
</TD>
</TR>
</xsl:for-each>
</TABLE>
</BODY>
</HTML>
</xsl:template>
</xsl:stylesheet>
- 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).
- 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("employees.xml"))
'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("sortEmployees.xslt"))
'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("employeesHTMLTable.xslt"))
'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)
- Save and build the solution.
back to the top
Test the Sample ASP.NET Application- Execute the XSLTPiplelineDemo ASP.NET application to start and display WebForm1.aspx in the browser.
- 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.
- Close the browser window to quit the ASP.NET application.
back to the top
Modification Type: | Major | Last Reviewed: | 9/4/2003 |
---|
Keywords: | kbBCL kbHOWTOmaster KB320847 kbAudDeveloper |
---|
|