Page 1 of 1

current year

Posted: Wed May 23, 2012 8:30 pm
by gungy
hi,

how can i get the current year to display - i am declaring <xsl:param name="" /> but what should i enclose in the brackets to grab the current date or year?

Re: current year

Posted: Thu May 24, 2012 10:37 am
by adrian
Hi,

For XSLT 1.0 there is no date or time function. You will have to use an extension that provides that. A readily available extension in Oxygen is EXSLT, it can be used with both Saxon6.5.5 and Xalan transformers.
e.g. for ex:date()

Code: Select all

<xsl:stylesheet version="1.0"  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ex="http://exslt.org/dates-and-times" extension-element-prefixes="ex">
<xsl:template match="/">
<xsl:value-of select="ex:date()"/>
</xsl:template>
</xsl:stylesheet>
For XSLT 2.0 you can simply use the function: current-date()

The stackoverflow community has a lot of topics and solutions for things like this:
http://stackoverflow.com/questions/1575 ... rrent-date

Regards,
Adrian

Re: current year

Posted: Thu May 24, 2012 3:56 pm
by gungy
hi adrian,

thanks for reply, the ex:year doesnt seem to work when trying to extract the year from my dob element i.e. <dob>YYYY-MM-DD</dob>

i have in my xslt: <xsl:param name="dobyear" select="(ex:year('dob'))" />

and then: <xsl:value-of select="$dobyear"/>

this is outputting NaN?

Re: current year

Posted: Thu May 24, 2012 4:37 pm
by adrian
Hi,

You don't want the string 'dob', but the value of the element dob, so you should remove the single quotes around dob:
ex:year(dob)

Regards,
Adrian

Re: current year

Posted: Thu May 24, 2012 4:59 pm
by gungy
single quotes removed as you suggested, but its still returning NaN?

<xsl:param name="dobyear" select="(ex:year(dob))" />
<xsl:value-of select="$dobyear"/>

Re: current year

Posted: Thu May 24, 2012 5:21 pm
by adrian
You need to take into account the XML context from the template where you put that code.
e.g.
For the input XML file:

Code: Select all

<dob>2011-08-09</dob>
and the stylesheet:

Code: Select all

<xsl:stylesheet version="1.0"  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ex="http://exslt.org/dates-and-times" extension-element-prefixes="ex">
<xsl:template match="/">
<xsl:value-of select="ex:year(dob)"/>
</xsl:template>
</xsl:stylesheet>
The result is: 2011

Regards,
Adrian

Re: current year

Posted: Fri May 25, 2012 1:58 pm
by gungy
Hiya,

i think ive noticed that when calling this exslt extension for getting the current year, its breaking the internet explorer browser (version 9) so just displays raw xml only....ive copied over all the date xsl files into the same folder as my stylesheet and did the <xsl:import href="date.xsl" />, but oxygen is throwing up a lot of compile errors? any advice?

Re: current year

Posted: Fri May 25, 2012 3:23 pm
by adrian
Hi,

As you've guessed, Internet Explorer does not support EXSLT. You will have to use a different extension that is compatible with IE and the Microsoft ecosystem: msxsl

Searching the web, I've found this:
http://dpcarlisle.blogspot.com/2007/05/ ... ction.html

Regards,
Adrian