current year

Here should go questions about transforming XML with XSLT and FOP.
gungy
Posts: 7
Joined: Tue May 22, 2012 5:07 pm

current year

Post 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?
adrian
Posts: 2855
Joined: Tue May 17, 2005 4:01 pm

Re: current year

Post 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
Adrian Buza
<oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
http://www.oxygenxml.com
gungy
Posts: 7
Joined: Tue May 22, 2012 5:07 pm

Re: current year

Post 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?
adrian
Posts: 2855
Joined: Tue May 17, 2005 4:01 pm

Re: current year

Post 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
Adrian Buza
<oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
http://www.oxygenxml.com
gungy
Posts: 7
Joined: Tue May 22, 2012 5:07 pm

Re: current year

Post 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"/>
adrian
Posts: 2855
Joined: Tue May 17, 2005 4:01 pm

Re: current year

Post 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
Adrian Buza
<oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
http://www.oxygenxml.com
gungy
Posts: 7
Joined: Tue May 22, 2012 5:07 pm

Re: current year

Post 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?
adrian
Posts: 2855
Joined: Tue May 17, 2005 4:01 pm

Re: current year

Post 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
Adrian Buza
<oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
http://www.oxygenxml.com
Post Reply