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">
<xsl:variable name="date1" select="//date1"/>
<xsl:variable name="date2" select="//date2"/>
<msxsl:script language="C#" implements-prefix="cs">
<![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));
}
]]>
</msxsl:script><br>
<xsl:template match="datediff"><br>
<xsl:element name="{local-name()}"><br>
<xsl:value-of select="cs:datediff($date1, $date2)" /><br>
</xsl:element><br>
</xsl:template><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 ..
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!
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
).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With