代码之家  ›  专栏  ›  技术社区  ›  Rookie Programmer Aravind

使用XslCompiledTransform时发生XSLT编译错误。从应用程序间接调用Load

  •  1
  • Rookie Programmer Aravind  · 技术社区  · 14 年前

    我有一个用C写的组件。此外,它还对收集的XML数据执行XSL转换。当我使用另一个使用组件的C项目测试这个特性时,它工作得很好。但是,当我将组件导出为COM组件并尝试从应用程序中使用此功能时,它在xslcompiledTransform.load命令上失败,并出现xslt编译错误。

    这是C代码: (click_me)

    我得到的错误被复制到一个文件中。请在这里找到: (click_me)

    XSLT文件以及模板的数量还包括一些高级计算所用的“C脚本”,而XSLT不能这样做。

    下面是我使用的典型XSL代码:

    <?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>
    

    XML内容:

    <?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 回复  |  直到 14 年前
        1
  •  2
  •   Dimitre Novatchev    14 年前

    我希望用对扩展函数(传递给转换的扩展对象的方法)的调用替换内联脚本可以解决这个问题。

    建议优先使用扩展函数而不是内联脚本。 如果在IIS服务器环境中广泛使用内联脚本,则会导致(并且已经观察到)内存泄漏,最终导致服务器停机。这是因为XSLCompiledTransform将脚本编译为动态DLL,在回收IIS之前无法卸载这些DLL。