Selecting on date

P

Peter Laman

My XML data contains elements with a date attribute. I need to write
an XSLT transformation that only selects the elements for the current
month. Is that possible with XSLT only, or should I preselect the data
a different way?
Thanks
 
P

pr

Peter said:
My XML data contains elements with a date attribute. I need to write
an XSLT transformation that only selects the elements for the current
month. Is that possible with XSLT only, or should I preselect the data
a different way?

It's simple if your dates are in a consistent format, for example
'yyyy-mm-dd'. In XSLT 1.0:

<xsl:template match="element[starts-with(@date, '2008') and
substring(@date, 6, 2) = '04']">

or using a key:

<xsl:key name="months" match="element[starts-with(@date, '2008')]"
use="substring(@date, 6, 2)"/>

...

<xsl:apply-templates select="key('months', '04')"/>
 
M

Martin Honnen

Peter said:
My XML data contains elements with a date attribute. I need to write
an XSLT transformation that only selects the elements for the current
month. Is that possible with XSLT only, or should I preselect the data
a different way?

Pass in the current month/date as a parameter, then use string
processing to compare the relevant parts of dates.
Or move up to XSLT 2.0 where you have xs:date/xs:dateTime data type
support and functions like current-date and month-from-date.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,001
Messages
2,570,254
Members
46,849
Latest member
Fira

Latest Threads

Top