Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

XslTransform Class Implements the XSLT Processor

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

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)
[C#]
public void Load(string);
[Visual Basic]
Overloads Public Sub Load(String, XmlResolver)
[C#]
public void Load(string, XmlResolver);
[Visual Basic]
Overloads Public Sub Load(XmlReader, XmlResolver, Evidence)
[C#]
public void Load(XmlReader, XmlResolver, Evidence);
[Visual Basic]
Overloads Public Sub Load(XPathNavigator, XmlResolver, Evidence)
[C#]
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 System.Security.Policy.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 parameter, 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 XslArgumentList.

For more information on the overloaded Load methods, see XslTransform.Load Method. For more information about security levels and Evidence and how it affects scripting, see XSLT Stylesheet Scripting using <msxml:script>. For information about security levels and Evidence and how it affects extension objects, see XsltArgumentList for Stylesheet Parameters and Extension Objects.

For information about security levels and Evidence and how it affects the document() function, see Resolving External XSLT Stylesheets 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 Members.

Recommended Secure Use of XslTransform Class

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.

ScenarioType of Evidence to Provide
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)
[C#]
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)
[C#]
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 a null reference (Nothing in VB). 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 a null reference (Nothing) 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:

  • XmlReader
  • XmlWriter
  • TextWriter
  • Stream
  • String URL of file

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 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)
[C#]
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 node fragment, you must create an XmlDocument containing just the node fragment, and pass that XmlDocument to the Transform method. The following example performs a transformation on a node 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 node fragment.
Dim testNode As XmlNode = doc.DocumentElement.FirstChild
Dim tmpDoc As New XmlDocument()
tmpDoc.LoadXml(testNode.OuterXml)
' Pass the document containing the node fragment 
' to the Transform method.
Console.WriteLine(("Passing " + tmpDoc.OuterXml + " to print_root.xsl"))
xslt.Transform(tmpDoc, Nothing, Console.Out, Nothing)
[C#]
XslTransform xslt = new XslTransform();     
xslt.Load("print_root.xsl");
XmlDocument doc = new XmlDocument();
doc.Load("library.xml");
// Create a new document containing just the node fragment.
XmlNode testNode = doc.DocumentElement.FirstChild; 
XmlDocument tmpDoc = new XmlDocument(); 
tmpDoc.LoadXml(testNode.OuterXml);
// Pass the document containing the node 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 XslTransform.Load. 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 XslTransform.Transform. 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

XSLT Transformations with the XslTransform Class | XPathNavigator in Transformations | XPathNodeIterator in Transformations | XPathDocument Input to XslTransform | XmlDataDocument Input to XslTransform | XmlDocument Input to XslTransform | XslTransform Class | XslTransform Members

Show:
© 2015 Microsoft