4

I am currently achieving the desired outcome with two PHP statements:

$thisBlarg = $xmlResource->xpath('//blarg[@ID='.$someBlargID.']');
echo $thisBlarg[0]->name;

But, not wanting to settle for second best, I'd really prefer this to be one statement, but PHP doesn't like this:

echo $xmlResource->xpath('//blarg[@ID='.$someBlargID.']')[0]->name;

And for good reason. But I can't find a way to force an xpath query to return the result directly. Any suggestions?

hakre
  • 193,403
  • 52
  • 435
  • 836
MBuscemi
  • 102
  • 8
  • Is `name` an element that is child of `blarg` or is it something PHP-specific? – Dimitre Novatchev May 06 '10 at 03:11
  • Sorry, should have been clearer. My example assumes that $xmlResource was instantiated as such: $xmlResource = simplexml_load_file('someXMLfile.xml'); So, the 'name' property corresponds to one of the elements of the supposed XML file. – MBuscemi May 08 '10 at 03:18

1 Answers1

3

Try this

echo current(($xmlResource->xpath('//blarg[@ID='.$someBlargID.']')))->name;
hakre
  • 193,403
  • 52
  • 435
  • 836
Rwky
  • 2,116
  • 1
  • 18
  • 22