System.Xml.Xsl.XslTransform Class

Assembly: System.Xml.dll
Namespace: System.Xml.Xsl
Summary
Transforms XML data using an XSLT stylesheet.
C# Syntax:
public sealed class XslTransform
Thread Safety
XslTransform objects are only thread safe for transform operations. Other operations are not guaranteed to be thread safe. You must ensure that no other methods are called on the object during load operations.
Remarks
XslTransform supports the XSLT 1.0 syntax. The XSLT stylesheet must use the namespace http://www.w3.org/1999/XSL/Transform.

Additional arguments can also be added to the stylesheet using the XsltArgumentList class. This class contains input parameters for the stylesheet and extension objects which can be called from the stylesheet.

To transform XML data:

  1. Create an XslTransform object.
  2. Use the XslTransform.Load method to load the stylesheet for the transformation. This method has several overloads and can load a stylesheet using an XmlReader, XPathNavigator, IXPathNavigable, or a URL with the location of the file.
  3. Use the XslTransform.Transform method to transform the XML data. This method has several overloads and can handle different types of input and output. You can also specify an XsltArgumentList containing additional arguments to use as input during the transform.

Scripting Support:

This class supports embedded scripting using the msxsl:script element. When the stylesheet is loaded, all defined functions are compiled and wrapped in a class definition. The msxsl:script element requires that:

The msxsl:script element belongs to the namespace urn:schemas-microsoft-com:xslt. The stylesheet must include the namespace declaration xmlns:msxsl=urn:schemas-microsoft-com:xslt.

The msxsl:script element can include a language attribute that specifies the scripting language to use. The value of the language attribute must be one of the following: C#, CSharp, VB, VisualBasic, JScript, or JavaScript. Because the language name is not case-sensitive, JavaScript and javascript are both valid. If a language attribute is not specified, it defaults to JScript.

The msxsl:script element must include an implements-prefix attribute that contains the prefix representing the namespace associated with the script block. This namespace must be defined within the stylesheet. A stylesheet can include multiple script blocks which are grouped by namespace. You cannot have script blocks with multiple languages within the same namespace. Script blocks can call a function defined in another script block, provided the script blocks reside within the same namespace. The contents of a script block are parsed according to the rules and syntax of the scripting language (supplied by the language attribute). For example, if you had a C# script block comments would be prefixed by the // characters. The comments must be valid XML content.

Note It is recommended that you wrap script blocks in a CDATA section.

          <msxsl:script implements-prefix='xy' language='C#'>
            <![CDATA[
            // Add code here.
            ]]>
          </msxsl:script>
        

Functions can be declared within the msxsl:script element. The following table shows the namespaces that are supported by default. You can use classes outside the listed namespaces, however, these classes must be fully qualified.



Supported Namespaces Description
System System classes.
System.Collection Collection classes.
System.Text Text handling classes.
System.Xml Core XML classes.
System.Xml.Xsl XSLT classes.
System.Xml.XPath XPath classes.

The supplied arguments and return values defined by the script functions must be one of the W3C XPath types. The following table details the mapping between the W3C XPath types and the .NET XPath classes.



W3C XPath Type Equivalent .NET XPath Class
String System.String
Boolean System.Boolean
Number System.Double
Node fragment System.Xml.XPath.XPathNavigator
Node set System.Xml.XPath.XPathNodeIterator

If the script function utilizes one of the following numeric types: Int16, UInt16, Int32, UInt32, Int64, UInt64, Single, or Decimal, these types are coerced to Double, which maps to the W3C XPath type number.

An exception is thrown when a function is called that cannot convert the argument result to one of the required types.

For more information see the conceptual topic at MSDN: xslttransformationswithxsltransform

Example
The following example transforms the specified XML document and outputs the result to the console.
//Create a new XslTransform object.
XslTransform xslt = new XslTransform();

//Load the stylesheet.
xslt.Load("http://server/favorite.xsl");

//Create a new XPathDocument and load the XML data to be transformed.
XPathDocument mydata = new XPathDocument("inputdata.xml");

//Create an XmlTextWriter which outputs to the console.
XmlWriter writer = new XmlTextWriter(Console.Out);

//Transform the data and send the output to the console.
xslt.Transform(mydata,null,writer);

    
See also:
System.Xml.Xsl Namespace

System.Xml.Xsl.XslTransform Member List:

Public Constructors
ctor #1 Default constructor. This constructor is called by derived class constructors to initialize state in this type.
Initializes a new instance of the XslTransform class.
Public Properties
XmlResolver Write-only

Sets the XmlResolver used to resolve external resources when the XslTransform.Transform method is called.
Public Methods
Equals
(inherited from System.Object)
See base class member description: System.Object.Equals

Derived from System.Object, the primary base class for all objects.
GetHashCode
(inherited from System.Object)
See base class member description: System.Object.GetHashCode

Derived from System.Object, the primary base class for all objects.
GetType
(inherited from System.Object)
See base class member description: System.Object.GetType

Derived from System.Object, the primary base class for all objects.
Load Overloaded:
Load(IXPathNavigable stylesheet)

Loads the XSLT stylesheet contained in the IXPathNavigable.
Load Overloaded:
Load(string url)

Loads the XSLT stylesheet specified by a URL.
Load Overloaded:
Load(XmlReader stylesheet)

Loads the XSLT stylesheet contained in the XmlReader.
Load Overloaded:
Load(XPathNavigator stylesheet)

Loads the XSLT stylesheet contained in the XPathNavigator.
Load Overloaded:
Load(IXPathNavigable stylesheet, XmlResolver resolver)

Loads the XSLT stylesheet contained in the IXPathNavigable.
Load Overloaded:
Load(string url, XmlResolver resolver)

Loads the XSLT stylesheet specified by a URL.
Load Overloaded:
Load(XmlReader stylesheet, XmlResolver resolver)

Loads the XSLT stylesheet contained in the XmlReader.
Load Overloaded:
Load(XPathNavigator stylesheet, XmlResolver resolver)

Loads the XSLT stylesheet contained in the XPathNavigator.
ToString
(inherited from System.Object)
See base class member description: System.Object.ToString

Derived from System.Object, the primary base class for all objects.
Transform Overloaded:
Transform(IXPathNavigable input, XsltArgumentList args)

Transforms the XML data in the IXPathNavigable using the specified args and outputs the result to an XmlReader.
Transform Overloaded:
Transform(string inputfile, string outputfile)

Transforms the XML data in the input file and outputs the result to an output file.
Transform Overloaded:
Transform(XPathNavigator input, XsltArgumentList args)

Transforms the XML data in the XPathNavigator using the specified args and outputs the result to an XmlReader.
Transform Overloaded:
Transform(IXPathNavigable input, XsltArgumentList args, Stream output)

Transforms the XML data in the IXPathNavigable using the specified args and outputs the result to a Stream.
Transform Overloaded:
Transform(IXPathNavigable input, XsltArgumentList args, TextWriter output)

Transforms the XML data in the IXPathNavigable using the specified args and outputs the result to a TextWriter.
Transform Overloaded:
Transform(IXPathNavigable input, XsltArgumentList args, XmlWriter output)

Transforms the XML data in the IXPathNavigable using the specified args and outputs the result to an XmlWriter.
Transform Overloaded:
Transform(XPathNavigator input, XsltArgumentList args, Stream output)

Transforms the XML data in the XPathNavigator using the specified args and outputs the result to a Stream.
Transform Overloaded:
Transform(XPathNavigator input, XsltArgumentList args, TextWriter output)

Transforms the XML data in the XPathNavigator using the specified args and outputs the result to a TextWriter.
Transform Overloaded:
Transform(XPathNavigator input, XsltArgumentList args, XmlWriter output)

Transforms the XML data in the XPathNavigator using the specified args and outputs the result to an XmlWriter.
Protected Methods
Finalize
(inherited from System.Object)
See base class member description: System.Object.Finalize

Derived from System.Object, the primary base class for all objects.
MemberwiseClone
(inherited from System.Object)
See base class member description: System.Object.MemberwiseClone

Derived from System.Object, the primary base class for all objects.

Hierarchy:


System.Xml.Xsl.XslTransform Member Details

ctor #1
Summary
Initializes a new instance of the XslTransform class.

Default constructor. This constructor is called by derived class constructors to initialize state in this type.
C# Syntax:
public XslTransform();

Return to top


Property: XmlResolver (write-only)
Summary
Sets the XmlResolver used to resolve external resources when the XslTransform.Transform method is called.
C# Syntax:
XmlResolver XmlResolver {set;}
Remarks
This resolver is used when the XSLT document() function is invoked. It is not used to resolve xsl:import and xsl:include elements referenced in the stylesheet. To specify a custom XmlResolver to use to resolve xsl:import and xsl:include elements referenced in the stylesheet, use the XslTransform.Load overload which takes an XmlResolver as one of its arguments.

By default, XslTransform uses the XmlUrlResolver to resolve resources using the file:// and http:// protocols. However, if you have implemented a custom XmlResolver, you can specify your custom XmlResolver instead.

Return to top


Method: Equals(
   object obj
)
Inherited
See base class member description: System.Object.Equals
C# Syntax:
public virtual bool Equals(
   object obj
);

For more information on members inherited from System.Object click on the link above.

Return to top


Method: Finalize()
Inherited
See base class member description: System.Object.Finalize
C# Syntax:
~XslTransform();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: GetHashCode()
Inherited
See base class member description: System.Object.GetHashCode
C# Syntax:
public virtual int GetHashCode();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: GetType()
Inherited
See base class member description: System.Object.GetType
C# Syntax:
public Type GetType();

For more information on members inherited from System.Object click on the link above.

Return to top


Overloaded Method: Load(
   IXPathNavigable stylesheet
)
Summary
Loads the XSLT stylesheet contained in the IXPathNavigable.
C# Syntax:
public void Load(
   IXPathNavigable stylesheet
);
Parameters:

stylesheet

An object implementing the IXPathNavigable interface. In the .NET Framework this can be either an XmlNode (typically an XmlDocument), or an XPathDocument containing the XSLT stylesheet.

Exceptions
Exception Type Condition
XsltCompileException The loaded resource is not a valid stylesheet.
Remarks
This method loads the XSLT stylesheet including any stylesheets referenced in xsl:include and xsl:import elements. By default XslTransform uses an XmlUrlResolver with default credentials (that is CredentialCache.DefaultCredentials). To specify a custom resolver, or a resolver with a different credential set, use the overload which takes an XmlResolver as one of its arguments.

XslTransform supports the XSLT 1.0 syntax. The XSLT stylesheet must include the namespace declaration xmlns:xsl= http://www.w3.org/1999/XSL/Transform.

Return to top


Overloaded Method: Load(
   string url
)
Summary
Loads the XSLT stylesheet specified by a URL.
C# Syntax:
public void Load(
   string url
);
Parameters:

url

The URL that specifies the XSLT stylesheet to load.

Exceptions
Exception Type Condition
XsltCompileException The loaded resource is not a valid stylesheet.
Remarks
This method loads the XSLT stylesheet including any stylesheets referenced in xsl:include and xsl:import elements. By default XslTransform uses an XmlUrlResolver with default credentials (that is CredentialCache.DefaultCredentials). To specify a custom resolver, or a resolver with a different credential set, use the overload that takes an XmlResolver as one of its arguments.

XslTransform supports the XSLT 1.0 syntax. The XSLT stylesheet must include the namespace declaration xmlns:xsl=http://www.w3.org/1999/XSL/Transform.

Example
The following example transforms an XML document into an HTML document. It displays the ISBN, title, and price for each book in a table.


using System;
using System.IO;
using System.Xml;
using System.Xml.Xsl;
using System.Xml.XPath;

public class Sample
{
  private const String filename = "books.xml";
  private const String stylesheet = "output.xsl";

  public static void Main()
  {
    //Load the stylesheet.
    XslTransform xslt = new XslTransform();
    xslt.Load(stylesheet);

    //Load the file to transform.
    XPathDocument doc = new XPathDocument(filename);
             
    //Create an XmlTextWriter which outputs to the console.
    XmlTextWriter writer = new XmlTextWriter(Console.Out);

    //Transform the file and send the output to the console.
    xslt.Transform(doc, null, writer);
    writer.Close();

  }
}

    

The sample uses the following two input files.

books.xml

<?xml version='1.0'?>
<!-- This file represents a fragment of a book store inventory database -->
<bookstore>
  <book genre="autobiography" publicationdate="1981" ISBN="1-861003-11-0">
    <title>The Autobiography of Benjamin Franklin</title>
    <author>
      <first-name>Benjamin</first-name>
      <last-name>Franklin</last-name>
    </author>
    <price>8.99</price>
  </book>
  <book genre="novel" publicationdate="1967" ISBN="0-201-63361-2">
    <title>The Confidence Man</title>
    <author>
      <first-name>Herman</first-name>
      <last-name>Melville</last-name>
    </author>
    <price>11.99</price>
  </book>
  <book genre="philosophy" publicationdate="1991" ISBN="1-861001-57-6">
    <title>The Gorgias</title>
    <author>
      <name>Plato</name>
    </author>
    <price>9.99</price>
  </book>
</bookstore>

    

output.xsl

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="bookstore">
  <HTML>
    <BODY>
      <TABLE BORDER="2">
        <TR>
          <TD>ISBN</TD>
          <TD>Title</TD>
          <TD>Price</TD>
        </TR>
        <xsl:apply-templates select="book"/>
      </TABLE>
    </BODY>
  </HTML>
</xsl:template>
<xsl:template match="book">
  <TR>
    <TD><xsl:value-of select="@ISBN"/></TD>
    <TD><xsl:value-of select="title"/></TD>
    <TD><xsl:value-of select="price"/></TD>
  </TR>
</xsl:template>
</xsl:stylesheet>

    

Return to top


Overloaded Method: Load(
   XmlReader stylesheet
)
Summary
Loads the XSLT stylesheet contained in the XmlReader.
C# Syntax:
public void Load(
   XmlReader stylesheet
);
Parameters:

stylesheet

An XmlReader object that contains the XSLT stylesheet.

Exceptions
Exception Type Condition
XsltCompileException The current node does not conform to a valid stylesheet.
Remarks
XslTransform supports the XSLT 1.0 syntax. The XSLT stylesheet must include the namespace declaration xmlns:xsl= http://www.w3.org/1999/XSL/Transform.

This method loads the XSLT stylesheet including any stylesheets referenced in xsl:include and xsl:import elements. The stylesheet loads from the current node of the XmlReader through all its children. This enables you to use a portion of a document as the stylesheet. By default XslTransform uses an XmlUrlResolver with default credentials (that is CredentialCache.DefaultCredentials). To specify a custom resolver, or a resolver with a different credential set, use the overload that takes an XmlResolver as one of its arguments.

After the Load method returns, the XmlReader is positioned on the next node after the end of the stylesheet. If the end of the document is reached, the XmlReader is positioned at the end of file (EOF).

If the stylesheet contains entities, you should specify an XmlReader that can resolve entities ( XmlReader.CanResolveEntity returns true). In this case, an XmlValidatingReader can be used.

Example
The following example transforms an XML file sorting all the books by title.
using System;
using System.IO;
using System.Xml;
using System.Xml.Xsl;
using System.Xml.XPath;

public class Sample
{
  private const String filename = "books.xml";
  private const String stylesheet = "titles.xsl";

  public static void Main()
  {
    //Create the reader to load the stylesheet. 
    //Move the reader to the xsl:stylesheet node.
    XmlTextReader reader = new XmlTextReader(stylesheet);
    reader.Read();
    reader.Read();

    //Create the XslTransform object and load the stylesheet.
    XslTransform xslt = new XslTransform();
    xslt.Load(reader);

    //Load the file to transform.
    XPathDocument doc = new XPathDocument(filename);
             
    //Create an XmlTextWriter which outputs to the console.
    XmlTextWriter writer = new XmlTextWriter(Console.Out);

    //Transform the file and send the output to the console.
    xslt.Transform(doc, null, writer);
    writer.Close();  

  }
}

    
The example uses the following data files as input.books.xml
<bookstore>
  <book genre="autobiography" publicationdate="1981" ISBN="1-861003-11-0">
    <title>The Autobiography of Benjamin Franklin</title>
    <author>
      <first-name>Benjamin</first-name>
      <last-name>Franklin</last-name>
    </author>
    <price>8.99</price>
  </book>
  <book genre="novel" publicationdate="1967" ISBN="0-201-63361-2">
    <title>The Confidence Man</title>
    <author>
      <first-name>Herman</first-name>
      <last-name>Melville</last-name>
    </author>
    <price>11.99</price>
  </book>
  <book genre="philosophy" publicationdate="1991" ISBN="1-861001-57-6">
    <title>The Gorgias</title>
    <author>
      <name>Plato</name>
    </author>
    <price>9.99</price>
  </book>
</bookstore>

    
titles.xsl
<!--Stylesheet to sort all books by title-->
<!--Created 2/13/2001-->
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:template match="bookstore">
      <books>
        <xsl:apply-templates select="book">
          <xsl:sort select="title"/>
        </xsl:apply-templates>
       </books>
   </xsl:template>
  <xsl:template match="book">
          <book><xsl:copy-of select="node()"/></book>
  </xsl:template>
</xsl:stylesheet>

    

Return to top


Overloaded Method: Load(
   XPathNavigator stylesheet
)
Summary
Loads the XSLT stylesheet contained in the XPathNavigator.
C# Syntax:
public void Load(
   XPathNavigator stylesheet
);
Parameters:

stylesheet

An XPathNavigator object that contains the XSLT stylesheet.

Exceptions
Exception Type Condition
XsltCompileException The current node does not conform to a valid stylesheet.
Remarks
XslTransform supports the XSLT 1.0 syntax. The XSLT stylesheet must include the namespace declaration xmlns:xsl= http://www.w3.org/1999/XSL/Transform.

This method loads the XSLT stylesheet including any stylesheets referenced in xsl:include and xsl:import elements. By default XslTransform uses an XmlUrlResolver with default credentials (that is CredentialCache.DefaultCredentials). To specify a custom resolver, or a resolver with a different credential set, use the overload which takes an XmlResolver as one of its arguments.

The stylesheet is loaded from the current position of the XPathNavigator. To use just a portion of the loaded document as the stylesheet, navigate to the node corresponding to the beginning of the stylesheet. After the Load method returns, the XPathNavigator is positioned at the beginning of the stylesheet (on the xsl:stylesheet node).

Return to top


Overloaded Method: Load(
   IXPathNavigable stylesheet,
   XmlResolver resolver
)
Summary
Loads the XSLT stylesheet contained in the IXPathNavigable.
C# Syntax:
public void Load(
   IXPathNavigable stylesheet,
   XmlResolver resolver
);
Parameters:

stylesheet

An object implementing the IXPathNavigable interface. In the .NET Framework this can be either an XmlNode (typically an XmlDocument), or an XPathDocument containing the XSLT stylesheet.

resolver

The XmlResolver used to load any stylesheets referenced in xsl:import and xsl:include elements. If this is null, external resources are not resolved.

Exceptions
Exception Type Condition
XsltCompileException The loaded resource is not a valid stylesheet.
Remarks
XslTransform supports the XSLT 1.0 syntax. The XSLT stylesheet must include the namespace declaration xmlns:xsl= http://www.w3.org/1999/XSL/Transform.

The resolver is not cached after the Load method completes.

Return to top


Overloaded Method: Load(
   string url,
   XmlResolver resolver
)
Summary
Loads the XSLT stylesheet specified by a URL.
C# Syntax:
public void Load(
   string url,
   XmlResolver resolver
);
Parameters:

url

The URL that specifies the XSLT stylesheet to load.

resolver

The XmlResolver to use to load any stylesheets referenced in xsl:import and xsl:include elements. If this is null, external resources are not resolved.

Exceptions
Exception Type Condition
XsltCompileException The loaded resource is not a valid stylesheet.
Remarks
XslTransform supports the XSLT 1.0 syntax. The XSLT stylesheet must include the namespace declaration xmlns:xsl= http://www.w3.org/1999/XSL/Transform.

The resolver is not cached after the Load method completes.

Example
The following example transforms an XML document into an HTML document. The example loads an XSLT stylesheet which contains an xsl:include element referencing another stylesheet. An XmlUrlResolver is passed to the Load method which sets the credentials necessary to access the network resource for the included stylesheet.
using System;
using System.IO;
using System.Xml;
using System.Xml.XPath;
using System.Xml.Xsl;
using System.Net;

public class Sample
{
   private const String filename = "books.xml";
   private const String stylesheet = "sort.xsl";

   public static void Main()
   {
      //Create the XslTransform.
     XslTransform xslt = new XslTransform();

     //Create a resolver and set the credentials to use.
     XmlUrlResolver resolver = new XmlUrlResolver();
     NetworkCredential myCred = new NetworkCredential("username","password","domain");
     resolver.Credentials = myCred;
  
     //Load the stylesheet.
     xslt.Load(stylesheet, resolver);

     //Load the XML data file.
     XPathDocument doc = new XPathDocument(filename);

     //Create the XmlTextWriter to output to the console.            
     XmlTextWriter writer = new XmlTextWriter(Console.Out);

     //Transform the file.
     xslt.Transform(doc, null, writer);
     writer.Close();
  }
}

    

The example uses the following data files as input.

books.xml

<!-- This file represents a fragment of a book store inventory database -->
<bookstore>
  <book genre="autobiography" publicationdate="1981" ISBN="1-861003-11-0">
    <title>The Autobiography of Benjamin Franklin</title>
    <author>
      <first-name>Benjamin</first-name>
      <last-name>Franklin</last-name>
    </author>
    <price>8.99</price>
  </book>
  <book genre="novel" publicationdate="1967" ISBN="0-201-63361-2">
    <title>The Confidence Man</title>
    <author>
      <first-name>Herman</first-name>
      <last-name>Melville</last-name>
    </author>
    <price>11.99</price>
  </book>
  <book genre="philosophy" publicationdate="1991" ISBN="1-861001-57-6">
    <title>The Gorgias</title>
    <author>
      <name>Plato</name>
    </author>
    <price>9.99</price>
  </book>
</bookstore>

    

sort.xsl

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:template match="bookstore"/>
  <xsl:include href="http://serverA/includefile.xsl"/>  
  <xsl:template match="book">
     <TR>
      <TD><xsl:value-of select="@ISBN"/></TD>
      <TD><xsl:value-of select="title"/></TD>
      <TD><xsl:value-of select="price"/></TD>
    </TR>
  </xsl:template>
</xsl:stylesheet>

    

includefile.xsl

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="bookstore">
  <HTML>
    <BODY>
    <TABLE BORDER="2">
      <TR>
        <TD>ISBN</TD>
        <TD>Title</TD>
        <TD>Price</TD>
      </TR>
    <xsl:apply-templates select="book">
      <xsl:sort select="@ISBN"/>
    </xsl:apply-templates>
    </TABLE>
    </BODY>
  </HTML>
</xsl:template>
</xsl:stylesheet>

    

Return to top


Overloaded Method: Load(
   XmlReader stylesheet,
   XmlResolver resolver
)
Summary
Loads the XSLT stylesheet contained in the XmlReader.
C# Syntax:
public void Load(
   XmlReader stylesheet,
   XmlResolver resolver
);
Parameters:

stylesheet

An XmlReader object that contains the XSLT stylesheet.

resolver

The XmlResolver used to load any stylesheets referenced in xsl:import and xsl:include elements. If this is null, external resources are not resolved.

Exceptions
Exception Type Condition
XsltCompileException The current node does not conform to a valid stylesheet.
Remarks
XslTransform supports the XSLT 1.0 syntax. The XSLT stylesheet must include the namespace declaration xmlns:xsl= http://www.w3.org/1999/XSL/Transform.

This method loads the XSLT stylesheet including any stylesheets referenced in xsl:include and xsl:import elements. The stylesheet loads from the current node of the XmlReader through all its children. This enables you to use a portion of a document as the stylesheet.

After the Load method returns, the XmlReader is positioned on the next node after the end of the stylesheet. If the end of the document is reached, the XmlReader is positioned at the end of file (EOF).

If the stylesheet contains entities, you should specify an XmlReader that can resolve entities ( XmlReader.CanResolveEntity returns true). In this case, an XmlValidatingReader can be used.

The resolver is not cached after the Load method completes.

Return to top


Overloaded Method: Load(
   XPathNavigator stylesheet,
   XmlResolver resolver
)
Summary
Loads the XSLT stylesheet contained in the XPathNavigator.
C# Syntax:
public void Load(
   XPathNavigator stylesheet,
   XmlResolver resolver
);
Parameters:

stylesheet

An XPathNavigator object that contains the XSLT stylesheet.

resolver

The XmlResolver used to load any stylesheets referenced in xsl:import and xsl:include elements. If this is null, external resources are not resolved.

Exceptions
Exception Type Condition
XsltCompileException The current node does not conform to a valid stylesheet.
Remarks
XslTransform supports the XSLT 1.0 syntax. The XSLT stylesheet must include the namespace declaration xmlns:xsl= http://www.w3.org/1999/XSL/Transform.

The stylesheet is loaded from the current position of the XPathNavigator. To use a portion of the loaded document as the stylesheet, navigate to the node corresponding to the beginning of the stylesheet. After the Load method returns, the XPathNavigator is positioned at the beginning of the stylesheet (on the xsl:stylesheet node).

The resolver is not cached after the Load method completes.

Return to top


Method: MemberwiseClone()
Inherited
See base class member description: System.Object.MemberwiseClone
C# Syntax:
protected object MemberwiseClone();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: ToString()
Inherited
See base class member description: System.Object.ToString
C# Syntax:
public virtual string ToString();

For more information on members inherited from System.Object click on the link above.

Return to top


Overloaded Method: Transform(
   IXPathNavigable input,
   XsltArgumentList args
)
Summary
Transforms the XML data in the IXPathNavigable using the specified args and outputs the result to an XmlReader.
C# Syntax:
public XmlReader Transform(
   IXPathNavigable input,
   XsltArgumentList args
);
Parameters:

input

An object implementing the IXPathNavigable interface. In the .NET Framework this can be either an XmlNode (typically an XmlDocument), or an XPathDocument containing the data to be transformed.

args

An XsltArgumentList containing the namespace-qualified arguments used as input to the transform.

Return Value:
An XmlReader containing the results of the transform.
Remarks
XslTransform supports the XSLT 1.0 syntax. The XSLT stylesheet must include the namespace declaration xmlns:xsl= http://www.w3.org/1999/XSL/Transform.

The args are matched with the xsl:param elements defined in the stylesheet.

This method enables you to do an asynchronous transformation of the source document.

Transformations apply to the document as a whole. In other words, if you pass in a node other than the document root node, this does not prevent the transformation process from accessing all nodes in the loaded document. To transform a node fragment, you must create an XmlDocument containing just the node fragment, and pass that XmlDocument to the Transform method.

The following example performs a transformation on a node fragment.

              XslTransform xslt = new XslTransform();     
              xslt.Load("print_root.xsl");
              XmlDocument doc = new XmlDocument();
              doc.Load("library.xml");
              // Create a new document containing just the node fragment.
              XmlNode testNode = doc.DocumentElement.FirstChild; 
              XmlDocument tmpDoc = new XmlDocument(); 
              tmpDoc.LoadXml(testNode.OuterXml);
              // Pass the document containing the node fragment 
              // to the Transform method.
              Console.WriteLine("Passing " + tmpDoc.OuterXml + " to print_root.xsl");
              xslt.Transform(tmpDoc, null, Console.Out);
            

The example uses the library.xml and print_root.xsl files as input and outputs the following to the console.

              Passing <book genre="novel" ISBN="1-861001-57-5"><title>Pride And Prejudice</title></book> to print_root.xsl 
              Root node is book.
            

library.xml

              <library>
                <book genre='novel' ISBN='1-861001-57-5'>
                   <title>Pride And Prejudice</title>
                </book>
                <book genre='novel' ISBN='1-81920-21-2'>
                   <title>Hook</title>
                </book>
              </library>
            

print_root.xsl

              <stylesheet version="1.0" xmlns="http://www.w3.org/1999/XSL/Transform" >
                <output method="text" /> 
                <template match="/">
                   Root node is  <value-of select="local-name(//*[position() = 1])" /> 
                </template>
              </stylesheet>
            
Example
The following example transforms an XML document and outputs the results to an XmlReader.
//Create the XslTransform object.
XslTransform xslt = new XslTransform();

//Load the stylesheet.
xslt.Load("titles.xsl");

//Transform the file.
XPathDocument doc = new XPathDocument("books.xml");
XmlReader reader = xslt.Transform(doc, null);

    

Return to top


Overloaded Method: Transform(
   string inputfile,
   string outputfile
)
Summary
Transforms the XML data in the input file and outputs the result to an output file.
C# Syntax:
public void Transform(
   string inputfile,
   string outputfile
);
Parameters:

inputfile

The URL of the source document to be transformed.

outputfile

The URL of the output file.

Example
The following example transforms the books.xml file using the output.xsl stylesheet and outputs the results to the books.html file.
//Create the XslTransform object.
XslTransform xslt = new XslTransform();

//Load the stylesheet.
xslt.Load("output.xsl");

//Transform the file.
xslt.Transform("books.xml", "books.html");

    

Return to top


Overloaded Method: Transform(
   XPathNavigator input,
   XsltArgumentList args
)
Summary
Transforms the XML data in the XPathNavigator using the specified args and outputs the result to an XmlReader.
C# Syntax:
public XmlReader Transform(
   XPathNavigator input,
   XsltArgumentList args
);
Parameters:

input

An XPathNavigator containing the data to be transformed.

args

An XsltArgumentList containing the namespace-qualified arguments used as input to the transform.

Return Value:
An XmlReader containing the results of the transform.
Exceptions
Exception Type Condition
XsltException There was an error processing the XSLT transform.
Remarks
XslTransform supports the XSLT 1.0 syntax. The XSLT stylesheet must include the namespace declaration xmlns:xsl= http://www.w3.org/1999/XSL/Transform.

The args are matched with the xsl:param elements defined in the stylesheet. The transformation selections apply to the document as a whole. In other words, if the current node is set on a node tree other than the document root node, this does not prevent the transformation process from accessing all nodes in the loaded document. After the transformation has been performed, the XPathNavigator remains in its original state. This means that the node, which is current before the transformation process, remains the current node after the Transform method has been called.

This method enables you to do an asynchronous transformation of the source document.

Return to top


Overloaded Method: Transform(
   IXPathNavigable input,
   XsltArgumentList args,
   Stream output
)
Summary
Transforms the XML data in the IXPathNavigable using the specified args and outputs the result to a Stream.
C# Syntax:
public void Transform(
   IXPathNavigable input,
   XsltArgumentList args,
   Stream output
);
Parameters:

input

An object implementing the IXPathNavigable interface. In the .NET Framework this can be either an XmlNode (typically an XmlDocument), or an XPathDocument containing the data to be transformed.

args

An XsltArgumentList containing the namespace-qualified arguments used as input to the transform.

output

The stream to which you want to output.

Exceptions
Exception Type Condition
XsltException There was an error processing the XSLT transform.
Remarks
XslTransform supports the XSLT 1.0 syntax. The XSLT stylesheet must include the namespace declaration xmlns:xsl= http://www.w3.org/1999/XSL/Transform.

The args are matched with the xsl:param elements defined in the stylesheet.

Transformations apply to the document as a whole. In other words, if you pass in a node other than the document root node, this does not prevent the transformation process from accessing all nodes in the loaded document. To transform a node fragment, you must create an XmlDocument containing just the node fragment, and pass that XmlDocument to the Transform method.

The following example performs a transformation on a node fragment.

              XslTransform xslt = new XslTransform();     
              xslt.Load("print_root.xsl");
              XmlDocument doc = new XmlDocument();
              doc.Load("library.xml");
              // Create a new document containing just the node fragment.
              XmlNode testNode = doc.DocumentElement.FirstChild; 
              XmlDocument tmpDoc = new XmlDocument(); 
              tmpDoc.LoadXml(testNode.OuterXml);
              // Pass the document containing the node fragment 
              // to the Transform method.
              Console.WriteLine("Passing " + tmpDoc.OuterXml + " to print_root.xsl");
              xslt.Transform(tmpDoc, null, Console.Out);
            

The example uses the library.xml and print_root.xsl files as input and outputs the following to the console.

              Passing <book genre="novel" ISBN="1-861001-57-5"><title>Pride And Prejudice</title></book> to print_root.xsl 
              Root node is book.
            

library.xml

              <library>
                <book genre='novel' ISBN='1-861001-57-5'>
                   <title>Pride And Prejudice</title>
                </book>
                <book genre='novel' ISBN='1-81920-21-2'>
                   <title>Hook</title>
                </book>
              </library>
            

print_root.xsl

              <stylesheet version="1.0" xmlns="http://www.w3.org/1999/XSL/Transform" >
                <output method="text" /> 
                <template match="/">
                   Root node is  <value-of select="local-name(//*[position() = 1])" /> 
                </template>
              </stylesheet>
            
Example
The following example performs a transformation within an ASP.NET page.

Return to top


Overloaded Method: Transform(
   IXPathNavigable input,
   XsltArgumentList args,
   TextWriter output
)
Summary
Transforms the XML data in the IXPathNavigable using the specified args and outputs the result to a TextWriter.
C# Syntax:
public void Transform(
   IXPathNavigable input,
   XsltArgumentList args,
   TextWriter output
);
Parameters:

input

An object implementing the IXPathNavigable interface. In the .NET Framework this can be either an XmlNode (typically an XmlDocument), or an XPathDocument containing the data to be transformed.

args

An XsltArgumentList containing the namespace-qualified arguments used as input to the transform.

output

The TextWriter to which you want to output.

Exceptions
Exception Type Condition
XsltException There was an error processing the XSLT transform.
Remarks
XslTransform supports the XSLT 1.0 syntax. The XSLT stylesheet must include the namespace declaration xmlns:xsl= http://www.w3.org/1999/XSL/Transform.

The args are matched with the xsl:param elements defined in the stylesheet.

Transformations apply to the document as a whole. In other words, if you pass in a node other than the document root node, this does not prevent the transformation process from accessing all nodes in the loaded document. To transform a node fragment, you must create an XmlDocument containing just the node fragment, and pass that XmlDocument to the Transform method.

The following example performs a transformation on a node fragment.

              XslTransform xslt = new XslTransform();     
              xslt.Load("print_root.xsl");
              XmlDocument doc = new XmlDocument();
              doc.Load("library.xml");
              // Create a new document containing just the node fragment.
              XmlNode testNode = doc.DocumentElement.FirstChild; 
              XmlDocument tmpDoc = new XmlDocument(); 
              tmpDoc.LoadXml(testNode.OuterXml);
              // Pass the document containing the node fragment 
              // to the Transform method.
              Console.WriteLine("Passing " + tmpDoc.OuterXml + " to print_root.xsl");
              xslt.Transform(tmpDoc, null, Console.Out);
            

The example uses the library.xml and print_root.xsl files as input and outputs the following to the console.

              Passing <book genre="novel" ISBN="1-861001-57-5"><title>Pride And Prejudice</title></book> to print_root.xsl 
              Root node is book.
            

library.xml

              <library>
                <book genre='novel' ISBN='1-861001-57-5'>
                   <title>Pride And Prejudice</title>
                </book>
                <book genre='novel' ISBN='1-81920-21-2'>
                   <title>Hook</title>
                </book>
              </library>
            

print_root.xsl

              <stylesheet version="1.0" xmlns="http://www.w3.org/1999/XSL/Transform" >
                <output method="text" /> 
                <template match="/">
                   Root node is  <value-of select="local-name(//*[position() = 1])" /> 
                </template>
              </stylesheet>
            

Return to top


Overloaded Method: Transform(
   IXPathNavigable input,
   XsltArgumentList args,
   XmlWriter output
)
Summary
Transforms the XML data in the IXPathNavigable using the specified args and outputs the result to an XmlWriter.
C# Syntax:
public void Transform(
   IXPathNavigable input,
   XsltArgumentList args,
   XmlWriter output
);
Parameters:

input

An object implementing the IXPathNavigable interface. In the .NET Framework this can be either an XmlNode (typically an XmlDocument), or an XPathDocument containing the data to be transformed.

args

An XsltArgumentList containing the namespace-qualified arguments used as input to the transform.

output

The XmlWriter to which you want to output.

Exceptions
Exception Type Condition
XsltException There was an error processing the XSLT transform.
Remarks
XslTransform supports the XSLT 1.0 syntax. The XSLT stylesheet must include the namespace declaration xmlns:xsl= http://www.w3.org/1999/XSL/Transform.

The args are matched with the xsl:param elements defined in the stylesheet.

Transformations apply to the document as a whole. In other words, if you pass in a node other than the document root node, this does not prevent the transformation process from accessing all nodes in the loaded document. To transform a node fragment, you must create an XmlDocument containing just the node fragment, and pass that XmlDocument to the Transform method.

The following example performs a transformation on a node fragment.

              XslTransform xslt = new XslTransform();     
              xslt.Load("print_root.xsl");
              XmlDocument doc = new XmlDocument();
              doc.Load("library.xml");
              // Create a new document containing just the node fragment.
              XmlNode testNode = doc.DocumentElement.FirstChild; 
              XmlDocument tmpDoc = new XmlDocument(); 
              tmpDoc.LoadXml(testNode.OuterXml);
              // Pass the document containing the node fragment 
              // to the Transform method.
              Console.WriteLine("Passing " + tmpDoc.OuterXml + " to print_root.xsl");
              xslt.Transform(tmpDoc, null, Console.Out);
            

The example uses the library.xml and print_root.xsl files as input and outputs the following to the console.

              Passing <book genre="novel" ISBN="1-861001-57-5"><title>Pride And Prejudice</title></book> to print_root.xsl 
              Root node is book.
            

library.xml

              <library>
                <book genre='novel' ISBN='1-861001-57-5'>
                   <title>Pride And Prejudice</title>
                </book>
                <book genre='novel' ISBN='1-81920-21-2'>
                   <title>Hook</title>
                </book>
              </library>
            

print_root.xsl

              <stylesheet version="1.0" xmlns="http://www.w3.org/1999/XSL/Transform" >
                <output method="text" />
                <template match="/">
                Root node is  <value-of select="local-name(//*[position() = 1])" /> 
                </template>
              </stylesheet>
            
Example
The following example transforms an XML document into an HTML document. It displays the ISBN, title, and price for each book in a table.


using System;
using System.IO;
using System.Xml;
using System.Xml.Xsl;
using System.Xml.XPath;

public class Sample
{
  private const String filename = "books.xml";
  private const String stylesheet = "output.xsl";

  public static void Main()
  {
    //Load the stylesheet.
    XslTransform xslt = new XslTransform();
    xslt.Load(stylesheet);

    //Load the file to transform.
    XPathDocument doc = new XPathDocument(filename);
             
    //Create an XmlTextWriter which outputs to the console.
    XmlTextWriter writer = new XmlTextWriter(Console.Out);

    //Transform the file and send the output to the console.
    xslt.Transform(doc, null, writer);
    writer.Close();

  }
}

    

The example uses the following files as input.

books.xml

<?xml version='1.0'?>
<!-- This file represents a fragment of a book store inventory database -->
<bookstore>
  <book genre="autobiography" publicationdate="1981" ISBN="1-861003-11-0">
    <title>The Autobiography of Benjamin Franklin</title>
    <author>
      <first-name>Benjamin</first-name>
      <last-name>Franklin</last-name>
    </author>
    <price>8.99</price>
  </book>
  <book genre="novel" publicationdate="1967" ISBN="0-201-63361-2">
    <title>The Confidence Man</title>
    <author>
      <first-name>Herman</first-name>
      <last-name>Melville</last-name>
    </author>
    <price>11.99</price>
  </book>
  <book genre="philosophy" publicationdate="1991" ISBN="1-861001-57-6">
    <title>The Gorgias</title>
    <author>
      <name>Plato</name>
    </author>
    <price>9.99</price>
  </book>
</bookstore>

    

output.xsl

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="bookstore">
  <HTML>
    <BODY>
      <TABLE BORDER="2">
        <TR>
          <TD>ISBN</TD>
          <TD>Title</TD>
          <TD>Price</TD>
        </TR>
        <xsl:apply-templates select="book"/>
      </TABLE>
    </BODY>
  </HTML>
</xsl:template>
<xsl:template match="book">
  <TR>
    <TD><xsl:value-of select="@ISBN"/></TD>
    <TD><xsl:value-of select="title"/></TD>
    <TD><xsl:value-of select="price"/></TD>
  </TR>
</xsl:template>
</xsl:stylesheet>

    

Return to top


Overloaded Method: Transform(
   XPathNavigator input,
   XsltArgumentList args,
   Stream output
)
Summary
Transforms the XML data in the XPathNavigator using the specified args and outputs the result to a Stream.
C# Syntax:
public void Transform(
   XPathNavigator input,
   XsltArgumentList args,
   Stream output
);
Parameters:

input

An XPathNavigator containing the data to be transformed.

args

An XsltArgumentList containing the namespace-qualified arguments used as input to the transform.

output

The stream to which you want to output.

Exceptions
Exception Type Condition
XsltException There was an error processing the XSLT transform.
Remarks
XslTransform supports the XSLT 1.0 syntax. The XSLT stylesheet must include the namespace declaration xmlns:xsl= http://www.w3.org/1999/XSL/Transform.

The args are matched with the xsl:param elements defined in the stylesheet. The transformation selections apply to the document as a whole. In other words, if the current node is set on a node tree other than the document root node, this does not prevent the transformation process from accessing all nodes in the loaded document. After the transformation has been performed, the XPathNavigator remains in its original state. This means that the node, which is current before the transformation process, remains the current node after the Transform method has been called.

Example
The following example loads a customer table into an XmlDataDocument and performs an XSLT transformation to pull the customer data into an HTML table.
using System;
using System.IO;
using System.Data;
using System.Data.SqlClient;
using System.Xml;
using System.Xml.XPath;
using System.Xml.Xsl;


public class Sample
{
  public static void Main()
  {

     // Create a DataSet and load it with customer data.
     DataSet dsNorthwind = new DataSet();        
     String sConnect;
     sConnect="Data Source=localhost;Integrated Security=SSPI;Initial Catalog=Northwind";  
     SqlConnection nwconnect = new SqlConnection(sConnect);
     String sCommand = "Select * from Customers where Region='WA'";
     SqlDataAdapter myDataAdapter = new SqlDataAdapter(sCommand, nwconnect);
     myDataAdapter.Fill(dsNorthwind,"Customers"); 

     // Load the DataSet into an XmlDataDocument.
     XmlDataDocument doc = new XmlDataDocument(dsNorthwind);   

     // Create the XslTransform object and load the stylesheet.
     XslTransform xsl = new XslTransform();     
     xsl.Load("customers.xsl");

     // Create an XPathNavigator to use in the transform.
     XPathNavigator nav = doc.CreateNavigator();

     // Create a FileStream object.
     FileStream fs = new FileStream("cust.html", FileMode.Create);
 
     // Transform the data.
     xsl.Transform(nav, null, fs);
	
  }
}

    
The example uses the file, customers.xsl, as input.
<!-- Stylesheet to sort customers by city-->
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

  <xsl:template match="NewDataSet">
    <HTML>
      <BODY>
        <TABLE BORDER="2">
          <TR>
            <TD>Company Name</TD>
            <TD>Contact Name</TD>
            <TD>City</TD>
          </TR>
         <xsl:apply-templates select="Customers">
            <xsl:sort select="City"/>
         </xsl:apply-templates>
        </TABLE>
      </BODY>
    </HTML>
  </xsl:template>

  <xsl:template match="Customers">
    <TR>
      <TD><xsl:value-of select="CompanyName"/></TD>
      <TD><xsl:value-of select="ContactName"/></TD>
      <TD><xsl:value-of select="City"/></TD>
    </TR>
  </xsl:template>

</xsl:stylesheet>

    
See also:
XsltArgumentList

Return to top


Overloaded Method: Transform(
   XPathNavigator input,
   XsltArgumentList args,
   TextWriter output
)
Summary
Transforms the XML data in the XPathNavigator using the specified args and outputs the result to a TextWriter.
C# Syntax:
public void Transform(
   XPathNavigator input,
   XsltArgumentList args,
   TextWriter output
);
Parameters:

input

An XPathNavigator containing the data to be transformed.

args

An XsltArgumentList containing the namespace-qualified arguments used as input to the transform.

output

The TextWriter to which you want to output.

Exceptions
Exception Type Condition
XsltException There was an error processing the XSLT transform.
Remarks
XslTransform supports the XSLT 1.0 syntax. The XSLT stylesheet must include the namespace declaration xmlns:xsl= http://www.w3.org/1999/XSL/Transform.

The args are matched with the xsl:param elements defined in the stylesheet. The transformation selections apply to the document as a whole. In other words, if the current node is set on a node tree other than the document root node, this does not prevent the transformation process from accessing all nodes in the loaded document. After the transformation is performed, the XPathNavigator remains in its original state. This means that the node, which is current before the transformation process, remains the current node after the Transform method has been called.

See also:
XsltArgumentList

Return to top


Overloaded Method: Transform(
   XPathNavigator input,
   XsltArgumentList args,
   XmlWriter output
)
Summary
Transforms the XML data in the XPathNavigator using the specified args and outputs the result to an XmlWriter.
C# Syntax:
public void Transform(
   XPathNavigator input,
   XsltArgumentList args,
   XmlWriter output
);
Parameters:

input

An XPathNavigator containing the data to be transformed.

args

An XsltArgumentList containing the namespace-qualified arguments used as input to the transform.

output

The XmlWriter to which you want to output.

Exceptions
Exception Type Condition
XsltException There was an error processing the XSLT transform.
Remarks
XslTransform supports the XSLT 1.0 syntax. The XSLT stylesheet must include the namespace declaration xmlns:xsl= http://www.w3.org/1999/XSL/Transform.

The args are matched with the xsl:param elements defined in the stylesheet. The transformation selections apply to the document as a whole. In other words, if the current node is set on a node tree other than the document root node, this does not prevent the transformation process from accessing all nodes in the loaded document. After the transformation is performed, the XPathNavigator remains in its original state. This means that the node, that is current before the transformation process, remains the current node after the Transform method has been called.

Example
The following example loads and edits an XML document before performing an XSLT transform.
  XmlDocument doc = new XmlDocument();
  doc.Load("books.xml");

  // Modify the XML file.
  XmlElement root = doc.DocumentElement;
  root.FirstChild.LastChild.InnerText = "12.95";

  // Create an XPathNavigator to use for the transform.
  XPathNavigator nav = root.CreateNavigator();
  
  // Transform the file.
  XslTransform xslt = new XslTransform();
  xslt.Load("output.xsl");
  XmlTextWriter writer = new XmlTextWriter("books.html", null);
  xslt.Transform(nav, null, writer);

    
See also:
XsltArgumentList

Return to top


Top of page

Copyright (c) 2002 Microsoft Corporation. All rights reserved.