2008-10-07 138 views

回答

5

如果您可以使用EXSLT,则有several date functions可用。所有这些都是在Saxon中实现的,但是如果您使用的是MSXSL,Chris Bayes已将它们实现为扩展函数,您可以在msxsl:script元素中实际放置您的变换。他的实现从每个特定的日期函数页面链接。

week-in-year()您正在寻找的功能?

编辑:每JeniT的评论,有在同一地点用相同的功能星期 - 年()(这是她写的,我认为),这可能会更好地满足您需求的pure XSLT 1.0 template可用。

+0

如果您在该网站上查看,则会发现一个纯XSLT 1.0模板,可以为您执行此操作:http://www.exslt.org/date/functions/week-in- year/date.week-in-year.template.xsl – JeniT 2008-10-08 21:26:23

+0

所以有,我错过了,甚至更好 – 2008-10-08 23:23:50

-2

我在Visual Basic程序,所以我知道如何使用VB.NET做。看了你的XML日期为变量(姑且称之为SomeDate),然后构造一个新的。你知道日期是包含未知日期年初然后,你让则DateDiff功能做工作,告诉你周数

Dim SomeDate As Date = ReadDateFromXML() 
Dim YearStart As New Date(Year(SomeDate), 1, 1) 
Dim WeekNumber As Integer = DateDiff(DateInterval.WeekOfYear, YearStart, SomeDate) 
+0

它需要是纯粹的XSLT解决方案。我不能使用任何代码:( – 2008-10-07 15:17:45

-3

而在C#:

DateTime date = DateTime.Now; 
int week = date.DayOfYear/7; 
Console.WriteLine(week); 
+0

它需要是纯粹的XSLT解决方案我不能使用任何代码:( – 2008-10-07 15:16:16

+0

这是不正确的... 看看这里看看为什么: http://social.msdn.microsoft .com/Forums/en-US/vbgeneral/thread/e38accde-7eaa-462e-95d0-5a47b7dab832/ – 2009-03-29 11:23:02

1

如果您始终希望一周的同一天开始,那么一周计算会变得非常复杂,因为一年中的第一天总是在变化。有一个计算它的ISO标准,见this Wikipedia article

+0

我意识到这一点,但并不是必需的,Everyweek可以从星期天或星期一开始,我只需要计算两个日期之间的差异(以星期为单位) – 2008-10-07 15:20:19

0

查看Sal Mangano的XSLT食谱。有趣的是它可在books.google.com http://books.google.com/books?id=su4pWUPWwuEC&pg=PA125&lpg=PA125&dq=xslt+weeknumber&source=web&ots=nBBc3DVJYU&sig=l19cpOhwd9_PqrB72b9CCZk9wUA

了XSLT 2.0的方法是:

<xsl:function name="chkbk:calculate-week-number" as="xs:integer"> 
    <xsl:param name="date" as="xs:date" /> 
    <xsl:sequence select="xs:integer(format-date($date,'[W'))" /> 
</xsl:function> 

为1.0的方式,请参阅预览cookbox。顺便说一句,我只是搜索xslt weeknumber找到这个。

1

这是一个纯粹XSLT 1.0溶液

人们可以使用由Martin罗林森,附带了XSelerator(一个不错的XSLT IDE的datetime_lib.xsl样式表模块,最近提出sourceforge上自由使用)。您将不得不下载并安装此应用程序,然后您将找到大量额外的库和高级技术和解决方案样本。

datetime_lib.xsl文件上可以找到(典型安装):

              C:\ Program Files文件\ Marrowsoft \ Xselerator25 \的Samples \库\

从这个库,这里是名为“周编号”的模板:

<xsl:template name="week-number"> 
    <xsl:param name="year"/> 
    <xsl:param name="month"/> 
    <xsl:param name="day"/> 
    <!-- or --> 
    <xsl:param name="date" select="''"/> <!-- format: yyyymmdd or yyyy-mm-dd --> 
    <!-- or --> 
    <xsl:param name="julian-day" select="''"/> 
    <!-- trim down date --> 
    <xsl:variable name="tdate" select="translate($date,'-','')"/> 
    <!-- decide which params were passed --> 
    <xsl:variable name="yyyy"> 
     <xsl:choose> 
      <xsl:when test="string-length($date) > 0"><xsl:value-of select="substring($tdate,1,4)"/></xsl:when> 
      <xsl:when test="string-length($julian-day) > 0"> 
       <xsl:variable name="jdate"> 
        <xsl:call-template name="julian-day-to-date"> 
         <xsl:with-param name="julian-day" select="$julian-day"/> 
        </xsl:call-template> 
       </xsl:variable> 
       <xsl:value-of select="substring($jdate,1,4)"/> 
      </xsl:when> 
      <xsl:otherwise><xsl:value-of select="$year"/></xsl:otherwise> 
     </xsl:choose> 
    </xsl:variable> 
    <!-- get the julian day number --> 
    <xsl:variable name="jd"> 
     <xsl:choose> 
      <xsl:when test="string-length($julian-day) > 0"><xsl:value-of select="$julian-day"/></xsl:when> 
      <xsl:otherwise> 
       <xsl:call-template name="date-to-julian-day"> 
        <xsl:with-param name="year" select="$year"/> 
        <xsl:with-param name="month" select="$month"/> 
        <xsl:with-param name="day" select="$day"/> 
        <xsl:with-param name="date" select="$date"/> 
       </xsl:call-template> 
      </xsl:otherwise> 
     </xsl:choose> 
    </xsl:variable> 
    <!-- get the julian day number for the first working day of next year --> 
    <xsl:variable name="fyjd"> 
     <xsl:call-template name="first-day-of-year"> 
      <xsl:with-param name="year" select="$yyyy+1"/> 
      <xsl:with-param name="as-julian-day" select="true()"/> 
     </xsl:call-template> 
    </xsl:variable> 
    <!-- decide which the 'working' year for this date is --> 
    <xsl:variable name="start-jd"> 
     <xsl:choose> 
      <xsl:when test="$jd >= $fyjd"><xsl:value-of select="$fyjd"/></xsl:when> 
      <xsl:otherwise> 
       <xsl:call-template name="date-to-julian-day"> 
        <xsl:with-param name="date"> 
         <xsl:call-template name="first-day-of-year"> 
          <xsl:with-param name="year" select="$yyyy"/> 
         </xsl:call-template> 
        </xsl:with-param> 
       </xsl:call-template> 
      </xsl:otherwise> 
     </xsl:choose> 
    </xsl:variable> 
    <!-- final calc output --> 
    <xsl:value-of select="floor(($jd - $start-jd) div 7) + 1"/> 
</xsl:template>



下面是使用 “周数” 模板一个简单的XSLT转换:


 
<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:msxsl="urn:schemas-microsoft-com:xslt"> 

<xsl:import href= 
"C:\Program Files\Marrowsoft\Xselerator25\Samples\Libraries\datetime_lib.xsl"/> 

<xsl:output method="text"/> 

<xsl:template match="/"> 
    <xsl:call-template name="week-number"> 
     <xsl:with-param name="date" select="'2008-11-16'"/> 
    </xsl:call-template> 
</xsl:template> 
</xsl:stylesheet>

当任何源XML文档(未使用)应用,通缉结果产生:

     

希望这次的答案真的更有帮助。

干杯,

Dimitre Novatchev。