Selecting on date

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Peter Laman

    Selecting on date

    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
  • pr

    #2
    Re: Selecting on date

    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('mo nths', '04')"/>

    Comment

    • Martin Honnen

      #3
      Re: Selecting on date

      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

      Comment

      Working...