XslTransform.Transform Method (IXPathNavigable, XsltArgumentList, XmlResolver)

 
System_CAPS_noteNote

The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience.

Transforms the XML data in the IXPathNavigable using the specified args and outputs the result to an XmlReader.

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

public XmlReader Transform(
	IXPathNavigable input,
	XsltArgumentList args,
	XmlResolver resolver
)

Parameters

input
Type: System.Xml.XPath.IXPathNavigable

An object implementing the IXPathNavigable interface. In the .NET Framework, this can be either an XmlNode (typically an XmlDocument), or an XPathDocument containing the data to be transformed.

args
Type: System.Xml.Xsl.XsltArgumentList

An XsltArgumentList containing the namespace-qualified arguments used as input to the transformation.

resolver
Type: System.Xml.XmlResolver

The XmlResolver used to resolve the XSLT document() function. If this is null, the document() function is not resolved.

The XmlResolver is not cached after the Transform method completes.

Return Value

Type: System.Xml.XmlReader

An XmlReader containing the results of the transformation.

System_CAPS_noteNote

The XslTransform class is obsolete in the .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 include the namespace declaration xmlns:xsl= http://www.w3.org/1999/XSL/Transform.

The args are matched with the xsl:param elements defined in the style sheet. The xsl:output element is not supported when outputting to an XmlReader (the xsl:output element is ignored). See Outputs from an XslTransform for more information.

This method enables you to do an asynchronous transformation of the source 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.

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

<style sheet 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>
</style sheet>

The following example transforms an XML document and outputs the results to an XmlReader. An XmlUrlResolver with the necessary credentials is used to process any XSLT document() functions.

// Create the XslTransform object.
XslTransform xslt = new XslTransform();

// Load the stylesheet.
xslt.Load("titles.xsl");

// Create a resolver and specify the necessary credentials.
XmlUrlResolver resolver = new XmlUrlResolver();
System.Net.NetworkCredential myCred;
myCred  = new System.Net.NetworkCredential(UserName,SecurelyStoredPassword,Domain);  
resolver.Credentials = myCred;

// Transform the file using the resolver. The resolver is used
// to process the XSLT document() function.
XPathDocument doc = new XPathDocument("books.xml");
XmlReader reader = xslt.Transform(doc, null, resolver);

// Load the reader into a new document for more processing.
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load(reader);

.NET Framework
Available since 1.1
Return to top
Show: