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.

另请参阅