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
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. |