XslTransform 类

注意:此类现在已过时。

使用可扩展样式表转换语言 (XSLT) 转换 XML 数据。

**命名空间:**System.Xml.Xsl
**程序集:**System.Xml(在 system.xml.dll 中)

语法

声明
<ObsoleteAttribute("This class has been deprecated. Please use System.Xml.Xsl.XslCompiledTransform instead. https://go.microsoft.com/fwlink/?linkid=14202")> _
Public NotInheritable Class XslTransform
用法
Dim instance As XslTransform
[ObsoleteAttribute("This class has been deprecated. Please use System.Xml.Xsl.XslCompiledTransform instead. https://go.microsoft.com/fwlink/?linkid=14202")] 
public sealed class XslTransform
[ObsoleteAttribute(L"This class has been deprecated. Please use System.Xml.Xsl.XslCompiledTransform instead. https://go.microsoft.com/fwlink/?linkid=14202")] 
public ref class XslTransform sealed
/** @attribute ObsoleteAttribute("This class has been deprecated. Please use System.Xml.Xsl.XslCompiledTransform instead. https://go.microsoft.com/fwlink/?linkid=14202") */ 
public final class XslTransform
ObsoleteAttribute("This class has been deprecated. Please use System.Xml.Xsl.XslCompiledTransform instead. https://go.microsoft.com/fwlink/?linkid=14202") 
public final class XslTransform

备注

提示

XslTransform 类在 Microsoft .NET Framework 2.0 版中已过时。XslCompiledTransform 类是新的 XSLT 处理器。有关更多信息,请参见 使用 XslCompiledTransform 类从 XslTransform 类迁移

XslTransform 支持 XSLT 1.0 语法。XSLT 样式表必须使用命名空间 **http://www.w3.org/1999/xsl/transform**。

也可以使用 XsltArgumentList 类将其他参数添加到样式表。该类包含用于样式表的输入参数以及可从样式表中调用的扩展对象。

转换 XML 数据:

  1. 创建 XslTransform 对象。

  2. 使用 Load 方法来加载用于转换的样式表。该方法具有若干种加载,并可以使用 XmlReaderXPathNavigatorIXPathNavigable 或带有文件位置的 URL 加载样式表。

  3. 使用 Transform 方法来转换 XML 数据。该方法具有若干种加载,并可以处理不同类型的输入和输出。您也可以指定一个包含其他参数的 XsltArgumentList,以用作转换期间的输入。

安全注意事项

创建使用 XslTransform 类的应用程序时,应注意以下几项及其含义:

  • 默认情况下启用扩展对象。如果将包含扩展对象的 XsltArgumentList 对象传递到 Transform 方法,则可以利用这些扩展对象。

  • XSLT 样式表可以包含对其他文件的引用和嵌入的脚本块。某个恶意用户可能会利用这一点,如果执行该用户提供的数据或样式表,就会导致系统不停地进行处理直至计算机资源消耗殆尽。

  • 运行在混合信任环境中的 XSLT 应用程序能够导致样式表欺骗。例如,恶意用户能够加载包含有害样式表的对象,并将其传递给随后会调用 Transform 方法来执行转换的其他用户。

通过拒绝接受来自不受信任源的 XslTransform 对象、XSLT 样式表和 XML 资源数据,可以缓解这些安全问题。

脚本支持

该类使用 msxsl:script 元素支持嵌入的脚本。

在.NET Framework 1.1 版中,样式表的证据确定赋与嵌入脚本的权限。

  • 如果样式表是从统一资源标识符 (URI) 加载的,则此 URI 用于创建证据。该证据包括 URI 及其站点和区域。

  • 如果样式表是使用其他源加载的,可通过将 System.Security.Policy.Evidence 对象传递到 Load 方法来提供证据。否则脚本集将完全受信任。

不完全受信任的调用方:需要 UnmanagedCode 权限才可编译嵌入脚本。需要 ControlEvidence 权限才可向 Load 方法提供 Evidence。如果调用方不具有所需要的权限,则引发 SecurityException。有关更多信息,请参见 System.Security.Permissions.SecurityPermissionSystem.Security.Permissions.SecurityPermissionFlag

msxsl:script 元素具有下列要求:

  • msxsl:script 元素属于 urn:schemas-microsoft-com:xslt 命名空间。样式表必须包含命名空间声明 xmlns:msxsl=urn:schemas-microsoft-com:xslt

  • msxsl:script 元素可包含一个 language 属性,该属性指定要使用的脚本语言。language 属性的值必须是下列之一:C#、CSharp、VB、VisualBasic、JScript 或 JavaScript。由于语言名称不区分大小写,所以 JavaScript 和 javascript 都是有效名称。如果未指定 language 属性,则其默认值为 JScript。

  • msxsl:script 元素必须包含 implements-prefix 属性,该属性包含代表与脚本块关联的命名空间的前缀。此命名空间必须在样式表中定义。样式表可包含按命名空间分组的多个脚本块。相同的命名空间中不可以具有多种语言的脚本块。只要脚本块驻留在相同的命名空间中,脚本块就可以调用在另一个脚本块中定义的函数。脚本块的内容根据脚本语言(由 language 属性提供)的规则和语法进行分析。例如,如果具有 C# 脚本块,注释就会以 // 字符作为前缀。注释必须是有效的 XML 内容。

Note 建议将脚本块包装在 CDATA 部分中。

 <msxsl:script implements-prefix='xy' language='C#'>
   <![CDATA[
   // Add code here.
   ]]>
 </msxsl:script>

函数可以在 msxsl:script 元素中声明。下表显示了默认支持的命名空间。

支持的命名空间

说明

System

系统类。

System.Collection

集合类。

System.Text

文本处理类。

System.Xml

核心 XML 类。

System.Xml.Xsl

XSLT 类。

System.Xml.XPath

XML 路径语言 (XPath) 类。

所提供的由脚本函数定义的参数和返回值必须为以下所列的 WWW 联合会 (W3C) 类型之一。下表详细说明了 W3C 类型(XPath 或 XSLT)和相应的 .NET Framework 类之间的映射。

W3C 类型

等效的 .NET 类

String (XPath)

System.String

Boolean (XPath)

System.Boolean

Number (XPath)

System.Double

Result Tree Fragment (XSLT)

System.Xml.XPath.XPathNavigator

Node Set (XPath)

System.Xml.XPath.XPathNodeIterator

如果脚本函数使用下列数值类型之一:Int16、UInt16、Int32、UInt32、Int64、UInt64、Single 或 Decimal,则这些类型强迫为 Double(它映射到 W3C XPath 类型数)。

调用函数时引发异常,无法将参数结果转换为要求的类型之一。

提示

msxsl:scriptmsxsl:node-listurn:schemas-microsoft-com:xslt 命名空间中唯一受 XslTransform 类支持的功能。

XslTransform 能够将公共语言运行库 (CLR) 代码作为扩展机制使用。这通过将类实例传递到 XslTransform 类并在 XSLT 样式表中调用其公共方法完成。以 params 关键字定义的方法(允许传递未指定数量的参数)在此方案中不能正常使用。有关更多详细信息,请参见 params(C# 参考)

有关更多信息,请参见 XslTransform 类的 XSLT 转换

主题 位置
如何:转换 XML Web 服务器控件中的 XML 数据 在 Visual Studio 中生成 ASP .NET Web 应用程序
如何:转换 XML Web 服务器控件中的 XML 数据 在 Visual Studio 中生成 ASP .NET Web 应用程序
如何:转换 XML Web 服务器控件中的 XML 数据 生成 ASP .NET Web 应用程序

示例

下面的示例转换指定的 XML 文档并将结果输出到控制台。

'Create a new XslTransform object.
Dim xslt As New XslTransform()

'Load the stylesheet.
xslt.Load(CType("https://server/favorite.xsl", String))

'Create a new XPathDocument and load the XML data to be transformed.
Dim mydata As New XPathDocument("inputdata.xml")

'Create an XmlTextWriter which outputs to the console.
Dim writer As New XmlTextWriter(Console.Out)

'Transform the data and send the output to the console.
xslt.Transform(mydata, Nothing, writer, Nothing)
//Create a new XslTransform object.
XslTransform xslt = new XslTransform();

//Load the stylesheet.
xslt.Load("https://server/favorite.xsl");

//Create a new XPathDocument and load the XML data to be transformed.
XPathDocument mydata = new XPathDocument("inputdata.xml");

//Create an XmlTextWriter which outputs to the console.
XmlWriter writer = new XmlTextWriter(Console.Out);

//Transform the data and send the output to the console.
xslt.Transform(mydata,null,writer, null);
//Create a new XslTransform object.
XslTransform^ xslt = gcnew XslTransform;

//Load the stylesheet.
xslt->Load( "https://server/favorite.xsl" );

//Create a new XPathDocument and load the XML data to be transformed.
XPathDocument^ mydata = gcnew XPathDocument( "inputdata.xml" );

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

//Transform the data and send the output to the console.
xslt->Transform^(mydata,0,writer,0);
//Create a new XslTransform object.
XslTransform xslt = new XslTransform();

//Load the stylesheet.
xslt.Load("https://server/favorite.xsl");

//Create a new XPathDocument and load the XML data to be transformed.
XPathDocument myData = new XPathDocument("inputdata.xml");

//Create an XmlTextWriter which outputs to the console.
XmlWriter writer = new XmlTextWriter(Console.get_Out());

//Transform the data and send the output to the console.
xslt.Transform(myData, null, writer, null);

继承层次结构

System.Object
  System.Xml.Xsl.XslTransform

线程安全

XslTransform 对象只对于转换操作是线程安全的。其他操作不保证是线程安全的。您必须确保在加载操作期间不对该对象调用其他方法。

平台

Windows 98、Windows 2000 SP4、Windows Millennium Edition、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:1.0、1.1
在 2.0 中过时(编译器警告)

请参见

参考

XslTransform 成员
System.Xml.Xsl 命名空间