Search code examples
xpathxpath-1.0

Compare two TimeStrings in XPath 1.0?


How can I compare these Strings in XPath1.0:

08-jul-2014 05:00:00
08-jul-2014 06:00:00

For example:

08-jul-2014 05:00:00 > 08-jul-2014 06:00:00 should return false

08-jul-2014 05:00:00 < 08-jul-2014 06:00:00 should return true

I looked at Xpath built funcions and I can't find any funciton to convert String to UTC Seconds for example...

Thanks in advance!


Solution

  • You can use the following hack. Remove delimiters using translate() and use substring() to make it into format yyyyMMddHHmmss and then compare this atrocity.

    article[number(substring(translate(translate(translate(@pub-date,'-',''),':',''),' ','') ,5,4)+substring(translate(translate(translate(@pub-date,'-',''),':',''),' ',''),2,3)+substring(translate(translate(translate(@pub-date,'-',''),':',''),' ',''),0,2)+substring(translate(translate(translate(@pub-date,'-',''),':',''),' ',''),9,6)) > number(substring(translate(translate(translate(@pub-date2,'-',''),':',''),' ','') ,5,4)+substring(translate(translate(translate(@pub-date2,'-',''),':',''),' ',''),2,3)+substring(translate(translate(translate(@pub-date2,'-',''),':',''),' ',''),0,2)+substring(translate(translate(translate(@pub-date2,'-',''),':',''),' ',''),9,6))]
    

    I just realised that since you have month as string you are pretty much stuffed.