Compartir a través de


XslTransform Class Implements the XSLT Processor

Note

The XslTransform class is obsolete in the .NET Framework 2.0. You can perform Extensible Stylesheet Language for Transformations (XSLT) transformations using the XslCompiledTransform class. See Using the XslCompiledTransform Class and Migrating From the XslTransform Class for more information.

The XslTransform class is an XSLT processor implementing the XSL Transformations (XSLT) Version 1.0 recommendation. The Load method locates and reads style sheets, and the Transform method transforms the given source document. Any store that implements the IXPathNavigable interface can be used as the source document for the XslTransform. The .NET Framework currently implements the IXPathNavigable interface on the XmlDocument, the XmlDataDocument, and the XPathDocument, so all of these can be used as the input source document to a transformation.

The XslTransform object in the .NET Framework only supports the XSLT 1.0 specification, defined with the following namespace:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

The style sheet can be loaded, using the Load method, from one of the following classes:

  • XPathNavigator

  • XmlReader

  • A string representing a URL

There is a different Load method for each of the above input classes. Some methods take in a combination of one of these classes and the XmlResolver class as arguments. The XmlResolver locates resources referenced by <xsl:import> or <xsl:include> found in the style sheet. The following methods take a string, XmlReader, or XPathNavigator as input.

Overloads Public Sub Load(String)
public void Load(string);
Overloads Public Sub Load(String, XmlResolver)
public void Load(string, XmlResolver);
Overloads Public Sub Load(XmlReader, XmlResolver, Evidence)
public void Load(XmlReader, XmlResolver, Evidence);
Overloads Public Sub Load(XPathNavigator, XmlResolver, Evidence)
public void Load(XPathNavigator, XmlResolver, Evidence);

Most of the Load methods shown above take an XmlResolver as a parameter. The XmlResolver is used to load the style sheet and any style sheet(s) referenced in xsl:import and xsl:include elements.

Most of the Load methods also take evidence as a parameter. The evidence parameter is the Evidence that is associated with the style sheet. The security level of the style sheet affects the security level of any subsequent resources it references, such as the script it contains, any document() functions it uses, and any extension objects used by the XsltArgumentList.

If the style sheet is loaded using a Load method that contains a URL parameter and no evidence is provided, the evidence of the style sheet is calculated by combining the given URL with its site and zone.

If no URI or evidence is provided, then the evidence set for the style sheet is fully trusted. Do not load style sheets from untrusted sources, or add untrusted extension objects into the XsltArgumentList.

For more information about security levels and evidence and how it affects scripting, see XSLT Stylesheet Scripting Using <msxsl:script>. For information about security levels and evidence and how it affects extension objects, see XsltArgumentList for Style Sheet Parameters and Extension Objects.

For information about security levels and evidence and how it affects the document() function, see Resolving External XSLT Style Sheets and Documents.

A style sheet can be supplied with a number of input parameters. The style sheet can also call functions on extension objects. Both parameters and extension objects are supplied to the style sheet using the XsltArgumentList class. For more information about the XsltArgumentList, see XsltArgumentList.

The security privileges of the style sheet depend on the evidence provided. The following table summarizes the location of the style sheet and gives an explanation of what type of evidence to give.

  • The XSLT style sheet has no external references, or the style sheet comes from a code base that you trust.

    • Provide the evidence from your assembly:

      Dim xslt = New XslTransform() xslt.Load(stylesheet, resolver, Me.GetType().Assembly.Evidence)
      
      XsltTransform xslt = new XslTransform();  xslt.Load(stylesheet, resolver, this.GetType().Assembly.Evidence);
      
  • The XSLT style sheet comes from an outside source. The origin of the source is known and there is a verifiable URI.

    • Create evidence using the URI.

      Dim xslt As New XslTransform() Dim ev As Evidence = XmlSecureResolver.CreateEvidenceForUrl(stylesheetUri) xslt.Load(stylesheet, resolver, evidence)
      
      XslTransform xslt = new XslTransform(); Evidence ev = XmlSecureResolver.CreateEvidenceForUrl(stylesheetUri); xslt.Load(stylesheet, resolver, evidence);
      
  • The XSLT style sheet comes from an outside source. The origin of the source is not known.

    • Set evidence to null. Script blocks are not processed, the XSLT document() function is not supported, and privileged extension objects are disallowed.

      Additionally, you can also set the resolver parameter to null This ensures that xsl:import and xsl:include elements are not processed.

  • The XSLT style sheet comes from an outside source. The origin of the source is not known, but you require script support.

    • Request evidence from the caller.

Transformation of XML Data

Once a style sheet is loaded, the transformation starts by calling one of the Transform methods and supplying an input source document. The Transform method is overloaded to provide different transformation outputs. The transformation can result in the following output formats:

This last format, the string URL, provides for a commonly used scenario in transforming an input document located in a URL and writing the document to the output URL. This Transform method is a convenience method to load an XML document from a file, perform the XSLT transformation, and write the output to a file. This prevents you from having to create and load the input source document, and then write to a file stream. The following code sample shows this use of the Transform method using the string URL as input and output:

Dim xsltransform As XslTransform = New XslTransform()
xsltransform.Load("favorite.xsl")
xsltransform.Transform("MyDocument.Xml", "TransformResult.xml", Nothing)
XslTransform xsltransform = new XslTransform();
xsltransform.Load("favorite.xsl");
xsltransform.Transform("MyDocument.xml", "TransformResult.xml", null);

Transforming a Section of an XML Document

Transformations apply to the document as a whole. In other words, if you pass in a node other than the document root node, this does not prevent the transformation process from accessing all nodes in the loaded document. To transform a result tree fragment, you must create an XmlDocument containing just the result tree fragment and pass that XmlDocument to the Transform method. The following example performs a transformation on a result tree fragment.

Dim xslt As New XslTransform()
xslt.Load("print_root.xsl")
Dim doc As New XmlDocument()
doc.Load("library.xml")
' Create a new document containing just the result tree fragment.
Dim testNode As XmlNode = doc.DocumentElement.FirstChild
Dim tmpDoc As New XmlDocument()
tmpDoc.LoadXml(testNode.OuterXml)
' Pass the document containing the result tree fragment
' to the Transform method.
Console.WriteLine(("Passing " + tmpDoc.OuterXml + " to print_root.xsl"))
xslt.Transform(tmpDoc, Nothing, Console.Out, Nothing)
XslTransform xslt = new XslTransform();
xslt.Load("print_root.xsl");
XmlDocument doc = new XmlDocument();
doc.Load("library.xml");
// Create a new document containing just the result tree fragment.
XmlNode testNode = doc.DocumentElement.FirstChild;
XmlDocument tmpDoc = new XmlDocument();
tmpDoc.LoadXml(testNode.OuterXml);
// Pass the document containing the result tree fragment
// to the Transform method.
Console.WriteLine("Passing " + tmpDoc.OuterXml + " to print_root.xsl");
xslt.Transform(tmpDoc, null, Console.Out, null);

The example uses the library.xml and print_root.xsl files as input and outputs the following to the console:

Passing <book genre="novel" ISBN="1-861001-57-5"><title>Pride And Prejudice</title></book> to print_root.xsl
Root node is book.

library.xml

<library>
  <book genre='novel' ISBN='1-861001-57-5'>
     <title>Pride And Prejudice</title>
  </book>
  <book genre='novel' ISBN='1-81920-21-2'>
     <title>Hook</title>
  </book>
</library>

print_root.xsl

<stylesheet version="1.0" xmlns="http://www.w3.org/1999/XSL/Transform" >
  <output method="text" />
  <template match="/">
     Root node is  <value-of select="local-name(//*[position() = 1])" />
  </template>
</stylesheet>

Migration of XSLT from .NET Framework version 1.0 to .NET Framework version 1.1

The following table shows the obsolete .NET Framework version 1.0 methods and new .NET Framework version 1.1 methods for the Load method. The new methods enable you to limit the permissions of the style sheet by specifying evidence.

Obsolete .NET Framework version 1.0 Load Methods Replacement .NET Framework version 1.1 Load Methods
Load(XPathNavigator input);

Load(XPathNavigator input, XmlResolver resolver);
Load(XPathNavigator stylesheet, XmlResolver resolver, Evidence evidence);
Load(IXPathNavigable stylesheet);

Load(IXPathNavigable stylesheet, XmlResolver resolver);
Load(IXPathNavigable stylesheet, XmlResolver resolver, Evidence evidence);
Load(XmlReader stylesheet);

Load(XmlReader stylesheet, XmlResolver resolver);
Load(XmlReader stylesheet, XmlResolver resolver, Evidence evidence);

The following table shows the obsolete and new methods for the Transform method. The new methods take an XmlResolver object.

Obsolete .NET Framework version 1.0 Transform Methods Replacement .NET Framework version Transform 1.1 Methods
XmlReader Transform(XPathNavigator input, XsltArgumentList args) XmlReader Transform(XPathNavigator input, XsltArgumentList args, XmlResolver resolver)
XmlReader Transform(IXPathNavigable input, XsltArgumentList args) XmlReader Transform(IXPathNavigable input, XsltArgumentList args, XmlResolver resolver)
Void Transform(XPathNavigator input, XsltArgumentList args, XmlWriter output) Void Transform(XPathNavigator input, XsltArgumentList args, XmlWriter output, XmlResolver resolver)
Void Transform(IXPathNavigable input, XsltArgumentList args, XmlWriter output) Void Transform(IXpathNavigable input, XsltArgumentList args, XmlWriter output, XmlResolver resolver)
Void Transform(XPathNavigator input, XsltArgumentList args, TextWriter output) Void Transform(XPathNavigator input, XsltArgumentList args, TextWriter output, XmlResolver resolver)
Void Transform(IXPathNavigable input, XsltArgumentList args, TextWriter output) Void Transform(IXPathNavigable input, XsltArgumentList args, TextWriter output, XmlResolver resolver)
Void Transform(XPathNavigator input, XsltArgumentList args, Stream output) Void Transform(XPathNavigator input, XsltArgumentList args, Stream output, XmlResolver resolver)
Void Transform(IXPathNavigable input, XsltArgumentList args, Stream output) Void Transform(IXPathNavigable input, XsltArgumentList args, Stream output, XmlResolver resolver)
Void Transform(String input, String output); Void Transform(String input, String output, XmlResolver resolver);

The XslTransform.XmlResolver property is obsolete in .NET Framework version 1.1. Instead, use the new Transform overloads which take an XmlResolver object.

See also