1

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!

D. Caan
  • 1,907
  • 6
  • 22
  • 36

1 Answers1

1

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.

Matas Vaitkevicius
  • 58,075
  • 31
  • 238
  • 265
  • What's "20101115"? Is it a hard-coded value just for the comparison? – D. Caan Jul 08 '14 at 12:03
  • @Dalek I your case just copy paste the 'atrocity' to the other side of equation, and change the attribute name. Done it for you. – Matas Vaitkevicius Jul 08 '14 at 12:04
  • translate(translate(translate("08-jul-2014 05:00:00",'-',''),':',''),' ','') is working fine but substring(number(translate(translate(translate("08-jul-2014 05:00:00",'-',''),':',''),' ','') ,5,4)+substring(number(translate(translate(translate("08-jul-2014 05:00:00",'-',''),':',''),' ',''),2,3)+substring(number(translate(translate(translate("08-jul-2014 05:00:00",'-',''),':',''),' ',''),0,2)+substring(number(translate(translate(translate("08-jul-2014 05:00:00",'-',''),':',''),' ',''),9,6) returns nothing. What can be wrong? – D. Caan Jul 08 '14 at 12:07
  • 1
    jul is wrong. The idea is to transform date components to `number` and compare them that way but there is no way to do it for string months. I suppose you could add another 12 translates like: `translate("08-jul-2014", 'jul', '06')` for each month. – Matas Vaitkevicius Jul 08 '14 at 12:09
  • I am trying substring(number(translate(translate(translate("01-01-2014 00:00:00",'-',''),':',''),' ','') ,5,4)+substring(number(translate(translate(translate("01-01-2014 00:00:00",'-',''),':',''),' ',''),2,3)+substring(number(translate(translate(translate("01-01-2014 00:00:00",'-',''),':',''),' ',''),0,2)+substring(number(translate(translate(translate("01-01-2014 00:00:00",'-',''),':',''),' ',''),9,6) with no return either. What is the expected datetime format for this expression? – D. Caan Jul 08 '14 at 12:14
  • you should wrap only final concatenated value with `number()`. expected - 20140101000000 – Matas Vaitkevicius Jul 08 '14 at 12:22
  • There's a possible bug on this XPath. http://stackoverflow.com/questions/24852596/xpath-1-0-timestring-comparison – D. Caan Jul 20 '14 at 16:47