Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to enable XSLT scripting in C# ..?

Tags:

c#

xslt

I have modified the title of the question after finding the answer :) :P

I am loading an XML file and an XSL file by a C# program and triggering the XSL transformation .. here is the code for it:

static void Main(string[] args)
{

    // Create the XslCompiledTransform and load the stylesheet.
    XslCompiledTransform xslt = new XslCompiledTransform();
    xslt.Load("input.xsl");    //located in Debug folder
    //Load XSL argument list
    XsltArgumentList xslArg = new XsltArgumentList();

    // Transform the file.
    using (XmlWriter w = XmlWriter.Create("output.xml"))
    {
        xslt.Transform("input.xml", xslArg, w);    //located in Debug folder
    }
}

The error is I am not able to load XML file.

The XSL file contains some C# code which is meant to calculate the difference between two DateTime strings .. well, I can transform the XML file manually using the same XSL file .. But when I try to trigger the transformation using C# code .. then it says "It can't load XML file"

Here is my (part of) XSL code ..

<span bgcolor="#EEEEEE">

   &lt;xsl:variable name="date1" select="//date1"/&gt;
   &lt;xsl:variable name="date2" select="//date2"/&gt;

   &lt;msxsl:script language="C#" implements-prefix="cs"&gt;
   &lt;![CDATA[
         public string datediff(string date1, string date2) {

            DateTime startTime = new DateTime(Convert.ToInt32(date1.Substring(6, 4)), Convert.ToInt32(date1.Substring(0, 2)), Convert.ToInt32(date1.Substring(3, 2)), Convert.ToInt32(date1.Substring(11, 2)), Convert.ToInt32(date1.Substring(14, 2)), Convert.ToInt32(date1.Substring(17, 2)), 0);

            DateTime endTime = new DateTime(Convert.ToInt32(date2.Substring(6, 4)), Convert.ToInt32(date2.Substring(0, 2)), Convert.ToInt32(date2.Substring(3, 2)), Convert.ToInt32(date2.Substring(11, 2)), Convert.ToInt32(date2.Substring(14, 2)), Convert.ToInt32(date2.Substring(17, 2)), 0);

    return(endTime.Subtract(startTime));

         }
     ]]>
   &lt;/msxsl:script&gt;<br>
   &lt;xsl:template match="datediff"&gt;<br>
    &lt;xsl:element name="{local-name()}"&gt;<br>
      &lt;xsl:value-of select="cs:datediff($date1, $date2)" /&gt;<br>
    &lt;/xsl:element&gt;<br>
   &lt;/xsl:template&gt;<br></span>

Is that, because of script(C# code to calculate date diff..) I am getting this error?
By the way C# code runs perfectly when I use some other input XML and XSL files ..

please help me to overcome this error ..

like image 925
InfantPro'Aravind' Avatar asked Dec 09 '09 09:12

InfantPro'Aravind'


4 Answers

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.

like image 98
InfantPro'Aravind' Avatar answered Oct 10 '22 02:10

InfantPro'Aravind'


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.

like image 33
Steve Cooper Avatar answered Oct 10 '22 02:10

Steve Cooper


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!

like image 33
David Castro Avatar answered Oct 10 '22 00:10

David Castro


I would suggest trying to load the XML file completely separately - I wouldn't be surprised to find that this had nothing at all to do with XSL, and everything to do with it not being able to find the file or something similar.

Try loading the XML file into an XmlDocument and checking that it looks correct. If that works, use the overload accepting an IXPathNavigable as input (XmlDocument implements IXPathNavigable).

like image 2
Jon Skeet Avatar answered Oct 10 '22 01:10

Jon Skeet