Transform Method (XPathNavigator, XsltArgumentList, Stream, XmlResolver)

XslTransform.Transform Method (XPathNavigator, XsltArgumentList, Stream, XmlResolver)

 

Transforms the XML data in the XPathNavigator using the specified args and outputs the result to a Stream.

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

public void Transform(
	XPathNavigator input,
	XsltArgumentList args,
	Stream output,
	XmlResolver resolver
)

Parameters

input
Type: System.Xml.XPath.XPathNavigator

An XPathNavigator 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.Stream

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

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

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

See Outputs from an XslTransform for specifics on which xsl:output attributes are supported.

The following example loads a customer table into an XmlDataDocument and performs an XSLT transformation to pull the customer data into an HTML table. The example uses the Microsoft SQL Server 2000 Northwind database.

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

  }
}

The example uses the customers.xsl file 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>

.NET Framework
Available since 1.1
Return to top
Show:
© 2016 Microsoft