XslTransform.Load Method (String, XmlResolver)

 

Loads the XSLT style sheet specified by a URL.

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

public void Load(
	string url,
	XmlResolver resolver
)

Parameters

url
Type: System.String

The URL that specifies the XSLT style sheet to load.

resolver
Type: System.Xml.XmlResolver

The XmlResolver to use to load the style sheet and any style sheet(s) referenced in xsl:import and xsl:include elements.

If this is null, a default XmlUrlResolver with no user credentials is used to open the style sheet. The default XmlUrlResolver is not used to resolve any external resources in the style sheet, so xsl:import and xsl:include elements are not resolved.

The XmlResolver is not cached after the Load method completes.

Exception Condition
XsltCompileException

The loaded resource is not a valid style sheet.

SecurityException

The style sheet contains embedded script, and the caller does not have UnmanagedCode permission.

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.

If the style sheet contains embedded scripting, the script is compiled to an assembly. The URI of the style sheet is used to create evidence, which is applied to the assembly. For issues with the Load method and style sheets with embedded scripts, see article Q316755 in the Microsoft Knowledge Base at http://support.microsoft.com.

System_CAPS_noteNote

If the caller does not have UnmanagedCode permission, the embedded script is not compiled and a SecurityException is thrown. See SecurityPermission and SecurityPermissionFlag.UnmanagedCode for more information.

The following example transforms an XML document into an HTML document. The example loads an XSLT style sheet which contains an xsl:include element referencing another style sheet. An XmlUrlResolver is passed to the Load method which sets the credentials necessary to access the network resource for the included style sheet.

using System;
using System.IO;
using System.Xml;
using System.Xml.XPath;
using System.Xml.Xsl;
using System.Net;

public class Sample
{
   private const String filename = "books.xml";
   private const String stylesheet = "sort.xsl";

   public static void Main()
   {
      //Create the XslTransform.
     XslTransform xslt = new XslTransform();

     //Create a resolver and set the credentials to use.
     XmlUrlResolver resolver = new XmlUrlResolver();
     resolver.Credentials = CredentialCache.DefaultCredentials;

     //Load the stylesheet.
     xslt.Load(stylesheet, resolver);

     //Load the XML data file.
     XPathDocument doc = new XPathDocument(filename);

     //Create the XmlTextWriter to output to the console.            
     XmlTextWriter writer = new XmlTextWriter(Console.Out);

     //Transform the file.
     xslt.Transform(doc, null, writer, null);
     writer.Close();
  }
}

The example uses the following data files as input.

books.xml

<!-- This file represents a fragment of a book store inventory database -->
<bookstore>
  <book genre="autobiography" publicationdate="1981" ISBN="1-861003-11-0">
    <title>The Autobiography of Benjamin Franklin</title>
    <author>
      <first-name>Benjamin</first-name>
      <last-name>Franklin</last-name>
    </author>
    <price>8.99</price>
  </book>
  <book genre="novel" publicationdate="1967" ISBN="0-201-63361-2">
    <title>The Confidence Man</title>
    <author>
      <first-name>Herman</first-name>
      <last-name>Melville</last-name>
    </author>
    <price>11.99</price>
  </book>
  <book genre="philosophy" publicationdate="1991" ISBN="1-861001-57-6">
    <title>The Gorgias</title>
    <author>
      <name>Plato</name>
    </author>
    <price>9.99</price>
  </book>
</bookstore>

sort.xsl

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:template match="bookstore"/>
  <xsl:include href="http://serverA/includefile.xsl"/>  
  <xsl:template match="book">
     <TR>
      <TD><xsl:value-of select="@ISBN"/></TD>
      <TD><xsl:value-of select="title"/></TD>
      <TD><xsl:value-of select="price"/></TD>
    </TR>
  </xsl:template>
</xsl:stylesheet>

includefile.xsl

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="bookstore">
  <HTML>
    <BODY>
    <TABLE BORDER="2">
      <TR>
        <TD>ISBN</TD>
        <TD>Title</TD>
        <TD>Price</TD>
      </TR>
    <xsl:apply-templates select="book">
      <xsl:sort select="@ISBN"/>
    </xsl:apply-templates>
    </TABLE>
    </BODY>
  </HTML>
</xsl:template>
</xsl:stylesheet>

.NET Framework
Available since 1.1
Return to top
Show: