XslTransform.Transform XslTransform.Transform XslTransform.Transform XslTransform.Transform Method

定義

使用載入的 XSLT 樣式表轉換 XML 資料。Transforms the XML data using the loaded XSLT style sheet.

多載

Transform(XPathNavigator, XsltArgumentList, XmlResolver) Transform(XPathNavigator, XsltArgumentList, XmlResolver) Transform(XPathNavigator, XsltArgumentList, XmlResolver) Transform(XPathNavigator, XsltArgumentList, XmlResolver)

使用指定的 args 來轉換 XPathNavigator 中的 XML 資料,並將結果輸出至 XmlReaderTransforms the XML data in the XPathNavigator using the specified args and outputs the result to an XmlReader.

Transform(XPathNavigator, XsltArgumentList, TextWriter, XmlResolver) Transform(XPathNavigator, XsltArgumentList, TextWriter, XmlResolver) Transform(XPathNavigator, XsltArgumentList, TextWriter, XmlResolver) Transform(XPathNavigator, XsltArgumentList, TextWriter, XmlResolver)

使用指定的 args 來轉換 XPathNavigator 中的 XML 資料,並將結果輸出至 TextWriterTransforms the XML data in the XPathNavigator using the specified args and outputs the result to a TextWriter.

Transform(XPathNavigator, XsltArgumentList, Stream, XmlResolver) Transform(XPathNavigator, XsltArgumentList, Stream, XmlResolver) Transform(XPathNavigator, XsltArgumentList, Stream, XmlResolver) Transform(XPathNavigator, XsltArgumentList, Stream, XmlResolver)

使用指定的 args 來轉換 XPathNavigator 中的 XML 資料,並將結果輸出至 StreamTransforms the XML data in the XPathNavigator using the specified args and outputs the result to a Stream.

Transform(IXPathNavigable, XsltArgumentList, XmlWriter, XmlResolver) Transform(IXPathNavigable, XsltArgumentList, XmlWriter, XmlResolver) Transform(IXPathNavigable, XsltArgumentList, XmlWriter, XmlResolver) Transform(IXPathNavigable, XsltArgumentList, XmlWriter, XmlResolver)

使用指定的 args 來轉換 IXPathNavigable 中的 XML 資料,並將結果輸出至 XmlWriterTransforms the XML data in the IXPathNavigable using the specified args and outputs the result to an XmlWriter.

Transform(IXPathNavigable, XsltArgumentList, TextWriter, XmlResolver) Transform(IXPathNavigable, XsltArgumentList, TextWriter, XmlResolver) Transform(IXPathNavigable, XsltArgumentList, TextWriter, XmlResolver) Transform(IXPathNavigable, XsltArgumentList, TextWriter, XmlResolver)

使用指定的 args 來轉換 IXPathNavigable 中的 XML 資料,並將結果輸出至 TextWriterTransforms the XML data in the IXPathNavigable using the specified args and outputs the result to a TextWriter.

Transform(IXPathNavigable, XsltArgumentList, Stream, XmlResolver) Transform(IXPathNavigable, XsltArgumentList, Stream, XmlResolver) Transform(IXPathNavigable, XsltArgumentList, Stream, XmlResolver) Transform(IXPathNavigable, XsltArgumentList, Stream, XmlResolver)

使用指定的 args 來轉換 IXPathNavigable 中的 XML 資料,並將結果輸出至 StreamTransforms the XML data in the IXPathNavigable using the specified args and outputs the result to a Stream.

Transform(XPathNavigator, XsltArgumentList, XmlWriter) Transform(XPathNavigator, XsltArgumentList, XmlWriter) Transform(XPathNavigator, XsltArgumentList, XmlWriter) Transform(XPathNavigator, XsltArgumentList, XmlWriter)

使用指定的引數來轉換 XPathNavigator 中的 XML 資料,並將結果輸出至 XmlWriterTransforms the XML data in the XPathNavigator using the specified args and outputs the result to an XmlWriter.

Transform(XPathNavigator, XsltArgumentList, TextWriter) Transform(XPathNavigator, XsltArgumentList, TextWriter) Transform(XPathNavigator, XsltArgumentList, TextWriter) Transform(XPathNavigator, XsltArgumentList, TextWriter)

使用指定的 args 來轉換 XPathNavigator 中的 XML 資料,並將結果輸出至 TextWriterTransforms the XML data in the XPathNavigator using the specified args and outputs the result to a TextWriter.

Transform(String, String, XmlResolver) Transform(String, String, XmlResolver) Transform(String, String, XmlResolver) Transform(String, String, XmlResolver)

在輸入檔案中轉換 XML 資料,並將結果輸出至輸出檔案。Transforms the XML data in the input file and outputs the result to an output file.

Transform(IXPathNavigable, XsltArgumentList, XmlWriter) Transform(IXPathNavigable, XsltArgumentList, XmlWriter) Transform(IXPathNavigable, XsltArgumentList, XmlWriter) Transform(IXPathNavigable, XsltArgumentList, XmlWriter)

使用指定的 args 來轉換 IXPathNavigable 中的 XML 資料,並將結果輸出至 XmlWriterTransforms the XML data in the IXPathNavigable using the specified args and outputs the result to an XmlWriter.

Transform(IXPathNavigable, XsltArgumentList, XmlResolver) Transform(IXPathNavigable, XsltArgumentList, XmlResolver) Transform(IXPathNavigable, XsltArgumentList, XmlResolver) Transform(IXPathNavigable, XsltArgumentList, XmlResolver)

使用指定的 args 來轉換 IXPathNavigable 中的 XML 資料,並將結果輸出至 XmlReaderTransforms the XML data in the IXPathNavigable using the specified args and outputs the result to an XmlReader.

Transform(IXPathNavigable, XsltArgumentList, TextWriter) Transform(IXPathNavigable, XsltArgumentList, TextWriter) Transform(IXPathNavigable, XsltArgumentList, TextWriter) Transform(IXPathNavigable, XsltArgumentList, TextWriter)

使用指定的 args 來轉換 IXPathNavigable 中的 XML 資料,並將結果輸出至 TextWriterTransforms the XML data in the IXPathNavigable using the specified args and outputs the result to a TextWriter.

Transform(IXPathNavigable, XsltArgumentList, Stream) Transform(IXPathNavigable, XsltArgumentList, Stream) Transform(IXPathNavigable, XsltArgumentList, Stream) Transform(IXPathNavigable, XsltArgumentList, Stream)

使用指定的 args 來轉換 IXPathNavigable 中的 XML 資料,並將結果輸出至 StreamTransforms the XML data in the IXPathNavigable using the specified args and outputs the result to a Stream.

Transform(XPathNavigator, XsltArgumentList, XmlWriter, XmlResolver) Transform(XPathNavigator, XsltArgumentList, XmlWriter, XmlResolver) Transform(XPathNavigator, XsltArgumentList, XmlWriter, XmlResolver) Transform(XPathNavigator, XsltArgumentList, XmlWriter, XmlResolver)

使用指定的引數來轉換 XPathNavigator 中的 XML 資料,並將結果輸出至 XmlWriterTransforms the XML data in the XPathNavigator using the specified args and outputs the result to an XmlWriter.

Transform(XPathNavigator, XsltArgumentList) Transform(XPathNavigator, XsltArgumentList) Transform(XPathNavigator, XsltArgumentList) Transform(XPathNavigator, XsltArgumentList)

使用指定的 args 來轉換 XPathNavigator 中的 XML 資料,並將結果輸出至 XmlReaderTransforms the XML data in the XPathNavigator using the specified args and outputs the result to an XmlReader.

Transform(IXPathNavigable, XsltArgumentList) Transform(IXPathNavigable, XsltArgumentList) Transform(IXPathNavigable, XsltArgumentList) Transform(IXPathNavigable, XsltArgumentList)

使用指定的 args 來轉換 IXPathNavigable 中的 XML 資料,並將結果輸出至 XmlReaderTransforms the XML data in the IXPathNavigable using the specified args and outputs the result to an XmlReader.

Transform(String, String) Transform(String, String) Transform(String, String) Transform(String, String)

在輸入檔案中轉換 XML 資料,並將結果輸出至輸出檔案。Transforms the XML data in the input file and outputs the result to an output file.

Transform(XPathNavigator, XsltArgumentList, Stream) Transform(XPathNavigator, XsltArgumentList, Stream) Transform(XPathNavigator, XsltArgumentList, Stream) Transform(XPathNavigator, XsltArgumentList, Stream)

使用指定的 args 來轉換 XPathNavigator 中的 XML 資料,並將結果輸出至 StreamTransforms the XML data in the XPathNavigator using the specified args and outputs the result to a Stream.

備註

注意

XslTransform類別在 .NET Framework 版本2.0 中已過時。The XslTransform class is obsolete in the .NET Framework version 2.0. XslCompiledTransform類別是新的 XSLT 處理器。The XslCompiledTransform class is the new XSLT processor. 如需詳細資訊, 請參閱使用 XslCompiledTransform 類別從 XslTransform 類別進行遷移For more information, see Using the XslCompiledTransform Class and Migrating From the XslTransform Class.

Transform(XPathNavigator, XsltArgumentList, XmlResolver) Transform(XPathNavigator, XsltArgumentList, XmlResolver) Transform(XPathNavigator, XsltArgumentList, XmlResolver) Transform(XPathNavigator, XsltArgumentList, XmlResolver)

使用指定的 args 來轉換 XPathNavigator 中的 XML 資料,並將結果輸出至 XmlReaderTransforms the XML data in the XPathNavigator using the specified args and outputs the result to an XmlReader.

public:
 System::Xml::XmlReader ^ Transform(System::Xml::XPath::XPathNavigator ^ input, System::Xml::Xsl::XsltArgumentList ^ args, System::Xml::XmlResolver ^ resolver);
public System.Xml.XmlReader Transform (System.Xml.XPath.XPathNavigator input, System.Xml.Xsl.XsltArgumentList args, System.Xml.XmlResolver resolver);
member this.Transform : System.Xml.XPath.XPathNavigator * System.Xml.Xsl.XsltArgumentList * System.Xml.XmlResolver -> System.Xml.XmlReader
Public Function Transform (input As XPathNavigator, args As XsltArgumentList, resolver As XmlResolver) As XmlReader

參數

input
XPathNavigator XPathNavigator XPathNavigator XPathNavigator

包含要轉換資料的 XPathNavigatorAn XPathNavigator containing the data to be transformed.

args
XsltArgumentList XsltArgumentList XsltArgumentList XsltArgumentList

XsltArgumentList,包含用做轉換輸入之符合命名空間的引數。An XsltArgumentList containing the namespace-qualified arguments used as input to the transformation.

resolver
XmlResolver XmlResolver XmlResolver XmlResolver

XmlResolver 用來解析 XSLT document() 函式。The XmlResolver used to resolve the XSLT document() function. 如果值為 null,則不會解析 document() 函式。If this is null, the document() function is not resolved.

XmlResolverTransform(XPathNavigator, XsltArgumentList, XmlResolver) 方法完成後不會加以快取。The XmlResolver is not cached after the Transform(XPathNavigator, XsltArgumentList, XmlResolver) method completes.

傳回

包含轉換結果的 XmlReaderAn XmlReader containing the results of the transformation.

例外狀況

處理 XSLT 轉換時發生錯誤。There was an error processing the XSLT transformation.

注意:這是先前版本的行為變更之一。Note: This is a change in behavior from earlier versions. 如果您使用的是 Microsoft .NET Framework 1.1 版 (含) 以前的版本,則會擲回 XsltExceptionAn XsltException is thrown if you are using Microsoft .NET Framework version 1.1 or earlier.

備註

注意

XslTransform類別在 .NET Framework 版本2.0 中已過時。The XslTransform class is obsolete in the .NET Framework version 2.0. XslCompiledTransform類別是新的 XSLT 處理器。The XslCompiledTransform class is the new XSLT processor. 如需詳細資訊, 請參閱使用 XslCompiledTransform 類別從 XslTransform 類別進行遷移For more information, see Using the XslCompiledTransform Class and Migrating From the XslTransform Class.

XslTransform支援 XSLT 1.0 語法。XslTransform supports the XSLT 1.0 syntax. XSLT 樣式表單必須包含命名空間xmlns:xsl= http://www.w3.org/1999/XSL/Transform宣告。The XSLT style sheet must include the namespace declaration xmlns:xsl= http://www.w3.org/1999/XSL/Transform.

會與樣式表單中定義的元素相符。xsl:param argsThe 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. 在執行轉換之後, XPathNavigator會保持其原始狀態。After the transformation has been performed, the XPathNavigator remains in its original state. 這表示在呼叫Transform方法之後, 節點 (在轉換程式之前是最新的) 會維持目前的節點。This means that the node, which is current before the transformation process, remains the current node after the Transform method has been called.

因為XmlReader提供唯讀輸出xsl:output , 所以會忽略元素。Because XmlReader provides read-only output, the xsl:output element is ignored. 如需詳細資訊, 請參閱XslTransform 的輸出See Outputs from an XslTransform for more information.

這個方法可讓您進行來源文件的非同步轉換。This method enables you to do an asynchronous transformation of the source document.

另請參閱

Transform(XPathNavigator, XsltArgumentList, TextWriter, XmlResolver) Transform(XPathNavigator, XsltArgumentList, TextWriter, XmlResolver) Transform(XPathNavigator, XsltArgumentList, TextWriter, XmlResolver) Transform(XPathNavigator, XsltArgumentList, TextWriter, XmlResolver)

使用指定的 args 來轉換 XPathNavigator 中的 XML 資料,並將結果輸出至 TextWriterTransforms the XML data in the XPathNavigator using the specified args and outputs the result to a TextWriter.

public:
 void Transform(System::Xml::XPath::XPathNavigator ^ input, System::Xml::Xsl::XsltArgumentList ^ args, System::IO::TextWriter ^ output, System::Xml::XmlResolver ^ resolver);
public void Transform (System.Xml.XPath.XPathNavigator input, System.Xml.Xsl.XsltArgumentList args, System.IO.TextWriter output, System.Xml.XmlResolver resolver);
member this.Transform : System.Xml.XPath.XPathNavigator * System.Xml.Xsl.XsltArgumentList * System.IO.TextWriter * System.Xml.XmlResolver -> unit
Public Sub Transform (input As XPathNavigator, args As XsltArgumentList, output As TextWriter, resolver As XmlResolver)

參數

input
XPathNavigator XPathNavigator XPathNavigator XPathNavigator

包含要轉換資料的 XPathNavigatorAn XPathNavigator containing the data to be transformed.

args
XsltArgumentList XsltArgumentList XsltArgumentList XsltArgumentList

XsltArgumentList,包含用做轉換輸入之符合命名空間的引數。An XsltArgumentList containing the namespace-qualified arguments used as input to the transformation.

output
TextWriter TextWriter TextWriter TextWriter

您要輸出的目標 TextWriterThe TextWriter to which you want to output.

resolver
XmlResolver XmlResolver XmlResolver XmlResolver

XmlResolver 用來解析 XSLT document() 函式。The XmlResolver used to resolve the XSLT document() function. 如果值為 null,則不會解析 document() 函式。If this is null, the document() function is not resolved.

XmlResolverTransform(XPathNavigator, XsltArgumentList, TextWriter, XmlResolver) 方法完成後不會加以快取。The XmlResolver is not cached after the Transform(XPathNavigator, XsltArgumentList, TextWriter, XmlResolver) method completes.

例外狀況

處理 XSLT 轉換時發生錯誤。There was an error processing the XSLT transformation.

注意:這是先前版本的行為變更之一。Note: This is a change in behavior from earlier versions. 如果您使用的是 Microsoft .NET Framework 1.1 版 (含) 以前的版本,則會擲回 XsltExceptionAn XsltException is thrown if you are using Microsoft .NET Framework version 1.1 or earlier.

備註

注意

XslTransform類別在 .NET Framework 版本2.0 中已過時。The XslTransform class is obsolete in the .NET Framework version 2.0. XslCompiledTransform類別是新的 XSLT 處理器。The XslCompiledTransform class is the new XSLT processor. 如需詳細資訊, 請參閱使用 XslCompiledTransform 類別從 XslTransform 類別進行遷移For more information, see Using the XslCompiledTransform Class and Migrating From the XslTransform Class.

XslTransform支援 XSLT 1.0 語法。XslTransform supports the XSLT 1.0 syntax. XSLT 樣式表單必須包含命名空間xmlns:xsl= http://www.w3.org/1999/XSL/Transform宣告。The XSLT style sheet must include the namespace declaration xmlns:xsl= http://www.w3.org/1999/XSL/Transform.

會與樣式表單中定義的元素相符。xsl:param argsThe 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. 在執行轉換之後, XPathNavigator會保持其原始狀態。After the transformation is performed, the XPathNavigator remains in its original state. 這表示在呼叫Transform方法之後, 節點 (在轉換程式之前是最新的) 會維持目前的節點。This means that the node, which is current before the transformation process, remains the current node after the Transform method has been called.

當輸出xsl:output TextWriter至時, 不支援元素上的編碼屬性。The encoding attribute on an xsl:output element is not supported when outputting to a TextWriter. 需支援哪些xsl:output屬性的詳細資訊, 請參閱 XslTransform 的輸出。See Outputs from an XslTransform for specifics on which xsl:output attributes are supported.

另請參閱

Transform(XPathNavigator, XsltArgumentList, Stream, XmlResolver) Transform(XPathNavigator, XsltArgumentList, Stream, XmlResolver) Transform(XPathNavigator, XsltArgumentList, Stream, XmlResolver) Transform(XPathNavigator, XsltArgumentList, Stream, XmlResolver)

使用指定的 args 來轉換 XPathNavigator 中的 XML 資料,並將結果輸出至 StreamTransforms the XML data in the XPathNavigator using the specified args and outputs the result to a Stream.

public:
 void Transform(System::Xml::XPath::XPathNavigator ^ input, System::Xml::Xsl::XsltArgumentList ^ args, System::IO::Stream ^ output, System::Xml::XmlResolver ^ resolver);
public void Transform (System.Xml.XPath.XPathNavigator input, System.Xml.Xsl.XsltArgumentList args, System.IO.Stream output, System.Xml.XmlResolver resolver);
member this.Transform : System.Xml.XPath.XPathNavigator * System.Xml.Xsl.XsltArgumentList * System.IO.Stream * System.Xml.XmlResolver -> unit
Public Sub Transform (input As XPathNavigator, args As XsltArgumentList, output As Stream, resolver As XmlResolver)

參數

input
XPathNavigator XPathNavigator XPathNavigator XPathNavigator

包含要轉換資料的 XPathNavigatorAn XPathNavigator containing the data to be transformed.

args
XsltArgumentList XsltArgumentList XsltArgumentList XsltArgumentList

XsltArgumentList,包含用做轉換輸入之符合命名空間的引數。An XsltArgumentList containing the namespace-qualified arguments used as input to the transformation.

output
Stream Stream Stream Stream

要輸出至的資料流。The stream to which you want to output.

resolver
XmlResolver XmlResolver XmlResolver XmlResolver

XmlResolver 用來解析 XSLT document() 函式。The XmlResolver used to resolve the XSLT document() function. 如果值為 null,則不會解析 document() 函式。If this is null, the document() function is not resolved.

XmlResolverTransform(XPathNavigator, XsltArgumentList, Stream, XmlResolver) 方法完成後不會加以快取。The XmlResolver is not cached after the Transform(XPathNavigator, XsltArgumentList, Stream, XmlResolver) method completes.

例外狀況

處理 XSLT 轉換時發生錯誤。There was an error processing the XSLT transformation.

注意:這是先前版本的行為變更之一。Note: This is a change in behavior from earlier versions. 如果您使用的是 Microsoft .NET Framework 1.1 版 (含) 以前的版本,則會擲回 XsltExceptionAn XsltException is thrown if you are using Microsoft .NET Framework version 1.1 or earlier.

範例

下列範例會將 customer 資料表XmlDataDocument載入, 並執行 XSLT 轉換, 將客戶資料提取至 HTML 資料表。The following example loads a customer table into an XmlDataDocument and performs an XSLT transformation to pull the customer data into an HTML table. 此範例會使用 Microsoft SQL Server 2000 Northwind 資料庫。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);
	
  }
}
Imports System.IO
Imports System.Data
Imports System.Data.SqlClient
Imports System.Xml
Imports System.Xml.XPath
Imports System.Xml.Xsl

public class Sample

  public shared sub Main()

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

     ' Load the DataSet into an XmlDataDocument.
     Dim doc as XmlDataDocument = new XmlDataDocument(dsNorthwind)   

     ' Create the XslTransform object and load the stylesheet.
     Dim xsl as XslTransform = new XslTransform()     
     xsl.Load("customers.xsl")

     ' Create an XPathNavigator to use in the transform.
     Dim nav as XPathNavigator = doc.CreateNavigator()

     ' Create a FileStream object.
     Dim fs as FileStream = new FileStream("cust.html", FileMode.Create)
 
     ' Transform the data.
     xsl.Transform(nav, nothing, fs, nothing)
	
  end sub
end class

該範例使用 customers.xsl 檔案做為輸入。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>

備註

注意

XslTransform類別在 .NET Framework 版本2.0 中已過時。The XslTransform class is obsolete in the .NET Framework version 2.0. XslCompiledTransform類別是新的 XSLT 處理器。The XslCompiledTransform class is the new XSLT processor. 如需詳細資訊, 請參閱使用 XslCompiledTransform 類別從 XslTransform 類別進行遷移For more information, see Using the XslCompiledTransform Class and Migrating From the XslTransform Class.

XslTransform支援 XSLT 1.0 語法。XslTransform supports the XSLT 1.0 syntax. XSLT 樣式表單必須包含命名空間xmlns:xsl= http://www.w3.org/1999/XSL/Transform宣告。The XSLT style sheet must include the namespace declaration xmlns:xsl= http://www.w3.org/1999/XSL/Transform.

會與樣式表單中定義的元素相符。xsl:param argsThe 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. 在執行轉換之後, XPathNavigator會保持其原始狀態。After the transformation has been performed, the XPathNavigator remains in its original state. 這表示在呼叫Transform方法之後, 節點 (在轉換程式之前是最新的) 會維持目前的節點。This means that the node, which is current before the transformation process, remains the current node after the Transform method has been called.

需支援哪些xsl:output屬性的詳細資訊, 請參閱 XslTransform 的輸出。See Outputs from an XslTransform for specifics on which xsl:output attributes are supported.

另請參閱

Transform(IXPathNavigable, XsltArgumentList, XmlWriter, XmlResolver) Transform(IXPathNavigable, XsltArgumentList, XmlWriter, XmlResolver) Transform(IXPathNavigable, XsltArgumentList, XmlWriter, XmlResolver) Transform(IXPathNavigable, XsltArgumentList, XmlWriter, XmlResolver)

使用指定的 args 來轉換 IXPathNavigable 中的 XML 資料,並將結果輸出至 XmlWriterTransforms the XML data in the IXPathNavigable using the specified args and outputs the result to an XmlWriter.

public:
 void Transform(System::Xml::XPath::IXPathNavigable ^ input, System::Xml::Xsl::XsltArgumentList ^ args, System::Xml::XmlWriter ^ output, System::Xml::XmlResolver ^ resolver);
public void Transform (System.Xml.XPath.IXPathNavigable input, System.Xml.Xsl.XsltArgumentList args, System.Xml.XmlWriter output, System.Xml.XmlResolver resolver);
member this.Transform : System.Xml.XPath.IXPathNavigable * System.Xml.Xsl.XsltArgumentList * System.Xml.XmlWriter * System.Xml.XmlResolver -> unit
Public Sub Transform (input As IXPathNavigable, args As XsltArgumentList, output As XmlWriter, resolver As XmlResolver)

參數

input
IXPathNavigable IXPathNavigable IXPathNavigable IXPathNavigable

實作 IXPathNavigable 介面的物件。An object implementing the IXPathNavigable interface. 在 .NET Framework 中,這可以是 XmlNode (一般為 XmlDocument),或 XPathDocument,其中包含要轉換的資料。In the .NET Framework, this can be either an XmlNode (typically an XmlDocument), or an XPathDocument containing the data to be transformed.

args
XsltArgumentList XsltArgumentList XsltArgumentList XsltArgumentList

XsltArgumentList,包含用做轉換輸入之符合命名空間的引數。An XsltArgumentList containing the namespace-qualified arguments used as input to the transformation.

output
XmlWriter XmlWriter XmlWriter XmlWriter

您要輸出的目標 XmlWriterThe XmlWriter to which you want to output.

resolver
XmlResolver XmlResolver XmlResolver XmlResolver

XmlResolver 用來解析 XSLT document() 函式。The XmlResolver used to resolve the XSLT document() function. 如果值為 null,則不會解析 document() 函式。If this is null, the document() function is not resolved.

XmlResolverTransform(IXPathNavigable, XsltArgumentList, XmlWriter, XmlResolver) 方法完成後不會加以快取。The XmlResolver is not cached after the Transform(IXPathNavigable, XsltArgumentList, XmlWriter, XmlResolver) method completes.

例外狀況

處理 XSLT 轉換時發生錯誤。There was an error processing the XSLT transformation.

注意:這是先前版本的行為變更之一。Note: This is a change in behavior from earlier versions. 如果您使用的是 Microsoft .NET Framework 1.1 版 (含) 以前的版本,則會擲回 XsltExceptionAn XsltException is thrown if you are using Microsoft .NET Framework version 1.1 or earlier.

範例

下列範例會將 XML 檔轉換成 HTML 檔案。The following example transforms an XML document into an HTML document. 它會顯示資料表中每一本書的 ISBN、標題和價格。It displays the ISBN, title, and price for each book in a table.

#using <System.dll>
#using <System.Xml.dll>

using namespace System;
using namespace System::IO;
using namespace System::Xml;
using namespace System::Xml::Xsl;
using namespace System::Xml::XPath;

int main()
{
   String^ filename = "books.xml";
   String^ stylesheet = "output.xsl";

   //Load the stylesheet.
   XslTransform^ xslt = gcnew XslTransform;
   xslt->Load( stylesheet );

   //Load the file to transform.
   XPathDocument^ doc = gcnew XPathDocument( filename );

   //Create an XmlTextWriter which outputs to the console.
   XmlTextWriter^ writer = gcnew XmlTextWriter( Console::Out );

   //Transform the file and send the output to the console.
   xslt->Transform(doc,nullptr,writer,nullptr);
   writer->Close();
}
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();

  }
}
Option Strict
Option Explicit

Imports System.IO
Imports System.Xml
Imports System.Xml.Xsl
Imports System.Xml.XPath

Public Class Sample
    Private Shared filename1 As String = "books.xml"
    Private Shared stylesheet1 As String = "output.xsl"
    
    
    Public Shared Sub Main()
        'Load the stylesheet.
        Dim xslt As New XslTransform()
        xslt.Load(stylesheet1)
        
        'Load the file to transform.
        Dim doc As New XPathDocument(filename1)
        
        'Create an XmlTextWriter which outputs to the console.
        Dim writer As New XmlTextWriter(Console.Out)
        
        'Transform the file and send the output to the console.
        xslt.Transform(doc, Nothing, writer, Nothing)
        writer.Close()
    End Sub
End Class 'Sample

範例會使用下列兩個輸入檔案。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>

備註

注意

XslTransform類別在 .NET Framework 版本2.0 中已過時。The XslTransform class is obsolete in the .NET Framework version 2.0. XslCompiledTransform類別是新的 XSLT 處理器。The XslCompiledTransform class is the new XSLT processor. 如需詳細資訊, 請參閱使用 XslCompiledTransform 類別從 XslTransform 類別進行遷移For more information, see Using the XslCompiledTransform Class and Migrating From the XslTransform Class.

XslTransform支援 XSLT 1.0 語法。XslTransform supports the XSLT 1.0 syntax. XSLT 樣式表單必須包含命名空間xmlns:xsl= http://www.w3.org/1999/XSL/Transform宣告。The XSLT style sheet must include the namespace declaration xmlns:xsl= http://www.w3.org/1999/XSL/Transform.

會與樣式表單中定義的元素相符。xsl:param argsThe args are matched with the xsl:param elements defined in the style sheet. xsl:output輸出XmlWriter至(xsl:output會忽略) 時, 不支援元素。The xsl:output element is not supported when outputting to an XmlWriter (xsl:output is ignored). 如需詳細資訊, 請參閱XslTransform 的輸出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. 若要轉換節點片段, 您必須建立XmlDocument僅包含節點片段的, 並將XmlDocument其傳遞給Transform方法。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);  

此範例會使用library.xmlprint_root.xsl檔案做為輸入, 並將下列輸出至主控台。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>  
另請參閱

Transform(IXPathNavigable, XsltArgumentList, TextWriter, XmlResolver) Transform(IXPathNavigable, XsltArgumentList, TextWriter, XmlResolver) Transform(IXPathNavigable, XsltArgumentList, TextWriter, XmlResolver) Transform(IXPathNavigable, XsltArgumentList, TextWriter, XmlResolver)

使用指定的 args 來轉換 IXPathNavigable 中的 XML 資料,並將結果輸出至 TextWriterTransforms the XML data in the IXPathNavigable using the specified args and outputs the result to a TextWriter.

public:
 void Transform(System::Xml::XPath::IXPathNavigable ^ input, System::Xml::Xsl::XsltArgumentList ^ args, System::IO::TextWriter ^ output, System::Xml::XmlResolver ^ resolver);
public void Transform (System.Xml.XPath.IXPathNavigable input, System.Xml.Xsl.XsltArgumentList args, System.IO.TextWriter output, System.Xml.XmlResolver resolver);
member this.Transform : System.Xml.XPath.IXPathNavigable * System.Xml.Xsl.XsltArgumentList * System.IO.TextWriter * System.Xml.XmlResolver -> unit
Public Sub Transform (input As IXPathNavigable, args As XsltArgumentList, output As TextWriter, resolver As XmlResolver)

參數

input
IXPathNavigable IXPathNavigable IXPathNavigable IXPathNavigable

實作 IXPathNavigable 介面的物件。An object implementing the IXPathNavigable interface. 在 .NET Framework 中,這可以是 XmlNode (一般為 XmlDocument),或 XPathDocument,其中包含要轉換的資料。In the .NET Framework, this can be either an XmlNode (typically an XmlDocument), or an XPathDocument containing the data to be transformed.

args
XsltArgumentList XsltArgumentList XsltArgumentList XsltArgumentList

XsltArgumentList,包含用做轉換輸入之符合命名空間的引數。An XsltArgumentList containing the namespace-qualified arguments used as input to the transformation.

output
TextWriter TextWriter TextWriter TextWriter

您要輸出的目標 TextWriterThe TextWriter to which you want to output.

resolver
XmlResolver XmlResolver XmlResolver XmlResolver

XmlResolver 用來解析 XSLT document() 函式。The XmlResolver used to resolve the XSLT document() function. 如果值為 null,則不會解析 document() 函式。If this is null, the document() function is not resolved.

XmlResolverTransform(IXPathNavigable, XsltArgumentList, TextWriter, XmlResolver) 方法完成後不會加以快取。The XmlResolver is not cached after the Transform(IXPathNavigable, XsltArgumentList, TextWriter, XmlResolver) method completes.

例外狀況

處理 XSLT 轉換時發生錯誤。There was an error processing the XSLT transformation.

注意:這是先前版本的行為變更之一。Note: This is a change in behavior from earlier versions. 如果您使用的是 Microsoft .NET Framework 1.1 版 (含) 以前的版本,則會擲回 XsltExceptionAn XsltException is thrown if you are using Microsoft .NET Framework version 1.1 or earlier.

範例

下列範例會轉換 XML 檔案, 並輸出 HTML 字串。The following example transforms an XML file and outputs an HTML string. StringReaderStringWriter類別是用來讀取和寫入字串。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();

  }
}
Imports System.IO
Imports System.Xml
Imports System.Xml.Xsl
Imports System.Xml.XPath

public class Sample 

  public shared sub Main() 
   
    ' Create a string containing the XSLT stylesheet.
    Dim xsltString as 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.
    Dim xslt as XslTransform = new XslTransform()

    ' Load the stylesheet.
    Dim rdr as StringReader = new StringReader(xsltString)
    xslt.Load(new XPathDocument(rdr), nothing, nothing)

    ' Transform the file and output an HTML string.
    Dim HTMLoutput as string
    Dim writer as StringWriter = new StringWriter()
    xslt.Transform(new XPathDocument("books.xml"), nothing, writer, nothing)
    HTMLoutput = writer.ToString()

  end sub
end class

該範例使用 books.xml 檔案做為輸入。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>

備註

注意

XslTransform類別在 .NET Framework 版本2.0 中已過時。The XslTransform class is obsolete in the .NET Framework version 2.0. XslCompiledTransform類別是新的 XSLT 處理器。The XslCompiledTransform class is the new XSLT processor. 如需詳細資訊, 請參閱使用 XslCompiledTransform 類別從 XslTransform 類別進行遷移For more information, see Using the XslCompiledTransform Class and Migrating From the XslTransform Class.

XslTransform支援 XSLT 1.0 語法。XslTransform supports the XSLT 1.0 syntax. XSLT 樣式表單必須包含命名空間xmlns:xsl= http://www.w3.org/1999/XSL/Transform宣告。The XSLT style sheet must include the namespace declaration xmlns:xsl= http://www.w3.org/1999/XSL/Transform.

會與樣式表單中定義的元素相符。xsl:param argsThe args are matched with the xsl:param elements defined in the style sheet.

當輸出xsl:output TextWriter至時, 不支援元素上的編碼屬性。The encoding attribute on an xsl:output element is not supported when outputting to a TextWriter. 需支援哪些xsl:output屬性的詳細資訊, 請參閱 XslTransform 的輸出。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. 若要轉換節點片段, 您必須建立XmlDocument僅包含節點片段的, 並將XmlDocument其傳遞給Transform方法。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);  

此範例會使用library.xmlprint_root.xsl檔案做為輸入, 並將下列輸出至主控台。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>  
另請參閱

Transform(IXPathNavigable, XsltArgumentList, Stream, XmlResolver) Transform(IXPathNavigable, XsltArgumentList, Stream, XmlResolver) Transform(IXPathNavigable, XsltArgumentList, Stream, XmlResolver) Transform(IXPathNavigable, XsltArgumentList, Stream, XmlResolver)

使用指定的 args 來轉換 IXPathNavigable 中的 XML 資料,並將結果輸出至 StreamTransforms the XML data in the IXPathNavigable using the specified args and outputs the result to a Stream.

public:
 void Transform(System::Xml::XPath::IXPathNavigable ^ input, System::Xml::Xsl::XsltArgumentList ^ args, System::IO::Stream ^ output, System::Xml::XmlResolver ^ resolver);
public void Transform (System.Xml.XPath.IXPathNavigable input, System.Xml.Xsl.XsltArgumentList args, System.IO.Stream output, System.Xml.XmlResolver resolver);
member this.Transform : System.Xml.XPath.IXPathNavigable * System.Xml.Xsl.XsltArgumentList * System.IO.Stream * System.Xml.XmlResolver -> unit
Public Sub Transform (input As IXPathNavigable, args As XsltArgumentList, output As Stream, resolver As XmlResolver)

參數

input
IXPathNavigable IXPathNavigable IXPathNavigable IXPathNavigable

實作 IXPathNavigable 介面的物件。An object implementing the IXPathNavigable interface. 在 .NET Framework 中,這可以是 XmlNode (一般為 XmlDocument),或 XPathDocument,其中包含要轉換的資料。In the .NET Framework, this can be either an XmlNode (typically an XmlDocument), or an XPathDocument containing the data to be transformed.

args
XsltArgumentList XsltArgumentList XsltArgumentList XsltArgumentList

XsltArgumentList,包含用做轉換輸入之符合命名空間的引數。An XsltArgumentList containing the namespace-qualified arguments used as input to the transformation.

output
Stream Stream Stream Stream

要輸出至的資料流。The stream to which you want to output.

resolver
XmlResolver XmlResolver XmlResolver XmlResolver

XmlResolver 用來解析 XSLT document() 函式。The XmlResolver used to resolve the XSLT document() function. 如果值為 null,則不會解析 document() 函式。If this is null, the document() function is not resolved.

XmlResolverTransform 方法完成後不會加以快取。The XmlResolver is not cached after the Transform method completes.

例外狀況

處理 XSLT 轉換時發生錯誤。There was an error processing the XSLT transformation.

注意:這是先前版本的行為變更之一。Note: This is a change in behavior from earlier versions. 如果您使用的是 Microsoft .NET Framework 1.1 版 (含) 以前的版本,則會擲回 XsltExceptionAn XsltException is thrown if you are using Microsoft .NET Framework version 1.1 or earlier.

範例

下列範例會在 ASP.NET 網頁中執行轉換。The following example performs a transformation within an ASP.NET page.

<%@ 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>
<%@ Import Namespace="System" %>
<%@ Import Namespace="System.IO" %>
<%@ import NameSpace="System.Web" %>
<%@ Import Namespace="System.Xml.XPath" %>
<%@ Import Namespace="System.Xml.Xsl" %>
<html>
   <script language="VB" runat="server">
      sub Page_Load(sender as Object, e as EventArgs) 

         ' Load the XML document to transform.
         dim doc as XPathDocument = new XPathDocument(Server.MapPath("books.xml"))

         ' Load the stylesheet and perform the transform.
         dim xslt as XslTransform = new XslTransform()
         xslt.Load(Server.MapPath("output.xsl"))
         xslt.Transform(doc, nothing, Response.OutputStream, nothing)
        
      end sub
   </script>
</html>

備註

注意

XslTransform類別在 .NET Framework 版本2.0 中已過時。The XslTransform class is obsolete in the .NET Framework version 2.0. XslCompiledTransform類別是新的 XSLT 處理器。The XslCompiledTransform class is the new XSLT processor. 如需詳細資訊, 請參閱使用 XslCompiledTransform 類別從 XslTransform 類別進行遷移For more information, see Using the XslCompiledTransform Class and Migrating From the XslTransform Class.

XslTransform支援 XSLT 1.0 語法。XslTransform supports the XSLT 1.0 syntax. XSLT 樣式表單必須包含命名空間xmlns:xsl= http://www.w3.org/1999/XSL/Transform宣告。The XSLT style sheet must include the namespace declaration xmlns:xsl= http://www.w3.org/1999/XSL/Transform.

會與樣式表單中定義的元素相符。xsl:param argsThe 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. 若要轉換節點片段, 您必須建立XmlDocument僅包含節點片段的, 並將XmlDocument其傳遞給Transform方法。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);  

此範例會使用library.xmlprint_root.xsl檔案做為輸入, 並將下列輸出至主控台。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>  
另請參閱

Transform(XPathNavigator, XsltArgumentList, XmlWriter) Transform(XPathNavigator, XsltArgumentList, XmlWriter) Transform(XPathNavigator, XsltArgumentList, XmlWriter) Transform(XPathNavigator, XsltArgumentList, XmlWriter)

警告

此 API 現已淘汰。

使用指定的引數來轉換 XPathNavigator 中的 XML 資料,並將結果輸出至 XmlWriterTransforms the XML data in the XPathNavigator using the specified args and outputs the result to an XmlWriter.

public:
 void Transform(System::Xml::XPath::XPathNavigator ^ input, System::Xml::Xsl::XsltArgumentList ^ args, System::Xml::XmlWriter ^ output);
[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);
member this.Transform : System.Xml.XPath.XPathNavigator * System.Xml.Xsl.XsltArgumentList * System.Xml.XmlWriter -> unit
Public Sub Transform (input As XPathNavigator, args As XsltArgumentList, output As XmlWriter)

參數

input
XPathNavigator XPathNavigator XPathNavigator XPathNavigator

包含要轉換資料的 XPathNavigatorAn XPathNavigator containing the data to be transformed.

args
XsltArgumentList XsltArgumentList XsltArgumentList XsltArgumentList

XsltArgumentList,包含用做轉換輸入之符合命名空間的引數。An XsltArgumentList containing the namespace-qualified arguments used as input to the transformation.

output
XmlWriter XmlWriter XmlWriter XmlWriter

您要輸出的目標 XmlWriterThe XmlWriter to which you want to output.

例外狀況

處理 XSLT 轉換時發生錯誤。There was an error processing the XSLT transformation.

注意:這是先前版本的行為變更之一。Note: This is a change in behavior from earlier versions. 如果您使用的是 Microsoft .NET Framework 1.1 版 (含) 以前的版本,則會擲回 XsltExceptionAn XsltException is thrown if you are using Microsoft .NET Framework version 1.1 or earlier.

備註

注意

XslTransform類別在 .NET Framework 版本2.0 中已過時。The XslTransform class is obsolete in the .NET Framework version 2.0. XslCompiledTransform類別是新的 XSLT 處理器。The XslCompiledTransform class is the new XSLT processor. 如需詳細資訊, 請參閱使用 XslCompiledTransform 類別從 XslTransform 類別進行遷移For more information, see Using the XslCompiledTransform Class and Migrating From the XslTransform Class.

XslTransform支援 XSLT 1.0 語法。XslTransform supports the XSLT 1.0 syntax. XSLT 樣式表單必須包含命名空間xmlns:xsl= http://www.w3.org/1999/XSL/Transform宣告。The XSLT style sheet must include the namespace declaration xmlns:xsl= http://www.w3.org/1999/XSL/Transform.

會與樣式表單中定義的元素相符。xsl:param argsThe 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. 在執行轉換之後, XPathNavigator會保持其原始狀態。After the transformation is performed, the XPathNavigator remains in its original state. 這表示在呼叫Transform方法之後, 節點 (在轉換程式之前) 會維持目前的節點。This means that the node, that is current before the transformation process, remains the current node after the Transform method has been called.

xsl:output輸出XmlWriter至(xsl:output會忽略) 時, 不支援元素。The xsl:output element is not supported when outputting to an XmlWriter (xsl:output is ignored). 如需詳細資訊, 請參閱XslTransform 的輸出See Outputs from an XslTransform for more information.

注意

此方法現在已過時。This method is now obsolete. XmlResolver屬性的設定會決定 XSLT document()函數的解析方式。The setting of the XmlResolver property determines how the XSLT document() function is resolved. 建議的作法是使用Transform XmlResolver採用物件做為其其中一個引數的方法。The recommended practice is to use the Transform method which takes an XmlResolver object as one of its arguments.

另請參閱

Transform(XPathNavigator, XsltArgumentList, TextWriter) Transform(XPathNavigator, XsltArgumentList, TextWriter) Transform(XPathNavigator, XsltArgumentList, TextWriter) Transform(XPathNavigator, XsltArgumentList, TextWriter)

警告

此 API 現已淘汰。

使用指定的 args 來轉換 XPathNavigator 中的 XML 資料,並將結果輸出至 TextWriterTransforms the XML data in the XPathNavigator using the specified args and outputs the result to a TextWriter.

public:
 void Transform(System::Xml::XPath::XPathNavigator ^ input, System::Xml::Xsl::XsltArgumentList ^ args, System::IO::TextWriter ^ output);
[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);
member this.Transform : System.Xml.XPath.XPathNavigator * System.Xml.Xsl.XsltArgumentList * System.IO.TextWriter -> unit
Public Sub Transform (input As XPathNavigator, args As XsltArgumentList, output As TextWriter)

參數

input
XPathNavigator XPathNavigator XPathNavigator XPathNavigator

包含要轉換資料的 XPathNavigatorAn XPathNavigator containing the data to be transformed.

args
XsltArgumentList XsltArgumentList XsltArgumentList XsltArgumentList

XsltArgumentList,包含用做轉換輸入之符合命名空間的引數。An XsltArgumentList containing the namespace-qualified arguments used as input to the transformation.

output
TextWriter TextWriter TextWriter TextWriter

您要輸出的目標 TextWriterThe TextWriter to which you want to output.

例外狀況

處理 XSLT 轉換時發生錯誤。There was an error processing the XSLT transformation.

注意:這是先前版本的行為變更之一。Note: This is a change in behavior from earlier versions. 如果您使用的是 Microsoft .NET Framework 1.1 版 (含) 以前的版本,則會擲回 XsltExceptionAn XsltException is thrown if you are using Microsoft .NET Framework version 1.1 or earlier.

備註

注意

XslTransform類別在 .NET Framework 版本2.0 中已過時。The XslTransform class is obsolete in the .NET Framework version 2.0. XslCompiledTransform類別是新的 XSLT 處理器。The XslCompiledTransform class is the new XSLT processor. 如需詳細資訊, 請參閱使用 XslCompiledTransform 類別從 XslTransform 類別進行遷移For more information, see Using the XslCompiledTransform Class and Migrating From the XslTransform Class.

XslTransform支援 XSLT 1.0 語法。XslTransform supports the XSLT 1.0 syntax. XSLT 樣式表單必須包含命名空間xmlns:xsl= http://www.w3.org/1999/XSL/Transform宣告。The XSLT style sheet must include the namespace declaration xmlns:xsl= http://www.w3.org/1999/XSL/Transform.

會與樣式表單中定義的元素相符。xsl:param argsThe 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. 在執行轉換之後, XPathNavigator會保持其原始狀態。After the transformation is performed, the XPathNavigator remains in its original state. 這表示在呼叫Transform方法之後, 節點 (在轉換程式之前是最新的) 會維持目前的節點。This means that the node, which is current before the transformation process, remains the current node after the Transform method has been called.

當輸出xsl:output TextWriter至時, 不支援元素上的編碼屬性。The encoding attribute on an xsl:output element is not supported when outputting to a TextWriter. 需支援哪些xsl:output屬性的詳細資訊, 請參閱 XslTransform 的輸出。See Outputs from an XslTransform for specifics on which xsl:output attributes are supported.

注意

此方法現在已過時。This method is now obsolete. XmlResolver屬性的設定會決定 XSLT document()函數的解析方式。The setting of the XmlResolver property determines how the XSLT document() function is resolved. 建議的作法是使用Transform XmlResolver採用物件做為其其中一個引數的方法。The recommended practice is to use the Transform method which takes an XmlResolver object as one of its arguments.

另請參閱

Transform(String, String, XmlResolver) Transform(String, String, XmlResolver) Transform(String, String, XmlResolver) Transform(String, String, XmlResolver)

在輸入檔案中轉換 XML 資料,並將結果輸出至輸出檔案。Transforms the XML data in the input file and outputs the result to an output file.

public:
 void Transform(System::String ^ inputfile, System::String ^ outputfile, System::Xml::XmlResolver ^ resolver);
public void Transform (string inputfile, string outputfile, System.Xml.XmlResolver resolver);
member this.Transform : string * string * System.Xml.XmlResolver -> unit
Public Sub Transform (inputfile As String, outputfile As String, resolver As XmlResolver)

參數

inputfile
String String String String

要轉換之來源文件的 URL。The URL of the source document to be transformed.

outputfile
String String String String

輸出檔案的 URL。The URL of the output file.

resolver
XmlResolver XmlResolver XmlResolver XmlResolver

XmlResolver 用來解析 XSLT document() 函式。The XmlResolver used to resolve the XSLT document() function. 如果值為 null,則不會解析 document() 函式。If this is null, the document() function is not resolved.

XmlResolverTransform 方法完成後不會加以快取。The XmlResolver is not cached after the Transform method completes.

範例

下列範例會執行 XSLT 轉換並輸出至檔案。The following example performs an XSLT transformation and outputs to a file. 使用XmlUrlResolver預設認證的會解析外部資源。An XmlUrlResolver with default credentials is used resolve an external resources. 如需詳細資訊,請參閱DefaultCredentialsSee DefaultCredentials for details.

// Create a resolver with default credentials.
XmlUrlResolver^ resolver = gcnew XmlUrlResolver;
resolver->Credentials = System::Net::CredentialCache::DefaultCredentials;

// Create the XslTransform object.
XslTransform^ xslt = gcnew XslTransform;

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

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

// 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);


' Create a resolver with default credentials.
Dim resolver as XmlUrlResolver = new XmlUrlResolver()
resolver.Credentials = System.Net.CredentialCache.DefaultCredentials

' Create the XslTransform object.
Dim xslt as XslTransform = new XslTransform()

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

' Transform the file. 
xslt.Transform("books.xml", "titles.xml", resolver)

備註

注意

XslTransform類別在 .NET Framework 版本2.0 中已過時。The XslTransform class is obsolete in the .NET Framework version 2.0. XslCompiledTransform類別是新的 XSLT 處理器。The XslCompiledTransform class is the new XSLT processor. 如需詳細資訊, 請參閱使用 XslCompiledTransform 類別從 XslTransform 類別進行遷移For more information, see Using the XslCompiledTransform Class and Migrating From the XslTransform Class.

XslTransform支援 XSLT 1.0 語法。XslTransform supports the XSLT 1.0 syntax. XSLT 樣式表單必須包含命名空間xmlns:xsl= http://www.w3.org/1999/XSL/Transform宣告。The XSLT style sheet must include the namespace declaration xmlns:xsl= http://www.w3.org/1999/XSL/Transform.

另請參閱

Transform(IXPathNavigable, XsltArgumentList, XmlWriter) Transform(IXPathNavigable, XsltArgumentList, XmlWriter) Transform(IXPathNavigable, XsltArgumentList, XmlWriter) Transform(IXPathNavigable, XsltArgumentList, XmlWriter)

警告

此 API 現已淘汰。

使用指定的 args 來轉換 IXPathNavigable 中的 XML 資料,並將結果輸出至 XmlWriterTransforms the XML data in the IXPathNavigable using the specified args and outputs the result to an XmlWriter.

public:
 void Transform(System::Xml::XPath::IXPathNavigable ^ input, System::Xml::Xsl::XsltArgumentList ^ args, System::Xml::XmlWriter ^ output);
[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);
member this.Transform : System.Xml.XPath.IXPathNavigable * System.Xml.Xsl.XsltArgumentList * System.Xml.XmlWriter -> unit
Public Sub Transform (input As IXPathNavigable, args As XsltArgumentList, output As XmlWriter)

參數

input
IXPathNavigable IXPathNavigable IXPathNavigable IXPathNavigable

實作 IXPathNavigable 介面的物件。An object implementing the IXPathNavigable interface. 在 .NET Framework 中,這可以是 XmlNode (一般為 XmlDocument),或 XPathDocument,其中包含要轉換的資料。In the .NET Framework, this can be either an XmlNode (typically an XmlDocument), or an XPathDocument containing the data to be transformed.

args
XsltArgumentList XsltArgumentList XsltArgumentList XsltArgumentList

XsltArgumentList,包含用做轉換輸入之符合命名空間的引數。An XsltArgumentList containing the namespace-qualified arguments used as input to the transformation.

output
XmlWriter XmlWriter XmlWriter XmlWriter

您要輸出的目標 XmlWriterThe XmlWriter to which you want to output.

例外狀況

處理 XSLT 轉換時發生錯誤。There was an error processing the XSLT transformation.

注意:這是先前版本的行為變更之一。Note: This is a change in behavior from earlier versions. 如果您使用的是 Microsoft .NET Framework 1.1 版 (含) 以前的版本,則會擲回 XsltExceptionAn XsltException is thrown if you are using Microsoft .NET Framework version 1.1 or earlier.

備註

注意

XslTransform類別在 .NET Framework 版本2.0 中已過時。The XslTransform class is obsolete in the .NET Framework version 2.0. XslCompiledTransform類別是新的 XSLT 處理器。The XslCompiledTransform class is the new XSLT processor. 如需詳細資訊, 請參閱使用 XslCompiledTransform 類別從 XslTransform 類別進行遷移For more information, see Using the XslCompiledTransform Class and Migrating From the XslTransform Class.

XslTransform支援 XSLT 1.0 語法。XslTransform supports the XSLT 1.0 syntax. XSLT 樣式表單必須包含命名空間xmlns:xsl= http://www.w3.org/1999/XSL/Transform宣告。The XSLT style sheet must include the namespace declaration xmlns:xsl= http://www.w3.org/1999/XSL/Transform.

注意

此方法現在已過時。This method is now obsolete. XmlResolver屬性的設定會決定 XSLT document()函數的解析方式。The setting of the XmlResolver property determines how the XSLT document() function is resolved. 建議的作法是使用Transform XmlResolver採用物件做為其其中一個引數的方法。The recommended practice is to use the Transform method which takes an XmlResolver object as one of its arguments.

會與樣式表單中定義的元素相符。xsl:param argsThe args are matched with the xsl:param elements defined in the style sheet. xsl:output輸出XmlWriter至(xsl:output會忽略) 時, 不支援元素。The xsl:output element is not supported when outputting to an XmlWriter (xsl:output is ignored). 如需詳細資訊, 請參閱XslTransform 的輸出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. 若要轉換節點片段, 您必須建立XmlDocument僅包含節點片段的, 並將XmlDocument其傳遞給Transform方法。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);  

此範例會使用library.xmlprint_root.xsl檔案做為輸入, 並將下列輸出至主控台。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>  
另請參閱

Transform(IXPathNavigable, XsltArgumentList, XmlResolver) Transform(IXPathNavigable, XsltArgumentList, XmlResolver) Transform(IXPathNavigable, XsltArgumentList, XmlResolver) Transform(IXPathNavigable, XsltArgumentList, XmlResolver)

使用指定的 args 來轉換 IXPathNavigable 中的 XML 資料,並將結果輸出至 XmlReaderTransforms the XML data in the IXPathNavigable using the specified args and outputs the result to an XmlReader.

public:
 System::Xml::XmlReader ^ Transform(System::Xml::XPath::IXPathNavigable ^ input, System::Xml::Xsl::XsltArgumentList ^ args, System::Xml::XmlResolver ^ resolver);
public System.Xml.XmlReader Transform (System.Xml.XPath.IXPathNavigable input, System.Xml.Xsl.XsltArgumentList args, System.Xml.XmlResolver resolver);
member this.Transform : System.Xml.XPath.IXPathNavigable * System.Xml.Xsl.XsltArgumentList * System.Xml.XmlResolver -> System.Xml.XmlReader
Public Function Transform (input As IXPathNavigable, args As XsltArgumentList, resolver As XmlResolver) As XmlReader

參數

input
IXPathNavigable IXPathNavigable IXPathNavigable IXPathNavigable

實作 IXPathNavigable 介面的物件。An object implementing the IXPathNavigable interface. 在 .NET Framework 中,這可以是 XmlNode (一般為 XmlDocument),或 XPathDocument,其中包含要轉換的資料。In the .NET Framework, this can be either an XmlNode (typically an XmlDocument), or an XPathDocument containing the data to be transformed.

args
XsltArgumentList XsltArgumentList XsltArgumentList XsltArgumentList

XsltArgumentList,包含用做轉換輸入之符合命名空間的引數。An XsltArgumentList containing the namespace-qualified arguments used as input to the transformation.

resolver
XmlResolver XmlResolver XmlResolver XmlResolver

XmlResolver 用來解析 XSLT document() 函式。The XmlResolver used to resolve the XSLT document() function. 如果值為 null,則不會解析 document() 函式。If this is null, the document() function is not resolved.

XmlResolverTransform(IXPathNavigable, XsltArgumentList, XmlResolver) 方法完成後不會加以快取。The XmlResolver is not cached after the Transform(IXPathNavigable, XsltArgumentList, XmlResolver) method completes.

傳回

包含轉換結果的 XmlReaderAn XmlReader containing the results of the transformation.

範例

下列範例會轉換 XML 檔, 並將結果輸出至XmlReaderThe following example transforms an XML document and outputs the results to an XmlReader. 具有XmlUrlResolver必要認證的會用來處理任何 XSLT document()函式。An XmlUrlResolver with the necessary credentials is used to process any XSLT document() functions.

// Create the XslTransform object.
XslTransform^ xslt = gcnew XslTransform;

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

// Create a resolver and specify the necessary credentials.
XmlUrlResolver^ resolver = gcnew XmlUrlResolver;
System::Net::NetworkCredential^ myCred;
myCred = gcnew 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 = gcnew XPathDocument( "books.xml" );
XmlReader^ reader = xslt->Transform( doc, nullptr, resolver );

// Load the reader into a new document for more processing.
XmlDocument^ xmldoc = gcnew XmlDocument;
xmldoc->Load( reader );
// 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);
' Create the XslTransform object.
Dim xslt as XslTransform = new XslTransform()

' Load the stylesheet.
xslt.Load("titles.xsl")

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

' Transform thefile using the resolver. The resolver is used
' to process the XSLT document() function.
Dim doc as XPathDocument = new XPathDocument("books.xml")
Dim reader as XmlReader = xslt.Transform(doc, nothing, resolver)

' Load the reader into a new document for more processing.
Dim xmldoc as XmlDocument = new XmlDocument()
xmldoc.Load(reader)

備註

注意

XslTransform類別在 .NET Framework 版本2.0 中已過時。The XslTransform class is obsolete in the .NET Framework version 2.0. XslCompiledTransform類別是新的 XSLT 處理器。The XslCompiledTransform class is the new XSLT processor. 如需詳細資訊, 請參閱使用 XslCompiledTransform 類別從 XslTransform 類別進行遷移For more information, see Using the XslCompiledTransform Class and Migrating From the XslTransform Class.

XslTransform支援 XSLT 1.0 語法。XslTransform supports the XSLT 1.0 syntax. XSLT 樣式表單必須包含命名空間xmlns:xsl= http://www.w3.org/1999/XSL/Transform宣告。The XSLT style sheet must include the namespace declaration xmlns:xsl= http://www.w3.org/1999/XSL/Transform.

會與樣式表單中定義的元素相符。xsl:param argsThe args are matched with the xsl:param elements defined in the style sheet. xsl:output輸出XmlReader至(xsl:output已忽略元素) 時, 不支援元素。The xsl:output element is not supported when outputting to an XmlReader (the xsl:output element is ignored). 如需詳細資訊, 請參閱XslTransform 的輸出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. 若要轉換節點片段, 您必須建立XmlDocument僅包含節點片段的, 並將XmlDocument其傳遞給Transform方法。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);  

此範例會使用library.xmlprint_root.xsl檔案做為輸入, 並將下列輸出至主控台。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>  
另請參閱

Transform(IXPathNavigable, XsltArgumentList, TextWriter) Transform(IXPathNavigable, XsltArgumentList, TextWriter) Transform(IXPathNavigable, XsltArgumentList, TextWriter) Transform(IXPathNavigable, XsltArgumentList, TextWriter)

警告

此 API 現已淘汰。

使用指定的 args 來轉換 IXPathNavigable 中的 XML 資料,並將結果輸出至 TextWriterTransforms the XML data in the IXPathNavigable using the specified args and outputs the result to a TextWriter.

public:
 void Transform(System::Xml::XPath::IXPathNavigable ^ input, System::Xml::Xsl::XsltArgumentList ^ args, System::IO::TextWriter ^ output);
[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);
member this.Transform : System.Xml.XPath.IXPathNavigable * System.Xml.Xsl.XsltArgumentList * System.IO.TextWriter -> unit
Public Sub Transform (input As IXPathNavigable, args As XsltArgumentList, output As TextWriter)

參數

input
IXPathNavigable IXPathNavigable IXPathNavigable IXPathNavigable

實作 IXPathNavigable 介面的物件。An object implementing the IXPathNavigable interface. 在 .NET Framework 中,這可以是 XmlNode (一般為 XmlDocument),或 XPathDocument,其中包含要轉換的資料。In the .NET Framework, this can be either an XmlNode (typically an XmlDocument), or an XPathDocument containing the data to be transformed.

args
XsltArgumentList XsltArgumentList XsltArgumentList XsltArgumentList

XsltArgumentList,包含用做轉換輸入之符合命名空間的引數。An XsltArgumentList containing the namespace-qualified arguments used as input to the transformation.

output
TextWriter TextWriter TextWriter TextWriter

您要輸出的目標 TextWriterThe TextWriter to which you want to output.

例外狀況

處理 XSLT 轉換時發生錯誤。There was an error processing the XSLT transformation.

注意:這是先前版本的行為變更之一。Note: This is a change in behavior from earlier versions. 如果您使用的是 Microsoft .NET Framework 1.1 版 (含) 以前的版本,則會擲回 XsltExceptionAn XsltException is thrown if you are using Microsoft .NET Framework version 1.1 or earlier.

備註

注意

XslTransform類別在 .NET Framework 版本2.0 中已過時。The XslTransform class is obsolete in the .NET Framework version 2.0. XslCompiledTransform類別是新的 XSLT 處理器。The XslCompiledTransform class is the new XSLT processor. 如需詳細資訊, 請參閱使用 XslCompiledTransform 類別從 XslTransform 類別進行遷移For more information, see Using the XslCompiledTransform Class and Migrating From the XslTransform Class.

XslTransform支援 XSLT 1.0 語法。XslTransform supports the XSLT 1.0 syntax. XSLT 樣式表單必須包含命名空間xmlns:xsl= http://www.w3.org/1999/XSL/Transform宣告。The XSLT style sheet must include the namespace declaration xmlns:xsl= http://www.w3.org/1999/XSL/Transform.

注意

此方法現在已過時。This method is now obsolete. XmlResolver屬性的設定會決定 XSLT document()函數的解析方式。The setting of the XmlResolver property determines how the XSLT document() function is resolved. 建議的作法是使用Transform XmlResolver採用物件做為其其中一個引數的方法。The recommended practice is to use the Transform method which takes an XmlResolver object as one of its arguments.

會與樣式表單中定義的元素相符。xsl:param argsThe args are matched with the xsl:param elements defined in the style sheet.

當輸出xsl:output TextWriter至時, 不支援元素上的編碼屬性。The encoding attribute on an xsl:output element is not supported when outputting to a TextWriter. 需支援哪些xsl:output屬性的詳細資訊, 請參閱 XslTransform 的輸出。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. 若要轉換節點片段, 您必須建立XmlDocument僅包含節點片段的, 並將XmlDocument其傳遞給Transform方法。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);  

此範例會使用library.xmlprint_root.xsl檔案做為輸入, 並將下列輸出至主控台。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>  
另請參閱

Transform(IXPathNavigable, XsltArgumentList, Stream) Transform(IXPathNavigable, XsltArgumentList, Stream) Transform(IXPathNavigable, XsltArgumentList, Stream) Transform(IXPathNavigable, XsltArgumentList, Stream)

警告

此 API 現已淘汰。

使用指定的 args 來轉換 IXPathNavigable 中的 XML 資料,並將結果輸出至 StreamTransforms the XML data in the IXPathNavigable using the specified args and outputs the result to a Stream.

public:
 void Transform(System::Xml::XPath::IXPathNavigable ^ input, System::Xml::Xsl::XsltArgumentList ^ args, System::IO::Stream ^ output);
[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);
member this.Transform : System.Xml.XPath.IXPathNavigable * System.Xml.Xsl.XsltArgumentList * System.IO.Stream -> unit
Public Sub Transform (input As IXPathNavigable, args As XsltArgumentList, output As Stream)

參數

input
IXPathNavigable IXPathNavigable IXPathNavigable IXPathNavigable

實作 IXPathNavigable 介面的物件。An object implementing the IXPathNavigable interface. 在 .NET Framework 中,這可以是 XmlNode (一般為 XmlDocument),或 XPathDocument,其中包含要轉換的資料。In the .NET Framework, this can be either an XmlNode (typically an XmlDocument), or an XPathDocument containing the data to be transformed.

args
XsltArgumentList XsltArgumentList XsltArgumentList XsltArgumentList

XsltArgumentList,包含用做轉換輸入之符合命名空間的引數。An XsltArgumentList containing the namespace-qualified arguments used as input to the transformation.

output
Stream Stream Stream Stream

要輸出至的資料流。The stream to which you want to output.

例外狀況

處理 XSLT 轉換時發生錯誤。There was an error processing the XSLT transformation.

注意:這是先前版本的行為變更之一。Note: This is a change in behavior from earlier versions. 如果您使用的是 Microsoft .NET Framework 1.1 版 (含) 以前的版本,則會擲回 XsltExceptionAn XsltException is thrown if you are using Microsoft .NET Framework version 1.1 or earlier.

備註

注意

XslTransform類別在 .NET Framework 版本2.0 中已過時。The XslTransform class is obsolete in the .NET Framework version 2.0. XslCompiledTransform類別是新的 XSLT 處理器。The XslCompiledTransform class is the new XSLT processor. 如需詳細資訊, 請參閱使用 XslCompiledTransform 類別從 XslTransform 類別進行遷移For more information, see Using the XslCompiledTransform Class and Migrating From the XslTransform Class.

XslTransform支援 XSLT 1.0 語法。XslTransform supports the XSLT 1.0 syntax. XSLT 樣式表單必須包含命名空間xmlns:xsl= http://www.w3.org/1999/XSL/Transform宣告。The XSLT style sheet must include the namespace declaration xmlns:xsl= http://www.w3.org/1999/XSL/Transform.

注意

此方法現在已過時。This method is now obsolete. XmlResolver屬性的設定會決定 XSLT document()函數的解析方式。The setting of the XmlResolver property determines how the XSLT document() function is resolved. 建議的作法是使用Transform XmlResolver採用物件做為其其中一個引數的方法。The recommended practice is to use the Transform method which takes an XmlResolver object as one of its arguments.

會與樣式表單中定義的元素相符。xsl:param argsThe 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. 若要轉換節點片段, 您必須建立XmlDocument僅包含節點片段的, 並將XmlDocument其傳遞給Transform方法。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);  

此範例會使用library.xmlprint_root.xsl檔案做為輸入, 並將下列輸出至主控台。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>  
另請參閱

Transform(XPathNavigator, XsltArgumentList, XmlWriter, XmlResolver) Transform(XPathNavigator, XsltArgumentList, XmlWriter, XmlResolver) Transform(XPathNavigator, XsltArgumentList, XmlWriter, XmlResolver) Transform(XPathNavigator, XsltArgumentList, XmlWriter, XmlResolver)

使用指定的引數來轉換 XPathNavigator 中的 XML 資料,並將結果輸出至 XmlWriterTransforms the XML data in the XPathNavigator using the specified args and outputs the result to an XmlWriter.

public:
 void Transform(System::Xml::XPath::XPathNavigator ^ input, System::Xml::Xsl::XsltArgumentList ^ args, System::Xml::XmlWriter ^ output, System::Xml::XmlResolver ^ resolver);
public void Transform (System.Xml.XPath.XPathNavigator input, System.Xml.Xsl.XsltArgumentList args, System.Xml.XmlWriter output, System.Xml.XmlResolver resolver);
member this.Transform : System.Xml.XPath.XPathNavigator * System.Xml.Xsl.XsltArgumentList * System.Xml.XmlWriter * System.Xml.XmlResolver -> unit
Public Sub Transform (input As XPathNavigator, args As XsltArgumentList, output As XmlWriter, resolver As XmlResolver)

參數

input
XPathNavigator XPathNavigator XPathNavigator XPathNavigator

包含要轉換資料的 XPathNavigatorAn XPathNavigator containing the data to be transformed.

args
XsltArgumentList XsltArgumentList XsltArgumentList XsltArgumentList

XsltArgumentList,包含用做轉換輸入之符合命名空間的引數。An XsltArgumentList containing the namespace-qualified arguments used as input to the transformation.

output
XmlWriter XmlWriter XmlWriter XmlWriter

您要輸出的目標 XmlWriterThe XmlWriter to which you want to output.

resolver
XmlResolver XmlResolver XmlResolver XmlResolver

XmlResolver 用來解析 XSLT document() 函式。The XmlResolver used to resolve the XSLT document() function. 如果值為 null,則不會解析 document() 函式。If this is null, the document() function is not resolved.

XmlResolverTransform(XPathNavigator, XsltArgumentList, XmlWriter, XmlResolver) 方法完成後不會加以快取。The XmlResolver is not cached after the Transform(XPathNavigator, XsltArgumentList, XmlWriter, XmlResolver) method completes.

例外狀況

處理 XSLT 轉換時發生錯誤。There was an error processing the XSLT transformation.

注意:這是先前版本的行為變更之一。Note: This is a change in behavior from earlier versions. 如果您使用的是 Microsoft .NET Framework 1.1 版 (含) 以前的版本,則會擲回 XsltExceptionAn XsltException is thrown if you are using Microsoft .NET Framework version 1.1 or earlier.

範例

下列範例會先載入和編輯 XML 檔, 然後再執行 XSLT 轉換。The following example loads and edits an XML document before performing an XSLT transform.

XmlDocument^ doc = gcnew 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 = gcnew XslTransform;
xslt->Load( "output.xsl" );
XmlTextWriter^ writer = gcnew XmlTextWriter( "books.html", nullptr );
xslt->Transform( nav, nullptr, writer, nullptr);
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);
Dim doc as XmlDocument = new XmlDocument()
doc.Load("books.xml")

' Modify the XML file.
Dim root as XmlElement = doc.DocumentElement
root.FirstChild.LastChild.InnerText = "12.95"

' Create an XPathNavigator to use for the transform.
Dim nav as XPathNavigator = root.CreateNavigator()

' Transform the file.
Dim xslt as XslTransform = new XslTransform()
xslt.Load("output.xsl")
Dim writer as XmlTextWriter = new XmlTextWriter("books.html", nothing)
xslt.Transform(nav,nothing, writer, nothing)

備註

注意

XslTransform類別在 .NET Framework 版本2.0 中已過時。The XslTransform class is obsolete in the .NET Framework version 2.0. XslCompiledTransform類別是新的 XSLT 處理器。The XslCompiledTransform class is the new XSLT processor. 如需詳細資訊, 請參閱使用 XslCompiledTransform 類別從 XslTransform 類別進行遷移For more information, see Using the XslCompiledTransform Class and Migrating From the XslTransform Class.

XslTransform支援 XSLT 1.0 語法。XslTransform supports the XSLT 1.0 syntax. XSLT 樣式表單必須包含命名空間xmlns:xsl= http://www.w3.org/1999/XSL/Transform宣告。The XSLT style sheet must include the namespace declaration xmlns:xsl= http://www.w3.org/1999/XSL/Transform.

會與樣式表單中定義的元素相符。xsl:param argsThe 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. 在執行轉換之後, XPathNavigator會保持其原始狀態。After the transformation is performed, the XPathNavigator remains in its original state. 這表示在呼叫Transform方法之後, 節點 (在轉換程式之前) 會維持目前的節點。This means that the node, that is current before the transformation process, remains the current node after the Transform method has been called.

xsl:output輸出XmlWriter至(xsl:output會忽略) 時, 不支援元素。The xsl:output element is not supported when outputting to an XmlWriter (xsl:output is ignored). 如需詳細資訊, 請參閱XslTransform 的輸出See Outputs from an XslTransform for more information.

另請參閱

Transform(XPathNavigator, XsltArgumentList) Transform(XPathNavigator, XsltArgumentList) Transform(XPathNavigator, XsltArgumentList) Transform(XPathNavigator, XsltArgumentList)

警告

此 API 現已淘汰。

使用指定的 args 來轉換 XPathNavigator 中的 XML 資料,並將結果輸出至 XmlReaderTransforms the XML data in the XPathNavigator using the specified args and outputs the result to an XmlReader.

public:
 System::Xml::XmlReader ^ Transform(System::Xml::XPath::XPathNavigator ^ input, System::Xml::Xsl::XsltArgumentList ^ args);
[System.Obsolete("You should pass XmlResolver to Transform() method")]
public System.Xml.XmlReader Transform (System.Xml.XPath.XPathNavigator input, System.Xml.Xsl.XsltArgumentList args);
member this.Transform : System.Xml.XPath.XPathNavigator * System.Xml.Xsl.XsltArgumentList -> System.Xml.XmlReader
Public Function Transform (input As XPathNavigator, args As XsltArgumentList) As XmlReader

參數

input
XPathNavigator XPathNavigator XPathNavigator XPathNavigator

包含要轉換資料的 XPathNavigatorAn XPathNavigator containing the data to be transformed.

args
XsltArgumentList XsltArgumentList XsltArgumentList XsltArgumentList

XsltArgumentList,包含用做轉換輸入之符合命名空間的引數。An XsltArgumentList containing the namespace-qualified arguments used as input to the transformation.

傳回

包含轉換結果的 XmlReaderAn XmlReader containing the results of the transformation.

例外狀況

處理 XSLT 轉換時發生錯誤。There was an error processing the XSLT transformation.

注意:這是先前版本的行為變更之一。Note: This is a change in behavior from earlier versions. 如果您使用的是 Microsoft .NET Framework 1.1 版 (含) 以前的版本,則會擲回 XsltExceptionAn XsltException is thrown if you are using Microsoft .NET Framework version 1.1 or earlier.

備註

注意

XslTransform類別在 .NET Framework 版本2.0 中已過時。The XslTransform class is obsolete in the .NET Framework version 2.0. XslCompiledTransform類別是新的 XSLT 處理器。The XslCompiledTransform class is the new XSLT processor. 如需詳細資訊, 請參閱使用 XslCompiledTransform 類別從 XslTransform 類別進行遷移For more information, see Using the XslCompiledTransform Class and Migrating From the XslTransform Class.

XslTransform支援 XSLT 1.0 語法。XslTransform supports the XSLT 1.0 syntax. XSLT 樣式表單必須包含命名空間xmlns:xsl= http://www.w3.org/1999/XSL/Transform宣告。The XSLT style sheet must include the namespace declaration xmlns:xsl= http://www.w3.org/1999/XSL/Transform.

會與樣式表單中定義的元素相符。xsl:param argsThe 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. 在執行轉換之後, XPathNavigator會保持其原始狀態。After the transformation has been performed, the XPathNavigator remains in its original state. 這表示在呼叫Transform方法之後, 節點 (在轉換程式之前是最新的) 會維持目前的節點。This means that the node, which is current before the transformation process, remains the current node after the Transform method has been called.

因為XmlReader提供唯讀輸出xsl:output , 所以會忽略元素。Because XmlReader provides read-only output, the xsl:output element is ignored. 如需詳細資訊, 請參閱XslTransform 的輸出See Outputs from an XslTransform for more information.

這個方法可讓您進行來源文件的非同步轉換。This method enables you to do an asynchronous transformation of the source document.

注意

這個方法已過時。This method is obsolete. XmlResolver屬性的設定會決定 XSLT document () 函數的解析方式。The setting of the XmlResolver property determines how the XSLT document() function is resolved. 建議的作法是使用Transform XmlResolver採用物件做為其其中一個引數的方法。The recommended practice is to use the Transform method which takes an XmlResolver object as one of its arguments.

另請參閱

Transform(IXPathNavigable, XsltArgumentList) Transform(IXPathNavigable, XsltArgumentList) Transform(IXPathNavigable, XsltArgumentList) Transform(IXPathNavigable, XsltArgumentList)

警告

此 API 現已淘汰。

使用指定的 args 來轉換 IXPathNavigable 中的 XML 資料,並將結果輸出至 XmlReaderTransforms the XML data in the IXPathNavigable using the specified args and outputs the result to an XmlReader.

public:
 System::Xml::XmlReader ^ Transform(System::Xml::XPath::IXPathNavigable ^ input, System::Xml::Xsl::XsltArgumentList ^ args);
[System.Obsolete("You should pass XmlResolver to Transform() method")]
public System.Xml.XmlReader Transform (System.Xml.XPath.IXPathNavigable input, System.Xml.Xsl.XsltArgumentList args);
member this.Transform : System.Xml.XPath.IXPathNavigable * System.Xml.Xsl.XsltArgumentList -> System.Xml.XmlReader
Public Function Transform (input As IXPathNavigable, args As XsltArgumentList) As XmlReader

參數

input
IXPathNavigable IXPathNavigable IXPathNavigable IXPathNavigable

實作 IXPathNavigable 介面的物件。An object implementing the IXPathNavigable interface. 在 .NET Framework 中,這可以是 XmlNode (一般為 XmlDocument),或 XPathDocument,其中包含要轉換的資料。In the .NET Framework, this can be either an XmlNode (typically an XmlDocument), or an XPathDocument containing the data to be transformed.

args
XsltArgumentList XsltArgumentList XsltArgumentList XsltArgumentList

XsltArgumentList,包含用做轉換輸入之符合命名空間的引數。An XsltArgumentList containing the namespace-qualified arguments used as input to the transformation.

傳回

包含轉換結果的 XmlReaderAn XmlReader containing the results of the transformation.

備註

注意

XslTransform類別在 .NET Framework 版本2.0 中已過時。The XslTransform class is obsolete in the .NET Framework version 2.0. XslCompiledTransform類別是新的 XSLT 處理器。The XslCompiledTransform class is the new XSLT processor. 如需詳細資訊, 請參閱使用 XslCompiledTransform 類別從 XslTransform 類別進行遷移For more information, see Using the XslCompiledTransform Class and Migrating From the XslTransform Class.

XslTransform支援 XSLT 1.0 語法。XslTransform supports the XSLT 1.0 syntax. XSLT 樣式表單必須包含命名空間xmlns:xsl= http://www.w3.org/1999/XSL/Transform宣告。The XSLT style sheet must include the namespace declaration xmlns:xsl= http://www.w3.org/1999/XSL/Transform.

注意

此方法現在已過時。This method is now obsolete. XmlResolver屬性的設定會決定 XSLT document()函數的解析方式。The setting of the XmlResolver property determines how the XSLT document() function is resolved. 建議的作法是使用Transform XmlResolver採用物件做為其其中一個引數的方法。The recommended practice is to use the Transform method which takes an XmlResolver object as one of its arguments.

會與樣式表單中定義的元素相符。xsl:param argsThe args are matched with the xsl:param elements defined in the style sheet. xsl:output輸出XmlReader至(xsl:output已忽略元素) 時, 不支援元素。The xsl:output element is not supported when outputting to an XmlReader (the xsl:output element is ignored). 如需詳細資訊, 請參閱XslTransform 的輸出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. 若要轉換節點片段, 您必須建立XmlDocument僅包含節點片段的, 並將XmlDocument其傳遞給Transform方法。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);  

此範例會使用library.xmlprint_root.xsl檔案做為輸入, 並將下列輸出至主控台。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>  
另請參閱

Transform(String, String) Transform(String, String) Transform(String, String) Transform(String, String)

警告

此 API 現已淘汰。

在輸入檔案中轉換 XML 資料,並將結果輸出至輸出檔案。Transforms the XML data in the input file and outputs the result to an output file.

public:
 void Transform(System::String ^ inputfile, System::String ^ outputfile);
[System.Obsolete("You should pass XmlResolver to Transform() method")]
public void Transform (string inputfile, string outputfile);
member this.Transform : string * string -> unit
Public Sub Transform (inputfile As String, outputfile As String)

參數

inputfile
String String String String

要轉換之來源文件的 URL。The URL of the source document to be transformed.

outputfile
String String String String

輸出檔案的 URL。The URL of the output file.

範例

下列範例會output.xsl使用樣式books.xml表單轉換檔案, 並將結果books.html輸出至檔案。The following example transforms the books.xml file using the output.xsl style sheet and outputs the results to the books.html file.

// Create the XslTransform object.
XslTransform^ xslt = gcnew XslTransform;

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

// Transform the file.
xslt->Transform("books.xml","books.html");
//Create the XslTransform object.
XslTransform xslt = new XslTransform();

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

//Transform the file.
xslt.Transform("books.xml", "books.html");
'Create the XslTransform object.
Dim xslt as XslTransform = 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 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>

備註

注意

XslTransform類別在 .NET Framework 版本2.0 中已過時。The XslTransform class is obsolete in the .NET Framework version 2.0. XslCompiledTransform類別是新的 XSLT 處理器。The XslCompiledTransform class is the new XSLT processor. 如需詳細資訊, 請參閱使用 XslCompiledTransform 類別從 XslTransform 類別進行遷移For more information, see Using the XslCompiledTransform Class and Migrating From the XslTransform Class.

XslTransform支援 XSLT 1.0 語法。XslTransform supports the XSLT 1.0 syntax. XSLT 樣式表單必須包含命名空間xmlns:xsl= http://www.w3.org/1999/XSL/Transform宣告。The XSLT style sheet must include the namespace declaration xmlns:xsl= http://www.w3.org/1999/XSL/Transform.

注意

這個方法在1.1 版的 .NET Framework 中已過時。This method is obsolete in version 1.1 of the .NET Framework. XmlResolver屬性的設定會決定 XSLT document()函數的解析方式。The setting of the XmlResolver property determines how the XSLT document() function is resolved. 建議的作法是使用Transform XmlResolver採用物件做為其其中一個引數的方法。The recommended practice is to use the Transform method which takes an XmlResolver object as one of its arguments.

另請參閱

Transform(XPathNavigator, XsltArgumentList, Stream) Transform(XPathNavigator, XsltArgumentList, Stream) Transform(XPathNavigator, XsltArgumentList, Stream) Transform(XPathNavigator, XsltArgumentList, Stream)

警告

此 API 現已淘汰。

使用指定的 args 來轉換 XPathNavigator 中的 XML 資料,並將結果輸出至 StreamTransforms the XML data in the XPathNavigator using the specified args and outputs the result to a Stream.

public:
 void Transform(System::Xml::XPath::XPathNavigator ^ input, System::Xml::Xsl::XsltArgumentList ^ args, System::IO::Stream ^ output);
[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);
member this.Transform : System.Xml.XPath.XPathNavigator * System.Xml.Xsl.XsltArgumentList * System.IO.Stream -> unit
Public Sub Transform (input As XPathNavigator, args As XsltArgumentList, output As Stream)

參數

input
XPathNavigator XPathNavigator XPathNavigator XPathNavigator

包含要轉換資料的 XPathNavigatorAn XPathNavigator containing the data to be transformed.

args
XsltArgumentList XsltArgumentList XsltArgumentList XsltArgumentList

XsltArgumentList,包含用做轉換輸入之符合命名空間的引數。An XsltArgumentList containing the namespace-qualified arguments used as input to the transformation.

output
Stream Stream Stream Stream

要輸出至的資料流。The stream to which you want to output.

例外狀況

處理 XSLT 轉換時發生錯誤。There was an error processing the XSLT transformation.

注意:這是先前版本的行為變更之一。Note: This is a change in behavior from earlier versions. 如果您使用的是 Microsoft .NET Framework 1.1 版 (含) 以前的版本,則會擲回 XsltExceptionAn XsltException is thrown if you are using Microsoft .NET Framework version 1.1 or earlier.

備註

注意

XslTransform類別在 .NET Framework 版本2.0 中已過時。The XslTransform class is obsolete in the .NET Framework version 2.0. XslCompiledTransform類別是新的 XSLT 處理器。The XslCompiledTransform class is the new XSLT processor. 如需詳細資訊, 請參閱使用 XslCompiledTransform 類別從 XslTransform 類別進行遷移For more information, see Using the XslCompiledTransform Class and Migrating From the XslTransform Class.

XslTransform支援 XSLT 1.0 語法。XslTransform supports the XSLT 1.0 syntax. XSLT 樣式表單必須包含命名空間xmlns:xsl= http://www.w3.org/1999/XSL/Transform宣告。The XSLT style sheet must include the namespace declaration xmlns:xsl= http://www.w3.org/1999/XSL/Transform.

會與樣式表單中定義的元素相符。xsl:param argsThe 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. 在執行轉換之後, XPathNavigator會保持其原始狀態。After the transformation has been performed, the XPathNavigator remains in its original state. 這表示在呼叫Transform方法之後, 節點 (在轉換程式之前是最新的) 會維持目前的節點。This means that the node, which is current before the transformation process, remains the current node after the Transform method has been called.

需支援哪些xsl:output屬性的詳細資訊, 請參閱 XslTransform 的輸出。See Outputs from an XslTransform for specifics on which xsl:output attributes are supported.

注意

此方法現在已過時。This method is now obsolete. XmlResolver屬性的設定會決定 XSLT document()函數的解析方式。The setting of the XmlResolver property determines how the XSLT document() function is resolved. 建議的作法是使用Transform XmlResolver採用物件做為其其中一個引數的方法。The recommended practice is to use the Transform method which takes an XmlResolver object as one of its arguments.

另請參閱

適用於