开发者

XSLT compile error when using XslCompiledTransform.Load indirectly called from an application

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>


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜