Selecting on date

Discussion in 'XML' started by Peter Laman, Apr 17, 2008.

  1. Peter Laman

    Peter Laman Guest

    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
     
    Peter Laman, Apr 17, 2008
    #1
    1. Advertising

  2. Peter Laman

    pr Guest

    Peter Laman wrote:
    > 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')"/>
     
    pr, Apr 17, 2008
    #2
    1. Advertising

  3. Peter Laman wrote:
    > 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.


    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
     
    Martin Honnen, Apr 17, 2008
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Matt
    Replies:
    1
    Views:
    608
    Matthew Speed
    Nov 8, 2003
  2. Peter Grison

    Date, date date date....

    Peter Grison, May 28, 2004, in forum: Java
    Replies:
    10
    Views:
    3,278
    Michael Borgwardt
    May 30, 2004
  3. Matt
    Replies:
    2
    Views:
    519
    Pete Becker
    Nov 8, 2003
  4. Matt
    Replies:
    3
    Views:
    724
    Richard Heathfield
    Nov 8, 2003
  5. Matt
    Replies:
    11
    Views:
    407
    Aaron Bertrand [MVP]
    Nov 8, 2003
Loading...

Share This Page