A little help with XPath expression
Posted: Tue Jan 26, 2010 1:02 pm
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...
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" ...
Any help with this would be greatly appreciated.
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";
};
?>
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>