Re: [xsl] current-dateTime()

Subject: Re: [xsl] current-dateTime()
From: Abel Braaksma <abel.online@xxxxxxxxx>
Date: Fri, 18 Apr 2008 16:39:09 +0200
Andrew Welch wrote:
I wrongly thought that the "implementation dependent" part meant the
processor could return different values for multiple calls to
current-dateTime()... instead of merely the freedom to choose the
instant at which it takes the fixed value.

It should be straightforward to write an extension function instead...

it may be tempting to write an extension function, but that binds you to a certain implementation and it really is unnecessary. It'd be nicer to get the current time/date at any moment of processing by means of standard XSLT / XPath functionality. This discussion thread makes it seem that such a thing is not possible, but, with a little help from the Internet, it actually is possible.


This is a short version of a related post of my, about one and a half years back.

Ingredients:
1) XSLT 2.0 processor that supports the document() function with the the HTTP URI scheme
2) An internet (HTTP) source that returns the current time and date
3) Access to that source (configure your firewall if necessary)
4) a little effort to make XSLT 2.0 act non-pure-functional


I assume for the moment that 1-3 are easy to grasp and arrange. Nr 4 may need a little help:

<xsl:function name="my:randomString" as="xs:string">
    <xsl:variable name="node">
        <xsl:comment />
    </xsl:variable>
    <xsl:sequence select="generate-id($node)" />
</xsl:function>

<xsl:function name="my:current-DateTime" as="xs:dateTime">
<xsl:variable name="dateTimeUrl" select="concat('http://yourtimesite/getTimeDate.pl?', my:randomString())" />
<xsl:sequence select="document($dateTimeUrl)/currentDateTimeNode" />
</xsl:function>



There are obvious drawbacks and caveats to this solution, one being that the Net overhead is significant, the other being that the dependencies on external systems may be a larger problem to deal with then an extension function. But whatever it is, the point I was trying to make is that you can get the current date or time at the current execution point (regardless the ordered or unordered execution of the stylesheet) without using anything non-XSLT-like (I won't say non-declarative like, simply because the side effects I employ are not part of declarative programming).


A time server can return either an XML document or, easier, a simple string. Then you'd use the unparsed-text() function.

Cheers,
-- Abel Braaksma

Current Thread