2012-02-23 21 views
3

我使用XSLT 1.0並需要將日期格式從dd/mm/yyyy轉換爲月yyyy,例如, 2011年1月。從dd/mm/yyyy到yyyy的XSLT日期翻譯

任何人都可以爲此提供一個樣本?我可以找到XSLT 2.0的示例,但我使用的是1.0。

謝謝, 科林。

+0

http://geekswithblogs.net/workdog/archive/2007/02/08/105858.aspx,涵蓋XSLT 1.0 – reevesy 2012-02-23 11:19:48

回答

4

用途:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
       xmlns:ext="metadata"> 
    <xsl:output method="text"/> 

    <ext:months> 
    <month>January</month> 
    <month>February</month> 
    <month>March</month> 
    <month>April</month> 
    <month>May</month> 
    <month>June</month> 
    <month>July</month> 
    <month>August</month> 
    <month>September</month> 
    <month>October</month> 
    <month>November</month> 
    <month>December</month> 
    </ext:months> 

    <xsl:variable name="date">23/02/2012</xsl:variable> 

    <xsl:template match="/"> 
    <xsl:value-of select="concat(document('') 
        //month[number(substring($date, 4, 2))], 
        ' ', 
        substring($date, 7, 4))"/> 
    </xsl:template> 

</xsl:stylesheet> 
+0

感謝您的迴應。我現在收到錯誤: 禁止執行'document()'功能。使用XsltSettings.EnableDocumentFunction屬性啓用它 我無法修改xslt的讀取或寫入方式,我該如何解決文檔('')聲明? – 2012-02-23 16:08:51

+0

@ColinBrown,如果你不能修改XSLT處理器的執行方式,你可以使用'',比如@DevNull建議。 – 2012-02-23 17:20:05

0

顯然它不可能用XSLT 1.0獨自..一個類似QI建議腳本..(C#)..

示例XML:

<?xml version="1.0" encoding="utf-8"?> 
<root> 
    <date>02/10/2012</date> 
    <date>2/9/2012</date> 
</root> 

示例XSLT:

<?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="yes"/> 

    <msxsl:script language="C#" implements-prefix="cs"> 
    <![CDATA[ 
     private static string[] formats = new string[] 
     { 
     "dd/MM/yyyy", 
     "dd/M/yyyy", 
     "d/M/yyyy", 
     "d/MM/yyyy", 
     "dd/MM/yy", 
     "dd/M/yy", 
     "d/M/yy", 
     "d/MM/yy" 
     }; 


      public string date_conv(string date1) 
     { 
      DateTime dDateTime; 
      DateTime.TryParseExact(date1, formats, new global::System.Globalization.CultureInfo("en-US"), global::System.Globalization.DateTimeStyles.None, out dDateTime); 
      return(String.Format("{0:MMMM yyyy}", dDateTime)); 
      } 
    ]]> 
    </msxsl:script> 
    <xsl:template match="@* | node()"> 
    <xsl:copy> 
     <xsl:apply-templates select="@* | node()"/> 
    </xsl:copy> 
    </xsl:template> 

    <xsl:template match="date"> 
    <xsl:copy> 
     <xsl:value-of select="cs:date_conv(.)"/> 
    </xsl:copy> 
    </xsl:template> 
</xsl:stylesheet> 

結果輸出:

<?xml version="1.0" encoding="utf-8"?> 
<root> 
    <date>October 2012</date> 
    <date>September 2012</date> 
</root> 

腳本可以駐留在同一個文件中(像我有它在我的示例XSLT代碼),或者如果觸發XSLTransformation代碼是C#然後將同樣的代碼在調用的地方:)

+0

「顯然它不可能單獨使用XSLT 1.0」? – 2012-02-23 16:56:27

+0

@DevNull,:)以及我認爲日期是在上述'格式'..此代碼使用tryparse方法,它可以消化並將其轉換爲所需的格式..在您的方法只能讀取「dd/MM/yyyy 「好吧,我把我的話回來!不可能不是一個不錯的選擇:) – 2012-02-24 02:25:27

3

我喜歡基里爾的回答更好,但這裏有一個不使用document()

XML輸入

<doc> 
    <date>01/01/2012</date> 
    <date>01/02/2011</date> 
    <date>01/03/2010</date> 
    <date>01/04/2009</date> 
</doc> 

XSLT 1.0

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:output indent="yes"/> 
    <xsl:strip-space elements="*"/> 

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

    <xsl:template match="date"> 
    <xsl:variable name="month"> 
     <xsl:call-template name="getMonth"> 
     <xsl:with-param name="monthNbr" select="number(substring-before(substring-after(.,'/'),'/'))"/> 
     </xsl:call-template> 
    </xsl:variable> 
    <xsl:variable name="year"> 
     <xsl:call-template name="getYear"> 
     <xsl:with-param name="date" select="."/> 
     </xsl:call-template> 
    </xsl:variable> 
    <date><xsl:value-of select="concat($month,' ',$year)"/></date> 
    </xsl:template> 

    <xsl:template name="getMonth"> 
    <xsl:param name="monthNbr"/> 
    <xsl:choose> 
     <xsl:when test="$monthNbr=1">January</xsl:when> 
     <xsl:when test="$monthNbr=2">February</xsl:when> 
     <xsl:when test="$monthNbr=3">March</xsl:when> 
     <xsl:when test="$monthNbr=4">April</xsl:when> 
     <xsl:when test="$monthNbr=5">May</xsl:when> 
     <xsl:when test="$monthNbr=6">June</xsl:when> 
     <xsl:when test="$monthNbr=7">July</xsl:when> 
     <xsl:when test="$monthNbr=8">August</xsl:when> 
     <xsl:when test="$monthNbr=9">September</xsl:when> 
     <xsl:when test="$monthNbr=10">October</xsl:when> 
     <xsl:when test="$monthNbr=11">November</xsl:when> 
     <xsl:when test="$monthNbr=12">December</xsl:when> 
    </xsl:choose> 
    </xsl:template> 

    <xsl:template name="getYear"> 
    <xsl:param name="date"/> 
    <xsl:variable name="year" select="substring-after($date,'/')"/> 
    <xsl:choose> 
     <xsl:when test="contains($year,'/')"> 
     <xsl:call-template name="getYear"> 
      <xsl:with-param name="date" select="$year"/> 
     </xsl:call-template> 
     </xsl:when> 
     <xsl:otherwise> 
     <xsl:value-of select="$year"/>   
     </xsl:otherwise> 
    </xsl:choose> 
    </xsl:template> 
</xsl:stylesheet> 

XML輸出

<doc> 
    <date>January 2012</date> 
    <date>February 2011</date> 
    <date>March 2010</date> 
    <date>April 2009</date> 
</doc> 

另外,我用的模板一年,而不是substring()處理月/天沒有前導零。

+0

感謝你,你介意如何從現有的xslt文件中調用函數嗎?我給了模板名稱「日期」,並試圖這樣做,但它不工作: ' ' 2012-02-23 21:35:43

+0

@ColinBrown - 」不工作「是什麼意思?你有錯誤嗎?你得到任何輸出? 'p:StartDate'是你正在做'call-template'的上下文的直接子對象嗎? – 2012-02-24 00:32:17

+0

另外,我在我的解決方案中有兩個命名模板;您添加到XSLT的哪個模板? – 2012-02-24 00:37:19