【问题标题】:Convert date from DD-MMM-YYYY to YYYYMMDD format in xslt 1.0在 xslt 1.0 中将日期从 DD-MMM-YYYY 转换为 YYYYMMDD 格式
【发布时间】:2013-05-10 14:08:52
【问题描述】:

我们如何在 XSLT 中将日期格式从 DD-MMM-YYYY 转换为 YYYY-MM-DD。

2013 年 1 月 10 日至 20130110

在 XSLT 1.0 中

【问题讨论】:

  • 我认为最好的办法是提取带有子字符串的部分,并用几个选择/何时转换 MMM

标签: xslt


【解决方案1】:

使用xsl:choose 元素非常简单,不需要任何扩展。

这个样式表

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

  <xsl:strip-space elements="*"/>
  <xsl:output method="xml" indent="yes"/>

  <xsl:template match="/root">
    <xsl:copy>
      <xsl:apply-templates/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="date">
    <xsl:copy>
      <xsl:call-template name="date">
        <xsl:with-param name="dd-mmm-yyyy" select="."/>
      </xsl:call-template>
    </xsl:copy>
  </xsl:template>

  <xsl:template name="date">
    <xsl:param name="dd-mmm-yyyy"/>
    <xsl:variable name="dd" select="substring-before($dd-mmm-yyyy, '-')"/>
    <xsl:variable name="mmm-yyyy" select="substring-after($dd-mmm-yyyy, '-')"/>
    <xsl:variable name="mmm" select="substring-before($mmm-yyyy, '-')"/>
    <xsl:variable name="yyyy" select="substring-after($mmm-yyyy, '-')"/>
    <xsl:value-of select="$yyyy"/>
    <xsl:choose>
      <xsl:when test="$mmm = 'JAN'">01</xsl:when>
      <xsl:when test="$mmm = 'FEB'">02</xsl:when>
      <xsl:when test="$mmm = 'MAR'">03</xsl:when>
      <xsl:when test="$mmm = 'APR'">04</xsl:when>
      <xsl:when test="$mmm = 'MAY'">05</xsl:when>
      <xsl:when test="$mmm = 'JUN'">06</xsl:when>
      <xsl:when test="$mmm = 'JUL'">07</xsl:when>
      <xsl:when test="$mmm = 'AUG'">08</xsl:when>
      <xsl:when test="$mmm = 'SEP'">09</xsl:when>
      <xsl:when test="$mmm = 'OCT'">10</xsl:when>
      <xsl:when test="$mmm = 'NOV'">11</xsl:when>
      <xsl:when test="$mmm = 'DEC'">12</xsl:when>
    </xsl:choose>
    <xsl:value-of select="$dd"/>
  </xsl:template>

</xsl:stylesheet>

应用于此XML数据

<?xml version="1.0" encoding="UTF-8"?>
<root>
  <date>10-JAN-2013</date>
  <date>04-JUL-1776</date>
  <date>31-DEC-1999</date>
</root>

产生这个输出

<?xml version="1.0" encoding="utf-8"?>
<root>
   <date>20130110</date>
   <date>17760704</date>
   <date>19991231</date>
</root>

【讨论】:

    【解决方案2】:

    如果您可以使用 node-set 作为 xslt 1.0 处理器的扩展,您可以试试这个。

    <?xml version="1.0"?>
    
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    
        xmlns:exsl="http://exslt.org/common"
        extension-element-prefixes="exsl">
    
    <xsl:output method="xml" indent="yes" />
    
    <xsl:variable name="date" select="'10-JAN-2013'" />
    
    <xsl:variable name="month_data_tmp">
        <month short="JAN" nr="01" />
        <!--- and so on for each month -->
    </xsl:variable>
    <xsl:variable name="month_data" select="exsl:node-set($month_data_tmp)" />
    
    
    <xsl:template name="format_date" >
        <xsl:param name ="date" />
        <xsl:variable name ="day" select="substring-before($date, '-')" />
        <xsl:variable name ="month_and_year" select="substring-after($date, '-')" />
        <xsl:variable name ="year" select="substring-after($month_and_year, '-')" />
        <xsl:variable name ="month" select="substring-before($month_and_year, '-')" />
        <xsl:value-of select="$year"/>
        <xsl:value-of select="$month_data/month[@short=$month]/@nr"/>
        <xsl:value-of select="$day"/>
    </xsl:template>
    
    <xsl:template match="/" >
        <xsl:call-template name="format_date" >
            <xsl:with-param name ="date" select="$date"/>
        </xsl:call-template>
    </xsl:template>
    

    输出将是:

    20130110
    

    将 doe 更新为命令中的其他问题:

    您可以在任何使用过&lt;xsl:value-of select="$date"/&gt;before的地方调用模板。

    <result>
        <xsl:call-template name="format_date" >
            <xsl:with-param name ="date" select="$date"/>
        </xsl:call-template>
    </result>
    

    或者您可以将结果分配给一个新变量并使用它。

    <xsl:variable name="newdate">
        <xsl:call-template name="format_date" >
            <xsl:with-param name ="date" select="$date"/>
        </xsl:call-template>
    </xsl:variable>
    
    <result>
        <xsl:value-of select="$newdate"/>
    </result>
    

    【讨论】:

    • 我们如何将它带入输出变量
    【解决方案3】:

    我意识到这个帖子现在已经过时了,但我想我会分享我的模板,以防有人想通过复制和粘贴来节省一些时间。 感谢上面的答案,因为这是基于它们。我的模板只是从 dd/MM/yyyy 转换为 yyyy-MM-dd;以及从 dd/MM/yyyy HH:mm:ss 到 yyyy-MM-ddTHH:mm:ss。

        <!--
      Template Name: Date
      Description: Takes a date in the format dd/MM/yyyy and outputs it in the format yyyy-mm-dd
                   Additionally will take a datetime in the format dd/MM/yyyy HH:mm:ss and output in the format yyyy-MM-ddTHH:mm:ss
      -->
      <xsl:template name="date">
        <xsl:param name="slashFormattedDate"/>
        <xsl:param name="hasTime"/>
        <xsl:variable name="dd" select="substring-before($slashFormattedDate, '/')"/>
        <xsl:variable name="monthYear" select="substring-after($slashFormattedDate, '/')"/>
        <xsl:variable name="mm" select="substring-before($monthYear, '/')"/>
        <xsl:variable name="yyyyTemp" select="substring-after($monthYear, '/')"/>
        <xsl:variable name="yyyy">
          <xsl:choose>
            <xsl:when test="$hasTime='Y'">
              <xsl:value-of select="substring-before($yyyyTemp, ' ')"/>
            </xsl:when>
            <xsl:otherwise>
              <xsl:value-of select="$yyyyTemp"/>
            </xsl:otherwise>
          </xsl:choose>
        </xsl:variable>
        <xsl:choose>
          <xsl:when test="$hasTime='Y'">
            <xsl:value-of select="$yyyy"/>-<xsl:value-of select="$mm"/>-<xsl:value-of select="$dd"/>T<xsl:value-of select="substring-after($yyyyTemp, ' ')"/>
          </xsl:when>
          <xsl:otherwise>
            <xsl:value-of select="$yyyy"/>-<xsl:value-of select="$mm"/>-<xsl:value-of select="$dd"/>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:template>
    

    【讨论】:

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