gusaindpk
gusaindpk

Reputation: 1253

How to compare dates in xpath for selecting nodes

I want to get nodes based on condition given in xpath for date comparison. How I can do that using xpath?

Do i need to use adjust-dateTime-to-timezone?

Upvotes: 6

Views: 13559

Answers (1)

Mads Hansen
Mads Hansen

Reputation: 66723

XPath 2.0 has a number of date/time functions and operators to help processing dates.

Assume that you had an XML document like this:

<doc>
    <event date="2011-02-05">foo</event>
    <event date="2011-08-01">bar</event>
    <event date="2011-08-20">baz</event>
    <event date="2011-11-07">qux</event>

</doc>

and you want to filter the events by @date for those in August 2011.

You could use this XPath:

/doc/event[xs:date(@date) le xs:date('2011-08-31') and 
                            xs:date(@date) ge xs:date('2011-08-01')]

and it would select the event elements for bar and baz.

Upvotes: 3

Related Questions