Export (0) Print
Expand All
4 out of 9 rated this helpful - Rate this topic

XslTransform Class

Transforms XML data using an XSLT stylesheet.

For a list of all members of this type, see XslTransform Members.

System.Object
   System.Xml.Xsl.XslTransform

[Visual Basic]
NotInheritable Public Class XslTransform
[C#]
public sealed class XslTransform
[C++]
public __gc __sealed class XslTransform
[JScript]
public class XslTransform

Thread Safety

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.

Remarks

XslTransform supports the XSLT 1.0 syntax. The XSLT stylesheet must use the namespace http://www.w3.org/1999/XSL/Transform.

Note   If you are upgrading from .NET Framework version 1.0, see XslTransform Class Implements the XSLT Processor for migration information.

Additional arguments can also be added to the stylesheet using the XsltArgumentList class. This class contains input parameters for the stylesheet and extension objects which can be called from the stylesheet. To transform XML data:

  1. Create an XslTransform object.
  2. Use the Load method to load the stylesheet for the transformation. This method has several overloads and can load a stylesheet using an XmlReader, XPathNavigator, IXPathNavigable, or a URL with the location of the file.
  3. 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 transform.

Scripting Support:

This class supports embedded scripting using the msxsl:script element.

In version 1.1 of the .NET Framework, the evidence of the stylesheet determines what permissions are given to embedded scripts.

  • If the stylesheet was loaded from a URI, the URI is used to create the evidence. This evidence includes the URI along with its site and zone.
  • If the stylesheet was loaded using another source, you can provide evidence by passing a System.Security.Policy.Evidence object to the Load method. Otherwise, the script assembly has full trust.

Semi-trusted callers: 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.

The msxsl:script element has the following requirements:

  • The msxsl:script element belongs to the urn:schemas-microsoft-com:xslt namespace. The stylesheet must include the namespace declaration xmlns:msxsl=urn:schemas-microsoft-com:xslt.
  • The msxsl:script element can include a language attribute that specifies the scripting language to use. The value of the language attribute must be one of the following: C#, CSharp, VB, VisualBasic, JScript, or JavaScript. Because the language name is not case-sensitive, JavaScript and javascript are both valid. If a language attribute is not specified, it defaults to JScript.
  • The msxsl:script element must include an implements-prefix attribute that contains the prefix representing the namespace associated with the script block. This namespace must be defined within the stylesheet. A stylesheet 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 language attribute). For example, if you had a C# script block comments would be prefixed by the // characters. The comments must be valid XML content.

Note 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.

Supported Namespaces Description
System System classes.
System.Collection Collection classes.
System.Text Text handling classes.
System.Xml Core XML classes.
System.Xml.Xsl XSLT classes.
System.Xml.XPath XPath classes.

The supplied arguments and return values defined by the script functions must be one of the W3C types listed below. The following table details the mapping between the W3C types, either XPath or XSLT, and the corresponding .NET classes.

W3C Type Equivalent .NET Class
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

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.

Note    script and 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 stylesheet. 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

Example

[Visual Basic, C#, C++] The following example transforms the specified XML document and outputs the result to the console.

[Visual Basic] 
'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)

[C#] 
//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);

[C++] 
//Create a new XslTransform object.
XslTransform* xslt = new XslTransform();

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

//Create a new XPathDocument and load the XML data to be transformed.
XPathDocument* mydata = new XPathDocument(S"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,0,writer, 0);

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Namespace: System.Xml.Xsl

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

Assembly: System.Xml (in System.Xml.dll)

See Also

XslTransform Members | System.Xml.Xsl Namespace | Implementation of Discretionary Behaviors in the XslTransform Class

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.