简体   繁体   中英

PHP xpath query giving no results

I have an xml document like this (just exemple because the real xml is biger):

<document>
<vol>
<sens>A</sens>
<date_vol>2013-05-26 00:00:00</date_vol>
<compagnie>AZA</compagnie>
<num_vol>802</num_vol>
<destination>FCO</destination>
<horaire>1900-01-01 00:20:00</horaire>
<statut>ARRIVE 00:45</statut>
</vol>
<vol>
<sens>A</sens>
<date_vol>2013-05-27 00:00:00</date_vol>
<compagnie>AZA</compagnie>
<num_vol>802</num_vol>
<destination>FCO</destination>
<horaire>1900-01-01 00:20:00</horaire>
<statut>ARRIVE 00:13</statut>
</vol>

i would like to query this document using php like this :

$simpleXml = new SimpleXMLElement($xml);
$vols = $simpleXml->xpath("/document/vol[sens='$sens_vols' and date_vol='2013-05-26 00:00:00' and horaire>'1900-01-01 00:20:00']");

this doesn't work, it gives me blank result, but if i remove the last and from query (this: and horaire>'1900-01-01 00:20:00') it works, i thinks there is a problem anywhere in my code when i query the string with ">" symbol, because it works with "=". can someone tell me what is wrong ? thanks

The answer from davmos goes in the right direction. In xpath-1.0 the operator > only compares numbers. Good news is that your date has an format that could easily converted into a meaningful number (by translate() function) But there is no need for nest translate call, On call for each parameter would be sufficient.
Try:

$vols = $simpleXml->xpath('/document/vol[sens="$sens_vols" and date_vol = "2013-05-26 00:00:00" and translate(horaire, "- :","") > translate("1900-01-01 00:20:00", "- :","")]');

In XPath 1.0, in order to compare like that you first need to remove the - , : and space characters using the translate() function...

$vols = $simpleXml->xpath('/document/vol[sens="$sens_vols" and date_vol = "2013-05-26 00:00:00" and translate(translate(translate(horaire, "-", "")," ",""),":","") > translate(translate(translate("1900-01-01 00:20:00"", "-", "")," ",""),":","")]');

It's more straightforward in XPath 2.0 using the compare() function, but XPath 2.0 is still not widely implemented yet.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM