Resolving External Resources During XSLT Processing

There are several times during an XSLT transformation when you may need to resolve external resources.

Using the XmlResolver Class

The XmlResolver class is used to resolve external resources. The following table describes when the XmlResolver becomes involved during XSLT processing.

XSLT task What the XmlResolver is used for
Compile the style sheet. Resolve the URI of the style sheet.

-and-

Resolve URI references in any xsl:import or xsl:include elements.
Execute the style sheet. Resolve the URI of the context document.

-and-

Resolve URI references in any XSLT document() functions.

The Load and Transform methods include overloads that take an XmlResolver object as one of its arguments. If an XmlResolver is not specified, a default XmlUrlResolver with no credentials is used.

The following list describes when you may want to specify an XmlResolver object:

  • If the XSLT process needs to access a network resource that requires authentication, you can use an XmlResolver with the necessary credentials.

  • If you want to restrict the resources that the XSLT process can access, you can use an XmlSecureResolver with the correct permission set. Use the XmlSecureResolver class if you need to open a resource that you do not control, or that is untrusted.

  • If you want to customize behavior, you can implement your own XmlResolver class and use it to resolve resources.

  • If you want to ensure that no external resources are accessed, you can specify null for the XmlResolver argument.

Example

The following example compiles a style sheet that is stored on a network resource. An XmlUrlResolver object specifies the credentials necessary to access the style sheet.

// Create the credentials.
NetworkCredential myCred = new NetworkCredential(UserName,SecurelyStoredPassword,Domain); 
CredentialCache myCache = new CredentialCache(); 
myCache.Add(new Uri("http://www.contoso.com/"), "Basic", myCred); 
myCache.Add(new Uri("http://app.contoso.com/"), "Basic", myCred);

// Set the credentials on the XmlUrlResolver object.
XmlUrlResolver resolver = new XmlUrlResolver();
resolver.Credentials = myCache;

// Compile the style sheet.
XslCompiledTransform xslt = new XslCompiledTransform();
xslt.Load("http://serverName/data/xsl/order.xsl",XsltSettings.Default, resolver);	
' Create the credentials.
Dim myCred As NetworkCredential = New NetworkCredential(UserName,SecurelyStoredPassword,Domain)
Dim myCache As CredentialCache = New CredentialCache()
myCache.Add(new Uri("http://www.contoso.com/"), "Basic", myCred)
myCache.Add(new Uri("http://app.contoso.com/"), "Basic", myCred)

' Set the credentials on the XmlUrlResolver object.
Dim resolver As XmlUrlResolver = New XmlUrlResolver()
resolver.Credentials = myCache

' Compile the style sheet.
Dim xslt As XslCompiledTransform = New XslCompiledTransform()
xslt.Load("http://serverName/data/xsl/order.xsl", XsltSettings.Default, resolver)

See Also

XslCompiledTransform
XsltSettings
XSLT Transformations