Export (0) Print
Expand All

XslTransform.Transform Method (IXPathNavigable, XsltArgumentList, TextWriter, XmlResolver)

Transforms the XML data in the IXPathNavigable using the specified args and outputs the result to a TextWriter.

Namespace:  System.Xml.Xsl
Assembly:  System.Xml (in System.Xml.dll)

public void Transform(
	IXPathNavigable input,
	XsltArgumentList args,
	TextWriter output,
	XmlResolver resolver
)

Parameters

input
Type: System.Xml.XPath.IXPathNavigable

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
Type: System.Xml.Xsl.XsltArgumentList

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

output
Type: System.IO.TextWriter

The TextWriter to which you want to output.

resolver
Type: System.Xml.XmlResolver

The XmlResolver used to resolve the XSLT document() function. If this is null, the document() function is not resolved.

The XmlResolver is not cached after the Transform method completes.

ExceptionCondition
InvalidOperationException

There was an error processing the XSLT transformation.

Note: This is a change in behavior from earlier versions. An XsltException is thrown if you are using Microsoft .NET Framework version 1.1 or earlier.

NoteNote

The XslTransform class is obsolete in the .NET Framework version 2.0. The XslCompiledTransform class is the new XSLT processor. For more information, see Using the XslCompiledTransform Class and Migrating From the XslTransform Class.

XslTransform supports the XSLT 1.0 syntax. The XSLT style sheet 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 style sheet.

The encoding attribute on an xsl:output element is not supported when outputting to a TextWriter. See Outputs from an XslTransform for specifics on which xsl:output attributes are supported.

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, null);

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

 <style sheet 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>
 </style sheet>

The following example transforms an XML file and outputs an HTML string. The StringReader and StringWriter classes are used to read and write the strings.

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

public class Sample {

  public static void Main() {

    // Create a string containing the XSLT stylesheet.
    String xsltString =
    @"<xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform' version='1.0'>
        <xsl:template match='bookstore'>
           <HTML>
              <HEAD>
                 <TITLE>Book Titles</TITLE>
             </HEAD>
             <BODY>
                <xsl:apply-templates select='book'/>
             </BODY>
          </HTML>
        </xsl:template>
        <xsl:template match='book'>
          <P><xsl:value-of select='title'/></P>
        </xsl:template>
        </xsl:stylesheet>";

    // Create the XslTransform object.
    XslTransform xslt = new XslTransform();

    // Load the stylesheet.
    StringReader rdr = new StringReader(xsltString);
    xslt.Load(new XPathDocument(rdr), null, null);

    // Transform the file and output an HTML string. 
    string HTMLoutput;
    StringWriter writer = new StringWriter();
    xslt.Transform(new XPathDocument("books.xml"), null, writer, null);
    HTMLoutput = writer.ToString();

  }
}

The example uses the books.xml file as input.

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

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0, 1.1

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft