0

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?

1 Answer 1

1

Does $current->AttributeSets->children('ns2', true)->ItemAttributes->ListPrice->Amount contain 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 Amount is 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:

$result = $xml->xpath('$5.00');

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Yes, it is a valid XPath expression and it is supposed to pull out a single value. How should this be written to execute correctly?
...->Amount should return something like //div or whatever. What is it returning now?
Amount is returning the price of the book. If you mean from the function, it is returning true if there is a price and false if there is none.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.