在XSLT中更改日期格式

时间:2017-07-24 13:37:02

标签: datetime xslt-1.0

我有一张XSLT表,它返回并格式化日期,如下所示:

datatables

理想情况下,我希望信息显示为' 7月8日星期二'。这可能吗?

当前的XSLT如下:

<result column="name" isNull="false">2017-08-08</result>
      <result column="display" isNull="false">08-08-2017</result>

XML:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"    version="1.0">
   <xsl:output method="xml" indent="yes" />
   <xsl:template match="@*|node()">
      <xsl:copy>
         <xsl:apply-templates select="@*|node()" />
      </xsl:copy>
   </xsl:template>
   <xsl:template match="ArrayOfdateItem">
      <date>
         <name>
            <xsl:apply-templates select="node()" />
     </name>
     <display>
        <xsl:value-of select="concat(substring(node(),9,2),'-',substring(node(),6,2),'-',substring(node(),1,4))" />
      </display>
    </date>
  </xsl:template>
</xsl:stylesheet>

1 个答案:

答案 0 :(得分:1)

纯粹在XSLT 1.0中这样做有点冗长。请考虑以下示例:

<强> XML

<dates>
    <date>2017-07-23</date>
    <date>2017-07-24</date>
    <date>2017-07-25</date>
    <date>2017-07-26</date>
    <date>2017-07-27</date>
    <date>2017-07-28</date>
    <date>2017-07-29</date>
</dates>

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="*"/>

<!-- identity transform -->
<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

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

<xsl:template name="format-date">
    <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:variable name="day-of-week" select="($JDN + 1) mod 7" />    

    <xsl:choose>
        <xsl:when test="$day-of-week=0">Sunday </xsl:when>
        <xsl:when test="$day-of-week=1">Monday </xsl:when>
        <xsl:when test="$day-of-week=2">Tuesday </xsl:when>
        <xsl:when test="$day-of-week=3">Wednesday </xsl:when>
        <xsl:when test="$day-of-week=4">Thursday </xsl:when>
        <xsl:when test="$day-of-week=5">Friday </xsl:when>
        <xsl:when test="$day-of-week=6">Saturday </xsl:when>
    </xsl:choose>

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

    <xsl:choose>
        <xsl:when test="$month=1"> January</xsl:when>
        <xsl:when test="$month=2"> February</xsl:when>
        <xsl:when test="$month=3"> March</xsl:when>
        <xsl:when test="$month=4"> April</xsl:when>
        <xsl:when test="$month=5"> May</xsl:when>
        <xsl:when test="$month=6"> June</xsl:when>
        <xsl:when test="$month=7"> July</xsl:when>
        <xsl:when test="$month=8"> August</xsl:when>
        <xsl:when test="$month=9"> September</xsl:when>
        <xsl:when test="$month=10"> October</xsl:when>
        <xsl:when test="$month=11"> November</xsl:when>
        <xsl:when test="$month=12"> December</xsl:when>
    </xsl:choose>
</xsl:template>

</xsl:stylesheet>

<强>结果

<?xml version="1.0" encoding="UTF-8"?>
<dates>
  <date>Sunday 23 July</date>
  <date>Monday 24 July</date>
  <date>Tuesday 25 July</date>
  <date>Wednesday 26 July</date>
  <date>Thursday 27 July</date>
  <date>Friday 28 July</date>
  <date>Saturday 29 July</date>
</dates>