The goal of the Extensible Stylesheet Language Transformation (XSLT) is to transform the content of a source XML document into another document that is different in format or structure. For example, to transform XML into HTML for use on a Web site or to transform it into a document that contains only the fields required by an application. This transformation process is specified by the W3C XSL Transformations (XSLT) Version 1.0 recommendation located at www.w3.org/TR/xslt. In the .NET Framework, the XslTransform class, found in the System.Xml.Xsl namespace, is the XSLT processor that implements the functionality of this specification. There are a small number of features that have not been implemented from the W3C XSLT Version 1.0 recommendation, listed in Outputs from an XslTransform. The following figure shows the transformation architecture of the .NET Framework. Transformation Architecture The XSLT recommendation uses XPath to select parts of an XML document, where XPath is a query language used to navigate nodes of a document tree. As shown in the diagram, the .NET Framework implementation of XPath is used to select parts of XML stored in several classes, such as an XmlDocument, an XmlDataDocument, and an XPathDocument. An XPathDocument is an optimized XSLT data store, and when used with XslTransform, it provides XSLT transformations with good performance. The following table list commonly used classes when working with XslTransform and XPath and their function.
The following code example loads an XSL style sheet, reads a file called mydata.xml into an XPathDocument, and performs a transformation on the data on a fictitious file called myStyleSheet.xsl, sending the formatted output to the console. [Visual Basic] Imports System Imports System.IO Imports System.Xml Imports System.Xml.XPath Imports System.Xml.Xsl Public Class Sample Private filename As [String] = "mydata.xml" Private stylesheet As [String] = "myStyleSheet.xsl" Public Shared Sub Main() Dim xslt As New XslTransform() xslt.Load(stylesheet) Dim xpathdocument As New XPathDocument(filename) Dim writer As New XmlTextWriter(Console.Out) writer.Formatting = Formatting.Indented xslt.Transform(xpathdocument, Nothing, writer, Nothing) End Sub ‘Main End Class ‘Sample [C#] using System; using System.IO; using System.Xml; using System.Xml.XPath; using System.Xml.Xsl; public class Sample { private const String filename = "mydata.xml"; private const String stylesheet = "myStyleSheet.xsl"; public static void Main() { XslTransform xslt = new XslTransform(); xslt.Load(stylesheet); XPathDocument xpathdocument = new XPathDocument(filename); XmlTextWriter writer = new XmlTextWriter(Console.Out); writer.Formatting=Formatting.Indented; xslt.Transform(xpathdocument, null, writer, null); } } See AlsoXslTransform Class Implements the XSLT Processor | XSLT Processor Implementation of Discretionary Behaviors in the XslTransform Class | XPathNavigator in Transformations | XPathNodeIterator in Transformations | XPathDocument Input to XslTransform | XmlDataDocument Input to XslTransform | XmlDocument Input to XslTransform | XslTransform Class | XslTransform Members |
|