Jump to content

xpath problems


real_illusions

Recommended Posts

I have this, based on a bit of code from php.net:

$sxe = simplexml_load_file("stuff.xml");foreach($sxe->xpath('//data') as $item) {$row = simplexml_load_string($item->asXML());$v = $row->xpath('//title[ =".$pageitem."]');if($v[0]){print $item->title;print $item->sizes;print $item->date;}}

However, this line is giving problems:$v = $row->xpath('//title[ =.$pageitem.]');It basically wont work with the $pageitem variable in there. If I put just text in, then it works. But I need it to be a variable.Any work arounds?

Link to comment
Share on other sites

didnt work:$xpathstring = "//name[ =.$pageitem.]";$v = $row->xpath("$xpathstring");Warning: SimpleXMLElement::xpath() [simplexmlelement.xpath]: Invalid expression in (filename) on line 50Which is the 2nd of those 2 lines.

Link to comment
Share on other sites

You haven't explained what the purpose of all this is, but I suspect it is simpler than your code makes it. Here's what I suspect you want to do.1. open stuff.xml2. get a reference to the element of kind 'data' that contains a child element of kind 'name' whose value is $pageitem.The following should do that, and without any looping.

$pageitem = 'nameofstuff'; // or whatever$sxe = simplexml_load_file("stuff.xml");$something = $sxe->xpath("//data/name[.='$pageitem']/.."); // returns an array of objectsvar_dump($something[0]);

There might be a more direct way to specify the path (boen will know) but this gets the job done. If this does not do what you want, please explain further.

Link to comment
Share on other sites

Just so you know (in case you didn't figured it out already), the error in your XPath expression was the missing "." and quotes around the value, which DD added in his snippet. As in:

"//name[ =".$pageitem."]";

should've been

"//name[. ='" . $pageitem . "']";

or better yet

"//name[. ='$pageitem']";

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...