views:

20

answers:

2

Hello everyone,

Could someone tell me how to avoid these errors? Testing for existance of namespace?

It starts when I want to add geo information to an array I thought it was enough to do this.

(from the picasa api feed)

foreach($feed->xpath('//gml:pos') as $pos)
{

$feed_arr['geo'][$i]['pos'] = (string)$pos[0];
$i++;
}

<b>Warning</b>:  SimpleXMLElement::xpath() [<a href='/phpmanual/simplexmlelement.xpath'>simplexmlelement.xpath</a>]: Undefined namespace prefix in <b>/home/woonbel/public_html/tsa.nl/applicatie/lib/gpicasa.class.php</b> on line <b>390</b><br />
<br />
<b>Warning</b>:  SimpleXMLElement::xpath() [<a href='/phpmanual/simplexmlelement.xpath'>simplexmlelement.xpath</a>]: xmlXPathEval: evaluation failed in <b>/home/woonbel/public_html/tsa.nl/applicatie/lib/gpicasa.class.php</b> on line <b>390</b><br />
<br />
<b>Warning</b>:  Invalid argument supplied for foreach() in <b>/home/woonbel/public_html/tsa.nl/applicatie/lib/gpicasa.class.php</b> on line <b>390</b><br />

regards, Richard

+1  A: 

Just don't query for it when it isn't in $element->getDocNamespaces();

Wrikken
use strpos or do you have a better suggestion
Richard
Erm, do you mean you are just applying random XPath's outside your control? If so: catching the error and returning nothing is about your only hope, if not: just add a precondition `if(){}` around the whole thing.
Wrikken
I don't understand. You say, test with getDocNa..(). That will return multiple ns. Then I say, find out if gml namespace is contained within the result using strpos(), and then you say I do things randomly? Need more explaining please.
Richard
What I mean is that usually you've got more or less hardcoded xpaths, and you don't need any string parsing, you already _know_ what your next XPath depends on, and you can just check the existence of the proper array keys.
Wrikken
Sorry, I am a bit late in my reply, I used simple xml instead to get what I want, atom fields are not visible in getDocSpaces() - I thought I read on php docs! xpath will be something I have to practice more, and in this case I could not find the right way.
Richard
A: 

The clean solution is the one posted by Wrikken, but you can try this


foreach( (($tmp = @$feed->xpath('//gml:pos')) ? $tmp : array()) as $pos ) {
    // body
}

$xml->xpath is called with '@' to suppress error messages (the first two warnings about xpath), the result is assigned to $tmp. Then the conditional statement is evaluated. $xml->xpath(...) ? $tmp : array() will return $tmp (the result of the xpath query) if it contains valid data, otherwise it will return an empty array. That will avoid the warning output for an invalid foreach argument.

hacksteak25
I have a problem finding out wich entry has a namespace and wich not? Your solution gives me two results for three foto's , but no way to associate them to the right foto? (the ones that actually have geo data)
Richard
Can you post the XML please? Do you generate the XML or do you get it from outside?
hacksteak25
I just used simplexml to retrieve the information and forget about xpath at the moment.@Hackstack, sometimes the namespace element is present and sometimes it's not in (atom/xml) feed.
Richard