Calculating day of the week XSL

后端 未结 1 1483
北恋
北恋 2020-12-20 09:04

Trying to calculate day of the week number from date. Found some examples in the internet, but instead of day of the week number i see this : NaN.



        
相关标签:
1条回答
  • 2020-12-20 10:09

    The following stylesheet:

    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>
    

    when applied to your input example:

    <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>
    

    will return the following result:

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

    Note:

    The main problem with your attempt is this:

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

    Your input has dates, not date-times, and they do not contain "T".

    0 讨论(0)
提交回复
热议问题