【问题标题】:convert date into XSLT将日期转换为 XSLT
【发布时间】:2018-01-06 18:23:32
【问题描述】:

我在 XML 文件中有一个日期(字符串)值,格式如下:

<UserValue title="fnd0EndDate" value="2017-01-18T09:18:19Z" />

我想使用 XSL 转换将字符串/日期转换成这种格式:

01JAN2017:09:18:19

如何在我的 XSL 转换中做到这一点?

【问题讨论】:

  • 是 xslt 版本 1 还是 2?
  • xslt 版本 1...

标签: xslt


【解决方案1】:

使用 XSLT 2.0(或 3),它可以像将 dateTime 值评估为 xs:dateTime 并使用 format-dateTime() 和所需图片一样简单:

format-dateTime(xs:dateTime(UserValue/@value), '[M01][MN,*-3][Y]:[H01]:[m01]:[s01]')

但是,对于 XSLT 1.0,您没有可用的 dateTime 方法,只能进行字符串解析:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    exclude-result-prefixes="xs"
    version="1.0">
  <xsl:output method="text"/>
  <xsl:variable name="MONTHS">
    <month>JAN</month>
    <month>FEB</month>
    <month>MAR</month>
    <month>APR</month>
    <month>MAY</month>
    <month>JUN</month>
    <month>JUL</month>
    <month>AUG</month>
    <month>SEP</month>
    <month>OCT</month>
    <month>NOV</month>
    <month>DEC</month>
  </xsl:variable>
  <xsl:template match="/">
    <xsl:variable name="val" select="//UserValue/@value"/>
    <xsl:variable name="month" select="substring($val, 6, 2)"/>
    <xsl:variable name="month-name" select="document('')/*/*/month[position() = number($month)]"/>
    <xsl:variable name="year" select="substring($val, 1, 4)"/>
    <xsl:variable name="time" select="substring($val, 12, 8)"/>

    <xsl:value-of select="concat($month, $month-name, $year, ':', $time)"/>
  </xsl:template>
</xsl:stylesheet>

【讨论】:

    猜你喜欢
    • 2022-01-11
    • 2017-10-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多