使用 XSLT 以 YYYY-MM-DD-HH.MI.Sec.Ms 格式获取当前时间

Ash*_*k.N 1 xml xslt datetime date

我有以下xml:

<root>
<Test>tested</Test>
</root>
Run Code Online (Sandbox Code Playgroud)

现在,我想使用 XSLT 将格式为 YYYY-MM-DD-HH.MI.Sec.Ms 的当前日期时间戳添加到上述 xml 的新节点。例如,我生成的 xml 应如下所示:

<root>
<Test>tested</Test>
<dateTimeStamp>2014-05-21-01.25.32.000000</dateTimeStamp> 
</root>
Run Code Online (Sandbox Code Playgroud)

有人可以帮我吗?

能否请您添加 XSLT 1.0 的代码,以便我可以找到不同之处?我会为此提供+1。

Dan*_*ley 6

您可以使用format-dateTime()...

XSLT 2.0

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output indent="yes"/>
    <xsl:strip-space elements="*"/>

    <xsl:template match="/*">
        <xsl:copy>
            <xsl:copy-of select="@*|node()"/>
            <dateTimeStamp>
                <xsl:value-of select="format-dateTime(current-dateTime(),'[Y0001]-[M01]-[D01]-[H01].[m01].[s].[f]')"/>
            </dateTimeStamp>
        </xsl:copy>        
    </xsl:template>

</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)

输出

<root>
   <Test>tested</Test>
   <dateTimeStamp>2014-05-21-01.44.58.312</dateTimeStamp>
</root>
Run Code Online (Sandbox Code Playgroud)

有关更多信息,请参阅http://www.w3.org/TR/2014/REC-xpath-functions-30-20140408/#rules-for-datetime-formatting