我有以下xml:
<root>
<Test>tested</Test>
</root>
现在,我想以YYYY-MM-DD-HH.MI.Sec的格式添加当前日期时间戳。使用XSLT将Ms转换为上述xml的新节点。例如,我得到的xml应该如下所示:
<root>
<Test>tested</Test>
<dateTimeStamp>2014-05-21-01.25.32.000000</dateTimeStamp>
</root>
有谁能帮我一下吗?你能不能把XSLT 1.0的代码也加进去,这样我就能找到区别了?我给它+1
您可以使用format-dateTime()
…
<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>
<root>
<Test>tested</Test>
<dateTimeStamp>2014-05-21-01.44.58.312</dateTimeStamp>
</root>
试试这样写:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="2.0">
<xsl:strip-space elements="*"/>
<xsl:output indent="yes" omit-xml-declaration="yes"/>
<xsl:template match="/root">
<xsl:variable name="timeNoMs" select="translate(substring-before(substring-before(string(current-time()), '+'), '.'), ':', '.')"/>
<xsl:variable name="timeMs" select="format-number(number(substring-after(substring-before(string(current-time()), '+'), '.')), '##0000')"/>
<xsl:copy>
<xsl:copy-of select="node()|@*"/>
<dateTimeStamp><xsl:value-of select="concat(substring-before(string(current-date()), '+'), '-', $timeNoMs, $timeMs)"/></dateTimeStamp>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>