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

C xslt:如何调试此错误?

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

    我必须在我的XSLT文件中包含一个C脚本。因此 计算两个日期时间值之间的差 (并将其添加到其他值中)。我 需要接受所有可能的日期格式 …我本来打算用XSLT来做的……但由于XSLT不允许这样做(含蓄地)。我找到了调用C脚本的补救方法。我必须从许多不同的C程序调用XSL转换。所以在所有调用C代码中编写这段代码会很痛苦(相当不可能)。(你能找到一些补救办法吗?

    这是XSLT代码。

     <xsl:variable name="date1" select="//date1"/>
      <xsl:variable name="date2" select="//date2"/>
      <msxsl:script language="C#" implements-prefix="cs">
        <![CDATA[
    
        private static string[] formats = new string[]
        {
            "MM/dd/yyyy HH:mm:ss tt",
            "MM/dd/yyyy HH:mm:ss",
            "MM/dd/yyyy H:mm:ss tt",
            "MM/dd/yyyy H:mm:ss",
    "M/dd/yyyy HH:mm:ss tt",
    "M/dd/yyyy HH:mm:ss",
    "M/dd/yyyy H:mm:ss tt",
    "M/dd/yyyy H:mm:ss",
    "MM/d/yyyy HH:mm:ss tt",
    "MM/d/yyyy HH:mm:ss",
    "MM/d/yyyy H:mm:ss tt",
    "MM/d/yyyy H:mm:ss", 
    "M/d/yyyy HH:mm:ss tt",   
    "M/d/yyyy HH:mm:ss", 
    "M/d/yyyy H:mm:ss tt", 
    "M/d/yyyy H:mm:ss",      
        };
    
    
          public string datedif(string date1, string date2) {
    
    
                DateTime startTime;
                DateTime endTime;
                DateTime ThirdDate;
    
    string date3="12/12/2009 00:00:00";
    
                DateTime.TryParseExact(date1, formats, new CultureInfo("en-US"), 
                                       DateTimeStyles.None, out startTime);
                DateTime.TryParseExact(date2, formats, new CultureInfo("en-US"), 
                                       DateTimeStyles.None, out endTime);
                DateTime.TryParseExact(date3, formats, new CultureInfo("en-US"), 
                                       DateTimeStyles.None, out ThirdDate);
    
    ThirdDate = ThirdDate.Add(endTime.Subtract(startTime));
    
    
    string result = ThirdDate.ToString("MM'/'dd'/'yyyy' 'HH':'mm':'ss");
    return(result);
    
             }
         ]]>
      </msxsl:script>
      <xsl:template match="datediff">
        <xsl:element name="{local-name()}">
          <xsl:value-of select="cs:datedif($date1, $date2)" />
        </xsl:element>
      </xsl:template>
    

    错误是:

    1. 当前上下文中不存在名称“dateTimeStyles”

    2. 找不到类型或命名空间名称“CultureInfo”(是否缺少using指令或程序集引用?)

    非常感谢……斯基特

    3 回复  |  直到 15 年前
        1
  •  6
  •   Jon Skeet    15 年前

    这些是C编译器的错误。你需要提供更多关于你想做什么的信息。

    听起来你是 至少 缺少的using指令:

    using System.Globalization;
    

    但是你没有说错误在哪里,或者你在做什么。

    编辑:如果不能更改其他内容,则将引用更改为 DateTimeStyles CultureInfo 完全合格:

    global::System.Globalization.DateTimeStyles
    global::System.Globalization.CultureInfo
    

    例如,你会有:

    DateTime.TryParseExact(date1, formats,
         new global::System.Globalization.CultureInfo("en-US"), 
         global::System.Globalization.DateTimeStyles.None, out startTime);
    
        2
  •  3
  •   Marc Gravell    15 年前

    将脚本移动到扩展对象中通常比较容易;通过在基本对象上公开常规方法并使用 XsltArgumentList 要在XSLT命名空间中公开它,请执行以下操作:

    XsltArgumentList args = new XsltArgumentList();
    args.AddExtensionObject("my-namespace", obj);
    

    在您的XSLT中,您将名称空间别名与名称空间相关联,并且只在XSLT中使用“alias:somefunc(…)”。这为您提供了更好的IDE支持(但您不能只使用XSLT);示例:

    using System;
    using System.Globalization;
    using System.IO;
    using System.Xml.XPath;
    using System.Xml.Xsl;
    public class MyExtensionObject
    {
        private static string[] formats = new string[] { /* ... */ };
        public string dateDiff(string x, string y)
        {
            CultureInfo culture = new CultureInfo("en-US");
            TimeSpan delta = DateTime.ParseExact(y, formats, culture, DateTimeStyles.None)
                - DateTime.ParseExact(x, formats, culture, DateTimeStyles.None);
            return delta.ToString();
        }
    }
    class Program
    {
        static void Main()
        {
            XsltArgumentList args = new XsltArgumentList();
            args.AddExtensionObject("my-namespace", new MyExtensionObject());
            XslTransform xslt = new XslTransform();
            xslt.Load("foo.xslt");
            using(TextWriter output = File.CreateText("out.txt")) {
                XPathDocument input = new XPathDocument("foo.xml");
                xslt.Transform(input, args, output);
            }
    
        }
    }
    

    XSLT:

    <?xml version="1.0" encoding="utf-8"?>
    <xsl:stylesheet version="1.0"
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        xmlns:bar="my-namespace" >
        <xsl:template match="xml">
          <result value="{bar:dateDiff(date1,date2)}"/>
        </xsl:template>
    </xsl:stylesheet>
    

    XML:

    <?xml version="1.0" encoding="utf-8" ?>
    <xml>
      <date1>01/01/2001 00:00:00</date1>
      <date2>2/2/2002 00:00:00</date2>
    </xml>
    
        3
  •  3
  •   Chris Ballance    15 年前

    错误在这一行:

    DateTime.TryParseExact(date1, formats, new CultureInfo("en-US"), DateTimeStyles.None, out startTime); 
    

    你提到过 System.Globalization 命名空间?

    正如@jonsket建议的,解决方法是更新您的引用,如下所示:

    global::System.Globalization.DateTimeStyles
    global::System.Globalization.CultureInfo