Xslt 如何转换xsl数据格式?

Xslt 如何转换xsl数据格式?,xslt,date-format,Xslt,Date Format,如何转换 2012年1月29日00:00 到 2012年1月29日,星期一 在xslt?源代码中: 我已经修改为将“1/20/2007 10:22:28 PM”转换为“2007年1月20日”…以节省任何认为它有用的人几分钟时间 <xsl:template name="FormatDate"> <!-- expected date format 1/20/2007 10:22:28 PM [OR] 01/20/2007 10:22:28 PM --> <xsl:par

如何转换

2012年1月29日00:00

2012年1月29日,星期一

在xslt?

源代码中:

我已经修改为将“1/20/2007 10:22:28 PM”转换为“2007年1月20日”…以节省任何认为它有用的人几分钟时间

<xsl:template name="FormatDate">
<!-- expected date format 1/20/2007 10:22:28 PM [OR] 01/20/2007 10:22:28 PM -->
<xsl:param name="DateTime" />
<!-- new date format January 20, 2007 -->
<xsl:variable name="mo">
<xsl:value-of select="substring-before($DateTime,'/')" />
</xsl:variable>
<xsl:variable name="day-temp">
<xsl:value-of select="substring-after($DateTime,'/')" />
</xsl:variable>
<xsl:variable name="day">
<xsl:value-of select="substring-before($day-temp,'/')" />
</xsl:variable>
<xsl:variable name="year-temp">
<xsl:value-of select="substring-after($day-temp,'/')" />
</xsl:variable>
<xsl:variable name="year">
<xsl:value-of select="substring($year-temp,1,4)" />
</xsl:variable>

<xsl:choose>
<xsl:when test="$mo = '1' or $mo = '01'">January</xsl:when>
<xsl:when test="$mo = '2' or $mo = '02'">February</xsl:when>
<xsl:when test="$mo = '3' or $mo = '03'">March</xsl:when>
<xsl:when test="$mo = '4' or $mo = '04'">April</xsl:when>
<xsl:when test="$mo = '5' or $mo = '05'">May</xsl:when>
<xsl:when test="$mo = '6' or $mo = '06'">June</xsl:when>
<xsl:when test="$mo = '7' or $mo = '07'">July</xsl:when>
<xsl:when test="$mo = '8' or $mo = '08'">August</xsl:when>
<xsl:when test="$mo = '9' or $mo = '09'">September</xsl:when>
<xsl:when test="$mo = '10'">October</xsl:when>
<xsl:when test="$mo = '11'">November</xsl:when>
<xsl:when test="$mo = '12'">December</xsl:when>
</xsl:choose>
<xsl:value-of select="' '"/>
<xsl:if test="(string-length($day) &lt; 2)">
<xsl:value-of select="0"/>
</xsl:if>
<xsl:value-of select="$day"/>
<xsl:value-of select="', '"/>
<xsl:value-of select="$year"/>
</xsl:template> 

一月
二月
前进
四月
也许
六月
七月
八月
九月
十月
十一月
十二月

来源:

我已经修改为将“1/20/2007 10:22:28 PM”转换为“2007年1月20日”…以节省任何认为它有用的人几分钟时间

<xsl:template name="FormatDate">
<!-- expected date format 1/20/2007 10:22:28 PM [OR] 01/20/2007 10:22:28 PM -->
<xsl:param name="DateTime" />
<!-- new date format January 20, 2007 -->
<xsl:variable name="mo">
<xsl:value-of select="substring-before($DateTime,'/')" />
</xsl:variable>
<xsl:variable name="day-temp">
<xsl:value-of select="substring-after($DateTime,'/')" />
</xsl:variable>
<xsl:variable name="day">
<xsl:value-of select="substring-before($day-temp,'/')" />
</xsl:variable>
<xsl:variable name="year-temp">
<xsl:value-of select="substring-after($day-temp,'/')" />
</xsl:variable>
<xsl:variable name="year">
<xsl:value-of select="substring($year-temp,1,4)" />
</xsl:variable>

<xsl:choose>
<xsl:when test="$mo = '1' or $mo = '01'">January</xsl:when>
<xsl:when test="$mo = '2' or $mo = '02'">February</xsl:when>
<xsl:when test="$mo = '3' or $mo = '03'">March</xsl:when>
<xsl:when test="$mo = '4' or $mo = '04'">April</xsl:when>
<xsl:when test="$mo = '5' or $mo = '05'">May</xsl:when>
<xsl:when test="$mo = '6' or $mo = '06'">June</xsl:when>
<xsl:when test="$mo = '7' or $mo = '07'">July</xsl:when>
<xsl:when test="$mo = '8' or $mo = '08'">August</xsl:when>
<xsl:when test="$mo = '9' or $mo = '09'">September</xsl:when>
<xsl:when test="$mo = '10'">October</xsl:when>
<xsl:when test="$mo = '11'">November</xsl:when>
<xsl:when test="$mo = '12'">December</xsl:when>
</xsl:choose>
<xsl:value-of select="' '"/>
<xsl:if test="(string-length($day) &lt; 2)">
<xsl:value-of select="0"/>
</xsl:if>
<xsl:value-of select="$day"/>
<xsl:value-of select="', '"/>
<xsl:value-of select="$year"/>
</xsl:template> 

一月
二月
前进
四月
也许
六月
七月
八月
九月
十月
十一月
十二月

I.XSLT 1.0解决方案(不生成一周中的某一天),比其他答案更简单、更短:

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

 <my:months>
  <m>Jan</m><m>Feb</m><m>Mar</m><m>Apr</m><m>May</m><m>Jun</m>
  <m>Jul</m><m>Aug</m><m>Sep</m><m>Oct</m><m>Nov</m><m>Dec</m>
 </my:months>

 <xsl:variable name="vMonthNames" select=
 "document('')/*/my:months/*"/>

 <xsl:template match="text()">
     <xsl:variable name="vnumMonth" select="substring-before(., '/')"/>

     <xsl:variable name="vDay" select=
     "substring-before(substring-after(., '/'), '/')"/>

     <xsl:variable name="vYear" select=
     "substring-before(substring-after(substring-after(., '/'), '/'), ' ')"/>

     <xsl:value-of select=
      "concat($vMonthNames[0+$vnumMonth], ' ',
              $vDay, ', ',
              $vYear
              )"/>
 </xsl:template>
</xsl:stylesheet>

II.在XSLT2.0中,可以使用非常强大的日期时间函数,例如

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

 <xsl:template match="text()">
     <xsl:variable name="vComps" select=
      "tokenize(., '/')"/>

    <xsl:variable name="vstdDate" select=
     "concat(substring-before($vComps[3], ' '), '-',
             $vComps[1], '-',
             $vComps[2]
          )"/>

  <xsl:sequence select=
      "format-date(xs:date($vstdDate), '[FNn], [MNn] [D], [Y]')"/>
 </xsl:template>
</xsl:stylesheet>

I.XSLT 1.0解决方案(不生成一周中的某一天),比其他答案更简单、更简短

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

 <my:months>
  <m>Jan</m><m>Feb</m><m>Mar</m><m>Apr</m><m>May</m><m>Jun</m>
  <m>Jul</m><m>Aug</m><m>Sep</m><m>Oct</m><m>Nov</m><m>Dec</m>
 </my:months>

 <xsl:variable name="vMonthNames" select=
 "document('')/*/my:months/*"/>

 <xsl:template match="text()">
     <xsl:variable name="vnumMonth" select="substring-before(., '/')"/>

     <xsl:variable name="vDay" select=
     "substring-before(substring-after(., '/'), '/')"/>

     <xsl:variable name="vYear" select=
     "substring-before(substring-after(substring-after(., '/'), '/'), ' ')"/>

     <xsl:value-of select=
      "concat($vMonthNames[0+$vnumMonth], ' ',
              $vDay, ', ',
              $vYear
              )"/>
 </xsl:template>
</xsl:stylesheet>

II.在XSLT2.0中,可以使用非常强大的日期时间函数,例如

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

 <xsl:template match="text()">
     <xsl:variable name="vComps" select=
      "tokenize(., '/')"/>

    <xsl:variable name="vstdDate" select=
     "concat(substring-before($vComps[3], ' '), '-',
             $vComps[1], '-',
             $vComps[2]
          )"/>

  <xsl:sequence select=
      "format-date(xs:date($vstdDate), '[FNn], [MNn] [D], [Y]')"/>
 </xsl:template>
</xsl:stylesheet>

对于这个极其复杂的解决方案,我深表歉意,但它将为您提供您在XSLT 1.0中想要的东西:

<xsl:variable name="months" select="'JanFebMarAprMayJunJulAugSepOctDec'" />
<xsl:variable name="weekdays"  select="'Monday   Tuesday  WednesdayThursday Friday   Saturday Sunday   '" />

<xsl:template match="date">
  <xsl:variable name="days">
    <xsl:value-of select="
      ((substring(.,7,4) - 1970) * 365)+floor((substring(.,7,4) - 1970) div 4)+
      substring('000,031,059,090,120,151,181,212,243,273,304,334,365',substring(.,1,2)*4-3,3)+
      (substring(.,4,2)-1)+
      (1-floor(((substring(.,7,4) mod 4) + 2) div 3))*floor((substring(.,1,2)+17) div 20)
    " />
  </xsl:variable>

  <xsl:value-of select="concat(
    normalize-space(substring($weekdays,(($days+3) mod 7) * 9 + 1, 9)),
    ', ',
    substring($months,substring(.,1,2) * 3 - 2, 3),
    ' ',
    substring(.,4,2) + 0,
    ', ',
    substring(.,7,4)
   )" />
</xsl:template>

变量“days”的构造使用了一个相当复杂的公式来确定自1970年1月1日以来的天数。从这里开始,添加3(因为1970年1月1日是星期四)并使用此图的mod 7从
工作日
变量和
substr
中获取一周中的某一天是很简单的


但是,如果您打算大量使用日期,请使用XSLT2

对于这个极其复杂的解决方案深表歉意,但它将为您提供您在XSLT 1.0中想要的东西:

<xsl:variable name="months" select="'JanFebMarAprMayJunJulAugSepOctDec'" />
<xsl:variable name="weekdays"  select="'Monday   Tuesday  WednesdayThursday Friday   Saturday Sunday   '" />

<xsl:template match="date">
  <xsl:variable name="days">
    <xsl:value-of select="
      ((substring(.,7,4) - 1970) * 365)+floor((substring(.,7,4) - 1970) div 4)+
      substring('000,031,059,090,120,151,181,212,243,273,304,334,365',substring(.,1,2)*4-3,3)+
      (substring(.,4,2)-1)+
      (1-floor(((substring(.,7,4) mod 4) + 2) div 3))*floor((substring(.,1,2)+17) div 20)
    " />
  </xsl:variable>

  <xsl:value-of select="concat(
    normalize-space(substring($weekdays,(($days+3) mod 7) * 9 + 1, 9)),
    ', ',
    substring($months,substring(.,1,2) * 3 - 2, 3),
    ' ',
    substring(.,4,2) + 0,
    ', ',
    substring(.,7,4)
   )" />
</xsl:template>

变量“days”的构造使用了一个相当复杂的公式来确定自1970年1月1日以来的天数。从这里开始,添加3(因为1970年1月1日是星期四)并使用此图的mod 7从
工作日
变量和
substr
中获取一周中的某一天是很简单的


但是,如果您打算大量使用日期,请使用XSLT2

检查这一点在很大程度上取决于它是XSLT1.0还是2.0——你真的需要说哪个。检查这一点在很大程度上取决于它是XSLT1.0还是2.0——你真的需要说哪个。