5.12 Perform an XSL Transform


Problem

You need to transform an XML document into another document using an XSLT stylesheet.

Solution

Use the System.Xml.Xsl.XslTransform class. Load the XSLT stylesheet using the XslTransform.Load method, and generate the output document by using the Transform method and supplying a source document.

Discussion

XSLT (or XSL transforms) is an XML-based language designed to transform one XML document into another document. XSLT can be used to create a new XML document with the same data but arranged in a different structure or to select a subset of the data in a document. It can also be used to create a different type of structured document. XSLT is commonly used in this manner to format an XML document into an HTML page.

XSLT is a rich language, and creating XSL transforms is beyond the scope of this book. However, you can learn how to create simple XSLT documents by looking at a basic example. This recipe transforms the orders.xml document shown in recipe 5.6 into an HTML document with a table and then displays the results. To perform this transformation, you'll need the following XSLT stylesheet:

 <?xml version="1.0" encoding="UTF-8" ?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"     version="1.0" >   <xsl:template match="Order">     <html><body><p>     Order <b><xsl:value-of select="Client/@id"/></b>     for <xsl:value-of select="Client/Name"/></p>     <table border="1">     <td>ID</td><td>Name</td><td>Price</td>     <xsl:apply-templates select="Items/Item"/>     </table></body></html>   </xsl:template>     <xsl:template match="Items/Item">     <tr>     <td><xsl:value-of select="@id"/></td>     <td><xsl:value-of select="Name"/></td>     <td><xsl:value-of select="Price"/></td>     </tr>   </xsl:template> </xsl:stylesheet> 

Essentially, every XSL stylesheet consists of a set of templates. Each template matches some set of elements in the source document and then describes the contribution that the matched element will make to the resulting document. To match the template, the XSLT document uses XPath expressions, as described in recipe 5.6.

The orders.xslt stylesheet contains two template elements (as children of the root stylesheet element). The first template matches the root Order element. When the XSLT processor finds an Order element, it outputs the tags necessary to start an HTML table with appropriate column headings and inserts some data about the client using the value-of command, which outputs the text result of an XPath expression. In this case, the XPath expressions (Client/@id and Client/Name) match the id attribute and the Name element.

Next the apply-templates command is used to branch off and perform processing of any contained Item elements. This is required because there might be multiple Item elements. Each Item element is matched using the XPath expression Items/Item. The root Order node isn't specified because Order is the current node. Finally, the initial template writes the tags necessary to end the HTML document.

If you execute this transform on the sample orders.xml file shown in recipe 5.6, you'll end up with the following HTML document:

 <html>   <body>     <p>     Order <b>ROS-930252034</b>     for Remarkable Office Supplies</p>     <table border="1">       <td>ID</td>       <td>Name</td>       <td>Price</td>       <tr>         <td>1001</td>         <td>Electronic Protractor</td>         <td>42.99</td>       </tr>       <tr>         <td>1002</td>         <td>Invisible Ink</td>         <td>200.25</td>       </tr>     </table>   </body> </html> 

To apply an XSLT stylesheet in .NET, you use the XslTransform class. The following code shows a Windows-based application that programmatically applies the transformation and then displays the transformed file in a Web browser window. In this example, the code uses the overloaded version of the Transform method that saves the result document directly to disk, although you could receive it as a stream and process it inside your application instead.

 using System; using System.Windows.Forms; using System.Xml.Xsl; public class TransformXml : System.Windows.Forms.Form {     private AxSHDocVw.AxWebBrowser webBrowser;      // (Designer code omitted.)     private void TransformXml_Load(object sender, System.EventArgs e) {         XslTransform transform = new XslTransform();                      // Load the XSL stylesheet.         transform.Load("orders.xslt");                      // Transform orders.xml into orders.html using orders.xslt.         transform.Transform("orders.xml", "orders.html", null);         object var = null;         webBrowser.Navigate(           "file:///" + Application.StartupPath + @"\orders.html",           ref var, ref var, ref var, ref var);     } } 
Note  

The .NET Framework does not include any controls for rendering HTML content. However, this functionality is available through COM interoperability if you use the ActiveX Web browser control provided with Microsoft Internet Explorer and the Microsoft Windows operating system. This window can show local or remote HTML files, and supports JavaScript, VBScript, and all Internet Explorer plug-ins. (See recipe 11.4 for details on adding the Web browser control to a project.)

The application is shown in Figure 5.2.


Figure 5.2: The stylesheet output for orders.xml.



C# Programmer[ap]s Cookbook
C# Programmer[ap]s Cookbook
ISBN: 735619301
EAN: N/A
Year: 2006
Pages: 266

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