I am attempting to use xpath to run a program and parse out xml data for repricing books. However, when I run the program I get the following errors:
PHP Warning: SimpleXMLElement::xpath() [<a href='simplexmlelement.xpath'>simplexmlelement.xpath</a>]: Invalid expression
and
PHP Warning: SimpleXMLElement::xpath() [<a href='simplexmlelement.xpath'>simplexmlelement.xpath</a>]: xmlXPathEval: evaluation failed
both on line 242 which is the line of $result…:
//function to check if child nodes exist for pricing
function xml_child_exists($xml, $childpath)
{
$result = $xml->xpath($childpath);
if (isset($result)) {
return true;
} else {
return false;
}
}
This function is run here:
// check to see if there are values
if(xml_child_exists($parsed_xml, $current->AttributeSets->children('ns2', true)->ItemAttributes->ListPrice->Amount))
{
$listPrice = $current->AttributeSets->children('ns2', true)->ItemAttributes->ListPrice->Amount;
} else {
$listPrice = 0;
}
Then I finally am ending up with:
PHP Fatal error: Call to a member function children() on a non-object in repricemws.php on line 67
Line 67 is where the function is being called.
What is wrong with this code and how do I make it so that it will run correctly?
Does
$current->AttributeSets->children('ns2', true)->ItemAttributes->ListPrice->Amountcontain a valid XPath expression?From the looks of that call chain, you’re pulling out a single value, e.g. ‘5.00’ and passing that directly to the xpath query executor. That’s not going to work, and produce your error messages.
followup:
ok, so
Amountis a price, so it’s something like $5.00 or 5.00, right? That means you’re using that exact string as your xpath query, basically doing:That is NOT a valid xpath expression. So $result is NOT a list of matching nodes in the document, it’s actually going to be a boolean FALSE.
You then do an isset() on that value. The variable IS set (to boolean false), so your function returns TRUE.