XslCompiledTransform 类
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
使用 XSLT 样式表转换 XML 数据。
public ref class XslCompiledTransform sealed
[System.Diagnostics.CodeAnalysis.RequiresDynamicCode("XslCompiledTransform requires dynamic code because it generates IL at runtime.")]
public sealed class XslCompiledTransform
public sealed class XslCompiledTransform
[<System.Diagnostics.CodeAnalysis.RequiresDynamicCode("XslCompiledTransform requires dynamic code because it generates IL at runtime.")>]
type XslCompiledTransform = class
type XslCompiledTransform = class
Public NotInheritable Class XslCompiledTransform
- 继承
-
XslCompiledTransform
- 属性
示例
以下示例执行转换并输出到文件。
// 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")
此示例使用以下两个输入文件:
<?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>
<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 处理器。 它是一个新实现方案,与过时的 XslTransform 类相比,提升了性能。 XslCompiledTransform 类的结构与 XslTransform 类非常相似。 该方法 Load 加载并编译样式表,而 Transform 该方法执行 XSLT 转换。
默认情况下禁用对 XSLT document() 函数和嵌入式脚本块的支持。 可以通过创建对象 XsltSettings 并将其传递给 Load 方法来启用这些功能。
有关详细信息,请参阅 使用 XslCompiledTransform 类 和 从 XslTransform 类迁移。
安全注意事项
创建使用该 XslCompiledTransform 类的应用程序时,应注意以下项及其含义:
默认情况下禁用 XSLT 脚本。 仅当需要脚本支持并且正在使用完全受信任的环境中时,才应启用 XSLT 脚本。
默认情况下禁用 XSLT
document()函数。 如果启用函数document(),请通过传递给ThrowingResolverTransform方法来限制可访问的资源。默认情况下启用扩展对象。 XsltArgumentList如果将包含扩展对象的对象传递给Transform方法,则会使用它们。
XSLT 样式表可以包含对其他文件和嵌入脚本块的引用。 恶意用户可以通过提供数据或样式表来利用这一点,导致系统持续处理,直到计算机资源耗尽。
在混合信任环境中运行的 XSLT 应用程序可能会导致样式表欺骗。 例如,恶意用户可以加载具有有害样式表的对象,并将其移交给随后调用 Transform 该方法和执行转换的其他用户。
通过不启用脚本或document()函数,除非样式表来自受信任的源,并且不接受来自不受信任的源的对象、XSLT 样式表或 XML 源数据,可以缓解这些安全问题。
构造函数
| 名称 | 说明 |
|---|---|
| XslCompiledTransform() |
初始化 XslCompiledTransform 类的新实例。 |
| XslCompiledTransform(Boolean) |
使用指定的调试设置初始化类的新实例 XslCompiledTransform 。 |
属性
| 名称 | 说明 |
|---|---|
| OutputSettings |
获取一个 XmlWriterSettings 对象,该对象包含派生自 |
| TemporaryFiles |
获取 TempFileCollection 在成功调用 Load 该方法后在磁盘上生成的临时文件。 |
方法
适用于
线程安全性
加载对象后,该 XslCompiledTransform 对象是线程安全的。 换句话说,在 Load 方法成功完成后, Transform 可以从多个线程同时调用该方法。
Load如果在另一个线程中调用该方法时Transform再次调用该方法,则对象XslCompiledTransform将继续使用旧状态来执行Transform调用。 新状态在方法成功完成时 Load 使用。
从多个线程同时调用时,该方法 Load 不是线程安全的。