Date and time conversions

2019-07-15 19:35发布

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

Ex: 20050728

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

To be displayed as 00:04

How is this done?

标签: xslt date
3条回答
疯言疯语
2楼-- · 2019-07-15 20:07
劫难
3楼-- · 2019-07-15 20:12

If you can use XPath 2.0, see this function reference.

查看更多
闹够了就滚
4楼-- · 2019-07-15 20:30

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>

When the above transformation is applied on this XML document:

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

the wanted result is produced:

 28 July 2005
 00:04
查看更多
登录 后发表回答