2013-03-11 202 views
0

我是新手到xslt的東西。我一直在努力與dateTime格式。我需要的是 從12/2/2011 2:57:18變換爲12022011T02:57:18xslt轉換日期時間格式

你怎麼能做到這一點?我真的很感謝你的幫助。請舉個好例子!感謝您的時間:)

回答

0

這將做到這一點:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:output method="xml" indent="yes" omit-xml-declaration="yes" /> 

    <xsl:template match="/"> 
    <xsl:call-template name="FormatDateTime"> 
     <xsl:with-param name="dateTime" select="'12/2/2011 2:57:18 AM'" /> 
    </xsl:call-template> 
    </xsl:template> 

    <xsl:template name="FormatDateTime"> 
    <xsl:param name="dateTime" /> 

    <xsl:call-template name="FormatDate"> 
     <xsl:with-param name="date" select="substring-before($dateTime, ' ')" /> 
    </xsl:call-template> 
    <xsl:text>T</xsl:text> 
    <xsl:call-template name="FormatTime"> 
     <xsl:with-param name="time" select="substring-after($dateTime, ' ')" /> 
    </xsl:call-template> 
    </xsl:template> 

    <xsl:template name="FormatTime"> 
    <xsl:param name="time" /> 
    <xsl:variable name="timeOnly" select="substring-before($time, ' ')" /> 
    <xsl:variable name="amPm" select="substring-after($time, ' ')" /> 
    <xsl:variable name="hour" select="substring-before($time, ':')" /> 
    <xsl:variable name="hourAdjusted" select="$hour mod 12 + 12 * ($amPm = 'PM')" /> 
    <xsl:variable name="afterHour" select="substring-after($timeOnly, ':')" /> 
    <xsl:variable name="minutes" select="substring-before($afterHour, ':')" /> 
    <xsl:variable name="seconds" select="substring-after($afterHour, ':')" /> 

    <xsl:value-of select="concat(format-number($hourAdjusted, '00'), ':', 
           format-number($minutes, '00'), ':', 
           format-number($seconds, '00'))"/> 
    </xsl:template> 

    <xsl:template name="FormatDate"> 
    <xsl:param name="date" /> 
    <xsl:variable name="month" select="substring-before($date, '/')" /> 
    <xsl:variable name="afterMonth" select="substring-after($date, '/')" /> 
    <xsl:variable name="day" select="substring-before($afterMonth, '/')" /> 
    <xsl:variable name="year" select="substring-after($afterMonth, '/')" /> 

    <xsl:value-of select="concat(format-number($month, '00'), 
           format-number($day, '00'), 
           $year)"/> 
    </xsl:template> 
</xsl:stylesheet> 

這XSLT中有一個樣本值,所以在運行時,它會產生:

12022011T02:57:18 

但你確定你想要的結果格式是MMddyyyy,而不是yyyy-MM-dd?如果你真的想後,您可以更改此部分:

<xsl:value-of select="concat(format-number($month, '00'), 
           format-number($day, '00'), 
           $year)"/> 

這樣:

<xsl:value-of select="concat($year, '-', 
           format-number($month, '00'), '-' 
           format-number($day, '00'))"/> 
+0

我主張yyyy-MM-dd因爲這個:https://xkcd.com/1179/ – sschrass 2013-03-11 14:37:31

+0

非常感謝。您的提示非常有用。我很感激 :-) – user1358072 2013-03-18 13:34:20

0

XML樣本

<?xml version="1.0" encoding="utf-8"?> 
<root> 
    <mydate atribdate="12/05/2010 22:10:10">01/10/2012 12:33:00</mydate> 
    <mydate atribdate="">12/12/2012 12:33:00</mydate>  
</root> 

XSL樣品XSLT 1.0

<?xml version="1.0"?> 
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output method="xml" indent="yes"/> 
    <xsl:template match = "/"> 
     <xsl:apply-templates select="root/mydate" />      
    </xsl:template> 
    <xsl:template match="mydate">   
      date element---><xsl:value-of select="translate(translate(.,'/',''),' ','T')"/>   
    date attirbute---><xsl:value-of select="translate(translate(@atribdate,'/',''),' ','T')"/> 
    </xsl:template> 
    </xsl:stylesheet> 

輸出xml

<?xml version="1.0" encoding="UTF-8"?>   
    date element---&gt;01102012T12:33:00   
    date attirbute---&gt;12052010T22:10:10   
    date element---&gt;12122012T12:33:00   
date attirbute---&gt;