Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to compare dates in xpath for selecting nodes

Tags:

xml

xpath

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?

like image 936
gusaindpk Avatar asked Aug 18 '11 13:08

gusaindpk


People also ask

Can you use XPath expression used to select the target node and its values?

XPath assertion uses XPath expression to select the target node and its values. It compares the result of an XPath expression to an expected value. XPath is an XML query language for selecting nodes from an XML.

Is XPath unique for each element?

Unlike ID attributes, every element in a web page has a unique XPath.


1 Answers

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.

like image 153
Mads Hansen Avatar answered Oct 19 '22 13:10

Mads Hansen