Inputs to the XslCompiledTransform Class

The Transform method accepts three input types for the source document: an object that implements the IXPathNavigable interface, an XmlReader object that reads the source document, or a string URI.

Note

The XslCompiledTransform class preserves white space by default. This is in accordance with section 3.4 of the W3C XSLT 1.0 recommendation.

IXPathNavigable Interface

The IXPathNavigable interface is implemented in the XmlNode and XPathDocument classes. These classes represent an in-memory cache of XML data.

  • The XmlNode class is based on the W3C Document Object Model (DOM) and includes editing capabilities.

  • The XPathDocument class is a read-only data store based on the XPath data model. XPathDocument is the recommended class for XSLT processing. It provides faster performance when compared to the XmlNode class.

Note

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 object containing just the node fragment, and pass that object to the Transform method. For more information, see How to: Transform a Node Fragment.

The following example uses the XslCompiledTransform.Transform method to transform the books.xml file to the books.html file using the transform.xsl style sheet. The books.xml and transform.xsl files can be found in this topic: How to: Perform an XSLT Transformation by Using an Assembly.

// Open books.xml as an XPathDocument.
XPathDocument doc = new XPathDocument("books.xml");

// Create a writer for writing the transformed file.
XmlWriter writer = XmlWriter.Create("books.html");

// Create and load the transform with script execution enabled.
XslCompiledTransform transform = new XslCompiledTransform();
XsltSettings settings = new XsltSettings();
settings.EnableScript = true;
transform.Load("transform.xsl", settings, null);

// Execute the transformation.
transform.Transform(doc, writer);
' Open books.xml as an XPathDocument.
Dim doc As XPathDocument = New XPathDocument("books.xml")

' Create a writer for writing the transformed file.
Dim writer As XmlWriter = XmlWriter.Create("books.html")

' Create and load the transform with script execution enabled.
Dim transform As XslCompiledTransform = New XslCompiledTransform()
Dim settings As XsltSettings = New XsltSettings()
settings.EnableScript = True
transform.Load("transform.xsl", settings, Nothing)

'Execute the transformation.
transform.Transform(doc, writer)

XmlReader Object

The Transform method loads from the current node of the XmlReader through all its children. This enables you to use a portion of a document as the context document. After the Transform method returns, the XmlReader is positioned on the next node after the end of the context document. If the end of the document is reached, the XmlReader is positioned at the end of file (EOF).

The following example uses the XslCompiledTransform.Transform method to transform the books.xml file to the books.html file using the transform.xsl style sheet. The books.xml and transform.xsl files can be found in this topic: How to: Perform an XSLT Transformation by Using an Assembly.

// Create a reader to read books.xml
XmlReader reader = XmlReader.Create("books.xml");

// Create a writer for writing the transformed file.
XmlWriter writer = XmlWriter.Create("books.html");

// Create and load the transform with script execution enabled.
XslCompiledTransform transform = new XslCompiledTransform();
XsltSettings settings = new XsltSettings();
settings.EnableScript = true;
transform.Load("transform.xsl", settings, null);

// Execute the transformation.
transform.Transform(reader, writer);
'Create a reader to read books.xml
Dim reader As XmlReader = XmlReader.Create("books.xml")

' Create a writer for writing the transformed file.
Dim writer As XmlWriter = XmlWriter.Create("books.html")

' Create and load the transform with script execution enabled.
Dim transform As XslCompiledTransform = New XslCompiledTransform()
Dim settings As XsltSettings = New XsltSettings()
settings.EnableScript = True
transform.Load("transform.xsl", settings, Nothing)

' Execute the transformation.
transform.Transform(reader, writer)

String URI

You can also specify the source document URI as your XSLT input. An XmlResolver is used to resolve the URI. You can specify the XmlResolver to use by passing it to the Transform method. If an XmlResolver is not specified, the Transform method uses a default XmlUrlResolver with no credentials.

The following example uses the XslCompiledTransform.Transform method to transform the books.xml file to the books.html file using the transform.xsl style sheet. The books.xml and transform.xsl files can be found in this topic: How to: Perform an XSLT Transformation by Using an Assembly.

// Create and load the transform with script execution enabled.
XslCompiledTransform transform = new XslCompiledTransform();
XsltSettings settings = new XsltSettings();
settings.EnableScript = true;
transform.Load("transform.xsl", settings, null);

// Execute the transformation.
transform.Transform("books.xml", "books.html");
' Create and load the transform with script execution enabled.
Dim transform As XslCompiledTransform = New XslCompiledTransform()
Dim settings As XsltSettings = New XsltSettings()
settings.EnableScript = True
transform.Load("transform.xsl", settings, Nothing)

' Execute the transformation.
transform.Transform("books.xml", "books.html")

For more information, see Resolving External Resources During XSLT Processing.

See also