I am trying to retrieve a single element from the following XML using an ID retrieved from the querystring to match the IDsuffix value:
<dataroot xmlns:od="urn:schemas-microsoft-com:officedata" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="inventory.xsd" generated="2009-01-31T11:57:25">
<XMLupdateQry>
<STATUS>ACTIVE</STATUS>
<IDsuffix>465</IDsuffix>
<VIN>1G8AJ55F06Z139166</VIN>
<YEAR>2006</YEAR>
<MAKE>Saturn</MAKE>
<MODEL>Ion 2</MODEL>
<MILESBEGINNING>41853</MILESBEGINNING>
<COLOR>red</COLOR>
<OPTIONS>This car runs and drives great with a 2.2L 4 cylinder engine and an automatic transmission. It has very low miles and features CD, power locks, and AC. It is a clean little car that is ready to go.</OPTIONS>
<COMMENTS> </COMMENTS>
<ASKINGPRICE>6000</ASKINGPRICE>
<PROBLEMS> </PROBLEMS>
<KBBRetail>10020</KBBRetail>
<KBBPPE>8070</KBBPPE>
<KBBPPG>7495</KBBPPG>
<KBBPPF>6770</KBBPPF>
</XMLupdateQry>
<XMLupdateQry>
<STATUS>ACTIVE</STATUS>
<IDsuffix>464</IDsuffix>
<VIN>1MEFM50U03A603185</VIN>
<YEAR>2003</YEAR>
<MAKE>Mercury</MAKE>
<MODEL>Sable GS</MODEL>
<MILESBEGINNING>84166</MILESBEGINNING>
<COLOR>silver</COLOR>
<OPTIONS>For sale is this 2003 Mercury Sable. This vehicle is in overall condition with a 3.0L V6 and an automatic transmission. The car features AC, cruise control, remote entry, and all power options.</OPTIONS>
<COMMENTS> The car currently has a check engine light on which we hope to diagnose and repair.</COMMENTS>
<ASKINGPRICE>2800</ASKINGPRICE>
<PROBLEMS>The front bumper does have some damage. See picture.</PROBLEMS>
<KBBRetail>5575</KBBRetail>
<KBBPPE>3900</KBBPPE>
<KBBPPG>3475</KBBPPG>
<KBBPPF>3000</KBBPPF>
</XMLupdateQry>
</dataroot>
Here's my PHP code:
<?php
$carid = (is_numeric($_GET['id']))?$_GET['id']:1;
if (file_exists("./xml/inventory.xml"))
{
$xml = simplexml_load_file("./xml/inventory.xml");
$car = $xml->xpath("XMLUpdateQry[IDsuffix='$carid']");
var_dump($car);
$make = $car->MAKE;
$model = $car->MODEL;
$color = $car->COLOR;
$price = $car->ASKINGPRICE;
}
?>
The result is not an XMLUpdateQry node but instead what I get for the variable $car is
array(0) { }
Why doesn't my XPath query work? How do I set the make, model, color, and price variables once I get the correct element?