2015-11-03 76 views
0

試圖從日期計算星期幾。在互聯網上發現了一些例子,但我看到這個數字而不是一週中的某一天:NaN。計算星期幾XSL

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 

    <xsl:strip-space elements="*"/> 
    <xsl:output indent="yes"/> 

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

    <xsl:template match="date"> 
    <xsl:copy> 
     <xsl:call-template name="date-format"> 
     <xsl:with-param name="date-time" select="."/> 
     </xsl:call-template> 
    </xsl:copy> 
    </xsl:template> 

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

    <xsl:variable name="a" select="floor((14 - $month) div 12)"/> 
    <xsl:variable name="y" select="$year - $a"/> 
    <xsl:variable name="m" select="$month + 12 * $a - 2"/> 

    <xsl:value-of select="($day + $y + floor($y div 4) - floor($y div 100) 
    + floor($y div 400) + floor((31 * $m) div 12)) mod 7"/> 

    </xsl:template> 

</xsl:stylesheet> 

<?xml version="1.0" encoding="UTF-8"?> 
<?xml-stylesheet href="test2.xsl" type="text/xsl" ?> 


<document> 
    <date>2013-01-01</date> 
    <date>2013-05-24</date> 
    <date>2013-12-25</date> 
    <date>1957-07-13</date> 
    <date>1776-07-04</date> 
</document> 
+0

轉換輸入示例的預期結果是什麼? –

+0

從0到6的整數(星期日到星期六) – Maxim

+0

是的,但是你想把這些數字放在哪裏? –

回答

1

以下樣式表:

XSLT 1.0

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

<xsl:template match="date"> 
    <xsl:call-template name="day-of-week"> 
     <xsl:with-param name="date" select="."/> 
    </xsl:call-template> 
</xsl:template> 

<xsl:template name="day-of-week"> 
    <xsl:param name="date"/> 

    <xsl:variable name="year" select="substring($date,1, 4)"/> 
    <xsl:variable name="month" select="substring($date, 6, 2)"/> 
    <xsl:variable name="day" select="substring($date, 9, 2)"/> 

    <xsl:variable name="a" select="floor((14 - $month) div 12)"/> 
    <xsl:variable name="y" select="$year + 4800 - $a"/> 
    <xsl:variable name="m" select="$month + 12*$a - 3"/> 

    <xsl:variable name="JDN" select="$day + floor((153*$m + 2) div 5) + 365*$y + floor($y div 4) - floor($y div 100) + floor($y div 400) - 32045" /> 

    <xsl:value-of select="($JDN + 1) mod 7" /> 
</xsl:template> 

</xsl:stylesheet> 

當施加到您的輸入例如:

<document> 
    <date>2013-01-01</date> 
    <date>2013-05-24</date> 
    <date>2013-12-25</date> 
    <date>1957-07-13</date> 
    <date>1776-07-04</date> 
</document> 

將返回以下結果:

<?xml version="1.0" encoding="UTF-8"?> 
25364 

注意

與你嘗試的主要問題是:

<xsl:param name="date" select="substring-before($date-time,'T')"/> 

您輸入有特定日期,沒有日期,時間,和它們不包含 「T」 。

+0

爲什麼不把它變成「 - 32044」? – LadyCygnus

+0

@LadyCygnus由於Julian日號是標準。 –