2009-12-11 61 views
1

我必須在我的XSLT文件中包含一個C#腳本。所以作爲來計算兩個dateTime值(並將其添加到其他值)之間的差異。我需要接受所有可能的格式的日期 ..我想這樣做與XSLT ..但作爲XSLT不允許這樣做(隱式)..我發現這個補救措施調用C#腳本..我必須從許多不同的C#程序調用XSL轉換..所以這將是痛苦(而不可能)寫在所有調用C#代碼的代碼.. :(你可以找到這個

這是XSLT代碼一些補救..

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指令或程序集引用?)

ThanQ非常..喬恩斯基特

+2

顯然Visual Studio調試器支持調試XSLT(斷點,...) – NDM 2009-12-11 15:40:11

+2

所以你要我們調試代碼爲你? – 2009-12-11 15:40:58

+0

我想我要改變菜單在我的IDE中添加請在每一個命令前 - 讓我的電腦更快樂的地方... – 2009-12-11 15:42:52

回答

6

這些是來自C#編譯器的錯誤。你需要提供更多關於你想要做什麼的信息。

這聽起來像你至少缺少using指令的:

using System.Globalization; 

,但你沒有說哪裏的錯誤,或者你在做什麼。

編輯:如果你不能別人改變什麼,然後換DateTimeStylesCultureInfo引用是完全合格的:

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); 
+0

我建議在編輯中解決方法,但 – 2009-12-11 17:11:34

+3

它們不是語句 - 我的觀點是腳本中的每一處使用「DateTimeStyles」的地方都使用「global :: System.Globalizat ion.DateTimeStyles「和CultureInfo相同。 – 2009-12-11 17:32:41

+0

不,你還沒有提供任何背景。這個XSLT腳本應用於什麼?爲什麼你不能只寫一個方法來完成這一切,並調用該方法? – 2009-12-11 17:35:31

3

該錯誤是在這條線上:

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

您是否參考了System.Globalization命名空間?

由於@JonSkeet建議,解決方法是更新您的參考,像這樣:

global::System.Globalization.DateTimeStyles 
global::System.Globalization.CultureInfo 
+0

@克里斯平衡,我覺得對不稱行爲感到抱歉..我不寫它來傳達你在未來幫助我..但我只是覺得..這不是一個適當的態度,我可以有感謝有意幫助的人..真的很抱歉.. – 2009-12-11 18:45:23

+0

不用擔心。這裏的大部分人都非常好,非常有幫助。不要只是回答那些不是100%正確的答案,要求澄清或更多信息,大多數響應者都樂意調整自己的答案,以更好地解決您的問題。我預留了明顯錯誤的答案。 – 2009-12-11 18:59:30

+0

oh .. thanx :) – 2009-12-12 05:16:59

3

它往往是更容易移動腳本到擴展的對象;您可以通過暴露一個基本的對象上一個普通的方法,以及使用XsltArgumentList揭露它在XSLT命名空間中做到這一點:

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

在XSLT,你與命名空間的命名空間的別名相關聯,只使用「別名:someFunc(...)「在xslt中。這給你更好的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> 
+0

不,我不能這樣做..我想這是用XSLT做的..但是因爲XSLT不是一種編程語言..我發現這種調用C#腳本的補救措施..我有從許多不同的C#程序中調用XSL轉換..所以在所有調用的C#代碼中編寫此代碼將是痛苦的(而不可能).. :(你能找到一些解決方法來爲你的答案... thanx .. – 2009-12-11 17:09:48

+0

然後使用顯式方法Jon提到不涉及任何使用指令。 – 2009-12-11 17:39:29

+0

ya ..它的完成.. – 2009-12-11 17:43:13