real_illusions Posted October 5, 2010 Share Posted October 5, 2010 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 More sharing options...
jeffman Posted October 5, 2010 Share Posted October 5, 2010 Concatenate the xpath string before you pass it to xpath? Link to comment Share on other sites More sharing options...
real_illusions Posted October 5, 2010 Author Share Posted October 5, 2010 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 More sharing options...
jeffman Posted October 5, 2010 Share Posted October 5, 2010 Is 'name' an element or an attribute? Link to comment Share on other sites More sharing options...
real_illusions Posted October 5, 2010 Author Share Posted October 5, 2010 Part of the XML file, for example:<data><title>set title</title><date>october 12th 2010</date><item>some stuff</item><name>nameofstuff</name></data> Link to comment Share on other sites More sharing options...
jeffman Posted October 5, 2010 Share Posted October 5, 2010 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 More sharing options...
real_illusions Posted October 5, 2010 Author Share Posted October 5, 2010 That seems to do the trick.It was basically to echo out some data from an xml file where a certain element's data is matched to a variable.Thanks Link to comment Share on other sites More sharing options...
jeffman Posted October 5, 2010 Share Posted October 5, 2010 Ah, good. I was guessing about what you needed, but it seemed like a good guess. As you've discovered, xpath can really simplify xml navigation. Link to comment Share on other sites More sharing options...
real_illusions Posted October 6, 2010 Author Share Posted October 6, 2010 Ah, good. I was guessing about what you needed, but it seemed like a good guess. As you've discovered, xpath can really simplify xml navigation. Yeah, when it works Link to comment Share on other sites More sharing options...
boen_robot Posted October 6, 2010 Share Posted October 6, 2010 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 More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.