Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
This class has been deprecated. Please use System.Xml.Xsl.XslCompiledTransform instead. http://go.microsoft.com/fwlink/?linkid=14202
Transforms XML data using an Extensible Stylesheet Language for Transformations (XSLT) style sheet.
public ref class XslTransform sealed
public sealed class XslTransform
[System.Obsolete("This class has been deprecated. Please use System.Xml.Xsl.XslCompiledTransform instead. http://go.microsoft.com/fwlink/?linkid=14202")] public sealed class XslTransform
type XslTransform = class
[<System.Obsolete("This class has been deprecated. Please use System.Xml.Xsl.XslCompiledTransform instead. http://go.microsoft.com/fwlink/?linkid=14202")>] type XslTransform = class
Public NotInheritable Class XslTransform
The following example transforms the specified XML document and outputs the result to the console.
//Create a new XslTransform object. XslTransform xslt = new XslTransform(); //Load the stylesheet. xslt.Load("http://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. Dim xslt As New XslTransform() 'Load the stylesheet. xslt.Load(CType("http://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)
The XslTransform class is obsolete in the Microsoft .NET Framework version 2.0. The XslCompiledTransform class is the new XSLT processor. For more information, see Using the XslCompiledTransform Class and Migrating From the XslTransform Class.
XslTransform supports the XSLT 1.0 syntax. The XSLT style sheet must use the namespace
Additional arguments can also be added to the style sheet using the XsltArgumentList class. This class contains input parameters for the style sheet and extension objects which can be called from the style sheet.
To transform XML data:
Create an XslTransform object.
Use the Load method to load the style sheet for the transformation. This method has several overloads and can load a style sheet using an XmlReader, XPathNavigator, IXPathNavigable, or a URL with the location of the file.
Use the Transform method to transform the XML data. This method has several overloads and can handle different types of input and output. You can also specify an XsltArgumentList containing additional arguments to use as input during the transformation.
When creating an application that uses the XslTransform class, you should be aware of the following items and their implications:
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 will cause your system to process until the computer runs low on resources.
XSLT applications that run in a mixed trust environment can result in style sheet spoofing. 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.
These security issues can be mitigated by not accepting XslTransform objects, XSLT style sheets, or XML source data from untrusted sources.
This class supports embedded scripting using the
In version 1.1 of the .NET Framework, the evidence of the style sheet determines what permissions are given to embedded scripts.
If the style sheet was loaded from a Uniform Resource Identifier (URI), the URI is used to create the evidence. This evidence includes the URI along with its site and zone.
UnmanagedCode permission is required to compile the embedded script.
ControlEvidence permission is required to provide Evidence to the Load method. A SecurityException is thrown if the caller does not have the necessary permissions. See System.Security.Permissions.SecurityPermission and System.Security.Permissions.SecurityPermissionFlag for more information.
msxsl:script element has the following requirements:
msxsl:scriptelement belongs to the
urn:schemas-microsoft-com:xsltnamespace. The style sheet must include the namespace declaration
msxsl:scriptelement can include a
languageattribute that specifies the scripting language to use. The value of the
languageattribute is not specified, it defaults to JScript.
msxsl:scriptelement must include an
implements-prefixattribute that contains the prefix representing the namespace associated with the script block. This namespace must be defined within the style sheet. A style sheet can include multiple script blocks which are grouped by namespace. You cannot have script blocks with multiple languages within the same namespace. Script blocks can call a function defined in another script block, provided the script blocks reside within the same namespace. The contents of a script block are parsed according to the rules and syntax of the scripting language (supplied by the
languageattribute). For example, if you had a C# script block, comments would be prefixed by the
//characters. The comments must be valid XML content.
It is recommended that you wrap script blocks in a CDATA section.
<msxsl:script implements-prefix='xy' language='C#'> <![CDATA[ // Add code here. ]]> </msxsl:script>
Functions can be declared within the
msxsl:script element. The following table shows the namespaces that are supported by default.
||Text handling classes.|
||Core XML classes.|
||XML Path Language (XPath) classes.|
The supplied arguments and return values defined by the script functions must be one of the World Wide Web Consortium (W3C) types listed below. The following table details the mapping between the W3C types, either XPath or XSLT, and the corresponding .NET Framework classes.
|W3C Type||Equivalent .NET class|
|Result Tree Fragment (XSLT)||
|Node Set (XPath)||
If the script function utilizes one of the following numeric types: Int16, UInt16, Int32, UInt32, Int64, UInt64, Single, or Decimal, these types are coerced to Double, which maps to the W3C XPath type number.
An exception is thrown when a function is called that cannot convert the argument result to one of the required types.
msxsl:node-list are the only functions from the
urn:schemas-microsoft-com:xslt namespace that are supported by the XslTransform class.
XslTransform has the ability to utilize common language runtime (CLR) code as an extension mechanism. This is accomplished by passing an instance of a class to the XslTransform class and calling its public methods within an XSLT style sheet. Methods that are defined with the
params keyword, which allows an unspecified number of parameters to be passed, do not work correctly in this scenario. See params for more details.
For more information see XSLT Transformations with the XslTransform Class.
Initializes a new instance of the XslTransform class.
XslTransform objects are only thread-safe for transform operations. Other operations are not guaranteed to be thread-safe. You must ensure that no other methods are called on the object during load operations.