XslCompiledTransform 類別

定義

使用 XSLT 樣式表轉換 XML 資料。Transforms XML data using an XSLT style sheet.

public ref class XslCompiledTransform sealed
public sealed class XslCompiledTransform
type XslCompiledTransform = class
Public NotInheritable Class XslCompiledTransform
繼承
XslCompiledTransform

範例

下列範例會執行轉換並輸出至檔案。The following example executes a transform and outputs to a file.

// Load the style sheet.
XslCompiledTransform xslt = new XslCompiledTransform();
xslt.Load("output.xsl");

// Execute the transform and output the results to a file.
xslt.Transform("books.xml", "books.html");
' Load the style sheet.
Dim xslt As New XslCompiledTransform()
xslt.Load("output.xsl")
        
' Execute the transform and output the results to a 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>

備註

XslCompiledTransform類別是支援 xslt 1.0 語法的 xslt 處理器。The XslCompiledTransform class is an XSLT processor that supports the XSLT 1.0 syntax. 這是新的實作為,相較于淘汰的類別,它包含了效能提升 XslTransformIt is a new implementation and includes performance gains when compared to the obsolete XslTransform class. 類別的結構與 XslCompiledTransform 類別非常類似 XslTransformThe structure of the XslCompiledTransform class is very similar to the XslTransform class. Load方法會載入和編譯樣式表單,而方法會 Transform 執行 XSLT 轉換。The Load method loads and compiles the style sheet, while the Transform method executes the XSLT transform.

document()預設會停用 XSLT 函數和內嵌腳本區塊的支援。Support for the XSLT document() function and embedded script blocks are disabled by default. 您可以藉由建立 XsltSettings 物件並將其傳遞給方法,來啟用這些功能 LoadThese features can be enabled by creating an XsltSettings object and passing it to the Load method.

如需詳細資訊,請參閱 使用 XslCompiledTransform 類別從 XslTransform 類別進行遷移For more information, see Using the XslCompiledTransform Class and Migrating From the XslTransform Class.

安全性考量Security Considerations

建立使用 XslCompiledTransform 類別的應用程式時,您應留意下列項目及其含意:When creating an application that uses the XslCompiledTransform class, you should be aware of the following items and their implications:

  • 依預設會停用 XSLT 指令碼。XSLT scripting is disabled by default. 僅當需要指令碼支援且在完全受信任的環境中運作時,才應啟用 XSLT 指令碼。XSLT scripting should be enabled only if you require script support and you are working in a fully trusted environment.

  • 依預設會停用 XSLT document() 函式。The XSLT document() function is disabled by default. 如果您啟用 document() 函式,則藉由傳遞 XmlSecureResolver 物件至 Transform 方法,限制可存取的資源。If you enable the document() function, restrict the resources that can be accessed by passing an XmlSecureResolver object to the Transform method.

  • 依預設會啟用擴充物件。Extension objects are enabled by default. 如果將包含擴充物件的 XsltArgumentList 物件傳遞到 Transform 方法,則會加以利用。If an XsltArgumentList object containing extension objects is passed to the Transform method, they are utilized.

  • XSLT 樣式表可以包含其他檔案及內嵌指令碼區塊的參考。XSLT style sheets can include references to other files and embedded script blocks. 居心不良的使用者會利用這一點,藉由提供您資料或樣式表,使您因執行他們而導致系統持續處理,直到電腦資源不足,來進行攻擊。A malicious user can exploit this by supplying you with data or style sheets that when executed can cause your system to process until the computer runs low on resources.

  • 在混合的信任環境中執行的 XSLT 應用程式,會產生用於詐騙的樣式表。XSLT applications that run in a mixed trust environment can result in style sheet spoofing. 例如,居心不良的使用者可載入含有害樣式表的物件,然後將其傳遞給另一使用者,該使用者隨後會呼叫 Transform 方法並執行轉換。For example, a malicious user can load an object with a harmful style sheet and hand it off to another user who subsequently calls the Transform method and executes the transformation.

不啟用指令碼或 document() 函式 (除非樣式表來自信任的來源),不接受來自不受信任來源的 XslCompiledTransform 物件、XSLT 樣式表或 XML 來源資料,可減輕這些安全性問題。These security issues can be mitigated by not enabling scripting or the document() function unless the style sheet comes from a trusted source, and by not accepting XslCompiledTransform objects, XSLT style sheets, or XML source data from an untrusted source.

建構函式

XslCompiledTransform()

初始化 XslCompiledTransform 類別的新執行個體。Initializes a new instance of the XslCompiledTransform class.

XslCompiledTransform(Boolean)

使用指定的偵錯設定,初始化 XslCompiledTransform 類別的新執行個體。Initializes a new instance of the XslCompiledTransform class with the specified debug setting.

屬性

OutputSettings

取得 XmlWriterSettings 物件,其中包含衍生自樣式表之 xsl:output 元素的輸出資訊。Gets an XmlWriterSettings object that contains the output information derived from the xsl:output element of the style sheet.

TemporaryFiles

取得 TempFileCollection,包含成功呼叫 Load 方法後,在磁碟上產生的暫存檔案。Gets the TempFileCollection that contains the temporary files generated on disk after a successful call to the Load method.

方法

CompileToType(XmlReader, XsltSettings, XmlResolver, Boolean, TypeBuilder, String)

將 XSLT 樣式表編譯為指定的類型。Compiles an XSLT style sheet to a specified type.

Equals(Object)

判斷指定的物件是否等於目前的物件。Determines whether the specified object is equal to the current object.

(繼承來源 Object)
GetHashCode()

做為預設雜湊函式。Serves as the default hash function.

(繼承來源 Object)
GetType()

取得目前執行個體的 TypeGets the Type of the current instance.

(繼承來源 Object)
Load(IXPathNavigable)

編譯包含在 IXPathNavigable 物件中的樣式表。Compiles the style sheet contained in the IXPathNavigable object.

Load(IXPathNavigable, XsltSettings, XmlResolver)

編譯包含在 IXPathNavigable 中的 XSLT 樣式表。Compiles the XSLT style sheet contained in the IXPathNavigable. XmlResolver 會解析所有 XSLT importinclude 元素,且 XSLT 設定會決定樣式表的權限。The XmlResolver resolves any XSLT import or include elements and the XSLT settings determine the permissions for the style sheet.

Load(MethodInfo, Byte[], Type[])

從使用 XSLTC.exe 公用程式編譯的樣式表中載入方法。Loads a method from a style sheet compiled using the XSLTC.exe utility.

Load(String)

載入並編譯位於指定之 URI 的樣式表。Loads and compiles the style sheet located at the specified URI.

Load(String, XsltSettings, XmlResolver)

載入並編譯 URI 所指定的 XSLT 樣式表。Loads and compiles the XSLT style sheet specified by the URI. XmlResolver 會解析所有 XSLT importinclude 元素,且 XSLT 設定會決定樣式表的權限。The XmlResolver resolves any XSLT import or include elements and the XSLT settings determine the permissions for the style sheet.

Load(Type)

載入已編譯的樣式表,此樣式表是使用 XSLT 編譯器 (xsltc.exe)所建立的。Loads the compiled style sheet that was created using the XSLT Compiler (xsltc.exe).

Load(XmlReader)

編譯包含在 XmlReader 中的樣式表。Compiles the style sheet contained in the XmlReader.

Load(XmlReader, XsltSettings, XmlResolver)

編譯包含在 XmlReader 中的 XSLT 樣式表。Compiles the XSLT style sheet contained in the XmlReader. XmlResolver 會解析所有 XSLT importinclude 元素,且 XSLT 設定會決定樣式表的權限。The XmlResolver resolves any XSLT import or include elements and the XSLT settings determine the permissions for the style sheet.

MemberwiseClone()

建立目前 Object 的淺層複製。Creates a shallow copy of the current Object.

(繼承來源 Object)
ToString()

傳回代表目前物件的字串。Returns a string that represents the current object.

(繼承來源 Object)
Transform(IXPathNavigable, XmlWriter)

使用 IXPathNavigable 物件所指定的輸入文件執行轉換,並將結果輸出至 XmlWriterExecutes the transform using the input document specified by the IXPathNavigable object and outputs the results to an XmlWriter.

Transform(IXPathNavigable, XsltArgumentList, Stream)

使用 IXPathNavigable 物件所指定的輸入文件執行轉換,並將結果輸出至資料流。Executes the transform using the input document specified by the IXPathNavigable object and outputs the results to a stream. XsltArgumentList 會提供額外的執行階段引數。The XsltArgumentList provides additional runtime arguments.

Transform(IXPathNavigable, XsltArgumentList, TextWriter)

使用 IXPathNavigable 物件所指定的輸入文件執行轉換,並將結果輸出至 TextWriterExecutes the transform using the input document specified by the IXPathNavigable object and outputs the results to an TextWriter. XsltArgumentList 會提供額外的執行階段引數。The XsltArgumentList provides additional run-time arguments.

Transform(IXPathNavigable, XsltArgumentList, XmlWriter)

使用 IXPathNavigable 物件所指定的輸入文件執行轉換,並將結果輸出至 XmlWriterExecutes the transform using the input document specified by the IXPathNavigable object and outputs the results to an XmlWriter. XsltArgumentList 會提供額外的執行階段引數。The XsltArgumentList provides additional run-time arguments.

Transform(IXPathNavigable, XsltArgumentList, XmlWriter, XmlResolver)

使用 IXPathNavigable 物件所指定的輸入文件執行轉換,並將結果輸出至 XmlWriterExecutes the transform by using the input document that is specified by the IXPathNavigable object and outputs the results to an XmlWriter. XsltArgumentList 會提供額外的執行階段引數,且 XmlResolver 會解析 XSLT document() 函式。The XsltArgumentList provides additional run-time arguments and the XmlResolver resolves the XSLT document() function.

Transform(String, String)

使用 URI 所指定的輸入文件執行轉換,並將結果輸出至檔案。Executes the transform using the input document specified by the URI and outputs the results to a file.

Transform(String, XmlWriter)

使用 URI 所指定的輸入文件執行轉換,並將結果輸出至 XmlWriterExecutes the transform using the input document specified by the URI and outputs the results to an XmlWriter.

Transform(String, XsltArgumentList, Stream)

使用 URI 所指定的輸入文件執行轉換,並將結果輸出至資料流。Executes the transform using the input document specified by the URI and outputs the results to stream. XsltArgumentList 會提供額外的執行階段引數。The XsltArgumentList provides additional run-time arguments.

Transform(String, XsltArgumentList, TextWriter)

使用 URI 所指定的輸入文件執行轉換,並將結果輸出至 TextWriterExecutes the transform using the input document specified by the URI and outputs the results to a TextWriter.

Transform(String, XsltArgumentList, XmlWriter)

使用 URI 所指定的輸入文件執行轉換,並將結果輸出至 XmlWriterExecutes the transform using the input document specified by the URI and outputs the results to an XmlWriter. XsltArgumentList 會提供額外的執行階段引數。The XsltArgumentList provides additional run-time arguments.

Transform(XmlReader, XmlWriter)

使用 XmlReader 物件所指定的輸入文件執行轉換,並將結果輸出至 XmlWriterExecutes the transform using the input document specified by the XmlReader object and outputs the results to an XmlWriter.

Transform(XmlReader, XsltArgumentList, Stream)

使用 XmlReader 物件所指定的輸入文件執行轉換,並將結果輸出至資料流。Executes the transform using the input document specified by the XmlReader object and outputs the results to a stream. XsltArgumentList 會提供額外的執行階段引數。The XsltArgumentList provides additional run-time arguments.

Transform(XmlReader, XsltArgumentList, TextWriter)

使用 XmlReader 物件所指定的輸入文件執行轉換,並將結果輸出至 TextWriterExecutes the transform using the input document specified by the XmlReader object and outputs the results to a TextWriter. XsltArgumentList 會提供額外的執行階段引數。The XsltArgumentList provides additional run-time arguments.

Transform(XmlReader, XsltArgumentList, XmlWriter)

使用 XmlReader 物件所指定的輸入文件執行轉換,並將結果輸出至 XmlWriterExecutes the transform using the input document specified by the XmlReader object and outputs the results to an XmlWriter. XsltArgumentList 會提供額外的執行階段引數。The XsltArgumentList provides additional run-time arguments.

Transform(XmlReader, XsltArgumentList, XmlWriter, XmlResolver)

使用 XmlReader 物件所指定的輸入文件執行轉換,並將結果輸出至 XmlWriterExecutes the transform using the input document specified by the XmlReader object and outputs the results to an XmlWriter. XsltArgumentList 會提供額外的執行階段引數,且 XmlResolver 會解析 XSLT document() 函式。The XsltArgumentList provides additional run-time arguments and the XmlResolver resolves the XSLT document() function.

適用於

執行緒安全性

XslCompiledTransform物件載入後,即為安全線程。The XslCompiledTransform object is thread safe once it has been loaded. 換句話說,在 Load 方法順利完成之後, Transform 可以從多個執行緒同時呼叫方法。In other words, after the Load method has successfully completed, the Transform method can be called simultaneously from multiple threads.

如果在 Load 另一個執行緒中呼叫方法時,在一個執行緒中再次呼叫方法 Transform ,則 XslCompiledTransform 物件會繼續使用舊的狀態,以完成執行 Transform 呼叫。If the Load method is called again in one thread while the Transform method is being called in another thread, the XslCompiledTransform object finishes executing the Transform call by continuing to use the old state. 當方法成功完成時,就會使用新的狀態 LoadThe new state is used when the Load method successfully completes.


Load從多個執行緒同時呼叫方法時,方法不是安全線程。The Load method is not thread safe when called simultaneously from multiple threads.

另請參閱