Olvasás angol nyelven Szerkesztés

Megosztás a következőn keresztül:


XslTransform.Transform Method

Definition

Transforms the XML data using the loaded XSLT style sheet.

Overloads

Transform(XPathNavigator, XsltArgumentList, XmlResolver)

Transforms the XML data in the XPathNavigator using the specified args and outputs the result to an XmlReader.

Transform(XPathNavigator, XsltArgumentList, TextWriter, XmlResolver)

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

Transform(XPathNavigator, XsltArgumentList, Stream, XmlResolver)

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

Transform(IXPathNavigable, XsltArgumentList, XmlWriter, XmlResolver)

Transforms the XML data in the IXPathNavigable using the specified args and outputs the result to an XmlWriter.

Transform(IXPathNavigable, XsltArgumentList, TextWriter, XmlResolver)

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

Transform(IXPathNavigable, XsltArgumentList, Stream, XmlResolver)

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

Transform(XPathNavigator, XsltArgumentList, XmlWriter)
Obsolete.

Transforms the XML data in the XPathNavigator using the specified args and outputs the result to an XmlWriter.

Transform(XPathNavigator, XsltArgumentList, TextWriter)
Obsolete.

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

Transform(String, String, XmlResolver)

Transforms the XML data in the input file and outputs the result to an output file.

Transform(IXPathNavigable, XsltArgumentList, XmlWriter)
Obsolete.

Transforms the XML data in the IXPathNavigable using the specified args and outputs the result to an XmlWriter.

Transform(IXPathNavigable, XsltArgumentList, XmlResolver)

Transforms the XML data in the IXPathNavigable using the specified args and outputs the result to an XmlReader.

Transform(IXPathNavigable, XsltArgumentList, TextWriter)
Obsolete.

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

Transform(IXPathNavigable, XsltArgumentList, Stream)
Obsolete.

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

Transform(XPathNavigator, XsltArgumentList, XmlWriter, XmlResolver)

Transforms the XML data in the XPathNavigator using the specified args and outputs the result to an XmlWriter.

Transform(XPathNavigator, XsltArgumentList)
Obsolete.

Transforms the XML data in the XPathNavigator using the specified args and outputs the result to an XmlReader.

Transform(IXPathNavigable, XsltArgumentList)
Obsolete.

Transforms the XML data in the IXPathNavigable using the specified args and outputs the result to an XmlReader.

Transform(String, String)
Obsolete.

Transforms the XML data in the input file and outputs the result to an output file.

Transform(XPathNavigator, XsltArgumentList, Stream)
Obsolete.

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

Remarks

Megjegyzés

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.

Transform(XPathNavigator, XsltArgumentList, XmlResolver)

Source:
XslTransform.cs
Source:
XslTransform.cs
Source:
XslTransform.cs

Transforms the XML data in the XPathNavigator using the specified args and outputs the result to an XmlReader.

C#
public System.Xml.XmlReader Transform (System.Xml.XPath.XPathNavigator input, System.Xml.Xsl.XsltArgumentList? args, System.Xml.XmlResolver? resolver);
C#
public System.Xml.XmlReader Transform (System.Xml.XPath.XPathNavigator input, System.Xml.Xsl.XsltArgumentList args, System.Xml.XmlResolver resolver);

Parameters

input
XPathNavigator

An XPathNavigator containing the data to be transformed.

args
XsltArgumentList

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

resolver
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(XPathNavigator, XsltArgumentList, XmlResolver) method completes.

Returns

An XmlReader containing the results of the transformation.

Exceptions

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.

Remarks

Megjegyzés

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.

Because XmlReader provides read-only output, the xsl:output element is ignored. See Outputs from an XslTransform for more information.

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

See also

Applies to

.NET 9 és más verziók
Termék Verziók
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

Transform(XPathNavigator, XsltArgumentList, TextWriter, XmlResolver)

Source:
XslTransform.cs
Source:
XslTransform.cs
Source:
XslTransform.cs

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

C#
public void Transform (System.Xml.XPath.XPathNavigator input, System.Xml.Xsl.XsltArgumentList? args, System.IO.TextWriter output, System.Xml.XmlResolver? resolver);
C#
public void Transform (System.Xml.XPath.XPathNavigator input, System.Xml.Xsl.XsltArgumentList args, System.IO.TextWriter output, System.Xml.XmlResolver resolver);

Parameters

input
XPathNavigator

An XPathNavigator containing the data to be transformed.

args
XsltArgumentList

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

output
TextWriter

The TextWriter to which you want to output.

resolver
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(XPathNavigator, XsltArgumentList, TextWriter, XmlResolver) method completes.

Exceptions

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.

Remarks

Megjegyzés

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

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.

See also

Applies to

.NET 9 és más verziók
Termék Verziók
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

Transform(XPathNavigator, XsltArgumentList, Stream, XmlResolver)

Source:
XslTransform.cs
Source:
XslTransform.cs
Source:
XslTransform.cs

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

C#
public void Transform (System.Xml.XPath.XPathNavigator input, System.Xml.Xsl.XsltArgumentList? args, System.IO.Stream output, System.Xml.XmlResolver? resolver);
C#
public void Transform (System.Xml.XPath.XPathNavigator input, System.Xml.Xsl.XsltArgumentList args, System.IO.Stream output, System.Xml.XmlResolver resolver);

Parameters

input
XPathNavigator

An XPathNavigator containing the data to be transformed.

args
XsltArgumentList

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

output
Stream

The stream to which you want to output.

resolver
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(XPathNavigator, XsltArgumentList, Stream, XmlResolver) method completes.

Exceptions

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.

Examples

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.

C#
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.

XML
<!-- 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>

Remarks

Megjegyzés

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.

See also

Applies to

.NET 9 és más verziók
Termék Verziók
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

Transform(IXPathNavigable, XsltArgumentList, XmlWriter, XmlResolver)

Source:
XslTransform.cs
Source:
XslTransform.cs
Source:
XslTransform.cs

Transforms the XML data in the IXPathNavigable using the specified args and outputs the result to an XmlWriter.

C#
public void Transform (System.Xml.XPath.IXPathNavigable input, System.Xml.Xsl.XsltArgumentList? args, System.Xml.XmlWriter output, System.Xml.XmlResolver? resolver);
C#
public void Transform (System.Xml.XPath.IXPathNavigable input, System.Xml.Xsl.XsltArgumentList args, System.Xml.XmlWriter output, System.Xml.XmlResolver resolver);

Parameters

input
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
XsltArgumentList

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

output
XmlWriter

The XmlWriter to which you want to output.

resolver
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(IXPathNavigable, XsltArgumentList, XmlWriter, XmlResolver) method completes.

Exceptions

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.

Examples

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

C#
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, null);
    writer.Close();
  }
}

The sample uses the following two input files.

books.xml

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

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

Remarks

Megjegyzés

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 xsl:output element is not supported when outputting to an XmlWriter (xsl:output is ignored). See Outputs from an XslTransform for more information.

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.

C#
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

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

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

See also

Applies to

.NET 9 és más verziók
Termék Verziók
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

Transform(IXPathNavigable, XsltArgumentList, TextWriter, XmlResolver)

Source:
XslTransform.cs
Source:
XslTransform.cs
Source:
XslTransform.cs

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

C#
public void Transform (System.Xml.XPath.IXPathNavigable input, System.Xml.Xsl.XsltArgumentList? args, System.IO.TextWriter output, System.Xml.XmlResolver? resolver);
C#
public void Transform (System.Xml.XPath.IXPathNavigable input, System.Xml.Xsl.XsltArgumentList args, System.IO.TextWriter output, System.Xml.XmlResolver resolver);

Parameters

input
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
XsltArgumentList

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

output
TextWriter

The TextWriter to which you want to output.

resolver
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(IXPathNavigable, XsltArgumentList, TextWriter, XmlResolver) method completes.

Exceptions

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.

Examples

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.

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

Remarks

Megjegyzés

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.

C#
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

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

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

See also

Applies to

.NET 9 és más verziók
Termék Verziók
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

Transform(IXPathNavigable, XsltArgumentList, Stream, XmlResolver)

Source:
XslTransform.cs
Source:
XslTransform.cs
Source:
XslTransform.cs

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

C#
public void Transform (System.Xml.XPath.IXPathNavigable input, System.Xml.Xsl.XsltArgumentList? args, System.IO.Stream output, System.Xml.XmlResolver? resolver);
C#
public void Transform (System.Xml.XPath.IXPathNavigable input, System.Xml.Xsl.XsltArgumentList args, System.IO.Stream output, System.Xml.XmlResolver resolver);

Parameters

input
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
XsltArgumentList

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

output
Stream

The stream to which you want to output.

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

Exceptions

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.

Examples

The following example performs a transformation within an ASP.NET page.

ASP.NET (C#)
<%@ Import Namespace="System" %>
<%@ Import Namespace="System.IO" %>
<%@ import NameSpace="System.Web" %>
<%@ Import Namespace="System.Xml.XPath" %>
<%@ Import Namespace="System.Xml.Xsl" %>
<html>
   <script language="C#" runat="server">
      void Page_Load(Object sender, EventArgs e) 
      {
         // Load the XML document to transform.
         XPathDocument doc = new XPathDocument(Server.MapPath("books.xml"));

         // Load the stylesheet and perform the transform.
         XslTransform xslt = new XslTransform();
         xslt.Load(Server.MapPath("output.xsl"));
         xslt.Transform(doc, null, Response.OutputStream, null);
        
      }
   </script>
</html>

Remarks

Megjegyzés

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.

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.

C#
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

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

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

See also

Applies to

.NET 9 és más verziók
Termék Verziók
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

Transform(XPathNavigator, XsltArgumentList, XmlWriter)

Source:
XslTransform.cs
Source:
XslTransform.cs
Source:
XslTransform.cs

Caution

You should pass XmlResolver to Transform() method

Transforms the XML data in the XPathNavigator using the specified args and outputs the result to an XmlWriter.

C#
public void Transform (System.Xml.XPath.XPathNavigator input, System.Xml.Xsl.XsltArgumentList? args, System.Xml.XmlWriter output);
C#
public void Transform (System.Xml.XPath.XPathNavigator input, System.Xml.Xsl.XsltArgumentList args, System.Xml.XmlWriter output);
C#
[System.Obsolete("You should pass XmlResolver to Transform() method")]
public void Transform (System.Xml.XPath.XPathNavigator input, System.Xml.Xsl.XsltArgumentList args, System.Xml.XmlWriter output);

Parameters

input
XPathNavigator

An XPathNavigator containing the data to be transformed.

args
XsltArgumentList

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

output
XmlWriter

The XmlWriter to which you want to output.

Attributes

Exceptions

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.

Remarks

Megjegyzés

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

The xsl:output element is not supported when outputting to an XmlWriter (xsl:output is ignored). See Outputs from an XslTransform for more information.

Megjegyzés

This method is now obsolete. The setting of the XmlResolver property determines how the XSLT document() function is resolved. The recommended practice is to use the Transform method which takes an XmlResolver object as one of its arguments.

See also

Applies to

.NET 9 és más verziók
Termék Verziók (Elavult)
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 (1.1)
.NET Standard 2.0, 2.1

Transform(XPathNavigator, XsltArgumentList, TextWriter)

Source:
XslTransform.cs
Source:
XslTransform.cs
Source:
XslTransform.cs

Caution

You should pass XmlResolver to Transform() method

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

C#
public void Transform (System.Xml.XPath.XPathNavigator input, System.Xml.Xsl.XsltArgumentList? args, System.IO.TextWriter output);
C#
public void Transform (System.Xml.XPath.XPathNavigator input, System.Xml.Xsl.XsltArgumentList args, System.IO.TextWriter output);
C#
[System.Obsolete("You should pass XmlResolver to Transform() method")]
public void Transform (System.Xml.XPath.XPathNavigator input, System.Xml.Xsl.XsltArgumentList args, System.IO.TextWriter output);

Parameters

input
XPathNavigator

An XPathNavigator containing the data to be transformed.

args
XsltArgumentList

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

output
TextWriter

The TextWriter to which you want to output.

Attributes

Exceptions

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.

Remarks

Megjegyzés

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

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.

Megjegyzés

This method is now obsolete. The setting of the XmlResolver property determines how the XSLT document() function is resolved. The recommended practice is to use the Transform method which takes an XmlResolver object as one of its arguments.

See also

Applies to

.NET 9 és más verziók
Termék Verziók (Elavult)
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 (1.1)
.NET Standard 2.0, 2.1

Transform(String, String, XmlResolver)

Source:
XslTransform.cs
Source:
XslTransform.cs
Source:
XslTransform.cs

Transforms the XML data in the input file and outputs the result to an output file.

C#
public void Transform (string inputfile, string outputfile, System.Xml.XmlResolver? resolver);
C#
public void Transform (string inputfile, string outputfile, System.Xml.XmlResolver resolver);

Parameters

inputfile
String

The URL of the source document to be transformed.

outputfile
String

The URL of the output file.

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

Examples

The following example performs an XSLT transformation and outputs to a file. An XmlUrlResolver with default credentials is used resolve an external resources. See DefaultCredentials for details.

C#

// Create a resolver with default credentials.
XmlUrlResolver resolver = new XmlUrlResolver();
resolver.Credentials = System.Net.CredentialCache.DefaultCredentials;

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

// Load the stylesheet.
xslt.Load("http://myServer/data/authors.xsl", resolver);

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

Remarks

Megjegyzés

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.

See also

Applies to

.NET 9 és más verziók
Termék Verziók
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

Transform(IXPathNavigable, XsltArgumentList, XmlWriter)

Source:
XslTransform.cs
Source:
XslTransform.cs
Source:
XslTransform.cs

Caution

You should pass XmlResolver to Transform() method

Transforms the XML data in the IXPathNavigable using the specified args and outputs the result to an XmlWriter.

C#
public void Transform (System.Xml.XPath.IXPathNavigable input, System.Xml.Xsl.XsltArgumentList? args, System.Xml.XmlWriter output);
C#
public void Transform (System.Xml.XPath.IXPathNavigable input, System.Xml.Xsl.XsltArgumentList args, System.Xml.XmlWriter output);
C#
[System.Obsolete("You should pass XmlResolver to Transform() method")]
public void Transform (System.Xml.XPath.IXPathNavigable input, System.Xml.Xsl.XsltArgumentList args, System.Xml.XmlWriter output);

Parameters

input
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
XsltArgumentList

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

output
XmlWriter

The XmlWriter to which you want to output.

Attributes

Exceptions

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.

Remarks

Megjegyzés

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.

Megjegyzés

This method is now obsolete. The setting of the XmlResolver property determines how the XSLT document() function is resolved. The recommended practice is to use the Transform method which takes an XmlResolver object as one of its arguments.

The args are matched with the xsl:param elements defined in the style sheet. The xsl:output element is not supported when outputting to an XmlWriter (xsl:output is ignored). See Outputs from an XslTransform for more information.

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.

C#
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

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

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

See also

Applies to

.NET 9 és más verziók
Termék Verziók (Elavult)
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 (1.1)
.NET Standard 2.0, 2.1

Transform(IXPathNavigable, XsltArgumentList, XmlResolver)

Source:
XslTransform.cs
Source:
XslTransform.cs
Source:
XslTransform.cs

Transforms the XML data in the IXPathNavigable using the specified args and outputs the result to an XmlReader.

C#
public System.Xml.XmlReader Transform (System.Xml.XPath.IXPathNavigable input, System.Xml.Xsl.XsltArgumentList? args, System.Xml.XmlResolver? resolver);
C#
public System.Xml.XmlReader Transform (System.Xml.XPath.IXPathNavigable input, System.Xml.Xsl.XsltArgumentList args, System.Xml.XmlResolver resolver);

Parameters

input
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
XsltArgumentList

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

resolver
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(IXPathNavigable, XsltArgumentList, XmlResolver) method completes.

Returns

An XmlReader containing the results of the transformation.

Examples

The following example transforms an XML document and outputs the results to an XmlReader. An XmlUrlResolver with the necessary credentials is used to process any XSLT document() functions.

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

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

// Create a resolver and specify the necessary credentials.
XmlUrlResolver resolver = new XmlUrlResolver();
System.Net.NetworkCredential myCred;
myCred  = new System.Net.NetworkCredential(UserName,SecurelyStoredPassword,Domain);
resolver.Credentials = myCred;

// Transform the file using the resolver. The resolver is used
// to process the XSLT document() function.
XPathDocument doc = new XPathDocument("books.xml");
XmlReader reader = xslt.Transform(doc, null, resolver);

// Load the reader into a new document for more processing.
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load(reader);

Remarks

Megjegyzés

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 xsl:output element is not supported when outputting to an XmlReader (the xsl:output element is ignored). See Outputs from an XslTransform for more information.

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.

C#
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

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

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

See also

Applies to

.NET 9 és más verziók
Termék Verziók
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

Transform(IXPathNavigable, XsltArgumentList, TextWriter)

Source:
XslTransform.cs
Source:
XslTransform.cs
Source:
XslTransform.cs

Caution

You should pass XmlResolver to Transform() method

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

C#
public void Transform (System.Xml.XPath.IXPathNavigable input, System.Xml.Xsl.XsltArgumentList? args, System.IO.TextWriter output);
C#
public void Transform (System.Xml.XPath.IXPathNavigable input, System.Xml.Xsl.XsltArgumentList args, System.IO.TextWriter output);
C#
[System.Obsolete("You should pass XmlResolver to Transform() method")]
public void Transform (System.Xml.XPath.IXPathNavigable input, System.Xml.Xsl.XsltArgumentList args, System.IO.TextWriter output);

Parameters

input
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
XsltArgumentList

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

output
TextWriter

The TextWriter to which you want to output.

Attributes

Exceptions

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.

Remarks

Megjegyzés

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.

Megjegyzés

This method is now obsolete. The setting of the XmlResolver property determines how the XSLT document() function is resolved. The recommended practice is to use the Transform method which takes an XmlResolver object as one of its arguments.

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.

C#
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

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

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

See also

Applies to

.NET 9 és más verziók
Termék Verziók (Elavult)
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 (1.1)
.NET Standard 2.0, 2.1

Transform(IXPathNavigable, XsltArgumentList, Stream)

Source:
XslTransform.cs
Source:
XslTransform.cs
Source:
XslTransform.cs

Caution

You should pass XmlResolver to Transform() method

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

C#
public void Transform (System.Xml.XPath.IXPathNavigable input, System.Xml.Xsl.XsltArgumentList? args, System.IO.Stream output);
C#
public void Transform (System.Xml.XPath.IXPathNavigable input, System.Xml.Xsl.XsltArgumentList args, System.IO.Stream output);
C#
[System.Obsolete("You should pass XmlResolver to Transform() method")]
public void Transform (System.Xml.XPath.IXPathNavigable input, System.Xml.Xsl.XsltArgumentList args, System.IO.Stream output);

Parameters

input
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
XsltArgumentList

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

output
Stream

The stream to which you want to output.

Attributes

Exceptions

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.

Remarks

Megjegyzés

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.

Megjegyzés

This method is now obsolete. The setting of the XmlResolver property determines how the XSLT document() function is resolved. The recommended practice is to use the Transform method which takes an XmlResolver object as one of its arguments.

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

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.

C#
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

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

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

See also

Applies to

.NET 9 és más verziók
Termék Verziók (Elavult)
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 (1.1)
.NET Standard 2.0, 2.1

Transform(XPathNavigator, XsltArgumentList, XmlWriter, XmlResolver)

Source:
XslTransform.cs
Source:
XslTransform.cs
Source:
XslTransform.cs

Transforms the XML data in the XPathNavigator using the specified args and outputs the result to an XmlWriter.

C#
public void Transform (System.Xml.XPath.XPathNavigator input, System.Xml.Xsl.XsltArgumentList? args, System.Xml.XmlWriter output, System.Xml.XmlResolver? resolver);
C#
public void Transform (System.Xml.XPath.XPathNavigator input, System.Xml.Xsl.XsltArgumentList args, System.Xml.XmlWriter output, System.Xml.XmlResolver resolver);

Parameters

input
XPathNavigator

An XPathNavigator containing the data to be transformed.

args
XsltArgumentList

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

output
XmlWriter

The XmlWriter to which you want to output.

resolver
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(XPathNavigator, XsltArgumentList, XmlWriter, XmlResolver) method completes.

Exceptions

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.

Examples

The following example loads and edits an XML document before performing an XSLT transform.

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

Remarks

Megjegyzés

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

The xsl:output element is not supported when outputting to an XmlWriter (xsl:output is ignored). See Outputs from an XslTransform for more information.

See also

Applies to

.NET 9 és más verziók
Termék Verziók
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

Transform(XPathNavigator, XsltArgumentList)

Source:
XslTransform.cs
Source:
XslTransform.cs
Source:
XslTransform.cs

Caution

You should pass XmlResolver to Transform() method

Transforms the XML data in the XPathNavigator using the specified args and outputs the result to an XmlReader.

C#
public System.Xml.XmlReader Transform (System.Xml.XPath.XPathNavigator input, System.Xml.Xsl.XsltArgumentList? args);
C#
public System.Xml.XmlReader Transform (System.Xml.XPath.XPathNavigator input, System.Xml.Xsl.XsltArgumentList args);
C#
[System.Obsolete("You should pass XmlResolver to Transform() method")]
public System.Xml.XmlReader Transform (System.Xml.XPath.XPathNavigator input, System.Xml.Xsl.XsltArgumentList args);

Parameters

input
XPathNavigator

An XPathNavigator containing the data to be transformed.

args
XsltArgumentList

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

Returns

An XmlReader containing the results of the transformation.

Attributes

Exceptions

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.

Remarks

Megjegyzés

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.

Because XmlReader provides read-only output, the xsl:output element is ignored. See Outputs from an XslTransform for more information.

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

Megjegyzés

This method is obsolete. The setting of the XmlResolver property determines how the XSLT document() function is resolved. The recommended practice is to use the Transform method which takes an XmlResolver object as one of its arguments.

See also

Applies to

.NET 9 és más verziók
Termék Verziók (Elavult)
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 (1.1)
.NET Standard 2.0, 2.1

Transform(IXPathNavigable, XsltArgumentList)

Source:
XslTransform.cs
Source:
XslTransform.cs
Source:
XslTransform.cs

Caution

You should pass XmlResolver to Transform() method

Transforms the XML data in the IXPathNavigable using the specified args and outputs the result to an XmlReader.

C#
public System.Xml.XmlReader Transform (System.Xml.XPath.IXPathNavigable input, System.Xml.Xsl.XsltArgumentList? args);
C#
public System.Xml.XmlReader Transform (System.Xml.XPath.IXPathNavigable input, System.Xml.Xsl.XsltArgumentList args);
C#
[System.Obsolete("You should pass XmlResolver to Transform() method")]
public System.Xml.XmlReader Transform (System.Xml.XPath.IXPathNavigable input, System.Xml.Xsl.XsltArgumentList args);

Parameters

input
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
XsltArgumentList

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

Returns

An XmlReader containing the results of the transformation.

Attributes

Remarks

Megjegyzés

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.

Megjegyzés

This method is now obsolete. The setting of the XmlResolver property determines how the XSLT document() function is resolved. The recommended practice is to use the Transform method which takes an XmlResolver object as one of its arguments.

The args are matched with the xsl:param elements defined in the style sheet. The xsl:output element is not supported when outputting to an XmlReader (the xsl:output element is ignored). See Outputs from an XslTransform for more information.

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.

C#
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

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

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

See also

Applies to

.NET 9 és más verziók
Termék Verziók (Elavult)
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 (1.1)
.NET Standard 2.0, 2.1

Transform(String, String)

Source:
XslTransform.cs
Source:
XslTransform.cs
Source:
XslTransform.cs

Caution

You should pass XmlResolver to Transform() method

Transforms the XML data in the input file and outputs the result to an output file.

C#
public void Transform (string inputfile, string outputfile);
C#
[System.Obsolete("You should pass XmlResolver to Transform() method")]
public void Transform (string inputfile, string outputfile);

Parameters

inputfile
String

The URL of the source document to be transformed.

outputfile
String

The URL of the output file.

Attributes

Examples

The following example transforms the books.xml file using the output.xsl style sheet and outputs the results to the books.html file.

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

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

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

The sample uses the following two input files:

books.xml

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

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

Remarks

Megjegyzés

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.

Megjegyzés

This method is obsolete in version 1.1 of the .NET Framework. The setting of the XmlResolver property determines how the XSLT document() function is resolved. The recommended practice is to use the Transform method which takes an XmlResolver object as one of its arguments.

See also

Applies to

.NET 9 és más verziók
Termék Verziók (Elavult)
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 (1.1)
.NET Standard 2.0, 2.1

Transform(XPathNavigator, XsltArgumentList, Stream)

Source:
XslTransform.cs
Source:
XslTransform.cs
Source:
XslTransform.cs

Caution

You should pass XmlResolver to Transform() method

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

C#
public void Transform (System.Xml.XPath.XPathNavigator input, System.Xml.Xsl.XsltArgumentList? args, System.IO.Stream output);
C#
public void Transform (System.Xml.XPath.XPathNavigator input, System.Xml.Xsl.XsltArgumentList args, System.IO.Stream output);
C#
[System.Obsolete("You should pass XmlResolver to Transform() method")]
public void Transform (System.Xml.XPath.XPathNavigator input, System.Xml.Xsl.XsltArgumentList args, System.IO.Stream output);

Parameters

input
XPathNavigator

An XPathNavigator containing the data to be transformed.

args
XsltArgumentList

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

output
Stream

The stream to which you want to output.

Attributes

Exceptions

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.

Remarks

Megjegyzés

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.

Megjegyzés

This method is now obsolete. The setting of the XmlResolver property determines how the XSLT document() function is resolved. The recommended practice is to use the Transform method which takes an XmlResolver object as one of its arguments.

See also

Applies to

.NET 9 és más verziók
Termék Verziók (Elavult)
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 (1.1)
.NET Standard 2.0, 2.1