Search code examples
phpxpathsimplexml

Simplexml xpath formula not working


I can not make work the formula in xpath. Here is a part of code to reproduce the issue:

//$val['path_xml'] = "price + 1"
$valID = $result[$i]->xpath($val['path_xml']);
echo "<br>".$val['path_xml']."<br>";
echo $result[$i]->price."<br>";
echo json_encode($valID)."<br>";

The output of the code is:

price + 1   // as expected - the correct path
525000      // just for test to check if price exists
[]          // empty array????

Test 2 - just to make sure everything works fine:

//$val['path_xml'] = "price"
$valID = $result[$i]->xpath($val['path_xml']);
echo "<br>".$val['path_xml']."<br>";
echo $result[$i]->price."<br>";
echo json_encode($valID)."<br>";

Output:

price             // correct
525000            // ok
[{"0":"525000"}]  // OK

Can someone give any idea why the formulas are not working? Maybe need a newer version of PHP (currently 5.6) or any other ideas?

Checked it with this online tool and it works perfectly: http://www.freeformatter.com/xpath-tester.html#ad-output


Solution

  • Correct answer by fusion3k: XPath can find price+1 but on php not all XPath functions are implemented, so you can't use price+1. Updating PHP doesn't change it.