【问题标题】:Split up datetime element in XML to date and time using XSL使用 XSL 将 XML 中的 datetime 元素拆分为日期和时间
【发布时间】:2012-01-05 17:21:06
【问题描述】:

我的 XML 文件中有一个日期字符串为

<DateTime>20120105T103030-0600</DateTime>

我想使用 XSLT 将其更改为

<date>20120105</date>
<time>103030</time>

谁能帮助我使用 XSL 将 DateTime 元素拆分为日期和时间。谢谢

【问题讨论】:

    标签: xml xslt


    【解决方案1】:

    前段时间我也遇到过类似的问题:

      <xsl:template name="FormatDate">
        <xsl:param name="DateTime" />
        <date>
          <xsl:value-of select="substring($DateTime,1,8)" />
        </date>
        <time>
          <xsl:value-of select="substring($DateTime,10,6)" />
        </time>
      </xsl:template>
    

    用法:

    <xsl:call-template name="FormatDate">
      <xsl:with-param name="DateTime" select="LastWriteTime" />
    </xsl:call-template>
    

    请注意,我不在乎时区!

    【讨论】:

      【解决方案2】:

      以下样式表:

      <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
          <xsl:template match="DateTime">
              <date><xsl:value-of select="substring-before(., 'T')"/></date>
              <time><xsl:value-of 
                       select="substring-before(substring-after(., 'T'), '-')"/></time>
          </xsl:template>
      </xsl:stylesheet>
      

      应用于此示例输入:

      <root>
          <DateTime>20120105T103030-0600</DateTime>
      </root>
      

      生产:

      <date>20120105</date>
      <time>103030</time>
      

      【讨论】:

        猜你喜欢
        • 2021-11-08
        • 1970-01-01
        • 2022-06-14
        • 2013-10-18
        • 1970-01-01
        • 2020-11-30
        • 2014-05-10
        • 2018-08-28
        • 1970-01-01
        相关资源
        最近更新 更多