Page 1 of 1

A little help with XPath expression

Posted: Tue Jan 26, 2010 1:02 pm
by jkotuby
Hi all,
I am new to the open source programming world.

I am studying PHP, JavaScript and XML data manipulation.

My first simple goal is to:
1. Pass a numeric ID value to a PHP script.
2. Use PHP to look up that ID in an XML file where the "id" is an attribute of a <person> element. I will post a snippet of the XML below.
3. I want to return the contents of a "name" attribute that is a sibling of the "id" attribute in the found <person> element.

So far I have managed the following PHP with XPath which returns to the browser "Array". At this point I don't even know how to process an array in PHP, but I really just need to return the value of the "name" attribute at this point.

Here is my PHP code so far...

Code: Select all

 
<?php
$asdf = simplexml_load_file('../data/xml/people.xml');
 
$query = "/people/person[@id = '400001']";
echo $asdf->xpath($query);
 
$aryperson =  $asdf->xpath($query);
foreach ($aryperson  as $key => $value) {
   echo "Key: $key; Value: $value<br />\n";
     };
 
?>
 
When I run this PHP code in FireFox 3.5 I get...
Array
Key: 0; Value:

Here is an example of a snippet of the XML code where a <person> has id="400001" ...

Code: Select all

 
  <?xml version="1.0" ?> 
  <people session="111">
  <person id="400001" lastname="Abercrombie" firstname="Neil" birthday="1938-06-26" gender="M" pvsid="26827" osid="N00007665" bioguideid="A000014" metavidid="Neil_Abercrombie" name="Rep. Neil Abercrombie [D, HI-1]" title="Rep." state="HI" district="1">
  <role type="rep" startdate="2009-01-06" enddate="2010-12-31" party="Democrat" state="HI" district="1" url="http://www.house.gov/abercrombie" current="1" /> 
  <committee-assignment committee="House Armed Services" /> 
  <committee-assignment committee="House Natural Resources" /> 
  </person>
 
Any help with this would be greatly appreciated.

Re: A little help with XPath expression

Posted: Tue Jan 26, 2010 2:17 pm
by Weirdan
Next time please wrap your code into

Code: Select all

 tags (edit your post to see how I did it to your code).

Regarding the code you posted, there's an obvious error where you're storing xpath() result into $aryperson variable, but trying to iterate over $asdf variable.

Re: A little help with XPath expression

Posted: Tue Jan 26, 2010 6:26 pm
by jkotuby
Thanks for the tip on using the code tags.

Code: Select all

 
<?php echo 'hello'; ?>
 
The obvious mistake in the PHP as you pointed out was fixed in my actual code uploaded to the web server, but I forgot to make the same change in my forum post.
I just double checked that the code in the forum and on my web server is the same.

I am getting the same results indicating (I think) an empty array.

When the XML was not being loaded properly I got an error about that, but since I am getting no errors I can only assume that the XML is being loaded correctly.
Heck, I even got an error when there was initially a carriage return following the last element in the original XML file, which I fixed and ftped back to the server.

Does anyone see an error in the Xpath query?

Thanks again

Re: A little help with XPath expression

Posted: Tue Jan 26, 2010 6:42 pm
by requinix
The string representation of a SimpleXMLElement, which is what $value is, will be whatever the inner text of the node is.
If the node doesn't have any text then you won't see anything.

Your code is working fine.