XSLT中的DateTime标记转换

时间:2014-07-02 20:27:24

标签: xml datetime xslt

我正在尝试使用Format Date函数执行我认为简单的XSLT转换。

我想将2012-01-18T11:47:41.877000000转换为YYYY / MM / DD格式。

所以我的样本数据集是:

<?xml version="1.0" encoding="UTF-8" ?>
<Users>
    <User>
        <FullName>Johnny Test</FullName>
        <DateHired>2012-01-18T11:47:41.877000000</DateHired>
    </User>
</Users>

我试图用日期改变日期:

<xsl:for-each select="Users/User">
<xsl:for-each select="*">
    <xsl:choose>
    <xsl:when test="local-name()='DateHired'">
        <Date>
            <Value>
                <xsl:value-of select="format-date(.,'YYYY/MM/DD')" /> 
            </Value>
        </Date>
    </xsl:when>
    </xsl:choose>
</xsl:for-each>
</xsl:for-each>

2 个答案:

答案 0 :(得分:0)

您需要使用format-dateTime(),因为您使用的是dateTime而不是日期。

您还需要更新picture string

示例:

<xsl:value-of select="format-dateTime(.,'[Y]/[M00]/[D00]')" />

答案 1 :(得分:0)

在我的初始帖子后,日期格式从2012-01-18T11:47:41.877000000更改为2012年1月18日我还需要格式为2012-01-18。

我最终接受了Michael Kay的建议并使用模板来解决这个问题。

我创建了一个名为reformatDate的模板:

  <xsl:template name="reformatDate">
    <xsl:param name="date" />
    <xsl:variable name="month" select="substring-before($date, '/')" />
    <xsl:variable name="day" select="substring-before(substring-after($date, '/'), '/')" />
    <xsl:variable name="year" select="substring-after(substring-after($date, '/'), '/')" />
    <xsl:value-of select="concat($year, '-', format-number($month, '00'), '-', format-number($day, '00'))"/>
  </xsl:template>

我使用名为date:

的参数调用了模板
<xsl:call-template name="reformatDate">
  <xsl:with-param name="date" select="."></xsl:with-param>
</xsl:call-template>

为了解决初始问题,我将创建一个类似的函数,但剥离以T开头的值(即时间部分)。此版本适用于XSLT vs 1,它没有vs 2的漂亮日期和日期时间函数。

相关问题