Jump to content

SimpleXMLElement::xpath(): Invalid expression in

Recommended Posts



I have an XML file listing vehicle details, one record looks like



<RegistrationNumber>SL61 TUA</RegistrationNumber>
<MakeDescription>ALFA ROMEO</MakeDescription>
<SpecDescription>Veloce Tb</SpecDescription>
<BodyStyle>5 Door</BodyStyle>
<option> Alloy Wheels</option>
<option> Adjustable Steering Colmn</option>
<option> Air Conditioning</option>
<option> C D Player</option>
<option> Height Adjustable Drivers Seat</option>
<option> Multiple Airbags</option>
<option> Remote Central Locking</option>
<InternetNotes />
<VehicleCode />


I am trying to search for a record on vehicle record id using the following

if(!isset($_GET['GUID']) && ($_GET['REF']) && ($_GET['VehID'])){
    header('Location: Vehicles.php');
    $guid = $_GET['GUID'];
    $ref = $_GET['REF'];
    $vId = $_GET['VehID'];
$url = "url to xml file";
//echo $url;
$vehicles =simplexml_load_file($url);
$value = $vehicles->xpath("string(//VehicleRecord/VehicleRecordID[contains(text(), $vId)]");

if(count($value) ==0) { // if found


But am getting the following errors



Warning: SimpleXMLElement::xpath(): Invalid expression in /Applications/MAMP/htdocs/VehicleSearch/vehicle.php on line 19

Warning: SimpleXMLElement::xpath(): xmlXPathEval: evaluation failed in /Applications/MAMP/htdocs/VehicleSearch/vehicle.php on line 19
Link to post
Share on other sites

I assume you're looking for a VehicleRecord that matches the ID given? That XQuery expression isn't really what it should be. Try

"//VehicleRecord[VehicleRecordID='" . (int)$vId . "']"
That's literally all the s that have a with the text value of $vId (which is forced to an integer to avoid an injection attack). You can count() the return value too.
Link to post
Share on other sites

xpath tools heaven sent:  Are you using one?


I couldn't work with xpath without them.


ddgo on sketchpath or xmlquire.  There looks like there are quite a few tools out there.  I read somewhere firebug even has support.

Link to post
Share on other sites
This thread is more than a year old.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.