当前-dateTime()到数字

当前-dateTime()到数字,date,xslt,type-conversion,Date,Xslt,Type Conversion,我需要将当前日期时间转换为一个数字,因为输出只允许整数或long。我该怎么做 我的期望输出: <?xml version="1.0" encoding="utf-8"?> <currentDate>NaN</currentDate> 我的XSLT: <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:soapenv="http://schemas.xmlsoap.

我需要将当前日期时间转换为一个数字,因为输出只允许整数或long。我该怎么做

我的期望输出:

<?xml version="1.0" encoding="utf-8"?>
<currentDate>NaN</currentDate>
我的XSLT:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions" version="2.0">
    <xsl:output method="xml" encoding="utf-8" indent="yes"/>
    <xsl:variable name="version" select="'1.0'"/>

    <xsl:template match="/">
        <xsl:element name="currentDate">
            <xsl:value-of select="number(current-dateTime())"/>
        </xsl:element>
    </xsl:template>        

</xsl:stylesheet>
但我明白了:

<?xml version="1.0" encoding="utf-8"?>
<currentDate>NaN</currentDate>
当我删除这个数字时,我确实得到了时间戳,但我需要一个干净的数字

<?xml version="1.0" encoding="utf-8"?>
<currentDate>2014-01-15T07:01:02.526+01:00</currentDate>

如何将时间戳转换为一个干净的数字?

在XSLT2.0中,可以使用并重新格式化日期:只需使用不带分隔符的picture参数


或者,假设所有组件值都已经用零填充以保持固定长度,您可以简单地将分隔符转换掉

如前所述,要将2014-01-15T07:01:02.526+01:00转换为20140115070102526,您可以使用translate函数,这将与XSLT 1.0一起使用:

<xsl:value-of select="translate(substring(string(current-dateTime()), 1, 23), '-:T.', '')"/>
或者,您可以计算当前日期和时间与任意开始之间的持续时间,并将此持续时间除以毫秒以获得自该日期起的毫秒数:

<xsl:value-of select="(current-dateTime() - xs:dateTime('1971-01-01T00:00:00')) div xs:dayTimeDuration('PT0.001S')"/>

是您想要的unix样式整数时间的数字吗?不,这不是必需的。对于上面的时间戳2014-01-15T07:01:02.526,2014011507102526是可以的。我使用了这个:@FiveO,因为我没有建议使用replace,所以它的代码在我的答案中没有位置。我使用了这个:小更正:current dateTime是一个2.0函数,所以它在1.0中不起作用。