Tech Off Thread

5 posts

Forum Read Only

This forum has been made read only by the site admins. No new threads or comments can be added.

comparing date in xpath expression

Back to Forum: Tech Off
  • User profile image
    trun_gup

    i am having a date field in xml, i want to write xpath expression for comparing date. 

  • User profile image
    Sven Groot

    Good luck with that, since XPath doesn't understand dates.

    If you're just interested in equality, well that's easy obviously (as long as they all use the same format), just compare the strings.

    If you want to know before/after use the yyyyMMdd or yyyy-MM-dd format which is numerically/lexicographically sorted so you can once again just use numeric or string < and > operators.

    If you want to do anything more complex, you're probably best of writing an XPath extension function (you didn't mention what you're using for this, if .Net it's not hard).

  • User profile image
    trun_gup

    yes i amusing it with c#.net
    my xml is this:
      <?xml version="1.0" encoding="UTF-8" ?>
    <dataroot>
     <Event Id="101" NoteId="" IsTrash="False">
      <EventDescription>Soccer Practice</EventDescription>
      <EventDateFrom>2006-04-04</EventDateFrom>
      <EventTimeFrom>18:00</EventTimeFrom>
      <EventDateTo>2006-04-04</EventDateTo>
      <EventTimeTo>20:00</EventTimeTo>
      <EventAuthoredByUser>1001</EventAuthoredByUser>
      <EventCreatedByUser>1002</EventCreatedByUser>
      </Event>
    <Event Id="102" NoteId="5" IsTrash="False">
      <EventDescription>Concert KennyG</EventDescription>
      <EventDateFrom>2006-04-10</EventDateFrom>
      <EventTimeFrom>22:00</EventTimeFrom>
      <EventDateTo>2006-04-10</EventDateTo>
      <EventAuthoredByUser>1002</EventAuthoredByUser>
      <EventCreatedByUser>1002</EventCreatedByUser>
      </Event>
      </dataroot>



    and i want to write xpath query to fetch me all the nodes between specific dates

  • User profile image
    samjudson

    Well you can just use string comparision, provided all you want to compare is the year. This is because the string values you are storing sort in the same way that the equivalent date does, e.g.

    2006-10-01 > 2006-09-31 <=> "2006-10-01" > "2006-09-31"

    However if you also want to include the time portion in your comparisons you will have to get very complicated. e.g.

    Date1 > Date2 OR (Date1 = Date2 AND Time1 > Time2)

    Note: these are not written in xpath format, they are just examples of the logic you will need to use.

  • User profile image
    littleguru

    XPath 2.0 understands the date type... With XPath 1.0 you need to do a string comparison... There is no other way.

Conversation locked

This conversation has been locked by the site admins. No new comments can be made.