tags:

views:

204

answers:

1

I have a component written in C#. Among other things it performs XSL transform on XML data it collects. When I test this feature using another C# project that uses the component it works just fine. However when I export the component as a COM component and try to use this feature from an application it fails on the XslCompiledTransform.Load command with an XSLT compile error.

Here is the C# code: (click_me)

And the error I am getting is copied in a file. Please find it here: (click_me)

The XSLT file along with the number of templates also consists of "C# script" meant for some advanced calculations, which XSLT isn't capable of.

Here is the typical XSL code that I use:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl" xmlns:cs="urn:cs">
  <xsl:output method="xml" indent="no"/>

  <msxsl:script language="C#" implements-prefix="cs">
    <![CDATA[
     private static string[] formats_datetime = new string[]
     {
        "MM/dd/yyyy HH:mm:ss"
     };

     public string date_add(string date_str, string time_span_par)
      {
            DateTime date_value;
            TimeSpan time_span_var = TimeSpan.Parse(time_span_par);

            DateTime.TryParseExact(date_str, formats_datetime, new global::System.Globalization.CultureInfo("en-US"), global::System.Globalization.DateTimeStyles.None, out date_value);
            date_value = date_value.Add(time_span_var);
            string temp = date_value.ToString("MM/dd/yyyy HH:mm:ss");
            return(temp);
      }
]]>
  </msxsl:script>

  <xsl:template match="@* | node()">
    <xsl:copy>
      <xsl:apply-templates select="@*| node()"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="date_node">
    <xsl:variable name="date_in">
      <xsl:value-of select="."/>
    </xsl:variable>
    <xsl:variable name="period">
      <xsl:value-of select="'06:00:00'"/>
    </xsl:variable>
    <xsl:copy>
      <xsl:value-of select="cs:date_add($date_in, $period)"/>
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>

And the XML content:

<?xml version="1.0" encoding="utf-8"?>
<root>
  <node1>34</node1>
  <node2>23</node2>
  <date_node>12/31/2020 23:59:59</date_node>
  <child>
    <node1>text</node1>
    <date_node>12/31/2020 23:59:59</date_node>
    <grand_child>
      <date_node>12/31/2020 23:59:59</date_node>
    </grand_child>
  </child>
</root>
+1  A: 

I hope that replacing the inline scripts with calls to extension functions (methods of an extension object, that is passed to the transformation) will solve the problem.

It is recommended to use extension functions in preference over inline scripts. If inline scripts are used extensively in an IIS server environment, this can result (and this has been observed) to memory leaks that eventually bring down the server. This is because the XslCompiledTransform compiles the scripts into dynamic dlls that cannot be unloaded until IIS is recycled.

Dimitre Novatchev
@Dimitre, glad to receive your response. (though I guessed it) it is nice to read the words "XSLT has nothing to do with it" :))
infant programmer
@infant-programmer: You are always welcome. :)
Dimitre Novatchev
@Dimitre, sorry for the late response. I actually was unaware of your comment. Just now saw your comment, so have edited my post accordingly.Regards.
infant programmer
@Dimitre, the worst part of this bug is, It works all fine in Local machine. :(
infant programmer
@infant-programmer: The perfect person to answer your question is @Antosha. Just drop him a comment somewhere and let me know if he doesn't respond in 1-2 days -- then I'll alert him. I am not sure if he will have the time to reply on SO, so it will be useful if you could provide him with an email address -- he might have additional questions, which are faster answered directly.
Dimitre Novatchev
@infant-programmer: I updated my answer -- try not to use inline scripts at all. Replace them with extension functions.
Dimitre Novatchev
@Dimitre, thanks I will follow you up. :)
infant programmer