Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

XSLT Transformations with the XslTransform Class 

NoteNote

The XslTransform class is obsolete in the Microsoft .NET Framework version 2.0. You can perform Extensible Stylesheet Language for Transformations (XSLT) transformations using the XslCompiledTransform class. See Using the XslCompiledTransform Class and Migrating From the XslTransform Class for more information.

The goal of the 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 World Wide Web Consortium (W3C) 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 1.0 recommendation, listed in Outputs from an XslTransform. The following figure shows the transformation architecture of the .NET Framework.

Overview

Transformation Architecture
XSLT transformation architecture

The XSLT recommendation uses XML Path Language (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 uses classes when working with XslTransform and XPath and their function.

Class or Interface Function

XPathNavigator

It is an API that provides a cursor style model for navigating over a store, along with XPath query support. It does not provide editing of the underlying store. For editing, use the XmlDocument class.

IXPathNavigable

It is an interface that provides a CreateNavigator method to an XPathNavigator for the store.

XmlDocument

It enables editing of this document. It implements IXPathNavigable, allowing document-editing scenarios where XSLT transformations are subsequently required. For more information, see XmlDocument Input to XslTransform.

XmlDataDocument

It is derived from the XmlDocument. It bridges the relational and XML worlds by using a DataSet to optimize storage of structured data within the XML document according to specified mappings on the DataSet. It implements IXPathNavigable, allowing scenarios where XSLT transformations can be performed over relational data retrieved from a database. For more information, see XML Integration with Relational Data and ADO.NET.

XPathDocument

This class is optimized for XslTransform processing and XPath queries, and it provides a read-only high performance cache. It implements IXPathNavigable and is the preferred store to use for XSLT transformations.

XPathNodeIterator

It provides navigation over XPath node sets. All XPath selection methods on the XPathNavigator return an XPathNodeIterator. Multiple XPathNodeIterator objects can be created over the same store, each representing a selected set of nodes.

MSXML XSLT Extensions

The msxsl:script and msxsl:node-set functions are the only Microsoft XML Core Services (MSXML) XSLT extensions supported by the XslTransform class.

Example

The following code example loads an XSLT 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.

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 Also

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

Show:
© 2014 Microsoft