Perform XSLT Transformations in .NET 1.1
Version 1.1 of the .NET platform sports a slightly different XslTransform object with more secure Load() and Transform() methods.
by Dan Wahlin
Posted August 5, 2003
Version 1.1 of the .NET platform sports a slightly different XslTransform object with more secure Load() and Transform() methods. Many of the overloaded versions of the Transform() method from .NET 1.0 are now considered obsolete. For example, this call to Transform() will generate a message when compiled, letting you know that things have changed in .NET 1.1:
StringWriter sw = new StringWriter();
XslTransform xsl = new XslTransform();
xsl.Load(xslPath);
//The following is considered "obsolete" in .NET 1.1
xsl.Transform(xmlDoc,null,sw);
lblOutput.Text = sw.ToString();
If you take a closer look at the Transform() method in the documentation for version 1.1 of the framework, you'll see that the new overloads expect you to pass an instance of an XmlResolver. Here's an example of one of these overloads:
public void Transform(XPathNavigator, XsltArgumentList, TextWriter,
XmlResolver);
This is a little confusing for some people because XmlResolver is an abstract class that cannot be created directly. However, you can instantiate a class named XmlUrlResolver, which derives from XmlResolver, using the familiar "new" keyword in VB.NET or C#. In cases where you must perform basic XSLT transformations (transformations that involve a single XML document), you can pass a null (Nothing in VB.NET) for the XmlResolver parameter:
StringWriter sw = new StringWriter();
XslTransform xsl = new XslTransform();
xsl.Load(xslPath);
//A null XmlResolver is passed for this basic transformation
xsl.Transform(xmlDoc,null,sw,null);
lblOutput.Text = sw.ToString();
Passing null for the XmlResolver parameter when the transformation involves more than one XML document presents a problem. If you need to use the document() function to transform multiple XML documents simultaneously, passing a null causes these external documents to be ignored. For example, assume you have one XML document that is used to dynamically create HTML form elements as shown here:
<?xml version="1.0" encoding="utf—8" ?>
<form method="post" action="XSLTResolverDemo2.aspx">
<ctl type="textbox" id="txtFirstName" label="First Name:" />
<ctl type="textbox" id="txtLastName" label="Last Name:" />
<ctl type="submit" id="btnSubmit" text="Submit!" />
</form>
This next XML document contains values that will be "plugged" into different HTML form elements during the XSLT transformation:
<?xml version="1.0" encoding="utf—8" ?>
<form>
<ctl id="txtFirstName" value="Dan" />
<ctl id="txtLastName" value="Wahlin" />
</form>
The XML document containing the values will be accessed during the XSLT transformation through the document() function. This code demonstrates how to use the document() function to reference the XML document containing values (named FormValues.xml) from within the stylesheet:
<xsl:variable name="formValuesDoc" select="document('../XML/FormValues.xml')" />
Back to top
|