How to enable XSLT scripting in C# ..?

As Steve cooper has mentioned .. you need to enable the XSLT script .. and here is the way to do it:

first define a new settings instance:

var settings = new XsltSettings();

then enable the script

settings.EnableScript = true;

Create the XslCompiledTransform object and load the style sheet, passing in the settings object.


In the MSDN Documentation it says "XSLT scripting is disabled by default. XSLT scripting should be enabled only if you require script support and you are working in a fully trusted environment."

This is probably your problem. Try loading the transform like this;

XslCompiledTransform xslt = new XslCompiledTransform();

// Disable script blocks and the document() function
// if a stylesheet came from an untrusted source
string untrustedUri = @"http://www.untrusted-site.com/meow.xsl";
XmlResolver secureResolver = new XmlSecureResolver(new XmlUrlResolver(), untrustedUri);
xslt.Load(untrustedUri, XsltSettings.Default, secureResolver);

// Enable script blocks and the document() function
// if a trusted stylesheet needs them
xslt.Load(@"C:\MyProject\purr.xsl", XsltSettings.TrustedXslt, new XmlUrlResolver());

You could add some detail to your question, too; can you say how you are able to do it manually? What program or engine are you using? For instance, XMLSpy uses a different transform engine from the .Net framework, so XSL files can be incompatable.


Define the settings variable Enabling Script Mode and then use it in the load process.

var settings = new XsltSettings();
settings.EnableScript = true;

XslCompiledTransform xslt = new XslCompiledTransform();
xslt.Load("input.xsl", settings , null);

It worked for me. Regards!

Tags:

C#

Xslt