Topic
  • 2 replies
  • Latest Post - ‏2010-01-27T11:06:00Z by SystemAdmin
jkotuby
jkotuby
1 Post

Pinned topic In what context do I use xQuery?

‏2010-01-26T14:00:23Z |
Hi all,
I am new to the open source programming world after 20 years working with other languages like cobol, dbase, and more recently Microsoft ASP.NET.

I am studying PHP, JavaScript and XML data manipulation.
I have spent quite a bit of time looking at xQuery as it appears to be the answer to my task of querying raw XML files that I must work with.
I would like to utilize a query such as:

for $x in doc("books.xml")/bookstore/book
where $x/price>30
order by $x/title
return $x/title

But, I have no idea what context to use it in. I have some XML files on a web server and wish to run a query like that with PHP if possible because I don't want to pull 1 MB files into the browser memory using JavaScript and AJAX. I just want to return the results of an XML query on the web server and display them in the browser in HTML.

So far searches with the keywords "xQuery" and "PHP" combined have yielded not much.

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 the value of the "name" attribute.

Here is my PHP code so far...
<?php
$asdf = simplexml_load_file('../data/xml/people.xml');
$query = "/people/person";
echo $asdf->xpath($query);
?>

Here is an example of the XML...

<?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. All the xQuery examples I have seen are out of context and I can't see how to use them in PHP or for that matter in ANY context on the web server side.

Thanks in advance to point me in the right direction. I have about 1 week to produce results. I work for a very small company (5 people and only 2 developers of which I am one).
Updated on 2010-01-27T11:06:00Z at 2010-01-27T11:06:00Z by SystemAdmin
  • SystemAdmin
    SystemAdmin
    746 Posts

    Re: In what context do I use xQuery?

    ‏2010-01-26T20:47:42Z  
    You might like to try the Zorba XQuery processor, which advertises a PHP language binding (API). I haven't used it myself.

    http://www.zorba-xquery.com/
  • SystemAdmin
    SystemAdmin
    746 Posts

    Re: In what context do I use xQuery?

    ‏2010-01-27T11:06:00Z  
    >At this point I don't even know how to process an array in PHP, but I really just need the value of the "name" attribute.

    You can do a lot simply with xpath 1.0 support and php's standard processing of arrays, like this and that would be simplest. (I provision some script lines in view of possible expanded needs, and I don't know if the passing of ID is via GET or POST, so I put some normally unnecessary provision to retrieve that data. Also, I would suppose id being unique, but it is not told, so I put many extra lines to guard against non-uniqueness.)
    
    <?php $id=(isset($_GET[
    'ID']))?$_GET[
    'ID']:((isset($_POST[
    'ID']))?$_POST[
    'ID']:NULL); 
    
    if (isset($id)) 
    { $asdf = simplexml_load_file(
    '../data/xml/people.xml'); $query = 
    "/people/person[@id='$id']"; $result=$asdf->xpath($query);   
    
    for ($i=0;$i<count($result);$i++) 
    { $query2=
    "/people/person[@id='$id'][$i+1]/@name"; $result2=$asdf->xpath($query2); echo $result2[0][
    'name'].
    "\n"; 
    }   
    } ?>