Jump to content


Xpath results without saving to var

  • Please log in to reply
1 reply to this topic

#1 Digicrat

  • New Members
  • Pip
  • Newbie
  • 1 posts

Posted 23 September 2006 - 06:11 PM

This isn't a major issue, but it is very annoying.

I have an xpath expression such as:

which I simply want to echo directly to the programs output at this point. 

Xpath always returns its result as an array, however I know for a fact that there will always be exactly one result for this query. 

I would like to put:
echo ($this->{XML}->xpath("/template/main/title"))[0];
however PHP5 considers this a syntax error. Is this a bug, or just a quirk of PHP syntax?  I know I've done shortcuts like that with other programming languages on the results of a function call.

Adding an extra variable just for one-time usage is simply annoying and (possibly) inefficient. 


#2 Barand

  • Moderators
  • Sen . ( ile || sei )
  • 18,015 posts

Posted 23 September 2006 - 08:36 PM

Does this help?
$xml = simplexml_load_file('myfile.xml');
$a = $xml->xpath('template/main');
foreach ($a as $item) {
    echo (string)$item->title,'<br>';

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received


|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users