Barand Posted March 14, 2018 Share Posted March 14, 2018 (edited) I have some xml (which I hate and think is the biggest waste of space in the history of computing, but I guess we have to live with it) containing image elements and I am trying to find those images updated in the last week. ie lastUpdate > 2018-03-07. Failing miserably I tried with the simpler type attributes with similar success. The data <?xml version="1.0" encoding="utf-8"?> <result> <item id="5018"> <texts> <text lastUpdate="2018-03-14T01:23:04+01:00" lang="en" /> </texts> <images> <image type="a" lastUpdate="2018-01-05T09:26:21+01:00"> </image> <image type="p" lastUpdate="2018-03-09T09:26:21+01:00"> </image> <image type="r" lastUpdate="2018-03-09T09:26:21+01:00"> </image> </images> </item> </result> The attempts: $xml = simplexml_load_file('test.xml'); $x = $xml->xpath("//image[@type = 'a']"); // 1 result as expected $x = $xml->xpath("//image[@type != 'a']"); // 2 results as expected (types p and r) $x = $xml->xpath("//image[@type > 'a']"); // 0 results ?????? $x = $xml->xpath("//image[@lastUpdate = '2018-03-09T09:26:21+01:00']"); // 2 results // and the similar main problem: $x = $xml->xpath("//image[@lastUpdate > '2018-03-07']"); // 0 results ?????? Any suggestions? Edited March 14, 2018 by Barand Quote Link to comment https://forums.phpfreaks.com/topic/306835-string-comparisons-using-xpath/ Share on other sites More sharing options...
phpmillion Posted March 14, 2018 Share Posted March 14, 2018 (edited) Which version of xpath do you use? Such a comparison ( >a ) was only added in v2.0. I guess it also applies to your 2nd attempt ( @lastUpdate > '2018-03-07' ) because it's actually a string comparison too. Edited March 14, 2018 by phpmillion Quote Link to comment https://forums.phpfreaks.com/topic/306835-string-comparisons-using-xpath/#findComment-1557160 Share on other sites More sharing options...
Solution Psycho Posted March 14, 2018 Solution Share Posted March 14, 2018 (edited) This seems to work: $x = $xml->xpath("//image[number(translate(substring-before(@lastUpdate, 'T'), '-', '')) > 20180307]"); The substring-before(@lastUpdate, 'T') returns the date string that comes before the 'T'. Then, that value is used within translate([VALUE], '-', '') to remove the dashes in the date string. Then, that value is used within number([VALUE]) to convert the resulting string 'YYYYMMDD' into a number value and is compared against the numeric value for your specified date. You can compare on date and time if you use '+' in the substring-before() function and use '-T:' in the second parameter for the translate() function. Edited March 14, 2018 by Psycho 1 Quote Link to comment https://forums.phpfreaks.com/topic/306835-string-comparisons-using-xpath/#findComment-1557161 Share on other sites More sharing options...
Barand Posted March 14, 2018 Author Share Posted March 14, 2018 Thanks Psycho. 1 Quote Link to comment https://forums.phpfreaks.com/topic/306835-string-comparisons-using-xpath/#findComment-1557164 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.