日期和时间转换 [英] Date and time conversions

查看:127
本文介绍了日期和时间转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是我收到例如连续格式的日期:

My problem is that I receive for example a date in a concatenated format:

Ex: 20050728

我必须通过我的xslt以可读格式检索它。

And I have to retrieve it in a readable format through my xslt.

Ex. 28 July 2005

我也有类似的问题时间。

I also have a similar question regards time.

Ex: 0004

要显示作为 00:04

如何完成?

推荐答案

使用 XPath substring() 功能如下面的解决方案

Use the XPath substring() function as shown in the solution below:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="text"/>
 <!--                                           -->
 <xsl:variable name="vrMonths">
   <m>January</m>
   <m>February</m>
   <m>March</m>
   <m>April</m>
   <m>May</m>
   <m>June</m>
   <m>July</m>
   <m>August</m>
   <m>September</m>
   <m>October</m>
   <m>November</m>
   <m>December</m>
 </xsl:variable>
 <!--                                           -->
 <xsl:variable name="vMonths" select=
  "document('')/*/xsl:variable[@name='vrMonths']/*"/>
<!--                                           -->
    <xsl:template match="date">
      <xsl:value-of select=
       "concat(substring(.,7), ' ',
             $vMonths[number(substring(current(),5,2))], ' ',
             substring(.,1,4))"
       />
    </xsl:template>
 <!--                                           -->
    <xsl:template match="time">
     <xsl:value-of select=
     "concat(substring(.,1,2),':',substring(.,3))"/>
    </xsl:template>
</xsl:stylesheet>

当上述转换应用于此XML文档时:

<t>
 <date>20050728</date>
 <time>0004</time>
</t>

想要的结果生成

 28 July 2005
 00:04

这篇关于日期和时间转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆