Transforming Documents with XSLT


An XSL Transformation (XSLT) essentially combines the XPath language for searching XML nodes and returning node lists with a set of functions designed specifically for converting a source XML document into a destination XML document. This destination document can be another XML document that simply contains the data in a different format, or it can be in the form of XHTML, an XML-compliant HTML document.

Converting XML into XHTML is probably the most common use for XSLT, though it is frequently used for converting document formats and facilitating data exchange between disparate systems.

To transform an XML document, you need an XSLT document. Listing 8.4 shows you an XSLT document for transforming the items.xml document shown earlier in the chapter. If you want more information on XSLT, there are several books available, as well as many online references, such as http://www.w3schools.com/xsl.

Listing 8.4. itemsTransform.xslt

<?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet version="1.0"     xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/">     <html>       <head>         <title>List of Game Items</title>       </head>     <body bgcolor="#c0c0c0">       <table width="100%" border="0" cellspacing="2" cellpadding="2">         <tr>           <th align="left">ID</th>           <th align="left">Name</th>         </tr>         <xsl:for-each select="/items/item">           <tr>             <td>               <xsl:value-of select="@id"/>             </td>             <td>               <xsl:value-of select="@name"/>             </td>           </tr>           <xsl:for-each select="attribute">             <tr>               <td colspan="2">                 <b>                   <xsl:value-of select="@name"/>                 </b>                 :                 <xsl:value-of select="@value"/>               </td>             </tr>           </xsl:for-each>           <xsl:for-each select="specials">             <xsl:for-each select="special">               <tr>                 <td colspan="2">                   <b>                     <xsl:value-of select="@name"/>                   </b>:                   <xsl:value-of select="@description"/>                 </td>               </tr>             </xsl:for-each>           </xsl:for-each>           <tr>             <td colspan="2">               <hr size="1" color="#880000"></hr>             </td>           </tr>         </xsl:for-each>       </table>     </body>     </html> </xsl:template> </xsl:stylesheet> 

The actual transformation is accomplished using the XslCompiledTransform class, as shown in the following code snippet:

XslCompiledTransform xct = new XslCompiledTransform(); xct.Load(@"..\..\..\itemsTransform.xslt"); xct.Transform(@"..\..\..\..\items.xml", "items.html"); 


The preceding code results in an HTML page that looks like the one shown in Figure 8.1.

Figure 8.1. Results of an XSL transformation of the items.xml document.




Microsoft Visual C# 2005 Unleashed
Microsoft Visual C# 2005 Unleashed
ISBN: 0672327767
EAN: 2147483647
Year: 2004
Pages: 298

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